Home > WeChat Applet > WeChat Development > WeChat payment development dynamic link Native payment example code

WeChat payment development dynamic link Native payment example code

高洛峰
Release: 2017-03-27 14:12:39
Original
2951 people have browsed it

This article mainly introduces the Native payment implementation process based on dynamic link QR code under WeChat payment, hoping to provide help to partners who develop WeChat payment

Note:

WeChat Pay is currently divided into v2 and v3 versions. Those who applied before September 10, 2014 will be in v2 version, and those who applied after that will be in v3 version. The V3 version of WeChat Pay does not have the paySignKey parameter. This article introduces WeChat Pay v3.

1. Generate product information

During the dynamic link payment process, the product transaction is generated first, and then the payment transaction is performed.

1. Product information preparation

The main thing is to first define the name and price of the product, as well as the transaction number. code show as below.

include_once("../WxPayPubHelper/WxPayPubHelper.php");

  //使用统一支付接口
  $unifiedOrder = new UnifiedOrder_pub();
  
  //设置统一支付接口参数
  //设置必填参数
  //appid已填,商户无需重复填写
  //mch_id已填,商户无需重复填写
  //noncestr已填,商户无需重复填写
  //spbill_create_ip已填,商户无需重复填写
  //sign已填,商户无需重复填写
  $unifiedOrder->setParameter("body","贡献一分钱");//商品描述
  $timeStamp = time();
  $out_trade_no = WxPayConf_pub::APPID."$timeStamp";
  $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
  $unifiedOrder->setParameter("total_fee","1");//总金额
  $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
  $unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
  //非必填参数,商户可根据实际情况选填
  $unifiedOrder->setParameter("device_info","100001");//设备号
Copy after login

The above parameters are finally encapsulated into the following similar XML parameters

<xml>
 <body><![CDATA[贡献一分钱]]></body>
 <out_trade_no><![CDATA[100001_1433009089]]></out_trade_no>
 <total_fee>1</total_fee>
 <notify_url><![CDATA[http://www.doucube.com/weixin/demo/notify_url.php]]></notify_url>
 <trade_type><![CDATA[NATIVE]]></trade_type>
 <device_info>100001</device_info>
 <appid><![CDATA[wx1d065b0628e21103]]></appid>
 <mch_id>1237905502</mch_id>
 <spbill_create_ip><![CDATA[61.129.47.79]]></spbill_create_ip>
 <nonce_str><![CDATA[gwpdlnn0zlfih21gipjj5z53i7vea8e8]]></nonce_str>
 <sign><![CDATA[C5A1E210F9B4402D8254F731882F41AC]]></sign>
</xml>
Copy after login

2. Call the unified payment request

Send the above XML to the unified payment interface

https://api.mch.weixin.qq.com/pay/unifiedorder

Get the following XML data

<xml>
 <return_code><![CDATA[SUCCESS]]></return_code> 
 <return_msg><![CDATA[OK]]></return_msg> 
 <appid><![CDATA[wx1d065b0628e21103]]></appid> 
 <mch_id><![CDATA[1237905502]]></mch_id> 
 <device_info><![CDATA[100001]]></device_info> 
 <nonce_str><![CDATA[6u8ovTtFupTagsiY]]></nonce_str> 
 <sign><![CDATA[E84D8BC2331766DD685591F908367FF1]]></sign> 
 <result_code><![CDATA[SUCCESS]]></result_code> 
 <prepay_id><![CDATA[wx20150531020450bb586eb2f70717331240]]></prepay_id> 
 <trade_type><![CDATA[NATIVE]]></trade_type> 
 <code_url><![CDATA[weixin://wxpay/bizpayurl?pr=dNp7omD]]></code_url> 
</xml>
Copy after login

In this way, you will get a code_url

The program generates a QR code from the code_url, and then provides it to the user to scan

2. User payment

1. User payment

User payment is successful Afterwards, WeChat will send an XML notification to notify_url with the following content:

<xml>
 <appid><![CDATA[wx1d065b0628e21103]]></appid> 
 <bank_type><![CDATA[CFT]]></bank_type> 
 <cash_fee><![CDATA[1]]></cash_fee> 
 <device_info><![CDATA[100001]]></device_info> 
 <fee_type><![CDATA[CNY]]></fee_type> 
 <is_subscribe><![CDATA[N]]></is_subscribe> 
 <mch_id><![CDATA[1237905502]]></mch_id> 
 <nonce_str><![CDATA[gwpdlnn0zlfih21gipjj5z53i7vea8e8]]></nonce_str> 
 <openid><![CDATA[oMNKHuBTW4YWgNGAVQW00tWK6auM]]></openid> 
 <out_trade_no><![CDATA[100001_1433009089]]></out_trade_no> 
 <result_code><![CDATA[SUCCESS]]></result_code> 
 <return_code><![CDATA[SUCCESS]]></return_code> 
 <sign><![CDATA[03EEDE7ADE2ABB7916795F37F19A3D13]]></sign> 
 <time_end><![CDATA[20150531020838]]></time_end> 
 <total_fee>1</total_fee> 
 <trade_type><![CDATA[NATIVE]]></trade_type> 
 <transaction_id><![CDATA[1002750205201505310187709542]]></transaction_id> 
</xml>
Copy after login

After notification_url is received, it will return a confirmation success message to the WeChat server

<xml>
 <return_code><![CDATA[SUCCESS]]></return_code>
</xml>
Copy after login

In this way, a dynamic link Native is completed.

Use the order query interface to query the order and return the following

Transaction status: SUCCESS
Equipment number: 100001
User ID: oMNKHuBTW4YWgNGAVQW00tWK6auM
Whether to follow the public account: N
Transaction type: NATIVE
Payment bank: CFT
Total amount: 1
Cash coupon amount:
Currency type: CNY
WeChat payment order number: 1002750205201505310187709542
Merchant order number: 100001_1433009089
Merchant data package: Array
Payment completion time: 20150531020837

The above is the collection of information on WeChat payment dynamic link Native payment. I hope it can help friends who develop WeChat payment.

The above is the detailed content of WeChat payment development dynamic link Native payment example code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template