日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

PHP实现小程序微信支付V2获取prepay_id

發(fā)布時(shí)間:2023/12/14 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP实现小程序微信支付V2获取prepay_id 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

<?php require_once('./API_Connect.php');$openid = $_POST['openid']; $body = $_POST['description']; $total = $_POST['total']; //$orderName = $_GET['orderName']; $description = $body; $total_fee = $total;//echo $openid;$getData = new v2Connect;$nonce_str = $getData->nonce_str(); //獲取隨機(jī)字符串$out_trade_no = $getData->out_trade_no(); //獲取商家訂單號(hào)$sign = $getData->getSign($body, $nonce_str, $openid, $out_trade_no, $total_fee); //獲取簽名值$getData->request_body($nonce_str, $sign, $description, $openid, $out_trade_no, $total); //獲取請(qǐng)求數(shù)據(jù)的主體$data = file_get_contents('./xmlTest.xml'); //將主體內(nèi)容存入變量 $URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //發(fā)起預(yù)支付的請(qǐng)求地址 $result = $getData->connect($URL, $data); //接收預(yù)支付的prepay_id參數(shù) file_put_contents('./result.xml', $result); //將返回的數(shù)據(jù)存入文本$preData = file_get_contents('./result.xml'); $getPrepay = simplexml_load_string($preData); $prepay_id = $getPrepay -> prepay_id; echo $prepay_id;

以上代碼就完成了獲取prepay_id的過(guò)程

下面展示一下request_body生成的xml格式數(shù)據(jù)
xmlTest.xml

<?xml version="1.0"?> <xml><appid>為了隱私,此處顯示你的小程序的appid</appid><mch_id>為了隱私,此處顯示你的微信支付商戶號(hào)</mch_id><nonce_str>3KAERHLXTZBQ74VGO6YMJD2N01IUP5S9</nonce_str><sign>D178D6F5E3B841F976062E320217E04F</sign><body>0.38mm.pen</body><openid>為了隱私,此處我刪掉了,此處顯示獲取到的用戶的openid</openid><out_trade_no>20220721165837390737469</out_trade_no><total_fee>1</total_fee><spbill_create_ip>127.0.0.1</spbill_create_ip><notify_url>為了隱私,此處我刪掉了,顯示的是你的nitify_url微信訂單通知的回調(diào)地址</notify_url><trade_type>JSAPI</trade_type> </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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。