Skip to content

Commit

Permalink
Re-work eileenmcnaughton#164 so getTotalAmount works in all cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Dec 21, 2020
1 parent 0e7f2f1 commit c34bb26
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions Metadata/js/omnipay_PaypalRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
function renderPaypal() {
paypal.Buttons({


onInit: function(data, actions) {
// Set up the buttons.
if (form.valid()) {
actions.enable()
actions.enable();
}
else {
actions.disable();
}

form.on('blur keyup change', 'input', function (event) {
if (form.valid()) {
actions.enable()
actions.enable();
}
else {
actions.disable();
Expand All @@ -28,23 +27,40 @@

createBillingAgreement: function (data, actions) {

// CRM.payment.getTotalAmount is implemented by webform_civicrm and mjwshared. The plan is to
// add CRM.payment.getTotalAmount() into CiviCRM core. This code allows it to work under any of
// these circumstances as well as if CRM.payment does not exist.
var totalAmount = 0.0;
if ((typeof CRM.payment !== 'undefined') && (CRM.payment.hasOwnProperty('getTotalAmount'))) {
totalAmount = CRM.payment.getTotalAmount();
}
else if (typeof calculateTotalFee == 'function') {
// This is ONLY triggered in the following circumstances on a CiviCRM contribution page:
// - With a priceset that allows a 0 amount to be selected.
// - When we are the ONLY payment processor configured on the page.
totalAmount = parseFloat(calculateTotalFee());
}
else if (document.getElementById('total_amount')) {
// The input#total_amount field exists on backend contribution forms
totalAmount = parseFloat(document.getElementById('total_amount').value);
}

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,
'amount': totalAmount,
'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,
'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(totalAmount) + recurText,
}
).then(function (result) {
if (result['is_error'] === 1) {
Expand Down

0 comments on commit c34bb26

Please sign in to comment.