支付宝开发文档网址:https://opendocs.alipay.com/apis
uniapp开发文档支付网址:https://uniapp.dcloud.net.cn/api/plugins/payment
支付宝支付测试错误排查工具网址:https://opensupport.alipay.com/support/tools/cloudparse
支付常见问题可参照网址:https://opensupport.alipay.com/support/helpcenter/89/201602479174?ant_source=opendoc_recommend
注:2018年1月5号 之后创建的应用只能上传rsa2签名方式的密钥(即RSA(SHA1)密钥入口下架),所以接口调用的签名类型必须要使用RSA2
PHP代码可借鉴网址:https://github.com/dcloudio/H5P.Server/tree/master/payment
配置密钥
将demo下载放在对应的路径下并引用。这里本人放的是extend文件夹下。
代码
require '../extend/alipay/aop/AopClient.php';
require '../extend/alipay/aop/request/AlipayTradeAppPayRequest.php';
$amount = $this->request->param('total');
$order_id = $this->request->param('order_id');
$aop = new \AopClient;
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$appid=config('base.web_AliAppid');
$aop->appId =$appid;
$prkey=config('base.web_rsaPrivateKey');//私钥
$aop->rsaPrivateKey = $prkey;
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$pukey=config('base.web_alipayrsaPublicKey');//公钥
$aop->alipayrsaPublicKey = $pukey;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
// 异步通知地址
$notify_url = urlencode('http://xxx/commodity/index/aliNotify');
//付款后页面返回地址
$return_url = urlencode('http://xxx');
// 订单标题
$subject = '购买商品';
// 订单详情
$body = '购买商品';
// 订单号,示例代码使用时间值作为唯一的订单ID号
$out_trade_no = $order_id;
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$bizcontent = "{\"body\":\"" . $body . "\","
. "\"subject\": \"" . $subject . "\","
. "\"out_trade_no\": \"" . $out_trade_no . "\","
. "\"timeout_express\": \"30m\","
. "\"total_amount\": \"" . $total . "\","
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}";
$request->setNotifyUrl($notify_url);
$request->setBizContent($bizcontent);
$request->setReturnUrl($return_url);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
// 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
return $reponse;
回调方法:异步回调不需要配置,但是要在上面方法中给对应的回调地址
代码:
//异步通知回调--app
public function aliNotify(){
if($_GET)file_put_contents('paylog.txt',"GET data:".print_r($_GET,true).'\n\n',FILE_APPEND);
if($_POST)file_put_contents('paylog.txt',"POST data:".print_r($_POST,true).'\n\n',FILE_APPEND);
//上面这个是记录支付参数,便于查看
try{
$info = $this->request->param();
$aop = new \AopClient;
$aop->alipayrsaPublicKey = 'XXXX';//私钥
$flag = $aop->rsaCheckV1($info,NULL,"RSA2"); //验签
if(!$flag){
Log::write('验签失败','ALIPAY_THORW');
}else if($info['trade_status']=='TRADE_SUCCESS'){
//支付状态
$res=Db::name('order')->where('order_id','=',$info['out_trade_no'])->find();
if(empty($res) ){
Db::name('order')->where('trade_no','=',$info['out_trade_no'])->update(['status'=>2,'transaction_id'=>$info['trade_no']]);
}
}
}
catch (Exception $e){
Log::write($e->getMessage(),'ALIPAY_THORW');
}
}
也可以加同步回调,支付完成后前端可以进行同步回调跳转想要的页面