This is the tenth article in the series on how to test wechat payment using a sandbox environment.
Analysis of wechat payment series has been updated ten yo ~, have not seen the friends can have a look.
Brief analysis of wechat Pay: Download statement and fund statement
Brief analysis of wechat Payment: refund application, refund callback interface, refund inquiry
Brief analysis of wechat Pay: query orders and close orders
Brief analysis of wechat Pay: Notification of payment results
In actual development, we usually develop in the development environment, and there are many limitations in the local environment, such as: wechat payment cannot be initiated, H5 link needs authentication, payment result notification needs the Internet, etc.
In the face of the above problems, wechat official gave the solution, that is, the sandbox environment of this article, which is the official test environment of wechat payment, what can this environment do? I think the most important point is that we can call the corresponding interface according to the official example in real time, and the interface will immediately return the result. Take the wechat Pay pre-payment interface for example, it will return the relevant information of our payment in real time after calling, so as to avoid the trouble of not knowing the return result during development.
Simulation test system
To lower the threshold of merchant testing, the wechat Pay team developed an independent simulation test system. The system returns different response messages according to the amount of acceptance cases, so as to meet the requirements of merchant normal function test, security/exception test and performance test.
Figure 1 is a simplified schematic diagram of the wechat Payment simulation test system (later referred to as the simulation system). The API protocol of the simulation system is exactly the same as the official API (API interface documentation). Merchant developers simply add a layer of sandboxNew to the call URL of the official API to connect to the emulated system.
For example, credit card payment URL:api.mch.weixin.qq.com/pay/micropa… Change to: api.mch.weixin.qq.com/sandboxnew/…
The simulation system is completely independent of the production environment, including the storage layer. All transactions made by merchants in the simulation system (following orders, payments, inquiries) are fake data with no cash flow, that is, users do not need to make real deductions, and merchants do not have funds to enter into the account. Similarly, in the sandbox environment, there is no need for merchants to actually make and issue vouchers, nor will there be a real situation of voucher deduction. API verification key of acceptance simulation test system should be obtained from API:
Source code & interactive process
The following is the official wechat simulation test system document:
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
Copy the code
The interaction process and the API of the simulation test system are described above. Why do you need this signature interface? This is because the sandbox environment uses the real merchant number, apet/public number APP_ID, but the API key parameter must use the sandbox_signkey of the sandbox environment, which this interface is mainly used to retrieve.
Note: The merchant number (parent-child merchant number) in the simulation test environment needs to use the real merchant number.
Here is an example of getting sandbox_signkey:
/** * get sandbox_signkey ** @author yclimb * @date 2018/9/18 */ private voiddoGetSandboxSignKey() throws Exception { WXPayConfigImpl config = WXPayConfigImpl.getInstance(); HashMap<String, String> data = new HashMap<String, String>(); // Data. put("mch_id", config.getMchID()); // Get random string data.put("nonce_str", WXPayUtil.generateNonceStr()); / / generated signature String sign = WXPayUtil. GenerateSignature (data and config. GetKey ()); data.put("sign", sign); // get sandbox_signkey WXPay WXPay = new WXPay(config); String result = wxPay.requestWithoutCert("/sandboxnew/pay/getsignkey", data, 10000, 10000);
System.out.println(result);
}
Copy the code
Sandbox_signkey = sandbox_signkey = sandbox_signkey = sandbox_signkey
Example of interactive flow of merchant access simulation system:
- Merchant credit card payment requests, using POST way call api.mch.weixin.qq.com/sandboxnew/…
- HTTPS requests with sandboxNew are routed to the emulated system by Nginx. The simulation system returns the expected message to the merchant according to the payment amount (total_fee field). At the same time, landing the pen request data;
- Merchants a check list, call api.mch.weixin.qq.com/sandboxnew/…
- After receiving the check request, the simulation system returns the expected check result to the merchant according to the order number and amount.
- Merchants download statement, call api.mch.weixin.qq.com/sandboxnew/… The simulation system returns a fixed bill format to the merchant. Note: the bill content may not be exactly the same as the transaction generated by the merchant in the simulation system.
Sandbox/SandboxNew wechat Pay sandbox environment is provided to developers of wechat Pay merchants for simulated payments and callback notifications. To verify that the merchant understands the callback notification, billing format, and that exceptions are handled correctly. ◆ How to connect sandbox environment? 1, modify the merchant has its own procedures or configuration, WeChat pay the API link, such as: sweeping pay website url as: api.mch.weixin.qq.com/pay/micropa… Increase sandboxnew path, change to https://api.mch.weixin.qq.com/sandboxnew/pay/micropay, you can access the sandbox acceptance environment, other interface similar; 2, in debugging WeChat pay development site (site link: McH.weixin.qq.com/wiki/doc/ap…). , fill in the correct payment parameters according to the interface document, initiate wechat payment request, and complete the payment; 3. After acceptance, modify API links in the program or configuration (important!) , remove the sandboxNew path. Interconnect with the live network environment.
Address: pay.weixin.qq.com/wiki/doc/ap… Pay.weixin.qq.com/wiki/doc/ap…
conclusion
In fact, the sandbox environment is mainly used to facilitate the timely acquisition of interface return values and merchant payment acceptance during development. This paper explains how to obtain sandbox_signkey parameter and then simulate docking. Sandboxnew can be added after the actual interface URL, and the interface will return the result parameters in real time. This is different from the formal environment (where payment is called asynchronously and sandbox is returned in real time).
Note: some interfaces in sandbox environment do not just add sandboxNew to the link, the whole link will be changed. In practice, we should check the official documentation for one by one, such as the payment refund interface, the official line interface is: / secAPI /pay/refund, and the sandbox environment interface is: /sandboxnew/pay/refund, secapi was removed from sandbox environment, please pay attention to it.
Notice: in order to better verify the security of wechat payment, we need access to wechat acceptance test, the next article payment acceptance sample and acceptance guidelines for you to explain, please look forward to!!
If you want to see the friend of the source in advance, can first take a look at my lot, address is as follows: https://github.com/YClimb/wxpay-sdk/blob/master/README.md
Add the author’s personal wechat account, and the author’s wechat account is as follows: Yclimb, indicating wechat Payment can be included in the wechat Payment discussion group with your friends. Be sure to indicate wechat payment
To this article is over, pay attention to the public account to view more push!!