Skip to content

Commit 85d0407

Browse files
committed
Multiple fixes to PaypalRest JS - requires CRM.payment library from mjwshared
1 parent ff545a8 commit 85d0407

File tree

3 files changed

+150
-11
lines changed

3 files changed

+150
-11
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/*
3+
+--------------------------------------------------------------------+
4+
| Copyright CiviCRM LLC. All rights reserved. |
5+
| |
6+
| This work is published under the GNU AGPLv3 license with some |
7+
| permitted exceptions and without any warranty. For full license |
8+
| and copyright information, see https://civicrm.org/licensing |
9+
+--------------------------------------------------------------------+
10+
*/
11+
12+
use CRM_Omnipaymultiprocessor_ExtensionUtil as E;
13+
14+
/**
15+
* Class CRM_Omnipaymultiprocessor_Check
16+
*/
17+
class CRM_Omnipaymultiprocessor_Check {
18+
19+
/**
20+
* @var string
21+
*/
22+
const MIN_VERSION_MJWSHARED = '0.9.3';
23+
24+
public static function checkRequirements(&$messages) {
25+
self::checkExtensionMjwshared($messages);
26+
}
27+
28+
/**
29+
* @param array $messages
30+
*
31+
* @throws \CiviCRM_API3_Exception
32+
*/
33+
private static function checkExtensionMjwshared(&$messages) {
34+
// mjwshared: required. Requires min version
35+
$extensionName = 'mjwshared';
36+
$extensions = civicrm_api3('Extension', 'get', [
37+
'full_name' => $extensionName,
38+
]);
39+
40+
if (empty($extensions['id']) || ($extensions['values'][$extensions['id']]['status'] !== 'installed')) {
41+
$message = new CRM_Utils_Check_Message(
42+
__FUNCTION__ . E::SHORT_NAME . '_requirements',
43+
E::ts('The <em>%1</em> extension requires the <em>Payment Shared</em> extension which is not installed. See <a href="%2" target="_blank">details</a> for more information.',
44+
[
45+
1 => ucfirst(E::SHORT_NAME),
46+
2 => 'https://civicrm.org/extensions/mjwshared',
47+
]
48+
),
49+
E::ts('%1: Missing Requirements', [1 => ucfirst(E::SHORT_NAME)]),
50+
\Psr\Log\LogLevel::ERROR,
51+
'fa-money'
52+
);
53+
$message->addAction(
54+
E::ts('Install now'),
55+
NULL,
56+
'href',
57+
['path' => 'civicrm/admin/extensions', 'query' => ['action' => 'update', 'id' => $extensionName, 'key' => $extensionName]]
58+
);
59+
$messages[] = $message;
60+
return;
61+
}
62+
if ($extensions['values'][$extensions['id']]['status'] === 'installed') {
63+
self::requireExtensionMinVersion($messages, $extensionName, self::MIN_VERSION_MJWSHARED, $extensions['values'][$extensions['id']]['version']);
64+
}
65+
}
66+
67+
/**
68+
* @param array $messages
69+
* @param string $extensionName
70+
* @param string $minVersion
71+
* @param string $actualVersion
72+
*/
73+
private static function requireExtensionMinVersion(&$messages, $extensionName, $minVersion, $actualVersion) {
74+
if (version_compare($actualVersion, $minVersion) === -1) {
75+
$message = new CRM_Utils_Check_Message(
76+
__FUNCTION__ . $extensionName . E::SHORT_NAME . '_requirements',
77+
E::ts('The %1 extension requires the %2 extension version %3 or greater but your system has version %4.',
78+
[
79+
1 => ucfirst(E::SHORT_NAME),
80+
2 => $extensionName,
81+
3 => $minVersion,
82+
4 => $actualVersion
83+
]),
84+
E::ts('%1: Missing Requirements', [1 => ucfirst(E::SHORT_NAME)]),
85+
\Psr\Log\LogLevel::ERROR,
86+
'fa-money'
87+
);
88+
$message->addAction(
89+
E::ts('Upgrade now'),
90+
NULL,
91+
'href',
92+
['path' => 'civicrm/admin/extensions', 'query' => ['action' => 'update', 'id' => $extensionName, 'key' => $extensionName]]
93+
);
94+
$messages[] = $message;
95+
}
96+
}
97+
98+
}

Metadata/js/omnipay_PaypalRest.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,56 @@
11
// @see https://developer.paypal.com/docs/checkout/integrate/
22
(function($) {
3-
var form = $('#billing-payment-block').closest('form');
4-
var qfKey = $('[name=qfKey]', form).val();
3+
var scriptName = 'omnipayPaypal';
54

65
if (typeof CRM.vars.omnipay === 'undefined') {
7-
console.log('CRM.vars.omnipay not defined! Not a Omnipay processor?');
6+
CRM.payment.debugging(scriptName, 'CRM.vars.omnipay not defined! Not a Omnipay processor?');
87
return;
98
}
109

1110
function renderPaypal() {
1211
paypal.Buttons({
1312

1413
onInit: function(data, actions) {
14+
15+
$('[type="submit"][formnovalidate="1"]',
16+
'[type="submit"][formnovalidate="formnovalidate"]',
17+
'[type="submit"].cancel',
18+
'[type="submit"].webform-previous'
19+
).on('click', function() {
20+
CRM.payment.debugging(scriptName, 'adding submitdontprocess: ' + this.id);
21+
CRM.payment.form.dataset.submitdontprocess = 'true';
22+
});
23+
24+
$(CRM.payment.getBillingSubmit()).on('click', function() {
25+
CRM.payment.debugging(scriptName, 'clearing submitdontprocess');
26+
CRM.payment.form.dataset.submitdontprocess = 'false';
27+
});
28+
29+
$(CRM.payment.form).on('submit', function(event) {
30+
if (CRM.payment.form.dataset.submitdontprocess === 'true') {
31+
CRM.payment.debugging(scriptName, 'non-payment submit detected - not submitting payment');
32+
event.preventDefault();
33+
return true;
34+
}
35+
if (document.getElementById('payment_token') && (document.getElementById('payment_token').value !== 'Authorisation token') &&
36+
document.getElementById('PayerID') && (document.getElementById('PayerID').value !== 'Payer ID')) {
37+
return true;
38+
}
39+
CRM.payment.debugging(scriptName, 'Unable to submit - paypal not executed');
40+
event.preventDefault();
41+
return true;
42+
});
43+
1544
// Set up the buttons.
16-
if (form.valid()) {
45+
if ($(CRM.payment.form).valid()) {
1746
actions.enable();
1847
}
1948
else {
2049
actions.disable();
2150
}
2251

23-
form.on('blur keyup change', 'input', function (event) {
24-
if (form.valid()) {
52+
$(CRM.payment.form).on('blur keyup change', 'input', function (event) {
53+
if ($(CRM.payment.form).valid()) {
2554
actions.enable();
2655
}
2756
else {
@@ -54,6 +83,7 @@
5483
var frequencyUnit = $('#frequency_unit').val() ? $('#frequency_interval').val() : CRM.vars.omnipay.frequency_unit;
5584
var isRecur = $('#is_recur').is(":checked");
5685
var recurText = isRecur ? ' recurring' : '';
86+
var qfKey = $('[name=qfKey]', $(CRM.payment.form)).val();
5787

5888
return new Promise(function (resolve, reject) {
5989
CRM.api3('PaymentProcessor', 'preapprove', {
@@ -97,11 +127,12 @@
97127
}
98128
document.getElementById('PayerID').value = data['payerID'];
99129
document.getElementById('payment_token').value = paymentToken;
100-
form.submit();
130+
131+
// CRM.$(CRM.payment.getBillingSubmit()).click();
101132
},
102133

103134
onError: function(err) {
104-
console.log(err);
135+
CRM.payment.debugging(scriptName, err);
105136
alert('Site is not correctly configured to process payments');
106137
}
107138

omnipaymultiprocessor.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,28 @@ function omnipaymultiprocessor_civicrm_preProcess($formName, &$form) {
104104
$form->assign('isJsValidate', TRUE);
105105
if (!empty($form->_values['is_recur'])) {
106106
$recurOptions = [
107-
'is_recur_interval' => $form->_values['is_recur_interval'],
107+
'is_recur_interval' => $form->_values['is_recur_interval'],
108108
'frequency_unit' => $form->_values['recur_frequency_unit'],
109109
'is_recur_installments' => $form->_values['is_recur_installments'],
110110
];
111111

112112
if (!$recurOptions['is_recur_interval']) {
113113
$recurOptions['frequency_interval'] = 1;
114114
}
115-
CRM_Core_Resources::singleton()->addVars(
115+
CRM_Core_Resources::singleton()->addVars(
116116
'omnipay', $recurOptions
117-
);
117+
);
118118
}
119119
}
120+
}
120121

122+
/**
123+
* Implements hook_civicrm_check().
124+
*
125+
* @throws \CiviCRM_API3_Exception
126+
*/
127+
function omnipaymultiprocessor_civicrm_check(&$messages) {
128+
CRM_Omnipaymultiprocessor_Check::checkRequirements($messages);
121129
}
130+
131+

0 commit comments

Comments
 (0)