wechat-pay-sdk
v1.1.0
Published
sdk for wechat developer
Downloads
8
Maintainers
Readme
wechat-pay-sdk
sdk for wechat developer, build with typescript.
Install
npm install wechat-pay-sdk --save
Install definition if you need!
npm install "git+https://[email protected]/mrkou47/wechat-pay-sdk.d.ts.git"
Usage
const wechatPay = require('wechat-pay-sdk');
const customize = new wechatPay({
appid: 'your appid',
secret: 'your wechat secret',
mch_id: 'your mch_id',
key: 'your key',
});
customize.getUserOpenId(code).then((res) => {
console.log(res); // receive opendid response
});
customize.payment({...options}).then((res) => {
console.log(res); // receive payment response
});
ATTENTION: you should use import wechatPay = require('wechat-pay-sdk');
if you used in the Typescript file.
API
wechatPay.getUserOpenId();
return Promise;
wechatPay.payment(options);
return Promise;
Options object for constructor()
All params is required
var wechatPayOptions = {
appid: 'your appid',
secret: 'your wechat secret',
mch_id: 'your mch_id',
key: 'your key',
};
- appid: your wechat appid,
- secret: your wechat secret,
- mch_id: your mchid, you can find it on 微信商户平台
- key: your key, you can find it on 微信商户平台, 微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
Options for getUserOpenId()
wechatPay.getUserOpenId('code');
This method need a string argument, which you can find on req.query
. see an Example;
Options object for payment()
var paymentOptions = {
attach: 'some detail',
body: 'description',
detail: 'description',
notify_url: 'http://www.weixin.qq.com/wxpay/pay.php',
openid: 'J892IK12e1A912309c',
out_trade_no: '20150806125346',
spbill_create_ip: '123.12.13.123',
total_fee: 100,
};
You can find params descrption on 微信支付文档
Run example
$ git clone https://github.com/MrKou47/wechat-pay-sdk.git
$ cd wechat-pay-sdk
Before start example, you shou edit wechat-config.js
with you config. And then:
$ npm run dev
How to use in wxml
?
Example:
fetch(YOUR_SERVER_URL, { method: 'POST', body: YOUR_REQUEST_BODY }).then(res => {
wx.requestPayment({
...res.wechatpay_data,
success() {
// success callback
},
fail() {
// failed callback
},
complete() {
// complete callback
}
});
});
DOC: https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html