微信Native下單V2版
阿新 • • 發佈:2021-06-24
商戶Native支付下單介面,微信後臺系統返回連結引數code_url,
商戶後臺系統將code_url值生成二維碼圖片,使用者使用微信客戶端掃碼後發起支付。
1.SDK與DEMO下載
https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
2.拷貝business與lib資料夾
3.將下載的證書解壓後拷貝到cert資料夾下
4.修改lib資料夾下的DemoConfig檔案
public string GetAppID(){ return "繫結支付的APPID"; } public string GetMchID(){ return "商戶號"; } public string GetKey(){ return "配置金鑰時輸入的字串"; } public string GetSSlCertPath(){ return "cert\\apiclient_cert.p12"; } public string GetSSlCertPassword(){ return "證書密碼"; } public string GetNotifyUrl(){ return "支付回撥方法";//最好是使用域名 }
5.呼叫API返回支付Url
NativePay nativePay = new NativePay(); //生成掃碼支付模式二url WxPayModel wxPayModel = new WxPayModel(); wxPayModel.body = data.body; wxPayModel.attach = data.attach; wxPayModel.total_fee = data.total_fee; wxPayModel.goods_tag = data.goods_tag; wxPayModel.trade_type = "NATIVE"; wxPayModel.product_id = data.product_id; wxPayModel.out_trade_no = out_trade_no; string url = nativePay.GetPayUrl(wxPayModel);
6.根據url生成二維碼
引用 qrcode.js,使用new QRCode生成二維碼圖片
new QRCode(document.getElementById("qrcode"), data.url);
7.支付結果回撥
var buffer = new byte[Convert.ToInt32(Request.ContentLength)]; Request.Body.ReadAsync(buffer, 0, buffer.Length); var body = Encoding.UTF8.GetString(buffer);//接收到的支付通知 //將xml格式的結果轉換為物件以返回 WxPayData results = new WxPayData(); results.FromXml(body); #region 更新訂單狀態 OrderNotify orderNotify = new OrderNotify(); orderNotify.return_code = results.GetValue("return_code").ToString(); if (orderNotify.return_code == "SUCCESS") { orderNotify.attach = results.GetValue("attach").ToString(); orderNotify.out_trade_no = results.GetValue("out_trade_no").ToString(); orderNotify.total_fee = Convert.ToDouble(results.GetValue("total_fee").ToString()) * 0.01; orderNotify.transaction_id = results.GetValue("transaction_id").ToString();//微信訂單號 } else { msg = "[return_code]返回狀態不為[SUCCESS]"; } #endregion
8.支付結果查詢
WxPayApi wxPayApi = new WxPayApi();
string transaction_id = data.transaction_id;//微信訂單號
string out_trade_no = data.out_trade_no;//商戶系統訂單號
resultmsg = OrderQuery.Run(transaction_id, out_trade_no);//呼叫訂單查詢業務邏輯