Home  >  Article  >  Backend Development  >  Dynamic link Native payment developed by WeChat

Dynamic link Native payment developed by WeChat

*文
*文Original
2017-12-29 18:22:041763browse

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 now 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. For related introduction to v2, please refer to other articles of Fangbei Studio. 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");//设备号

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>

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>

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>

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

<xml>
 <return_code><![CDATA[SUCCESS]]></return_code>
</xml>

In this way, a dynamic link Native is completed.

Related recommendations:

WeChat refund function example of PHP WeChat payment development

##Laravel WeChat payment development example

5-minute tutorial to implement WeChat payment access

The above is the detailed content of Dynamic link Native payment developed by WeChat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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