php支付寶APP支付功能
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WordPress中設(shè)置Post Type自定義文章類型的實(shí)例教程
這篇文章主要介紹了WordPress中設(shè)置Post Type自定義文章類型的實(shí)例教程,后臺(tái)文章類型的設(shè)置是WordPress的一大特色,然而自帶的文章類型往往并不夠用,需要的朋友可以參考下2016-05-05tp5.1 框架數(shù)據(jù)庫常見操作詳解【添加、刪除、更新、查詢】
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫常見操作,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5.1針對(duì)數(shù)據(jù)庫的添加、刪除、更新、查詢相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-05-05thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法
這篇文章主要介紹了thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了thinkPHP5數(shù)據(jù)庫的配置、模型、控制器的使用及數(shù)據(jù)插入相關(guān)操作技巧,需要的朋友可以參考下2017-10-10php上傳文件,創(chuàng)建遞歸目錄的實(shí)例代碼
這篇文章介紹了php上傳文件,創(chuàng)建遞歸目錄的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10ThinkPHP3.2框架使用addAll()批量插入數(shù)據(jù)的方法
這篇文章主要介紹了ThinkPHP3.2框架使用addAll()批量插入數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了thinkPHP針對(duì)單條數(shù)據(jù)插入及批量數(shù)據(jù)插入操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03thinkphp3.2框架集成QRcode生成二維碼的方法分析
這篇文章主要介紹了thinkphp3.2框架集成QRcode生成二維碼的方法,結(jié)合實(shí)例形式分析了QRcode的下載、擴(kuò)展以及thinkphp3.2使用QRcode生成二維碼的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03