本文實(shí)例為大家分享了php支付寶APP支付的具體代碼,供大家參考,具體內(nèi)容如下
支付寶網(wǎng)頁支付
1.支付寶開放平臺(tái)添加應(yīng)用,獲得appid,并簽約。
2.在支付寶開放品臺(tái)設(shè)置如下:

3.配置支付寶的應(yīng)用公鑰。(根據(jù)支付寶的文檔)
4.在開放平臺(tái)下載官方sdk demo。
5.代碼:
//支付寶
include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
include_once VENDOR_PATH . 'Alipay/aop/request/AlipayTradeAppPayRequest.php';
$notify_url='https://www.www.com/app/pay/AlipayStep3Notify';
$config = array(
'appid' =>$this->appid,//
'rsaPrivateKey' =>$this->rsaPrivateKey,//開發(fā)者私鑰私鑰
'alipayrsaPublicKey'=>$this->alipayrsaPublicKey,//支付寶公鑰
'charset'=>strtolower('utf-8'),//編碼
'notify_url' =>$notify_url,//回調(diào)地址(支付寶支付成功后回調(diào)修改訂單狀態(tài)的地址)
'payment_type' =>1,//(固定值)
'seller_id' =>'',//收款商家賬號(hào)
'charset' => 'utf-8',//編碼
'sign_type' => 'RSA2',//簽名方式
'timestamp' =>date("Y-m-d H:i:s"),
'version' =>"1.0",//固定值
'url' => 'https://openapi.alipay.com/gateway.do',//固定值
'method' => 'alipay.trade.app.pay',//固定值
);
$aop = new \AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = $config['appid'];
$aop->rsaPrivateKey = $config['rsaPrivateKey'];
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey=$config['alipayrsaPublicKey'];
//實(shí)例化具體API對(duì)應(yīng)的request類,類名稱和接口名稱對(duì)應(yīng),當(dāng)前調(diào)用接口名稱:alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
//SDK已經(jīng)封裝掉了公共參數(shù),這里只需要傳入業(yè)務(wù)參數(shù)
$bizcontent = json_encode([
'body'=>'**',
'subject'=>$subject,
'out_trade_no'=> $order_sn,//此訂單號(hào)為商戶唯一訂單號(hào)
'total_amount'=>$totalprice,//保留兩位小數(shù)
'product_code'=>'QUICK_MSECURITY_PAY'
]);
$request->setNotifyUrl($config['notify_url']);
$request->setBizContent($bizcontent);
//這里和普通的接口調(diào)用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是為了輸出到頁面時(shí)防止被瀏覽器將關(guān)鍵參數(shù)html轉(zhuǎn)義,實(shí)際打印到日志以及http傳輸不會(huì)有這個(gè)問題
$datas=$response;//就是orderString 可以直接給客戶端請(qǐng)求,無需再做處理。
$this->arr['code']=0;
$this->arr['msg']=$order_sn;
$this->arr['info']=$datas;
echo json_encode($this->arr);exit;
6.支付回調(diào)notify_url。
include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
$aop = new \AopClient();
$config['alipayrsaPublicKey']=$this->$alipayrsaPublicKey;//公鑰
$aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
//此處驗(yàn)簽方式必須與下單時(shí)的簽名方式一致
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
//驗(yàn)簽通過后再實(shí)現(xiàn)業(yè)務(wù)邏輯,比如修改訂單表中的支付狀態(tài)。
/**
①驗(yàn)簽通過后核實(shí)如下參數(shù)out_trade_no、total_amount、seller_id
②修改訂單表
**/
$out_trade_no = I('post.out_trade_no'); //商戶訂單號(hào)
之后對(duì)數(shù)據(jù)庫對(duì)應(yīng)的數(shù)據(jù)進(jìn)行修改。
7.訂單查詢接口:
include_once VENDOR_PATH . 'Alipay/aop/SignData.php';
include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
include_once VENDOR_PATH . 'Alipay/aop/request/AlipayTradeQueryRequest.php';
$config = array(
'appid' =>$this->appid,//
'rsaPrivateKey' =>$this->rsaPrivateKey,//開發(fā)者私鑰私鑰
'alipayrsaPublicKey'=>$this->alipayrsaPublicKey,//支付寶公鑰
'charset'=>strtolower('utf-8'),//編碼
'notify_url' =>'',//回調(diào)地址(支付寶支付成功后回調(diào)修改訂單狀態(tài)的地址)
'payment_type' =>1,//(固定值)
'seller_id' =>'',//收款商家賬號(hào)
'charset' => 'utf-8',//編碼
'sign_type' => 'RSA',//簽名方式
'timestamp' =>date("Y-m-d H:i:s"),
'version' =>"1.0",//固定值
'url' => 'https://openapi.alipay.com/gateway.do',//固定值
'method' => 'alipay.trade.query',//固定值
);
$aop = new \AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = $config['appid'];
$aop->rsaPrivateKey = $config['rsaPrivateKey'];
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->method = $config['method'];
$aop->apiVersion = '1.0';
$aop->alipayrsaPublicKey=$config['alipayrsaPublicKey'];
//實(shí)例化具體API對(duì)應(yīng)的request類,類名稱和接口名稱對(duì)應(yīng),當(dāng)前調(diào)用接口名稱:alipay.trade.query
$request = new \AlipayTradeQueryRequest();
$bizcontent = json_encode([
'out_trade_no'=>$order_sn,
'trade_no'=>''
]);
$request->setBizContent($bizcontent);
$response = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $response->$responseNode->code;
if(!empty($resultCode)$resultCode == 10000){
$this->arr['code']=0;
$this->arr['msg']='success';
echo json_encode($this->arr);exit;
} else {
$this->arr['code']=100001;
$this->arr['msg']='未查詢到訂單信息';
echo json_encode($this->arr);exit;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- php實(shí)現(xiàn)的支付寶網(wǎng)頁支付功能示例【基于TP5框架】
- PHP支付寶當(dāng)面付2.0代碼
- php實(shí)現(xiàn)單筆轉(zhuǎn)賬到支付寶功能
- php app支付寶回調(diào)(異步通知)詳解
- PHP實(shí)現(xiàn)QQ、微信和支付寶三合一收款碼實(shí)例代碼
- PHP實(shí)現(xiàn)支付寶即時(shí)到賬功能
- thinkPHP框架對(duì)接支付寶即時(shí)到賬接口回調(diào)操作示例
- PHP接入支付寶接口失效流程詳解