May 20, 2021 WeChat Mini Program Development Document
Support: Cloud function 2.0.2
Unified order
Merchants first call this interface in the small program to generate prepaid transaction orders in the background of WeChat payment services, return the correct prepaid transactions and then transfer payments.
Key parameters related to cloud development Description: Callback function settings: envId and functionName are used to set the cloud function that receives asynchronous notification callbacks after payment Return field payment: the object is the information required to call wx.requestPayment on the small terminal
The cloud function that pays the result callback must return the following object, otherwise the callback is considered unsuccessful and the cloud function receives a duplicate payment callback:
The field name | The name of the variable | Required | Type | Describe |
---|---|---|---|---|
Error code | errcode | Is | Number | 0 |
The error message | errmsg | Is | String |
The field name | The name of the variable | Required | Type | The sample value | Describe |
---|---|---|---|---|---|
Results notify the callback cloud function name | functionName | Is | String | paycallback | The name of the cloud function that receives WeChat payment asynchronous notification callbacks |
The result informs the callback cloud function environment | envId | Is | String | test-123 | The environment ID where the cloud function that receives WeChat payment asynchronous notification callbacks is located |
Sub-merchant number | subMchId | Is | String(32) | 1900000109 | WeChat pays for the assigned sub-merchant number |
The device number | deviceInfo | Whether | String(32) | 013467007045764 | Terminal device number (store number or cash register device ID), note: PC web page or public number payment please pass "WEB" |
Random string | nonceStr | Yes | String(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | Random strings, not less than 32 bits.Recommended random number generation algorithm |
product description | body | Yes | String(128) | Tencent Recharge Center-QQ member recharge | The product is simple to describe that this field must be transferred in strict accordance with the specification, and the specific parameters are required. |
product details | detail | no | String(6000) | Product detailed description, for merchants using single item, this field must be uploaded according to the specification, see "Single Products Discount Parameter Description" | |
Additional data | attach | no | String(127) | illustrate | Additional data, returning in the query API and payment notification, which is mainly used for custom data for merchants carrying orders. |
Merchant order number | outTradeNo | Is | String(32) | 1217752501201407033233368018 | Merchant system internal order number, required within 32 characters, can only be numbers, case letters - |
The currency type | feeType | Whether | String(16) | CNY | A three-letter code that complies with ISO 4217, default RMB: CNY, and a list of other values can be found in the currency type |
The total amount | totalFee | Is | Int | 888 | The total amount of the order can only be an integer, as detailed in the amount paid |
Terminal IP | spbillCreateIp | Is | String(64) | 123.12.12.123 | Ip addresses in both IPV4 and IPV6 formats are supported. Call WeChat to pay for the API's machine IP |
The start time of the transaction | timeStart | Whether | String(14) | 20091225091010 | Order generation time, format yyyy MMdhmmss, e.g. 9:10:10:10 on December 25, 2009 expressed as 20091225091010. See also time rules for details |
The close time of the transaction | timeExpire | Whether | String(14) | 20091227091010 |
The order expiration time, in the format yyyyYMdHHmmss, is expressed as 20091227091010 at 9:10:10 on December 27, 2009. T
he order expiration time is for the order number, and since there is a mandatory pass parameter prepay_id only two hours valid at the time of the request for payment, the order interface needs to be re-requested to get a new prepay_id when the re-entry time is more than 2 hours.
See also time rules for details.
Recommendation: The minimum failure interval is greater than 1 minute |
Order offer tags | goodsTag | Whether | String(32) | WXG | The parameters of the order coupon mark, voucher or discount function can be found in the voucher or discount |
The type of transaction | tradeType | Is | String(16) | JSAPI | The small program value is as follows: JSAPI, detailed description of the parameters |
Specify the payment method | limitPay | Whether | String(32) | no_credit | no_credit - specify that you cannot pay with a credit card |
The user ID | Openid | Whether | String(128) | oUpF8uMuAJO_M2pxb1Q9zNjWeS6o | trade_type is the JSAPI, this parameter must be passed, the user's unique identity under the merchant appid. How to get openid, you can refer to "Get openid". |
The user sub-identity | subOpenid | Whether | String(128) | oUpF8uMuAJO_M2pxb1Q9zNjWeS6o | trade_type is the JSAPI, this parameter must be passed, the user's unique identity under the sub-merchant appid. O penid and sub_openid can choose one of them, and if you choose to sub_openid, you must sub_appid. Before you can order, you need to call the Web page authorization to get user information interface to get the user's Openid. |
The electronic invoice entry open logo | receipt | Whether | String(8) | Y | Y, when Y is passed in, the payment success message and payment details page will appear at the insinsignation entry. Electronic invoice function needs to be opened on WeChat payment merchant platform or WeChat public platform before this field can take effect |
Scene information | sceneInfo | Whether | String(256) | Y | This field is often used for offline activities when the scene information reporting, support the reporting of actual store information, merchants can also report the relevant information according to the needs of their own. The field is JSON object data in the format of "store_info": "id": "store ID", "name": "name", "area_code": "coded", "address": "address" |
SceneInfo.storeInfo Object Description
The field name | The name of the variable | Required | Type | The sample value | Describe |
---|---|---|---|---|---|
Store id | Id | Whether | String(32) | SZTX001 | Store number, customized by the merchant |
The name of the store | name | Whether | String(64) | Tencent Building Teng Da Restaurant | Store name, customized by the merchant |
Store administrative code | area_code | Whether | String(6) | 440305 | Store location administrative division code, see the "latest county and above administrative division code" |
Store details | address | Whether | String(128) | Tencent Building all the way through the Science and Technology Park | Store details, customized by the merchant |
The field name | The name of the variable | Required | Type | The sample value | Describe |
---|---|---|---|---|---|
Return the status code | returnCode | Is | String(16) | SUCCESS | SUCCESS/FAIL This field is a communication identification, a non-transaction identification, and the success of a transaction needs to be result_code the transaction |
Return information | returnMsg | Whether | String(128) | The signature failed | The information returned, if not empty, is the cause of the error. Such as a failed signature, parameter format check error |
The following fields are returned when the returnCode is SUCCESS
The field name | The name of the variable | Required | Type | The sample value | Describe |
---|---|---|---|---|---|
The information required to initiate payments in a small program | payment | Is | Object | The information required for the small terminal to call wx.requestPayment | |
The service provider's APPID | appid | Is | String(32) | wxd678efh567hg6787 | The APPID of the service merchant |
Merchant number | mch_id | Is | String(32) | 1900000109 | Call the merchant number submitted by the interface |
APPID for small programs | sub_appid | Is | String(32) | wx8888888888888888 | WeChat assigned small program ID |
Sub-merchant number | sub_mch_id | Is | String(32) | 1900000109 | WeChat pays for the assigned sub-merchant number |
The device number | device_info | Whether | String(32) | 013467007045764 | Call the terminal device number submitted by the interface, |
Random string | nonce_str | Is | String(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | WeChat returns a random string |
Signature | sign | Is | String(64) | C380BEC2BFD727A4B6845133519F3AD6 | WeChat returned the signature, see the signature algorithm |
Business results | result_code | Is | String(16) | SUCCESS | SUCCESS/FAIL |
The error code | err_code | Whether | String(32) | SYSTEMERROR | See the list of errors in Section 6 for details |
The error code description | err_code_des | Whether | String(128) | System error | Description of the information returned by the error |
The following fields are returned when both result_code and the following fields are SUCCESS
The field name | The name of the variable | Required | Type | The sample value | Describe |
---|---|---|---|---|---|
The type of transaction | trade_type | Is | String(16) | JSAPI | Call the type of transaction submitted by the interface, the value is as follows: JSAPI, detailed description of the parameters |
Prepaid transaction session identification | prepay_id | Is | String(64) | wx201410272009395522657a690389285100 | WeChat generates a prepaid callback ID for use in subsequent interface calls, which are valid for 2 hours |
QR code link | code_url | Whether | String(64) | weixin://wxpay/bizpayurl/up?pr=NwY5Mz9&groupid=00 | trade_type returned when the user is available, this url is used to generate a payment QR code and then provide it to the user for scanning code payment. Note: code_url values of the code are not fixed and can be converted to QR codes in URL format when used |
Name | Describe | Reason | Solution |
---|---|---|---|
INVALID_REQUEST | The argument is wrong | The parameters are in the wrong format or are not uploaded as required | When the order is re-entered, the parameter value is required to match the original request, please confirm the parameter problem |
NOAUTH | Merchants do not have this interface permission | The merchant does not have this interface permission on | Ask the merchant to go and request permission for this interface |
NOTENOUGH | The balance is insufficient | The user account balance is insufficient | If the user account balance is insufficient, ask the user to top up or replace the payment card before paying |
ORDERPAID | Merchant orders have been paid | Merchant orders have been paid without duplication | Merchant orders have been paid and no further action is required |
ORDERCLOSED | The order has been closed | The current order is closed and cannot be paid | The current order has been closed, please place the order again |
SYSTEMERROR | System error | The system timed out | System exception, please call again with the same parameters |
APPID_NOT_EXIST | The APPID does not exist | The APPID is missing from the parameter | Please check that the APPID is correct |
MCHID_NOT_EXIST | MCHID does not exist | MCHID is missing from the parameter | Please check that the MCHID is correct |
APPID_MCHID_NOT_MATCH | The appid and mch_id do not match | The appid and mch_id do not match | Make sure that the appid mch_id match |
LACK_PARAMS | The argument is missing | The necessary request parameters are missing | Please check that the parameters are complete |
OUT_TRADE_NO_USED | The merchant order number repeats | The same transaction cannot be submitted more than once | Please verify that the merchant order number is submitted repeatedly |
SIGNERROR | The signature is wrong | The parameter signature result is incorrect | Check that both the signature parameters and methods meet the requirements of the signature algorithm |
XML_FORMAT_ERROR | The XML is in the wrong format | The XML is in the wrong format | Check that the XML parameters are in the correct format |
REQUIRE_POST_METHOD | Please use the post method | The post pass parameter is not used | Please check that the request parameters are submitted via the post method |
POST_DATA_EMPTY | The post data is empty | Post data cannot be empty | Please check that the post data is empty |
NOT_UTF8 | The encoding format is incorrect | The specified encoding format is not used | Please use the UTC-8 encoding format |
// 云函数代码
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
const res = await cloud.cloudPay.unifiedOrder({
"body" : "小秋TIT店-超市",
"outTradeNo" : "1217752501201407033233368018",
"spbillCreateIp" : "127.0.0.1",
"subMchId" : "1900009231",
"totalFee" : 1,
"envId": "test-f0b102",
"functionName": "pay_cb"
})
return res
}
// 小程序代码
wx.cloud.callFunction({
name: '函数名',
data: {
// ...
},
success: res => {
const payment = res.result.payment
wx.requestPayment({
...payment,
success (res) {
console.log('pay success', res)
},
fail (res) {
console.error('pay fail', err)
}
})
},
fail: console.error,
})