# wx.requestPayment(Object object)
with Promise style call: Supported
Mini Program plugin: Not supported
Initiate WeChat Pay. Before calling,Mini Program WeChat Official Platform -Function - Apply for access to WeChat Pay. For more information, you can refer to WeChat Pay Development Document:
- Development Guidelines
- Single interface
- Payment interface
- Old version (v2)
If usedCloud DevelopmentThen wx.requestPayment
The required parameters can be obtained through cloud development WeChat payment unified single interface from authentication, and can be certificate free, signature free security call WeChat payment server interface, and receive asynchronous payment results callback, seeCloud Development WeChat Pay。
# parameter
# Object object
attribute | type | Default values | Required | Introductions |
---|---|---|---|---|
timeStamp | string | yes | Timestamp, from 1970 year 1 Moon 1 day 00:00:00 Number of seconds to date, the current time | |
nonceStr | string | yes | A random string of 32 characters or less in length | |
package | string | yes | Unified single interface returns prepay_id Parameter values in the format of prepay_id=*** | |
signType | string | MD5 | no | Signature algorithm, should be consistent with the value of the background order |
paySign | string | yes | Signature, see WeChat payment documents | |
success | function | no | Interface calls the successful callback function | |
fail | function | no | Interface calls failed callback functions | |
complete | function | no | Callback function at the end of an interface call (both successful and unsuccessful calls are executed) |
object.signType Legal value
value | Introductions | Minimum version |
---|---|---|
MD5 | Only in v2 Version Interface | |
HMAC-SHA256 | Only in v2 Version Interface | |
RSA | Only in v3 Version Interface |
# sample code
wx.requestPayment({
timeStamp: '',
nonceStr: '',
package: '',
signType: 'MD5',
paySign: '',
success (res) { },
fail (res) { }
})
Note: If the server has the use of cloud development, you can use cloud development WeChat paymentUnified ordersInterface to get all required parameters above without authentication. Example:
// Cloud function code
const cloud = require('wx-server-sdk')
cloud.init({
The following are the following: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
const res = await cloud.cloudPay.unifiedOrder({
"body" : "Xiaoqiu TIT shop - supermarket,"
"outTradeNo" : "1217752501201407033233368018",
"spbillCreateIp" : "127.0.0.1",
"subMchId" : "1900009231",
"totalFee" : 1,
"envId": "test-f0b102",
"functionName": "pay_cb"
})
return res
}
// Mini Program code
wx.cloud.callFunction({
name: 'function name'
data: {
// ...
},
success: res => {
const payment = res.result.payment
wx.requestPayment({
...payment,
success (res) {
console.log('pay success', res)
},
fail (err) {
console.error('pay fail', err)
}
})
},
fail: console.error,
})