小程序支付
在小程序中,用户也可以唤起支付宝收银台进行支付,功能与APP 支付基本一致
准备
1、创建小程序,添加小程序支付能力,功能状态变为“已生效” 2、集成 Alipay SDK 3、appId(应用ID)、privateKey(开发者私钥)、publicKey(支付宝公钥)
服务端
public function aliPay($out_trade_no
, $total_amount
, $buyer_id
, $body
= '会员充值', $subject
= '会员充值', $timeout_express
= '30m', $product_code
= 'FACE_TO_FACE_PAYMENT')
{
$appId
= '应用ID';
$privateKey
= '开发者私钥';
$publicKey
= '支付宝公钥';
$notify_url
= '接收支付结果通知的回调地址';
$biz_data
=[
'out_trade_no' => $out_trade_no
,
'total_amount' => $total_amount
,
'buyer_id' => $buyer_id
,
'body'=> $body
,
'subject'=> $subject
,
'timeout_express'=> $timeout_express
,
'product_code'=> $product_code
,
];
$aop
= new AopClient();
$aop
->gatewayUrl
= "https://openapi.alipay.com/gateway.do";
$aop
->appId
= $appId
;
$aop
->rsaPrivateKey
= $privateKey
;
$aop
->alipayrsaPublicKey
= $publicKey
;
$aop
->apiVersion
= '1.0';
$aop
->signType
= "RSA2";
$aop
->postCharset
= "UTF-8";
$aop
->format
= "json";
$request
= new AlipayTradeCreateRequest();
$request
->setNotifyUrl($notify_url
);
$request
->setBizContent(json_encode($biz_data
));
$result
= $aop
->execute($request
);
$responseNode
= str_replace(".", "_", $request
->getApiMethodName()) . "_response";
$resultCode
= $result
->$responseNode
->code
;
if (!empty($resultCode
) && $resultCode
== 10000) {
$re_array
=['trade_no'=> $result
->$responseNode
->trade_no
];
return ['status' => true, 'msg' => $result
->$responseNode
->msg
, 'data' => $re_array
];
} else {
return ['status' => false, 'msg' => $result
->$responseNode
->msg
, 'data' => []];
}
}
public function notify()
{
echo
'success';
}
小程序支付
my
.tradePay({
tradeNO
:'trade_no',
success
: (res
) => {
content
: JSON.stringify(res
)
},
fail
: (res
) => {
content
: JSON.stringify(res
)
}
});
更多信息参考文档
https://opendocs.alipay.com/mini/introduce/pay