Introduction: With the increasing number of wechat users, wechat Payment has gradually become an indispensable link when people buy things. This article is composed ofYwl5320 authorizes this official account to publish exclusively,Ywl5320 blog link: http://blog.csdn.net/ywl5320/. No more words, read the text

Recently in the payment module, the most commonly used is wechat pay and Alipay pay, among them the most pit is wechat pay!! All kinds of problems, the official documentation is not detailed… Ah but finally successfully climb the pit completed the integration of wechat payment. First attach a successful payment page happy happy hahaha:



Here’s how to climb the pit

Wechat Pay is divided into the following steps:

1. First of all, you need to register on wechat open platform, add your own APP and successfully apply for the payment function

2. Download the SDK of wechat Pay and add it to your own project (the above is the most basic, no problem)

3. Now you can start to integrate wechat Payment (from ordering to payment) :

(1) : Prepare the required data and register the current APP with wechat

First in the current background configuration WeChat APP packaged key generated signature (WeChat website have signature tool), and then must set the key that merchants (32-bit, merchants set up), and is APP_ID WeChat it is generated for each APP, finally opened for the function of merchant ID user we put the money paid to the businessman.

And then register your APP in the appropriate place (onCreate)



(2) : call order with unified interface (https://api.mch.weixin.qq.com/pay/unifiedorder) to generate orders, this step is the most easy to get wrong (this is a big pit).

1. First, we design the necessary parameters to be passed to wechat (OrderPayBean) :



For example:



2. With the commodity parameters ready, let’s generate a signature for them:

The signaturealgorithmAs follows:



Construct commodity parameter set:

3. Since the unified ordering interface needs to be sent to wechat in XML format by POST, we first splice parameters in XML format:



Then there is our asynchronous thread request unified order interface:



The correct result returned from wechat here is: string in XML format, in which “prepay_id” is the important parameter we need to use in the call payment interface. The Util. HttpPost (url, STR) method can be copied in the demo provided by wechat. (In many cases, “signature error” is returned in this step, so it is necessary to check whether the merchant key is correct. The most common error is that the “body” field is in Chinese, and then the code is not converted to “ISO8859-1” when the POST is sent, resulting in a signature error.

4. After successfully obtaining “prepay_id” through unified order interface, you can call the payment interface (if the server generates the order, you can directly start from this step) :



Utility class methods used:





Among them:

Here we use the method of converting the XML to a list (using dom4j.jar) :

5, this successfully transferred to the payment interface



This is just the beginning to solve the Chinese garbled is the result of Chinese transcoding alone, wechat end without transcoding, like this.



This is when body is in English, it can be paid normally.

6. Finally, process the payment result on the wechat callback page:







So wechat pay climb pit end, not easy ah… Ha, ha, ha

The first time to get blog updates reminder, and more Android dry goods, source code analysis, welcome to my wechat public number, scan the qr code below or long press to identify the QR code, you can pay attention to.