-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathomnipay_PaypalRest.js
More file actions
92 lines (78 loc) · 3.16 KB
/
Copy pathomnipay_PaypalRest.js
File metadata and controls
92 lines (78 loc) · 3.16 KB
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
// @see https://developer.paypal.com/docs/checkout/integrate/
(function($) {
var form = $('#billing-payment-block').closest('form');
var qfKey = $('[name=qfKey]', form).val();
function renderPaypal() {
paypal.Buttons({
onInit: function(data, actions) {
// Set up the buttons.
if (form.valid()) {
actions.enable()
}
else {
actions.disable();
}
form.on('blur keyup change', 'input', function (event) {
if (form.valid()) {
actions.enable()
}
else {
actions.disable();
}
});
},
createBillingAgreement: function (data, actions) {
var frequencyInterval = $('#frequency_interval').val() || 1;
var frequencyUnit = $('#frequency_unit').val() ? $('#frequency_interval').val() : CRM.vars.omnipay.frequency_unit;
var paymentAmount = calculateTotalFee();
var isRecur = $('#is_recur').is(":checked");
var recurText = isRecur ? ' recurring' : '';
return new Promise(function (resolve, reject) {
CRM.api3('PaymentProcessor', 'preapprove', {
'payment_processor_id': CRM.vars.omnipay.paymentProcessorId,
'amount': paymentAmount,
'currencyID' : CRM.vars.omnipay.currency,
'qf_key': qfKey,
'is_recur' : isRecur,
'installments' : $('#installments').val(),
'frequency_unit' : frequencyUnit,
'frequency_interval' : frequencyInterval,
'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(paymentAmount) + recurText,
}
).then(function (result) {
if (result['is_error'] === 1) {
reject(result['error_message']);
}
else {
token = result['values'][0]['token'];
resolve(token);
}
})
.fail(function (result) {
reject('Payment failed. Check your site credentials');
});
});
},
onApprove: function (data, actions) {
var isRecur = 1;
var paymentToken = data['billingToken'];
if (!paymentToken) {
paymentToken = data['paymentID'];
isRecur = 0;
}
document.getElementById('paypal-button-container').style.visibility = "hidden";
document.getElementById('crm-submit-buttons').style.display = 'block';
document.getElementById('PayerID').value = data['payerID'];
document.getElementById('payment_token').value = paymentToken;
form.submit();
},
onError: function(err) {
console.log(err);
alert('Site is not correctly configured to process payments');
}
})
.render('#paypal-button-container');
}
var paypalScriptURL = 'https://www.paypal.com/sdk/js?client-id=' + CRM.vars.omnipay.client_id + '¤cy=' + CRM.vars.omnipay.currency + '&commit=false&vault=true';
CRM.loadScript(paypalScriptURL, false).done(renderPaypal);
})(CRM.$);