| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
最近由于項目重建,百度支付被重新包裝。這次增加了簽名處理和用戶退款,方便后期開發使用。
由于百度電商開放平臺的升級,支付功能被搬到了智能小程序上。具體申請流程:百度收銀支付開通指南,(https://smart program . Baidu . com/docs/operations/transform/pay/)。
(注意:在支付業務中,服務電話要填寫銀行預留手機號,如果填寫不正確,會上報銀行預留手機號格式驗證失敗。)
百度支付文檔:百度收銀接口2.0(https://smartprogram.baidu.com/docs/develop/function/tune _ up _ 2.0/)。
1.申請通過后,填寫百度支付的相關配置:
$ config = array(& amp;#39;交易id & amp#39;= & gt& amp#39;& amp#39;//百度收銀的財務結算憑證&;#39;app _ key & amp#39;= & gt& amp#39;& amp#39;,//表示應用程序標識的唯一ID#39;私人密鑰& amp#39;= & gt& amp#39;& amp#39;,//私鑰原始字符串&;#39;公鑰& amp#39;= & gt& amp#39;& amp#39;//平臺公鑰&;#39;通知_網址& amp#39;= & gt& amp#39;& amp#39;,//支付回撥地址);第二,調用封裝的支付方法,將返回的信息傳遞給百度小程序。
& lt?phpinclude & amp#39;。/Baidu pay . PHP & amp;#39;;$ Baidu pay = new \ feng \ Baidu pay($ config);$order_sn = time()。蘭特(10009999);$ order = array(& amp;#39;身體與健康。#39;= & gt& amp#39;檢驗貨物& amp;#39;//產品描述&;#39;總額及金額。#39;= & gt& amp#39;1 & amp#39;,//訂單金額(分鐘)&;#39;訂單序號& amp#39;= & gt$order_sn,//訂單號);$re = $baidupay->xcx pay($ order);die(JSON _ encode($ re));// JSON直接返回小程序客戶端PHP小程序支付類xcxPay:
/** * [xcxPay百度小程序支付]* @ param[type]$ order[訂單信息數組]* @ return[type][description]* $ order = array(* &;#39;身體與健康。#39;= & gt& amp#39;& amp#39;,//產品描述* &;#39;總額及金額。#39;= & gt& amp#39;& amp#39;,//訂單金額(分鐘)* &;#39;訂單序號& amp#39;= & gt& amp#39;& amp#39;,//訂單號*);*/公共靜態函數xcxPay($order){ if(!is _ array($ order)| | count($ order)& lt;3)模具(& quot缺少數組數據信息!");$ config = self::$ config;$ requestParamsArr = array(& amp;#39;appKey & amp#39;= & gt$ config[& amp;#39;app _ key & amp#39;],& amp#39;dealId & amp#39;= & gt$ config[& amp;#39;交易id & amp#39;],& amp#39;tpOrderId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],& amp#39;總金額& amp#39;= & gt$ order[& amp;#39;總額及金額。#39;], );$ RSA sign = self::makeSign($ requestParamsArr,$ config[& amp;#39;私人密鑰& amp#39;]);//聲稱百度支付簽名$ bizinfo = array(&;#39;tpData & amp#39;= & gt數組(& quotappKey & quot= & gt$ config[& amp;#39;app _ key & amp#39;],& quotdealId & quot= & gt$ config[& amp;#39;交易id & amp#39;],& quottpOrderId & quot= & gt$ order[& amp;#39;訂單序號& amp#39;],& quotrsaSign & quot= & gt$rsaSign,& quot總金額& quot= & gt$ order[& amp;#39;總額及金額。#39;],& quotreturnData & quot= & gt& amp#39;& amp#39;,& quot顯示數據= & gt數組(& quotcashierTopBlock & quot= & gtarray(數組([& quot;leftCol"= & gt"訂單名稱",& quot右欄& quot= & gt$ order[& amp;#39;身體與健康。#39;] ],[& quot;leftCol"= & gt"數量",& quot右欄& quot= & gt"1 & quot],[& quot;leftCol"= & gt"訂單金額",& quot右欄& quot= & gt$ order[& amp;#39;總額及金額。#39;] ])、數組([& quot;leftCol"= & gt"服務地址",& quot右欄& quot= & gt"北京市海淀區上地十街10號百度大廈],[& quot;leftCol"= & gt"服務時間”右欄& quot= & gt"2018/10/29 14:51 & quot;],[& quot;leftCol"= & gt"服務人員",& quot右欄& quot= & gt"百度App & quot])))),& quot交易標題& quot= & gt$ order[& amp;#39;身體與健康。#39;],& quotdealSubTitle & quot= & gt$ order[& amp;#39;身體與健康。#39;],& quotdealThumbView & quot= & gt"https://b . BD static . com/search box/icms/search box/img/swan -logo . png & quot;,),& quotorderDetailData & quot= & gt& amp#39;& amp#39;);$ b order = array(& amp;#39;dealId & amp#39;= & gt$ config[& amp;#39;交易id & amp#39;],& amp#39;appKey & amp#39;= & gt$ config[& amp;#39;app _ key & amp#39;],& amp#39;總金額& amp#39;= & gt$ order[& amp;#39;總額及金額。#39;],& amp#39;tpOrderId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],& amp#39;dealTitle & amp#39;= & gt$ order[& amp;#39;身體與健康。#39;],& amp#39;signFieldsRange & amp#39;= & gt1,& amp#39;rsaSign & amp#39;= & gt$ RSA sign & amp;#39;bizInfo & amp#39;= & gtjson_encode($bizInfo),);返回$ bdOrder}三、百度智能小程序的使用
天鵝
& ltview class = & quotwrap & quot& gt& ltview class = & quotcard -區域& quot& gt& lt按鈕綁定:tap = & quotrequestPolymerPayment & quottype = & quot主要& quot懸停-停止-傳播= & quottrue & quot& gt支付0.01元</button & gt;& lt/view & gt;& lt/view & gt;HTMLJS
page({ requestPolymerPayment(e){ swan . request({ URL:& amp;#39;https://mbd.baidu.com/xxx&第39名;//只是一個例子,不是真正的接口地址。開發人員從真實接口獲取orderInfo的值。成功:res = & gt{ RES . data . data . deal title = & amp;#39;百度小程序演示支付測試& amp;#39;;設data = res.dataif (data.errno!= = 0){ console . log(& amp;#39;創建訂單錯誤& amp#39;,數據);返回;} swan . requestpolymerpayment({ order info:data . data,success:RES = & gt;{ swan . show toast({ title:& amp;#39;支付成功&;#39;,圖標:& amp#39;成功& amp#39;});console . log(& amp;#39;支付成功和。#39;,RES);},失敗:err = & gt{ swan . show toast({ title:err . errmsg,icon:& amp;#39;無& amp#39;});console . log(& amp;#39;支付失敗和。#39;,err);} });},失敗:err = & gt{ swan . show toast({ title:& amp;#39;訂單創建失敗&;#39;,圖標:& amp#39;無& amp#39;});console . log(& amp;#39;創建訂單失敗。#39;,err);} });}});第四,回款
& lt?phpinclude & amp#39;。/Baidu pay . PHP & amp;#39;;$ Baidu pay = new \ feng \ Baidu pay($ config);$re = $baidupay->notify();If ($re) {//這里回調處理訂單操作//驗證支付成功后返回的信息,可以直接操作訂單。微信已獲悉支付成功$ Baidu pay -& gt;success();//支付成功返回,通知結果} else {//支付失敗$ Baidu pay -& gt;錯誤();//支付失敗,返回狀態(支付成功與否需要通知百度)}百度完整支付類(BaiduPay.php),包括小程序支付、簽收、回撥、退款:
& lt?PHP/* * * * @作者:[馮]* @日期:2020-09-27t 16:28:3zhujicankao08:00 * @最后修改人:[馮]* @最后修改時間:2020-10-15t 10:23:07+08:00 */命名空間馮;class Baidu pay { private static $ config = array(& amp;#39;交易id & amp#39;= & gt& amp#39;& amp#39;//百度收銀的財務結算憑證&;#39;app _ key & amp#39;= & gt& amp#39;& amp#39;,//表示應用程序標識的唯一ID#39;私人密鑰& amp#39;= & gt& amp#39;& amp#39;,//私鑰原始字符串&;#39;公鑰& amp#39;= & gt& amp#39;& amp#39;//平臺公鑰&;#39;通知_網址& amp#39;= & gt& amp#39;& amp#39;,//支付回撥地址);/* * *[_ _ construct constructor]* @ param[type]$ config[轉移支付相關配置]*/public function _ _ construct($ config = null){ $ config &;& ampself::$ config = $ config;} /** * [xcxPay百度小程序支付]* @ param[type]$ order[訂單信息數組]* @ return[type][description]* $ order = array(* &;#39;身體與健康。#39;= & gt& amp#39;& amp#39;,//產品描述* &;#39;總額及金額。#39;= & gt& amp#39;& amp#39;,//訂單金額(分鐘)* &;#39;訂單序號& amp#39;= & gt& amp#39;& amp#39;,//訂單號*);*/公共靜態函數xcxPay($order) { if(!is _ array($ order)| | count($ order)& lt;3)模具(& quot缺少數組數據信息!");$ config = self::$ config;$ requestParamsArr = array(& amp;#39;appKey & amp#39;= & gt$ config[& amp;#39;app _ key & amp#39;],& amp#39;dealId & amp#39;= & gt$ config[& amp;#39;交易id & amp#39;],& amp#39;tpOrderId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],& amp#39;總金額& amp#39;= & gt$ order[& amp;#39;總額及金額。#39;], );$ RSA sign = self::makeSign($ requestParamsArr,$ config[& amp;#39;私人密鑰& amp#39;]);//聲稱百度支付簽名$ bizinfo = array(&;#39;tpData & amp#39;= & gt數組(& quotappKey & quot= & gt$ config[& amp;#39;app _ key & amp#39;],& quotdealId & quot= & gt$ config[& amp;#39;交易id & amp#39;],& quottpOrderId & quot= & gt$ order[& amp;#39;訂單序號& amp#39;],& quotrsaSign & quot= & gt$rsaSign,& quot總金額& quot= & gt$ order[& amp;#39;總額及金額。#39;],& quotreturnData & quot= & gt& amp#39;& amp#39;,& quot顯示數據= & gt數組(& quotcashierTopBlock & quot= & gtarray(數組([& quot;leftCol"= & gt"訂單名稱",& quot右欄& quot= & gt$ order[& amp;#39;身體與健康。#39;] ],[& quot;leftCol"= & gt"數量",& quot右欄& quot= & gt"1 & quot],[& quot;leftCol"= & gt"訂單金額",& quot右欄& quot= & gt$ order[& amp;#39;總額及金額。#39;] ])、數組([& quot;leftCol"= & gt"服務地址",& quot右欄& quot= & gt"北京市海淀區上地十街10號百度大廈],[& quot;leftCol"= & gt"服務時間”右欄& quot= & gt"2018/10/29 14:51 & quot;],[& quot;leftCol"= & gt"服務人員",& quot右欄& quot= & gt"百度App & quot])))),& quot交易標題& quot= & gt$ order[& amp;#39;身體與健康。#39;],& quotdealSubTitle & quot= & gt$ order[& amp;#39;身體與健康。#39;],& quotdealThumbView & quot= & gt"https://b . BD static . com/search box/icms/search box/img/swan -logo . png & quot;,),& quotorderDetailData & quot= & gt& amp#39;& amp#39;);$ b order = array(& amp;#39;dealId & amp#39;= & gt$ config[& amp;#39;交易id & amp#39;],& amp#39;appKey & amp#39;= & gt$ config[& amp;#39;app _ key & amp#39;],& amp#39;總金額& amp#39;= & gt$ order[& amp;#39;總額及金額。#39;],& amp#39;tpOrderId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],& amp#39;dealTitle & amp#39;= & gt$ order[& amp;#39;身體與健康。#39;],& amp#39;signFieldsRange & amp#39;= & gt1,& amp#39;rsaSign & amp#39;= & gt$ RSA sign & amp;#39;bizInfo & amp#39;= & gtjson_encode($bizInfo),);返回$ bdOrder}/* * *[退款百度支付退款]* @ param[type]$ order[訂單信息]* @ param[type]$ type[退款類型]* $ order = array(* &;#39;身體與健康。#39;= & gt& amp#39;& amp#39;,//退款原因* &;#39;總額及金額。#39;= & gt& amp#39;& amp#39;,//退款金額(積分)* &;#39;訂單序號& amp#39;= & gt& amp#39;& amp#39;,//訂單號*&;#39;訪問令牌& amp#39;= & gt& amp#39;& amp#39;//獲取開發者的服務權限描述* &;#39;訂單id & amp#39;= & gt& amp#39;& amp#39;,//百度本票ID * &;#39;用戶id & amp#39;= & gt& amp#39;& amp#39;,//百度收銀用戶id *);*/公共靜態函數退款($order=[],$ type = 1){ $ config = self::$ config;$ data = array(& amp;#39;訪問令牌& amp#39;= & gt$ order[& amp;#39;訪問令牌& amp#39;],//獲取開發者服務權限描述&;#39;applyRefundMoney & amp#39;= & gt$ order[& amp;#39;總額及金額。#39;],//退款金額分鐘。& amp#39;bizRefundBatchId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],//開發者退款批量&;#39;isSkipAudit & amp#39;= & gt1.//是否跳過審核,如果不要求百度要求開發者退還審核費用,請通過1;默認值為0;0:不要跳過開發者業務審核;1.跳過開發者商業評論。& amp#39;訂單& amp#39;= & gt$ order[& amp;#39;訂單id & amp#39;],//百度收銀訂單ID &;#39;退款理由和。#39;= & gt$ order[& amp;#39;身體與健康。#39;],//退款原因&;#39;退款類型& amp#39;= & gt$type,//退款類型1:用戶發起的退款;2:開發商業務端的客服退款;3:開發者服務異常退款。& amp#39;tpOrderId & amp#39;= & gt$ order[& amp;#39;訂單序號& amp#39;],//開發者訂單id &;#39;用戶& amp#39;= & gt$ order[& amp;#39;用戶id & amp#39;],//百度收銀用戶id);$ array =[& amp;#39;errno & amp#39;= & gt0,& amp#39;味精& amp#39;= & gt& amp#39;成功& amp#39;,& amp#39;數據與信息。#39;= & gt[& amp;#39;isConsumed & amp#39;= & gt2] ];$ url = & amp#39;https://open API . Baidu . com/rest/2.0/smart app/pay/paymentservice/applyOrderRefund & amp;#39;;$response = self::post_curl($url,$ data);$result = json_decode($response,true);////顯示錯誤消息//if($result[&;#39;味精& amp#39;]!= & amp#39;成功& amp#39;){ //返回false////die($ result[& amp;#39;味精& amp#39;]);// }返回$ result}/* * *[通知回調驗證]* @ return[array][返回數組格式的通知數據]*/公共靜態函數notify(){ $ data = $ _ post;//get XML $ config = self::$ config;如果(!$ data | | empty($ data[& amp;#39;rsaSign & amp#39;]))骰子(& amp#39;目前沒有回撥信息&;#39;);$result = self::checkSign($data,$ config[& amp;#39;公鑰& amp#39;]);//驗證簽名//判斷簽名是否正確并判斷支付狀態if($ result & amp;& amp$ data[& amp;#39;地位與責任。#39;]= = 2){ return $ data;} else {返回false} }/* *[成功通知支付狀態]*/公共靜態函數success(){ $ array =[&;#39;errno & amp#39;= & gt0,& amp#39;味精& amp#39;= & gt& amp#39;成功& amp#39;,& amp#39;數據與信息。#39;= & gt[& amp;#39;isConsumed & amp#39;= & gt2] ];die(JSON _ encode($ array));}/* * *[錯誤通知支付狀態]*/公共靜態函數error(){ $ array =[&;#39;errno & amp#39;= & gt0,& amp#39;味精& amp#39;= & gt& amp#39;成功& amp#39;,& amp#39;數據與信息。#39;= & gt[& amp;#39;iserrorrorder & amp;#39;= & gt1,& amp#39;isConsumed & amp#39;= & gt2] ];die(JSON _ encode($ array));} /** * [makeSign使用私鑰生成簽名字符串]* @ param array $ assocar[into parameter group]* @ param[type]$ rsalikeystr[私鑰原始字符串,不帶PEM格式的前綴和后綴]* @ return[type][簽名結果字符串] */public靜態函數making(array $ assocar,$ rsalikeystr){ $ sign = &;#39;& amp#39;;if(empty($ rsaPriKeyStr)| | empty($ assocar)){ return $ sign;}如果(!函數_ exists(& amp;#39;openssl _ pkey _ get _ private & amp#39;) || !函數_ exists(& amp;#39;openssl _ sign & amp#39;)){拋出新異常(& quotOpenssl擴展不存在”);} $ rsaPriKeyPem = self::convertrsakeystr 2 PEM($ rsaPriKeyStr,1);$ priKey = OpenSSL _ pkey _ get _ private($ rsaPriKeyPem);if(isset($ asso carr[& amp;#39;標志與廣告。#39;]){ unset($ asso carr[& amp;#39;標志與廣告。#39;]);} ksort($ asso carr);//參數按字典順序排序$ parts = array();foreach($ asso carr as $ k = & gt;$ v){ $ parts[]= $ k & amp;#39;= & amp#39;。$ v;} $str =內爆(& amp#39;& amp& amp#39;,$ parts);openssl_sign($str,$sign,$ priKey);OpenSSL _ free _ key($ priKey);返回base64 _ encode($ sign);} /** * [checkSign使用公鑰檢查簽名]* @ param array $assocArr[輸入參數數據,簽名屬性名固定為RSA SIG]* @ param[type]$ rsapubkeystr[公鑰原始字符串,不帶PEM格式的前綴和后綴]* @ return[type][校驗符號通過|假校驗符號失敗]*/公共靜態函數校驗符號(array $ asso carr,$ rsapubkeystr) {if(!isset($ asso carr[& amp;#39;rsaSign & amp#39;])| | empty($ assocar)| | empty($ rsaPubKeyStr)){ return false;}如果(!函數_ exists(& amp;#39;openssl _ pkey _ get _ public & amp#39;) || !函數_ exists(& amp;#39;openssl _ verify & amp#39;)){拋出新異常(& quotOpenssl擴展不存在”);} $ sign = $ asso carr[& amp;#39;rsaSign & amp#39;];unset($ asso carr[& amp;#39;rsaSign & amp#39;]);if(empty($ assocar)){返回false} ksort($ asso carr);//參數按字典順序排序$ parts = array();foreach($ asso carr as $ k = & gt;$ v){ $ parts[]= $ k & amp;#39;= & amp#39;。$ v;} $str =內爆(& amp#39;& amp& amp#39;,$ parts);$ sign = base64 _ decode($ sign);$ rsaPubKeyPem = self::convertrsakeystr 2 PEM($ rsaPubKeyStr);$ pubKey = OpenSSL _ pkey _ get _ public($ rsaPubKeyPem);$ result =(bool)OpenSSL _ verify($ str,$sign,$ pubKey);OpenSSL _ free _ key($ pubKey);返回$ result} /** * [convertRSAKeyStr2Pem將密鑰從字符串(無換行符)轉換為Pem格式]* @ param[type]$ rsaKeyStr[原始密鑰字符串] * @param integer $keyType [0公鑰|1私鑰,默認為0] * @return [type] [PEM格式密鑰]*/公共靜態函數convertrsakeystr 2 PEM($ rsaKeyStr,$ key type = 0){ $ PEM width = 64;$ rsaKeyPem = & amp#39;& amp#39;;$ begin = & amp#39;----BEGIN & amp;#39;;$ end = & amp#39;-----END & amp;#39;;$ key = & amp#39;-----& amp;#39;;$type = $keyType?& amp#39;RSA私人& amp#39;:& amp#39;公共& amp#39;;$keyPrefix = $begin。$type。$ key$keySuffix = $end。$type。$ key$rsaKeyPem。= $keyPrefix。"\ n & quot;$rsaKeyPem。= wordwrap($rsaKeyStr,$pemWidth,& quot\ n & quot,真)。"\ n & quot;$rsaKeyPem。= $ keySuffix如果(!函數_ exists(& amp;#39;openssl _ pkey _ get _ public & amp#39;) || !函數_ exists(& amp;#39;openssl _ pkey _ get _ private & amp#39;)){返回false} if($ key type = = 0 & amp;& ampfalse = = OpenSSL _ pkey _ get _ public($ rsaKeyPem)){ return false;} if($ key type = = 1 & amp;& ampfalse = = OpenSSL _ pkey _ get _ private($ rsaKeyPem)){ return false;}返回$ rsaKeyPem} /** * curl post請求* @param string $url地址* @param string $postData數據* @param array $header頭* @ return bool | string * @ Date 2020/9/17 17:12 * @ Author wzb */public static function post _ curl($ URL = & amp;#39;& amp#39;,$ postData = & amp#39;& amp#39;,$ header =[]){ $ ch = curl _ init($ URL);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_HEADER,false);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5000);curl_setopt($ch,CURLOPT_TIMEOUT,5000);if($header){ curl_setopt($ch,CURLOPT_HTTPHEADER,$ header);} curl_setopt($ch,CURLOPT_POSTFIELDS,$ post data);$ result = curl _ exec($ ch);$httpCode = curl_getinfo($ch,curl info _ HTTP _ CODE);$ curlErrNo = curl _ errno($ ch);$ curlErr = curl _ error($ ch);curl _ close($ ch);返回$ result}}
這幾篇文章你可能也喜歡:
本文由主機參考刊發,轉載請注明:重新編寫打包百度支付(百度智能小程序支付)(百度支付叫什么) http://www.k2270.com/118291.html
主機參考

















評論前必須登錄!
注冊