/
paymentForm.js
54 lines (42 loc) · 1.44 KB
/
paymentForm.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
function initCheckout() {
// Because this might get executed before Paypal is loaded.
if (typeof paypal === "undefined") {
setTimeout(initCheckout, 200);
} else {
var $wrapper = $('.paypal-rest-form');
var $form = $wrapper.parents('form');
var paymentUrl = $wrapper.data('prepare');
paypal.Button.render({
env: $wrapper.data('env'),
commit: true,
payment: function() {
// Copy over all the data to simulate a form being submitted
var postData = {};
var $formElements = $form.find('input[type=hidden]');
for (var i = 0; i < $formElements.length; i++) {
if ($formElements[i].name === 'action') {
continue;
}
postData[$formElements[i].name] = $formElements.get(i).value;
}
return paypal.request.post(paymentUrl, postData).then(function(data) {
if (data.error) {
alert(data.error);
return false;
}
return data.transactionId;
});
},
onAuthorize: function(data) {
return paypal.request.post(data.returnUrl).then(function(data) {
window.location = data.url;
});
}
}, '#paypal-button');
$form.find('[type=submit]').remove();
if ($('.modal').data('modal')) {
$('.modal').data('modal').updateSizeAndPosition();
}
}
}
initCheckout();