php使用支付宝sdk,v2版本实现小程序支付功能。
下载路径:https://opendocs.alipay.com/common/02kg67?pathHash=dd3eccd5,
使用php通用版,下载后直接复制v2版本
假设你已经浏览过《小程序支付产品介绍》,《接入准备》,《接入指南》,地址:
https://opendocs.alipay.com/mini/03l5wk?pathHash=6ed8a077
以下是使用php实现
第一步:统一收单交易创建接口
在实现支付宝小程序支付的时候,需要提前创建交易,接口返回交易号,使用返回的交易号,在小程序设备端,唤起支付功能进行支付。
//这个文件是基本类,每次使用都要引入,使用的是秘钥请求
require_once APPPATH.'vender/aop/AopClient.php';
//这个文件是创建交易接口类,实例哪个方法就引入哪个类
require_once APPPATH.'vender/aop/request/AlipayTradeCreateRequest.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥,不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='utf-8';
$aop->format='json';
$object = new stdClass();
$object->out_trade_no = time().rand(1,9999); //这是商家自定义交易号,需要保存到数据库
$object->total_amount = "0.01"; //这是支付金额,单位:元
$object->subject = "这是商品名称";
$object->buyer_open_id = "这是小程序授权用户的openid"; //支付宝接口文档里边没有说明,提交工单,让这样使用的
$object->timeout_express = '15m'; //订单超时时间15分钟,如果在订单中心继续支付,我直接重新生成订单号,所以这个意义不大
$object->product_code = 'JSAPI_PAY'; //小程序开通的支付方式
//这是分账功能,如果用户支付成功以后,需要给运营平台或者第三方分账,使用以下分钟功能
//https://opendocs.alipay.com/open/repo-0038ln?ref=api
if($fenzhang_status==1) {
//如果需要分账,则判断是否绑定分账关系,并且绑定
$bd = $this->panduanbangding($userinfo['id']);
if(!$bd) {
//绑定
$this->bangdingfenzhang($canshu);
//此参数是锁定商家的支付宝,支付金额,要不然商家收到钱,就转出去了,没钱分账了
//这个参数也是支付宝技术提供的
$extendParams = ['royalty_freeze'=>'true'];
$object->extend_params = $extendParams;
}
}else if($fenzhang_status==2){
//如果不需要分账,则判断是否绑定分账关系,并且解除绑定
$bd = $this->panduanbangding($canshu);
if($bd) {
//解绑,这里就不需要锁定支付金额了
$this->jiebangfenzhang($canshu);
}
}
$json = json_encode($object);
//这里实例哪个类就引入这个文件AlipayTradeCreateRequest.php,以后需要什么类直接引入对应的php即可
$request = new AlipayTradeCreateRequest();
$request->setNotifyUrl('https://xx.xx.com/Notify/noticeurl'); //记得开发设置
$request->setBizContent($json);
$responseResult = $aop->execute($request);
$result = json_encode($responseResult);
$result = json_decode($result,true);
if (isset($result['alipay_trade_create_response']) && isset($result['alipay_trade_create_response']['code']) && ($result['alipay_trade_create_response']['code']!=10000)) {
exit(json_encode(['status'=>'fail','msg'=>$result['alipay_trade_create_response']['msg'],'sub_msg'=>$result['alipay_trade_create_response']['sub_msg'],'code'=>$result['alipay_trade_create_response']['code']]));
}else {
$data_zfb = [
//这是商家自定义交易号
"out_trade_no" => $result['alipay_trade_create_response']['out_trade_no'],
//这是支付宝返回交易号
"trade_no" => $result['alipay_trade_create_response']['trade_no'],
];
$order_id = $this->Model->updateData("order",['id'=>$data['order_id']],$data_c);
if ($order_id) {
//参数返回给小程序就可以唤醒支付了
exit(json_encode(['status'=>'success','msg'=>'成功','code'=>0,'data'=>$data_zfb]));
}else {
exit(json_encode(['status'=>'fail','msg'=>'添加失败联系管理员','code'=>10030]));
}
}
public function panduanbangding($canshu)
{
require_once APPPATH.'vender/aop/request/AlipayTradeRoyaltyRelationBatchqueryRequest.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥(支付宝公钥),不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new AlipayTradeRoyaltyRelationBatchqueryRequest ();
$object = new stdClass();
$object->page_num = 1;
$object->page_size = 1;
$object->out_request_no = time().rand(1,9999)."fenzhang"; //这是商家自定义的请求号
$request->setBizContent(json_encode($object));
$result = $aop->execute($request);
$res = json_encode($result);
$res = json_decode($res,true);
if(isset($res['alipay_trade_royalty_relation_batchquery_response']) && ($res['alipay_trade_royalty_relation_batchquery_response']['code']==10000) && ($res['alipay_trade_royalty_relation_batchquery_response']['msg']=='Success')) {
if($res['alipay_trade_royalty_relation_batchquery_response']['total_record_num']==0) {
return false;
}else {
return true;
}
}else {
exit(json_encode(['status'=>'fail','msg'=>'请求错误','code'=>10027]));
}
}
public function bangdingfenzhang($user_id=0)
{
//以下代码中需要new一个类,所以直接引入该文件
require_once APPPATH.'vender/aop/request/AlipayTradeRoyaltyRelationBindRequest.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥(支付宝公钥),不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new AlipayTradeRoyaltyRelationBindRequest ();
$object = new stdClass();
$object2 = new stdClass();
$object2->type = "userId";
$object2->account = "此处用的是商家平台的商户号,2088开头的";
$object2->memo = "分账给运营商-这是说明";
$object->receiver_list = [$object2];
$object->out_request_no = time().rand(1,9999)."fenzhang"; //这个是自定义请求编号
$request->setBizContent(json_encode($object));
$result = $aop->execute($request);
$res = json_encode($result);
$res = json_decode($res,true);
if(isset($res['alipay_trade_royalty_relation_bind_response']) && ($res['alipay_trade_royalty_relation_bind_response']['code']==10000) && ($res['alipay_trade_royalty_relation_bind_response']['result_code']=='SUCCESS')) {
return true;
}else {
exit(json_encode(['status'=>'fail','msg'=>'分账绑定失败','code'=>10037]));
}
}
public function jiebangfenzhang($user_id=0)
{
require_once APPPATH.'vender/aop/request/AlipayTradeRoyaltyRelationUnbindRequest.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥(支付宝公钥),不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new AlipayTradeRoyaltyRelationUnbindRequest();
$object = new stdClass();
$object2 = new stdClass();
$object2->type = "userId";
$object2->account = "此处用的是商家平台的商户号,2088开头的";
$object2->memo = "分账给运营商-这是说明";
$object->receiver_list = [$object2];
$object->out_request_no = time().rand(1,9999)."fenzhang";
$request->setBizContent(json_encode($object));
$result = $aop->execute ( $request);
$res = json_encode($result);
$res = json_decode($res,true);
if(isset($res['alipay_trade_royalty_relation_unbind_response']) && ($res['alipay_trade_royalty_relation_unbind_response']['code']==10000) && ($res['alipay_trade_royalty_relation_unbind_response']['result_code']=='SUCCESS')) {
return true;
}else {
exit(json_encode(['status'=>'fail','msg'=>'分账解绑失败','code'=>10037]));
}
}
第二步,根据创建的交易订单号,唤起小程序的支付功能
my.tradePay({});
第三步,支付宝回调接口,异步处理订单状态
需要在支付宝后台添加异步链接
//https://xx.xx.com/xx/noticeurl
public function noticeurl()
{
$arr = $_POST;
$data_c = [
'log' => "支付异步回调",
'data' => json_encode($arr,JSON_UNESCAPED_UNICODE),
'type' => 1,
'inputtime' => date("Y-m-d H:i:s"),
];
$this->Model->insertData("zfb_log",$data_c);
//对返回订单进行验签,使用php,自带sdk
$this->checksign($arr);
if ($arr['trade_status'] == 'TRADE_SUCCESS') {
$out_trade_no = $arr['out_trade_no'];
$trade_no = $arr['trade_no'];
$total_amount = $arr['total_amount'];
//通过返回信息验证订单是否存在,订单是否正确,订单状态是否正确,然后更改状态,支付时间
$orderinfo = $this->Model->getDataInfo("id,price,status,trade_no,out_trade_no,user_id"," and out_trade_no='{$out_trade_no}'","zfb_order",1);
if (empty($orderinfo)) {
echo "fail";
exit;
}
if ($orderinfo['status'] !=1) {
echo "success";
exit;
}
if ($total_amount != $orderinfo['price']) {
echo "fail";
exit;
}
$res = $this->Model->updateData("zfb_order",['id'=>$orderinfo['id']],['status'=>2,'paytime'=>time(),'trade_no'=>$trade_no]);
if (!$res) {
echo "fail";
exit;
}
if($fenzhang_status==1) {
//如果需要商家分账则执行分账操作
sleep(1.5);
$orderinfo['trade_no'] = $trade_no;
$this->fenzhang($orderinfo);
}
}
echo "success";
}
//支付宝自带验签php sdk
public function checksign($data)
{
require_once APPPATH.'vender/aop/AopClient.php';
$aop = new AopClient ();
$aop->postCharset="UTF-8";
$aop->alipayrsaPublicKey="此处是支付宝公钥,支付宝公钥,";
$sign_type="RSA2";
$flag = $aop->rsaCheckV1($data, null, $sign_type);
if (!$flag){
echo "fail";
exit();
}
return true;
}
//支付宝对于新的商家,当天结算的金额不能进行分账,支出成功以后回调接口,会尝试第一次分账,并记录日志,如果成功则更改订单状态,如果失败做定时任务,24小时后更新状态
public function fenzhang($orderinfo)
{
require_once APPPATH.'vender/aop/request/AlipayTradeOrderSettleRequest.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥(支付宝公钥),不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new AlipayTradeOrderSettleRequest();
$object = new stdClass();
//自定义分账请求编号
$out_request_no = time().'fenzhangnum'.rand(1,9999);
$object->out_request_no = $out_request_no;
$object->trade_no = $orderinfo['trade_no']; //这是支付宝交易号,支付宝返回的,不是自定义的
$royalty_parameters = [
[
"royalty_type" => "transfer",
"trans_in_type" => "userId",
"trans_in" => "分账商户号,2088开头",
"amount" => "分账金额",
"desc" => "分账",
]
];
$extend_params = ['royalty_finish'=>'true']; //分账成功是否要解冻资金
$object->extend_params = $extend_params;
$object->royalty_parameters = $royalty_parameters;
$object->royalty_mode = "sync"; //同步执行分账
$request->setBizContent(json_encode($object));
$result = $aop->execute ($request);
$res = json_encode($result);
$res = json_decode($res,true);
$data_log = [
'data' => json_encode($res,JSON_UNESCAPED_UNICODE)."##".json_encode($object,JSON_UNESCAPED_UNICODE),
'type' => 3,
'inputtime' => date("Y-m-d H:i:s"),
];
if(isset($res['alipay_trade_order_settle_response']) && ($res['alipay_trade_order_settle_response']['code']==10000) && ($res['alipay_trade_order_settle_response']['msg']=='Success')) {
//可以更新分账状态
$data_log['log'] = "同步分账成功";
}else {
$data_log['log'] = "同步分账失败";
}
$this->Model->insertData("zfb_log",$data_log);
return true;
}
二级标题,订单退款,原路返回用户支付宝
require_once APPPATH.'vender/aop/AopClient.php';
require_once APPPATH.'vender/aop/request/AlipayTradeRefundRequest.php';
//退款编号,商家自定义
$out_request_no = $orderinfo['out_trade_no'].$orderinfo['id'];
$aop = new AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = "这是小程序appid,在支付宝小程序后台复制";
$aop->rsaPrivateKey = "这是接口加密秘钥(应用私钥),不要搞错了";
$aop->alipayrsaPublicKey= "这是接口加密支付宝公钥(支付宝公钥),不要搞错了";
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$object = new stdClass();
$object->trade_no = "支付宝交易号,不是商家自定义的";
$object->refund_amount = "退款金额";
$object->out_request_no = $out_request_no;
$json = json_encode($object);
$request = new AlipayTradeRefundRequest();
$request->setBizContent($json);
$result = $aop->execute ($request);
$res = json_encode($result);
$res = json_decode($res,true);
$data_c = [
'log' => "退款同步结果",
'data' => json_encode($res,JSON_UNESCAPED_UNICODE),
'type' => 2,
'inputtime' => date("Y-m-d H:i:s"),
];
$this->Model->insertData("zfb_log",$data_c);
if (($res['alipay_trade_refund_response']['code']==10000) && ($res['alipay_trade_refund_response']['fund_change']=='Y'))
{
//更改订单状态
exit(json_encode(['status'=>'success','msg'=>'退款成功','code'=>0]));
}else {
exit(json_encode(['status'=>'success','msg'=>'退款失败','code'=>0]));
}
以上就是全部的支付宝小程序支付功能,使用php SDK v2版本实现,在开发过程中,可以直接复制开发文档中的示例代码,然后引入对应的文件就可以开发,引入的文件名称就是:实例的类名.php。