Micro-credit sweep code payment native native payment Java version PHP version

Source: Internet
Author: User
Tags cdata character set unique id

Java version Micro-letter sweep code Payment

Account configuration parameter Generation please refer to official documentation: Https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1

Micro-letter sweep code to pay. In short, it's the information you need to pay for the micro-letter to the two-dimensional code picture. Through a micro-letter sweep, the initiation of payment. What we need to do is two things:
One is: In accordance with the micro-scan code payment rules to generate two-dimensional code information. Second: Micro-letter does not provide the interface to generate two-dimensional code pictures. We need our own two-dimensional code information to be generated into two-dimensional code picture.

1. Mode selection:

Micro-credit Sweep code payment, there are two modes, documented in the introduction. In the second mode, the micro-letter interface will return two-dimensional code information to us. And the first model requires us to generate two-dimensional code information ourselves. There will be some trouble. In particular, the case of parameters, as well as the problem of signing, error prone. In general, the second model is simpler than the first model. All I'm using is the second model, which is more generic. Jingdong and Ctrip are also using the second model.

2. Call unified next single interface to get the URL with two-dimensional code information: (Mode II)

The micro-scan code payment of mode two needs to first invoke the unified single interface of the micro-letter and generate the pre-transaction order. (Parameter passing and receiving are all XML data formats.) )

When called correctly, the URL containing the transaction ID, and the two-dimensional code link is returned.


hashmap<string, string> parammap = Maps.newhashmap ();
Parammap.put ("Trade_type", "NATIVE"); Type of transaction
Parammap.put ("Spbill_create_ip", Localip ()); This computer's IP
Parammap.put ("product_id", payorderidsstr); The merchant must fill in the parameters according to their own business
Parammap.put ("Body", ordersubject); Describe
Parammap.put ("Out_trade_no", payorderidsstr); Merchant's backstage trade number
Parammap.put ("Total_fee", "" "+ totalcount); The amount must be an integer unit
Parammap.put ("Notify_url", "http://" + getaccessdomain () + "/wx_pay_notify"); After the payment is successful, the callback address
Parammap.put ("AppID", Siteconfig.getwxpayappid ()); AppID
Parammap.put ("mch_id", Siteconfig.getwxpaymchid ()); Merchant number
Parammap.put ("Nonce_str", Commonutilpub.createnoncestr (32)); Random number
Parammap.put ("Sign", Commonutilpub.getsign (Parammap,siteconfig.getwxpaypartnerkey ()))//Generate signature based on micro-letter signature Rules

String xmlData = Commonutilpub.maptoxml (PARAMMAP);//convert parameter to XML data format

/**
* Get native IP
*
* By capturing all the NetworkInterface network interfaces of the system and then traversing the interfaceaddress groups under each network.
* Obtain a IPV4 address that meets <code>inetaddress instanceof inet4address</code> conditions
* @return
*/
@SuppressWarnings ("Rawtypes")
Private String Localip () {
String IP = null;
Enumeration allnetinterfaces;
try {
Allnetinterfaces = Networkinterface.getnetworkinterfaces ();
while (Allnetinterfaces.hasmoreelements ()) {
NetworkInterface netinterface = (networkinterface) allnetinterfaces.nextelement ();
list<interfaceaddress> interfaceaddress = netinterface.getinterfaceaddresses ();
for (interfaceaddress add:interfaceaddress) {
InetAddress Ip = add.getaddress ();
if (IP!= null && IP instanceof inet4address) {
ip = ip.gethostaddress ();
}
}
}
catch (SocketException e) {
TODO auto-generated Catch block
Logger.warn ("Get native IP failure: Exception information:" +e.getmessage ());
}
return IP;
}

The XML data returned when successful is:

<xml><return_code><! [cdata[success]]></return_code>
<return_msg><! [cdata[ok]]></return_msg>
<appid><! [cdata[wx49342bda0ef105dd]]></appid>
<mch_id><! [cdata[10019460]]></mch_id>
<nonce_str><! [cdata[unemqd4qwqd0hj4l]]></nonce_str>
<sign><! [cdata[c621a9c586c1f0397d4c6b8003e0cbce]]></sign>
<result_code><! [cdata[success]]></result_code>
<prepay_id><! [cdata[wx2015070818251790742fea5e0865034508]]></prepay_id>
<trade_type><! [cdata[native]]></trade_type>
<code_url><! [cdata[weixin://wxpay/bizpayurl?pr=aofesxf]]></code_url>
</xml>

Parse XML fetch CODE_URL:

String resxml = Htmlutil.postdata ("Https://api.mch.weixin.qq.com/pay/unifiedorder", xmlData);
Document DD = null;
String Code_url=null;
try {
DD = Documenthelper.parsetext (resxml);
catch (Documentexception e) {
Return "";
}
if (dd!= null) {
Element root = Dd.getrootelement ();
if (root = = null) {
Return "";
}
Element Codeurl = root.element ("Code_url");
if (Piele = = null) {
Return "";
}
Code_url = Codeurl.gettext (); Parse XML to get Code_url




3. Dynamically generate two-dimensional code picture

The Google zxing Library is used. Provide a jar address directly into your project. http://download.csdn.net/detail/gonwy/7658135

Page code:

${code_url}</#if > "style=" width:300px;height:300px; " />


Java code:

/**
* Generate two-dimensional code picture and output directly to the page in the form of streaming
* @param code_url
* @param response
*/
@RequestMapping ("Qr_code.img")
@ResponseBody
public void Getqrcode (String code_url,httpservletresponse response) {
Generateqrcodeutil.encodeqrcode (Code_url, response);
}


/**
* Generate two-dimensional code pictures do not store direct output to the page as a stream
* @param content
* @param response
*/
@SuppressWarnings ({"Unchecked", "Rawtypes"})
public static void Encodeqrcode (String content,httpservletresponse response) {
if (Stringutils.isblank (content))
Return
Multiformatwriter multiformatwriter = new Multiformatwriter ();
Map hints = new HashMap ();
Hints.put (Encodehinttype.character_set, "UTF-8"); Set character Set encoding type
Bitmatrix Bitmatrix = null;
try {
Bitmatrix = Multiformatwriter.encode (content, Barcodeformat.qr_code, 300,hints);
BufferedImage image = Tobufferedimage (Bitmatrix);
Output two-dimensional code picture stream
try {
Imageio.write (Image, "PNG", Response.getoutputstream ());
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
catch (Writerexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
}

Then the generated picture, through the micro-letter sweep code can be initiated to pay.

After the payment succeeds, the micro-letter will invoke the address of the callback function you set before. And will bring you to the micro-letter merchant custom parameters, to help the merchant to complete the remaining business processes.





PHP version of micro-letter sweep code Payment

First, generate native payment URL

The Native (native) Payment URL is a series of URLs with Weixin://wxpay/bizpayurl prefixes, followed by a series of key-value pairs that identify the merchant. The rules for Native (native) payment URLs are as follows:

Weixin://wxpay/bizpayurl?sign=xxxxx&appid=xxxxxx&productid=xxxxxx&timestamp=xxxxxx&noncestr= Xxxxxx

The generated code is as follows

<?php
Include_once ("wxpayhelper.php");
$wxPayHelper = new Wxpayhelper ();
echo $wxPayHelper->create_native_url ("1234567890");
?>

Where ProductID is the product's unique ID, the developer needs to define and maintain its own product ID, which is equivalent to an order, which is used by the background of the post merchant to get the transaction information.

The above code generates the following URL:

weixin://wxpay/bizpayurl?appid=wxb489e8caeabcdefg&noncestr=bbvdr5atz9d7s08x&productid=1234567890& sign=e15d2466a85cd62b530e2f690604e7502f67ccb5&timestamp=1408025996


Two, generate two dimensional code

A two-dimensional code can be generated using a Third-party interface, or you can use your own code or plug-in, here is a description of PHP QR code.

PHP QR code is a PHP two-dimensional code generation class library, using it can easily generate two-dimensional code, the official website provided a download and multiple demo demo, view address: http://phpqrcode.sourceforge.net/.

The syntax for generating two-dimensional code is very simple, just fill in the URL as a parameter. Examples are as follows

Include ' phpqrcode.php ';
QRCode::p ng (' http://www.cnblogs.com/txw1958/');

This generates a paid two-dimensional code.


Iii. Generation of package

When the user scans the two-dimensional code above, it calls the native payment Url,url need to call the order information package back to the user, which is implemented by Wxpayhelper () of the package class, and the calling code is as follows:

<?php
Include_once ("wxpayhelper.php");

$commonUtil = new Commonutil ();
$wxPayHelper = new Wxpayhelper ();
$wxPayHelper->setparameter ("Bank_type", "WX");
$wxPayHelper->setparameter ("Body", "square times the studio micro-letter Payment Development tutorial");
$wxPayHelper->setparameter ("partner", "1900000109");
$wxPayHelper->setparameter ("Out_trade_no", $commonUtil->create_noncestr ());
$wxPayHelper->setparameter ("Total_fee", "1");
$wxPayHelper->setparameter ("Fee_type", "1");
$wxPayHelper->setparameter ("Notify_url", "htttp://www.baidu.com");
$wxPayHelper->setparameter ("Spbill_create_ip", "127.0.0.1");
$wxPayHelper->setparameter ("Input_charset", "GBK");
echo $wxPayHelper->create_native_package ();
?>

When the user scans the two-dimensional code, it jumps directly to the product page, as shown below


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.