Wechat pay plugin for egg.
$ npm i egg-wechat-pay
config/plugin.js
exports.wechatPay = {
enable: true,
package: 'egg-wechat-pay'
}
config/config.default.js
exports.wechatPay = {
client: {
// Optional,
bodyPrefix: '麦当劳',
appId,
merchantId,
secret,
notifyUrl,
pfx: fs.readFileSync(thePathToPFX)
}
}
Then:
...
async doSomething () {
const params = await this.app.wechatPay.requestPayment(order)
// {
// "appId": "wx...",
// "timeStamp": "1515043618",
// "nonceStr": "V0UGYV...",
// "signType": "MD5",
// "package": "prepay_id=wx2018...",
// "paySign": "54AD...",
// "timestamp": "1515043618"
// }
}
...
?String
商品描述的前缀,避免每次都需要写商品描述,格式见
String
应用的 appId,注意,该应用(小程序,服务号)需要开通微信支付功能,否则会报 商户号mch_id与appid不匹配
的错误String
微信商户号,即 mch_id
String
微信支付的 API 密钥,请到 "微信支付|商户平台 -> API安全" 页面获取URL
接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数Buffer | String
微信支付 API 证书(p12证书)Object
统一下单接口的参数
String
它是 spbill_create_ip
参数的简写返回再次签名的回调结果,该结果可以直接被小程序,JSBridge,或客户端调用。
const payment = await wechatPay.requestPayment(order)
// https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html#wxrequestpaymentobject
wx.requestPayment({
...payment,
success (res) {
},
fail (res) {
}
})
创建另一个应用的微信支付实例,可创建与同一个微信支付商户绑定的多个应用的实例
const params = this.app.wechatPay.newApp('wx2...')
.requestPayment(order)
将微信支付通知中的 return_msg
(XML) 转换为 JavaScript 对象。XML 通知的结构见 这个文档。
这个方法在转换的过程中,还会结合 config.wechatPay.client
校验 XML 是否有效,否则会 reject。
返回 Object
将 JavaScript 转换为 XML 文本
返回 String
EggContext
向微信返回成功响应
EggContext
String
作为 return_msg
的值向微信返回失败的响应
MIT