PHP实现小程序微信支付V2获取prepay_id
PS:本文旨在簡(jiǎn)單獲取prepay_id,只是簡(jiǎn)單的介紹一下流程,并非完整的訂單支付流程
小程序端JS代碼:
getxml(){var test = thiswx.getStorage({ //從緩存中獲取用戶的openidkey:'openid',success(res){console.log(res)test.setData({'openid':res.data})console.log(test.data.openid)}})wx.request({url: 'http://', //你的URl地址method:'POST',header:{'content-type':'application/x-www-form-urlencoded'},data:{'openid':test.data.openid, //用戶的opend'description':'0.38mm.pen', //商品描述,此處為簡(jiǎn)單寫(xiě)了個(gè)例子,根據(jù)需要進(jìn)行更改'total':1 //商品總金額,單位為分,根據(jù)需求可以進(jìn)行乘100},success(res){console.log(res)}})}PS:這些JS代碼通過(guò)點(diǎn)擊按鈕進(jìn)行觸發(fā)的,再次重申,本篇文章僅僅只是介紹獲取prepay_id的流程,以及本人在寫(xiě)代碼過(guò)程中遇到的一些問(wèn)題
PHP代碼:
API_connect.php
<?php require_once dirname(__DIR__) .'/getCurl/curlDock.php';class v2Connect {/*** @param $URL :訪問(wèn)的API接口地址* @param $data :通過(guò)POST傳遞的數(shù)據(jù),xml格式* @return bool|string :返回?cái)?shù)據(jù)*/public function connect($URL,$data){$this->action = curl_init();curl_setopt($this->action, CURLOPT_URL, $URL);curl_setopt($this->action, CURLOPT_HEADER, 0);//curl_setopt($this->action, CURLOPT_HTTPHEADER, 0);curl_setopt($this->action, CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($this->action, CURLOPT_SSL_VERIFYHOST, 0);curl_setopt($this->action, CURLOPT_RETURNTRANSFER, 1);curl_setopt($this->action, CURLOPT_CONNECTTIMEOUT, 60);curl_setopt($this->action, CURLOPT_POST, 1);curl_setopt($this->action, CURLOPT_POSTFIELDS, $data);$result = curl_exec($this->action);curl_close($this->action);return $result;}/*** @return string :返回隨機(jī)字符串32位*/public function nonce_str(): string{$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';$disposeData = str_shuffle($data);return substr($disposeData,0, 32);}/*** @return string :返回商戶訂單號(hào),非微信官方訂單號(hào)*/public function out_trade_no(): string{$data = '1234567890';$disposeData = str_shuffle($data);$getData = substr($disposeData,5);return date('Ymd').time().$getData;}/*** @param $body :商品描述,與$description相同* @param $nonce_str :隨機(jī)字符串32位* @param $openid :微信小程序用戶身份唯一標(biāo)識(shí)符* @param $out_trade_no :商品訂單號(hào),商家自行獲取,并非微信官方訂單號(hào)* @param $total_fee :訂單總金額* @return string :返回簽名,用于微信預(yù)支付訂單*/public function getSign ($body,$nonce_str,$openid,$out_trade_no,$total_fee): string{$data = 'appid=wx9c2877c657b56ed9&body='.$body.'&mch_id=1528263341&nonce_str='.$nonce_str."&".'notify_url=http://001.chutest.xyz/index.php&openid='.$openid.'&out_trade_no='.$out_trade_no.'&spbill_create_ip=127.0.0.1&total_fee='.$total_fee.'&trade_type=JSAPI&key=dv0p6271okvpcawwbin61ht3ds8y6vs7';$sign = strtoupper(MD5($data));return $sign;}/*** @param $nonce_str :隨機(jī)字符串32位* @param $sign :簽名,用于微信預(yù)支付訂單* @param $description :商品描述* @param $openid :微信小程序用戶身份唯一標(biāo)識(shí)符* @param $out_trade_no :商品訂單號(hào),此為商戶自行獲取,并非微信官方訂單號(hào)* @param $total :訂單總金額* @throws DOMException*/public function request_body($nonce_str,$sign,$description,$openid,$out_trade_no,$total){$getConfig = new curlConnect;$request_data = new DOMDocument();$request_data -> formatOutput = true;$xml = $request_data -> createElement('xml');$appid = $request_data -> createElement('appid',$getConfig -> appid);$mch_id = $request_data -> createElement('mch_id',$getConfig -> mchID);$nonce_str = $request_data -> createElement('nonce_str',$nonce_str);$sign = $request_data -> createElement('sign',$sign);//$signType = $request_data -> createElement('sign_type','MD5');$description = $request_data -> createElement('body',$description);$openid = $request_data -> createElement('openid',$openid);$out_trade_no = $request_data -> createElement('out_trade_no',$out_trade_no);$total_fee = $request_data -> createElement('total_fee',$total);$spbill_creat_ip = $request_data -> createElement('spbill_create_ip','127.0.0.1');$notify_url = $request_data -> createElement('notify_url','http://001.chutest.xyz/index.php');$trade_type = $request_data -> createElement('trade_type','JSAPI');$request_data -> appendChild($xml);$xml -> appendChild($appid);$xml -> appendChild($mch_id);$xml -> appendChild($nonce_str);$xml -> appendChild($sign);//$xml -> appendChild($signType);$xml -> appendChild($description);$xml -> appendChild($openid);$xml -> appendChild($out_trade_no);$xml -> appendChild($total_fee);$xml -> appendChild($spbill_creat_ip);$xml -> appendChild($notify_url);$xml -> appendChild($trade_type);$request_data -> save('./xmlTest.xml'); //此處可以在同級(jí)目錄下新建一個(gè)xmlTest.xml的文件用來(lái)看一下最后生成xml數(shù)據(jù)的樣子} }PS:注意以上PHP代碼中,request_body這一方法中的appid中的 i 是小寫(xiě)字母,若是將其寫(xiě)成大寫(xiě)字母 I 則會(huì)在之后的獲取微信返回?cái)?shù)據(jù)驗(yàn)證簽名失敗
下面是獲取prepay_id處理微信返回?cái)?shù)據(jù)的代碼
getPrepay_id.php
以上代碼就完成了獲取prepay_id的過(guò)程
下面展示一下request_body生成的xml格式數(shù)據(jù)
xmlTest.xml
下面展示一下,微信返回的包含prepay_id的xml數(shù)據(jù)
<xml><return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <mch_id><![CDATA[此處顯示你的商戶號(hào)]]></mch_id> <appid><![CDATA[此處顯示你的appid]]></appid> <nonce_str><![CDATA[TU7vv4aOH2sJdxkj]]></nonce_str> <sign><![CDATA[11A449D5E16CEB2B0423AE1EC6503E11]]></sign> <prepay_id><![CDATA[返回的prepay_id]]></prepay_id> <trade_type><![CDATA[JSAPI]]></trade_type> </xml>總之以上就是小程序微信支付獲取prepay_id的流程,還是更推薦大家去仔細(xì)看一下微信支付開(kāi)發(fā)者文檔,里面寫(xiě)的流程還是很全面的,本篇文章僅供大家參考,文章有紕漏之處,歡迎指正.
本篇文章原創(chuàng)為CSDN用戶:繾綣淡藍(lán)海
總結(jié)
以上是生活随笔為你收集整理的PHP实现小程序微信支付V2获取prepay_id的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【学习】——提问的智慧
- 下一篇: 动态规划算法php,php算法学习之动态