PHP实现小程序微信支付V2获取prepay_id
生活随笔
收集整理的這篇文章主要介紹了
PHP实现小程序微信支付V2获取prepay_id
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PS:本文旨在簡單獲取prepay_id,只是簡單的介紹一下流程,并非完整的訂單支付流程
小程序端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', //商品描述,此處為簡單寫了個例子,根據需要進行更改'total':1 //商品總金額,單位為分,根據需求可以進行乘100},success(res){console.log(res)}})}PS:這些JS代碼通過點擊按鈕進行觸發的,再次重申,本篇文章僅僅只是介紹獲取prepay_id的流程,以及本人在寫代碼過程中遇到的一些問題
PHP代碼:
API_connect.php
<?php require_once dirname(__DIR__) .'/getCurl/curlDock.php';class v2Connect {/*** @param $URL :訪問的API接口地址* @param $data :通過POST傳遞的數據,xml格式* @return bool|string :返回數據*/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 :返回隨機字符串32位*/public function nonce_str(): string{$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';$disposeData = str_shuffle($data);return substr($disposeData,0, 32);}/*** @return string :返回商戶訂單號,非微信官方訂單號*/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 :隨機字符串32位* @param $openid :微信小程序用戶身份唯一標識符* @param $out_trade_no :商品訂單號,商家自行獲取,并非微信官方訂單號* @param $total_fee :訂單總金額* @return string :返回簽名,用于微信預支付訂單*/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 :隨機字符串32位* @param $sign :簽名,用于微信預支付訂單* @param $description :商品描述* @param $openid :微信小程序用戶身份唯一標識符* @param $out_trade_no :商品訂單號,此為商戶自行獲取,并非微信官方訂單號* @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'); //此處可以在同級目錄下新建一個xmlTest.xml的文件用來看一下最后生成xml數據的樣子} }PS:注意以上PHP代碼中,request_body這一方法中的appid中的 i 是小寫字母,若是將其寫成大寫字母 I 則會在之后的獲取微信返回數據驗證簽名失敗
下面是獲取prepay_id處理微信返回數據的代碼
getPrepay_id.php
以上代碼就完成了獲取prepay_id的過程
下面展示一下request_body生成的xml格式數據
xmlTest.xml
下面展示一下,微信返回的包含prepay_id的xml數據
<xml><return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <mch_id><![CDATA[此處顯示你的商戶號]]></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的流程,還是更推薦大家去仔細看一下微信支付開發者文檔,里面寫的流程還是很全面的,本篇文章僅供大家參考,文章有紕漏之處,歡迎指正.
本篇文章原創為CSDN用戶:繾綣淡藍海
總結
以上是生活随笔為你收集整理的PHP实现小程序微信支付V2获取prepay_id的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【学习】——提问的智慧
- 下一篇: windows 驱动开发 DDK与WDK