Skip to content

Commit 136b09f

Browse files
committed
Re-work #164 so getTotalAmount works in all cases.
1 parent 8a75cb0 commit 136b09f

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

Metadata/js/omnipay_PaypalRest.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
function renderPaypal() {
77
paypal.Buttons({
88

9-
109
onInit: function(data, actions) {
1110
// Set up the buttons.
1211
if (form.valid()) {
13-
actions.enable()
12+
actions.enable();
1413
}
1514
else {
1615
actions.disable();
1716
}
1817

1918
form.on('blur keyup change', 'input', function (event) {
2019
if (form.valid()) {
21-
actions.enable()
20+
actions.enable();
2221
}
2322
else {
2423
actions.disable();
@@ -28,23 +27,41 @@
2827

2928
createBillingAgreement: function (data, actions) {
3029

30+
// CRM.payment.getTotalAmount is implemented by webform_civicrm and mjwshared. The plan is to
31+
// add CRM.payment.getTotalAmount() into CiviCRM core. This code allows it to work under any of
32+
// these circumstances as well as if CRM.payment does not exist.
33+
var totalAmount = 0.0;
34+
if ((typeof CRM.payment !== 'undefined') && (CRM.payment.hasOwnProperty('getTotalAmount'))) {
35+
totalAmount = CRM.payment.getTotalAmount();
36+
}
37+
38+
if (typeof calculateTotalFee == 'function') {
39+
// This is ONLY triggered in the following circumstances on a CiviCRM contribution page:
40+
// - With a priceset that allows a 0 amount to be selected.
41+
// - When we are the ONLY payment processor configured on the page.
42+
totalAmount = parseFloat(calculateTotalFee());
43+
}
44+
else if (document.getElementById('total_amount')) {
45+
// The input#total_amount field exists on backend contribution forms
46+
totalAmount = parseFloat(document.getElementById('total_amount').value);
47+
}
48+
3149
var frequencyInterval = $('#frequency_interval').val() || 1;
3250
var frequencyUnit = $('#frequency_unit').val() ? $('#frequency_interval').val() : CRM.vars.omnipay.frequency_unit;
33-
var paymentAmount = calculateTotalFee();
3451
var isRecur = $('#is_recur').is(":checked");
3552
var recurText = isRecur ? ' recurring' : '';
3653

3754
return new Promise(function (resolve, reject) {
3855
CRM.api3('PaymentProcessor', 'preapprove', {
3956
'payment_processor_id': CRM.vars.omnipay.paymentProcessorId,
40-
'amount': paymentAmount,
57+
'amount': totalAmount,
4158
'currencyID' : CRM.vars.omnipay.currency,
4259
'qf_key': qfKey,
4360
'is_recur' : isRecur,
4461
'installments' : $('#installments').val(),
4562
'frequency_unit' : frequencyUnit,
4663
'frequency_interval' : frequencyInterval,
47-
'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(paymentAmount) + recurText,
64+
'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(totalAmount) + recurText,
4865
}
4966
).then(function (result) {
5067
if (result['is_error'] === 1) {

0 commit comments

Comments
 (0)