|
Recently, I was doing WeChat official account development, and I encountered some problems with WeChat payment, and I was confused for 3 days, but today I finally got it done. During this period, I would like to thank some great gods for their help, and I will share my experience with them while they are hot. Before implementing WeChat Pay, I need to go to the WeChat development platform for certification, I won't say much about these authentication and configuration information, here is mainly from the code level to realize payment. Official documentation: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7. Facing several parameters in JS, I will give a main explanation:
During the development process, there are 4 very important parameters: one is appid, AppSecret, apikey and merchant number. The appID in js above is one of them. timeStamp is a timestamp, 10 digits, nonceStr is a random number, within 32 bits, the two most important parameters here, and the most error-prone are package and paySign. Let me tell you one by one. Let's talk about package first, we need to use prepay_id here, this parameter is the order number generated by WeChat, we need to call the unified order interface to get it. Official documentation: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1. As you can see from the documentation, to get prepay_id, many parameters are needed, and one of the most important parameters is the signature. Note: The signature here is different from the payment signature in JS. There are two points in the signed algorithm document, and I just emphasize two points. One is the order of parameters, which must be spliced according to ASCII from small to large, and the other is that the apikey must not be wrong, where is this apikey? Log in to the merchant platform to set it up. After splicing the strings required for the signature, MD5 encryption can be used to obtain the signature. Then combine the signature and all the previous parameters into an xml format string, and call the interface URL address:
https://api.mch.weixin.qq.com/pay/unifiedorder就可以返回一个xml结果,解析出其中的prepay_id,这样这个参数就成功获取到了。 Let's talk about the last parameter in JS, PaySign: PaySign. The same goes for the payment signature algorithm, splicing the other 5 parameters in JS in order, plus apikey, MD5 encryption, ok. The signature algorithm is the same, but the parameter values are different. Here's one thing to note. When paying for the signature, the random number nonceStr and the timestamp timeStamp are used, and these two parameters are used when signing If the values of these two parameters in JS are the same, they are the same random number and the same timestamp. Why? Although the documentation doesn't say it, my understanding is that payment signatures are generated by random numbers and timestamps, Then when paying, send the random number, timestamp, and payment signature together, then when WeChat verifies, it will also be based on the random number and timestamp in JS to generate a signature and compare it with the payment signature you sent. If you re-obtain the new random number and timestamp in js, the calculated signature will be different from the signature you sent, and an error will be reported: signature failed.
That's all for the code level, and of course, there are a few other points to note. For example, the useful openid parameter also needs to be obtained by calling the interface, and whether the directory for payment authorization is configured correctly. The last sentence summary: Look carefully at the document, and finally ask others, you can find WeChat payment-related groups, there are many gods in it, I encountered a problem before and struggled for 2 days without solving it, (sometimes just looking at the document is not enough), Then he angrily added 8 WeChat development groups, and finally came out under the guidance of the experts. Thank you again. Programmers are a group of people who love to share, and they are more than happy to share what they know. So when you don't understand, ask more.
|