工作环境,支付宝账号是公司的,app应用和秘钥配置都是我自己申请的,这其中的流程暂且跳过。
一、准备工作
appid、应用私钥、应用公钥、支付宝公钥
二、配置文件
'alipay'=>[
'appId' => '20180300000000',
'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
'rsaPrivateKey' => '应用私钥',
'rsaPublicKey' => '应用公钥',
'alipayrsaPublicKey'=> '支付宝公钥',
'seller' => '支付宝邮箱',//可不要
'format' => 'json',
'charset' => 'UTF-8',
'signType' => 'RSA2',
'transport' => 'http',
],
二、下载官方的SDK包,放在extend下,如图
三、创建支付方法
(一)创建支付类
<?php
namespace app\index\controller;
use think\Config;
class Alipay
{
/*
* 支付宝支付
* $body 名称
* $total_amount 价格
* $product_code 订单号
* $notify_url 异步回调地址
*/
public function alipay($body, $total_amount, $product_code, $notify_url)
{
/**
* 调用支付宝接口。
*/
import('.Alipay.aop.AopClient', '', '.php');
import('.Alipay.aop.request.AlipayTradeAppPayRequest', '', '.php');
$aop = new \AopClient();
$aop->gatewayUrl = Config::get('alipay')['gatewayUrl'];
$aop->appId = Config::get('alipay')['appId'];
$aop->rsaPrivateKey = Config::get('alipay')['rsaPrivateKey'];
$aop->format = Config::get('alipay')['format'];
$aop->charset = Config::get('alipay')['charset'];
$aop->signType = Config::get('alipay')['signType'];
$aop->alipayrsaPublicKey = Config::get('alipay')['alipayrsaPublicKey'];
$request = new \AlipayTradeAppPayRequest();
$arr['body'] = $body;
$arr['subject'] = $body;
$arr['out_trade_no'] = $product_code;
$arr['timeout_express'] = '30m';
$arr['total_amount'] = floatval($total_amount);
$arr['product_code'] = 'QUICK_MSECURITY_PAY';
$json = json_encode($arr);
$request->setNotifyUrl($notify_url);
$request->setBizContent($json);
$response = $aop->sdkExecute($request);
return $response;
}
}
(二)创建支付方法
namespace app\index\controller;
use think\Config;
use think\Request;
class Payment extends Common
{
//测试服务器
private $domain = 'http://xxxx.com';
public function __construct(Request $request = null)
{
parent::__construct($request);
}
public function payOrder()
{
//获取订单号
$where['id'] = input('post.orderid');
//查询订单信息
$order_info = db('order')->where($where)->find();
$reoderSn = $order_info['ordersn'];
//获取支付方式
$pay_type = input('post.paytype');//微信支付 或者支付宝支付
//获取支付金额
$money = 0.01;//$order_info['realprice'];
//判断支付方式
if ($pay_type == 'alipay') {
$type['paytype'] = 1;
db('order')->where($where)->update($type);
$alipay = new Alipay();
//异步回调地址
$url = $this->url_translation_address('/index/payment/alipay_notify');
$array = $alipay ->alipay(Config::get('company'), $money, $reoderSn, $url);
if ($array) {
return $this->response($array, 1, '成功');
} else {
return $this->response('', 0, '对不起请检查相关参数');
}
}
if ($pay_type == 'wechat') {
$type['paytype'] = 2;
}
}
/*
* 支付宝支付回调修改订单状态
*/
public function alipay_notify()
{
//原始订单号
$out_trade_no = input('out_trade_no');
//支付宝交易号
$trade_no = input('trade_no');
//交易状态
$trade_status = input('trade_status');
if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
$condition['ordersn'] = $out_trade_no;
$data['status'] = 2;
$data['third_ordersn'] = $trade_no;
$result=db('order')->where($condition)->update($data);//修改订单状态,支付宝单号到数据库
if($result){
echo 'success';
}else{
echo 'fail';
}
}else{
echo "fail";
}
}
//相对地址转绝对地址
protected function url_translation_address($url)
{
return $this->domain . $url;
}
}
四、接下来,安卓或者iOS 对接成功后,就可以进行测试了。如果出现回调地址没有响应或者出现其他问题,可在开放 平台联调日志排查 中查看支付宝的报头响应信息
地址 https://openmonitor.alipay.com/acceptance/cloudparse.htm