Latest News : 亮瞎双眼的那些年!

PHP 支付宝小程序 支付以及回调处理

乱弹 admin 544 views 0 comments

 

PHP 支付宝小程序 支付以及回调处理

 

一、去支付宝官网下载对应的PHP SDK
二、 在服务端调用 alipay.trade.create (统一收单交易创建接口),获得支付宝交易号 tradeNO

接下来直接上代码:

首先加载支付宝的系统参数,以及基础的配置

public function ali_init($config) {
        require  dirname(__FILE__) .'/../library/alipay/AopSdk.php';
        $aop = new \AopClient ();
        //支付宝网关
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';

        //第三方配置信息
        $aop->appId              = $config['ali_app_id'];//第三方应用的appid
        $aop->rsaPrivateKey      = $config['ali_rsa_pri_key'];//第三方应用的密钥
        $aop->alipayrsaPublicKey = $config['ali_rsa_pub_key'];//第三方应用的公钥
        $aop->apiVersion         = '1.0';
        $aop->signType           = 'RSA2';
        $aop->postCharset        = 'UTF-8';
        $aop->format             = 'json';
        return $aop;
    }

 

第二步:调用 alipay.trade.create (统一收单交易创建接口),获得支付宝交易号 tradeNO
public function Ali_Pay($data = [])
{
$config = pdo_get('bda_cardcrm_company', ['id' => $data['company_id']]);
//实例化客户端
$aop                     = $this->ali_init($config);
$aop->alipayrsaPublicKey = $config['ali_pay_rsa_pub_key'];
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.create.
$request = new AlipayTradeCreateRequest ();
$request_data = [
'out_trade_no' => $data['out_trade_no'],        //  商户订单号
'total_amount' => $data['total_amount'],        //  订单总金额
'subject'      => $data['subject'],             //  订单标题
'buyer_id'     => $data['buyer_id'],            //  买家的支付宝唯一用户号(2088开头的16位纯数字)
];
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$website = $http_type . $_SERVER['HTTP_HOST'];
$request->setNotifyUrl($website .'/payment/alipay/notify.php');
$request_data = json_encode($request_data);
$request->setBizContent($request_data);
$result = $aop->execute ( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode   = $result->$responseNode->code;
if(!empty($resultCode) && $resultCode == 10000){
return ['error'=>0 , 'res'=>$result->$responseNode];
} else {
return ['error'=>-1 , 'res'=>$result->$responseNode];
}
}

 

测试一下接口是否成功

到这一步,基本上服务端处理就差不多了;接下来,将后端生成的支付宝交易号 tradeNO返给前端;

在小程序端使用 tradeNO,调用 my.tradePay 接口唤起支付收银台。
my.tradePay({
tradeNO: '2017111521001104105336677922',  
success: function(res) {
my.alert(res.resultCode);
},
fail: function(res) {
my.alert(res.resultCode);
},
});

 

最后就是我们的回调处理了

我们可以将支付宝回调返回的数据写入文件中。

file_put_contents(‘alipay.txt’ , json_encode($_POST));

回调成功的话,会在文件中看到相应的json字符串,就像这样:

{"gmt_create":"2019-07-25 21:13:58","charset":"UTF-8","seller_email":"dust***************[email protected]","subject":"AliPay","sign":"ii\/ltz08zbJSb81KjMP9WqFWaES6eCcjQNfc80Y8ny9r5bDmB1WkKC+\/U+t+7uaL7y0QMARQcR2l20cn9WpqRU+6JPVjqL****************0MYeGQomluXDMvElvW1YUTywt59C2S15bxiNcZmDksDv3RHGSfhLlf0+521ff2gryDNr0X6MA2IKQiipwFKaQvNQsxvtPQzDYiAeJRy6z4jp5Hs0w1dsZuFBQOl5aas7fz88e8dmUFnSWi1poUIBFKijfWmZ1zdMinLK8y6VTHEIhD1LxHt5L\/JaqekDlpbE9X3hhIDN+5mnuwvheZz52mcsKfizCyGBj+ql\/oow==","buyer_id":"***********","invoice_amount":"0.01","notify_id":"***********","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","buyer_pay_amount":"0.01","app_id":"*********","sign_type":"RSA2","seller_id":"*************","gmt_payment":"2019-07-25 21:14:15","notify_time":"2019-07-25 21:14:16","version":"1.0","out_trade_no":"b-245-201907255d39ab15d37ac","total_amount":"0.01","trade_no":"*********","auth_app_id":"***********","buyer_logon_id":"cha***@outlook.com","point_amount":"0.00"}
我们需要验签
//验证签名
$aop = new \AopClient();
$aop->alipayPublicKey = $ali_pay_rsa_pub_key;
$flag = $aop->rsaCheckV1($data , NULL, "RSA2");
if(!empty($data) && $flag) {
//以下为回调业务逻辑代码
... ...数据处理
if ($trade_status == 'TRADE_SUCCESS') {
//支付成功业务处理
...	...
//
exit('success');
}
}
exit('failed');

 

最后就是支付方法的调用


// 支付宝
load()->classs("alipay");
$data['total_amount'] = '商品金额';
$data['subject']      = "AliPay";
$data['out_trade_no'] = '订单号';
$data['buyer_id']     = '用户openID';
$alipay = new AliPay();
$res    = $alipay->Ali_Pay($data);
if ($res['error'] == 0) {
pdo_commit();// 全部执行成功提交
return $this->result(0,'success',$res['res']);
} else {
return $this->result(-1,'error',$res['res']);
}

 

这样完整的支付宝小程序支付就完成了

 

Please indicate: 无趣的人生也产生有意思的事件 » PHP 支付宝小程序 支付以及回调处理

Hi, you must log in to comment !