-
Notifications
You must be signed in to change notification settings - Fork 176
/
wx_lite.js
75 lines (70 loc) · 2.16 KB
/
wx_lite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Created by yulitao on 2016/12/29.
*/
var stash = require('../stash');
var callbacks = require('../callbacks');
var hasOwn = {}.hasOwnProperty;
/*global wx*/
module.exports = {
PINGPP_NOTIFY_URL_BASE: 'https://notify.pingxx.com/notify',
handleCharge: function (charge) {
var credential = charge.credential[charge.channel];
var fields = [
'appId', 'timeStamp', 'nonceStr', 'package', 'signType', 'paySign'
];
for (var k = 0; k < fields.length; k++) {
if (!hasOwn.call(credential, fields[k])) {
callbacks.innerCallback('fail',
callbacks.error('invalid_credential', 'missing_field_' + fields[k]));
return;
}
}
stash.jsApiParameters = credential;
this.callpay();
},
wxLiteEnabled: function () {
return typeof wx !== 'undefined' && wx.requestPayment;
},
//微信小程序支付
callpay: function () {
if (!this.wxLiteEnabled()) {
console.log('请在微信小程序中打开');
return;
}
var wx_lite = stash.jsApiParameters;
delete wx_lite.appId;
wx_lite.complete = function (res) {
//支付成功
if (res.errMsg === 'requestPayment:ok') {
callbacks.innerCallback('success');
}
//取消支付
if (res.errMsg === 'requestPayment:cancel'
|| res.errMsg === 'requestPayment:fail cancel') {
callbacks.innerCallback('cancel', callbacks.error('用户取消支付'));
}
//支付验证签名失败
if (res.err_code !== 'undefined' && res.err_desc !== 'undefined') {
callbacks.innerCallback('fail', callbacks.error(res.err_desc, res));
}
};
wx.requestPayment(wx_lite);
},
runTestMode: function (charge) {
var path = '/charges/' + charge.id;
wx.request({
url: this.PINGPP_NOTIFY_URL_BASE + path + '?livemode=false',
success: function(res) {
if (res.data == 'success') {
callbacks.innerCallback('success');
} else {
callbacks.innerCallback('fail',
callbacks.error('testmode_notify_fail'));
}
},
fail:function() {
callbacks.innerCallback('fail', callbacks.error('network_err'));
}
});
}
};