-
Notifications
You must be signed in to change notification settings - Fork 9
/
commerce_wechatpay.js
139 lines (116 loc) · 4.26 KB
/
commerce_wechatpay.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function ($) {
/**
* Poll the condition function for ttl milliseconds, and callback when the
* condition function returns a truthy value.
* @param {Function} condition - a function that returns a truthy or falsy value
* @param {Function} done - invoked when the condition function returns true
* @param {Number} ttl - number of milliseconds before polling aborts
* @param {Number} frequency - number of milliseconds between polling iterations
* @return void
*/
var when = function(condition, done, ttl, frequency) {
ttl = ttl || 1000;
frequency = frequency || 10;
var abort = function() {
clear_timeouts();
done(new Error('operation timed out'));
};
var abort_timeout = setTimeout(abort, ttl);
var clear_timeouts = function() {
clearTimeout(abort_timeout);
clearTimeout(poll_timeout);
};
var poll_timeout;
var poll = function() {
if (condition()) {
clear_timeouts();
done(false);
}
else {
poll_timeout = setTimeout(poll, frequency);
}
};
poll();
};
/**
* Javascript handler function to use Wechat JS to control payment
* If you need to use another function, see hook_commerce_wechatpay_class_name_alter()
*
* @param (json string) configs
* The configuration parameters which Wechatpay need
* @param (string) return_success_url
* @param (string) return_failure_url
*
* @see hook_commerce_wechatpay_class_name_alter()
* @see commerce_wechatpay_get_js_function()
*/
window.proceedWXPayment = function(configs, return_success_url, return_failure_url) {
var pay = function() {
WeixinJSBridge.invoke('getBrandWCPayRequest', configs, function (res) {
switch (res.err_msg) {
case 'get_brand_wcpay_request:cancel':
alert('用户取消支付!');
window.location = return_failure_url;
break;
case 'get_brand_wcpay_request:fail':
alert('支付失败!');
//alert(res.err_desc);
window.location = return_failure_url;
break;
case 'get_brand_wcpay_request:ok':
alert('支付成功!');
window.location = return_success_url;
break;
default:
break;
}
});
};
var weixin_bridge_ready = function() {
if(typeof WeixinJSBridge !== 'undefined')
return true;
else
return false;
};
// wait 10 sec for WeixinJSBridge loaded from DOM
when(weixin_bridge_ready, function(err) {
if (!err) {
pay();
}
}, 10000);
};
window.qrCheckingLoop = function(url, return_success_url){
var flag = false;
var request = function () {
jQuery.ajax({
url: url,
success: function(data){
if(typeof data != 'undefined' && data.status == 1){
flag = true;
}
}
});
return false;
};
var weixin_payment_status_updated = function() {
return flag;
};
jQuery(document).ready(function(){
if (typeof QRCode !== 'undefined') {
jQuery('.wechat_qrcode_container').each(function(){
var text = jQuery(this).attr('data-qrsource');
new QRCode(jQuery(this)[0], text);
});
}
// send request every 2 seconds
when(request, function(err) {}, 60000, 2000);
// wait 60 sec for success status
when(weixin_payment_status_updated, function(err) {
if (!err) {
alert('支付成功!');
window.location = return_success_url;
}
}, 60000, 500);
});
}
})(jQuery);