Development Guide
Update Time:2025.03.24Service process
This payment method is explained below. The Unified Order API, Query Order API, and accepting order notifications require signatures, which are created on the Merchant's service backend, as shown in Figure 9.6.
Figure 9.6 In-app payment sequence chart
Below shows how the merchant's backend interacts with the WeChat payment system:
Step 1:Payer selects products within the Merchant's app, submits the order, and chooses WeChat payment.
Step 2:The Merchant receives payer's payment transaction and calls the Unified Order API. For more information, see 【Unified Order】.
Step 3:he Unified Order API returns a normal prepay_id, and creates a signature based on the signature rules. The relevant data is transferred to the Merchant's app. Fields contained in the signature include cappId, partnerId, prepayId, nonceStr, timeStamp, and package.
Note:the value format of package is Sign=WXPay
Step 4:The Merchant's app uses the SDK to open WeChat payment within WeChat. For more information, see 【App-based Development Guide】
Step 5:The Merchant's backend receives payment notifications. For more information, see 【Payment Result Notification】
Step 6:The Merchant's backend queries the payment result. For more information, see 【Query Order】.
App-based Development Guide
Instructions for iOS
We will use Xcode10.0 running an iOS 7.0 environment as an example to illustrate the process.
1.APPID for Project Settings
After the Merchant has successfully applied for an App in the WeChat Open Platform, the Platform will provide an unique APPID to the Merchant. When creating a project in Xcode, the developer should enter the APPID value in the “URL Schemes” field, as marked in red in Figure 8.7.
Figure 8.7
2.APPID for Registration
The WeChat SDK “lib” and “head” files should be imported into the Xcode project. Before calling the API, you should register your APPID with WeChat, as shown below:
3.Call Payment
The Merchant's server calls the Unified Order API (for more information, see Section 9.1 Unified Order) to create an advance transaction. After obtaining prepay_id and signing relevant parameters, the advance transaction data is transferred to the App to start a payment. See below for an example on how to do this:
4)Payment Result Callback
As shown in the sample included in the SDK, onResp() can be added to WXPayEntryActivity. After completing payment, WeChat will be redirected to the Merchant's app and do a callback using onResp(). The Developer receives notifications in this function and determines the returned error code if necessary. If the payment is successful, the payment result shall be queried from the WeChat payment system and shown to Payer. The payment result is subject to the payment notifications from the WeChat payment system and the result returned to the Payer after querying the API. See below for an example on how to do this:
errCode value list:
Name | Description | Solution |
---|---|---|
0 | Success | Displays the success page |
-1 | Error | This may be caused by signature error, unregistered APPID, incorrect APPID in project settings, unmatched APPID in the registration and project settings, or other exceptions. |
-2 | Canceled by user | This occurs when the Payer cancels payment and returns to the App. In this case, no further steps are required. |
Instructions for Android
1.Backend Settings
After the Merchant has successfully applied for an App in the WeChat Open Platform, the Platform will provide an unique APPID to the Merchant. For payment security reasons, the Merchant's app package name and signature must match in the Platform. Only when these are configured properly can payment be initiated. This can be done in “App Platform (应用平台)”->”Android App (Android 应用)” on the WeChat Open Platform, as marked in red in Figure 8.8.
Figure 8.8
The App package name is the same as the one set in the configuration file "AndroidManifest.xml" of the app’s project settings. For an example, look at the package name "net.sourceforge.simcpux" in Figure 8.9.
The App signature is the keystore used for compiling according to project's app package name, which should be a 32-bit md5 string generated by a signature tool. If developers install the signature tool on a testing phone, they can run it to generate the app’s signature string, as shown in the strings in green in Figure 8.9. Download a signature tool
Figure 8.9
2.APPID for Registration
The WeChat JAR package should be imported into the App project. Before calling the API, you need to register your APPID with WeChat, as shown below:
Before calling the API, you should register your APPID with WeChat, as shown below:
3.Call Payment
The Merchant's server calls the Unified Order API (for more information, see Section 9.1 Unified Order) to create an advance transaction. After obtaining prepay_id and signing relevant parameters, the advance transaction data is transferred to the App to start a payment. See below for an example of how to do this:
4.Payment Result Callback
As shown in the sample included in the SDK, onResp() can be added to WXPayEntryActivity. After completing payment, WeChat will be redirected to Merchant's app and do a callback using onResp(). The Developer receives notifications in this function and determines the returned error code if necessary. If the payment is successful, the payment result shall be queried from the WeChat payment system and shown to Payer. The payment result is subject to the payment notifications from the WeChat payment system and the result returned to the Payer after querying the API. See below for an example on how to do so:
errCode value list:
Name | Description | Solution |
---|---|---|
0 | Success | Displays the success page |
-1 | Error | This may be caused by signature error, unregistered APPID, incorrect APPID in project settings, unmatched APPID in the registration and project settings, or other exceptions. |
-2 | Canceled by users | This happens occurs when the Payer cancels payment and returns to App. In this case, no further steps are required. |