Commit 527fb83c authored by eileen's avatar eileen

Update to smart buttons

parent 5b65185f
...@@ -315,7 +315,9 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt ...@@ -315,7 +315,9 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt
try { try {
foreach ($fields as $name => $value) { foreach ($fields as $name => $value) {
$fn = "set{$name}"; $fn = "set{$name}";
$this->gateway->$fn($value); if (method_exists($this->gateway, $fn)) {
$this->gateway->$fn($value);
}
} }
if (\Civi::settings()->get('omnipay_test_mode')) { if (\Civi::settings()->get('omnipay_test_mode')) {
$this->_is_test = TRUE; $this->_is_test = TRUE;
...@@ -356,12 +358,9 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt ...@@ -356,12 +358,9 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt
'id' => $this->_paymentProcessor['payment_processor_type_id'], 'id' => $this->_paymentProcessor['payment_processor_type_id'],
'return' => $labelFields) 'return' => $labelFields)
); );
$clientSideCredentials = $this->getProcessorTypeMetadata('client_side_credentials');
foreach ($labelFields as $field => $label) { foreach ($labelFields as $field => $label) {
if (!isset($clientSideCredentials[$field])) { $result[$this->camelFieldName($processorFields[$label])] = $this->_paymentProcessor[$field];
$result[$this->camelFieldName($processorFields[$label])] = $this->_paymentProcessor[$field];
}
} }
return $result; return $result;
} }
......
// @see https://developer.paypal.com/docs/checkout/integrate/
(function($) {
var form = $('#billing-payment-block').closest('form');
var qfKey = $('[name=qfKey]', form).val();
// https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/upgrade-integration/ function renderPaypal() {
// https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/ paypal.Buttons({
var formID = CRM.$('#billing-payment-block').closest('form').attr('id'); createBillingAgreement: function (data, actions) {
var qfKey = CRM.$('#' + formID + ' [name=qfKey]').val();
renderPaypal = function() { var frequencyInterval = $('#frequency_interval').val() || 1;
paypal.Button.render({ var frequencyUnit = $('#frequency_unit').val() ? $('#frequency_interval').val() : CRM.vars.omnipay.frequency_unit;
env: (CRM.vars.omnipay.is_test ? 'sandbox' : 'production'), var paymentAmount = calculateTotalFee();
style: {layout: 'vertical', 'size': 'responsive'}, var isRecur = $('#is_recur').is(":checked");
funding: {disallowed: [paypal.FUNDING.CREDIT]}, var recurText = isRecur ? ' recurring' : '';
payment: function (data, actions) {
var frequencyInterval = CRM.$('#frequency_interval').val() ? CRM.$('#frequency_interval').val() : 1; return new Promise(function (resolve, reject) {
var frequencyUnit = CRM.$('#frequency_unit').val() ? CRM.$('#frequency_interval').val() : CRM.vars.omnipay.frequency_unit; CRM.api3('PaymentProcessor', 'preapprove', {
'payment_processor_id': CRM.vars.omnipay.paymentProcessorId,
return new paypal.Promise(function (resolve, reject) { 'amount': paymentAmount,
var paymentAmount = calculateTotalFee(); 'currencyID' : CRM.vars.omnipay.currency,
var isRecur = CRM.$('#is_recur').is(":checked"); 'qf_key': qfKey,
var recurText = isRecur ? ' recurring' : ''; 'is_recur' : isRecur,
CRM.api3('PaymentProcessor', 'preapprove', { 'installments' : $('#installments').val(),
'payment_processor_id': CRM.vars.omnipay.paymentProcessorId, 'frequency_unit' : frequencyUnit,
'amount': paymentAmount, 'frequency_interval' : frequencyInterval,
'currencyID' : CRM.vars.omnipay.currency, 'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(paymentAmount) + recurText,
'qf_key': qfKey, }
'is_recur' : isRecur, ).then(function (result) {
'installments' : CRM.$('#installments').val(), if (result['is_error'] === 1) {
'frequency_unit' : frequencyUnit, reject(result['error_message']);
'frequency_interval' : frequencyInterval, }
'description' : CRM.vars.omnipay.title + ' ' + CRM.formatMoney(paymentAmount) + recurText else {
} token = result['values'][0]['token'];
).done(function (result) { resolve(token);
if (result['is_error'] === 1) { }
reject(result['error_message']); })
} .fail(function (result) {
else { reject('Payment failed. Check your site credentials');
token = result['values'][0]['token']; });
resolve(token);
}
})
.fail(function (result) {
reject('Payment failed. Check your site credentials');
}); });
}); },
},
onAuthorize: function (data, actions) { onApprove: function (data, actions) {
var isRecur = 1; var isRecur = 1;
var paymentToken = data['billingToken']; var paymentToken = data['billingToken'];
if (!paymentToken) { if (!paymentToken) {
paymentToken = data['paymentID']; paymentToken = data['paymentID'];
isRecur = 0; 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;
document.getElementById(formID).submit();
},
onError: function(err) {
console.log(err);
alert('Site is not correctly configured to process payments');
}
}, '#paypal-button-container'); 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();
},
if (typeof paypal === "undefined") { onError: function(err) {
CRM.$.getScript('https://www.paypalobjects.com/api/checkout.js', function() { console.log(err);
renderPaypal(); alert('Site is not correctly configured to process payments');
}); }
}
else {
renderPaypal();
}
})
.render('#paypal-button-container');
}
var paypalScriptURL = 'https://www.paypal.com/sdk/js?client-id=' + CRM.vars.omnipay.client_id + '&currency=' + CRM.vars.omnipay.currency + '&commit=false&vault=true';
CRM.loadScript(paypalScriptURL, false)
.done(renderPaypal);
})(CRM.$);
...@@ -64,7 +64,12 @@ return array( ...@@ -64,7 +64,12 @@ return array(
'suppress_submit_button' => 1, 'suppress_submit_button' => 1,
'supports_preapproval' => 1, 'supports_preapproval' => 1,
'payment_fields' => ['payment_token', 'PayerID', 'post_authorize'], 'payment_fields' => ['payment_token', 'PayerID', 'post_authorize'],
'pass_through_fields' => ['referrerCode' => 'CiviCRM_SP'], 'pass_through_fields' => [
'referrerCode' => 'CiviCRM_SP',
'returnUrl' => 'https://www.paypal.com/checkoutnow/error',
'cancel_url' => 'https://www.paypal.com/checkoutnow/error',
],
'client_side_credentials' => ['user_name' => 'client_id'],
'payment_fields_metadata' => [ 'payment_fields_metadata' => [
'PayerID' => [ 'PayerID' => [
'name' => 'PayerID', 'name' => 'PayerID',
......
...@@ -68,7 +68,7 @@ class api_PreApproveTest extends \PHPUnit_Framework_TestCase implements Headless ...@@ -68,7 +68,7 @@ class api_PreApproveTest extends \PHPUnit_Framework_TestCase implements Headless
'version' => 3, 'version' => 3,
'email' => 'blah@example.org', 'email' => 'blah@example.org',
])['values'][0]; ])['values'][0];
$this->assertEquals('PAY-79M30569TN7125128LQGYFLI', $preApproval['token']); $this->assertEquals('EC-9T988732661526452', $preApproval['token']);
$outbound = $this->getRequestBodies(); $outbound = $this->getRequestBodies();
......
...@@ -61,7 +61,11 @@ class RestAuthorizeResponse extends RestResponse implements RedirectResponseInte ...@@ -61,7 +61,11 @@ class RestAuthorizeResponse extends RestResponse implements RedirectResponseInte
// The transaction reference for a paypal purchase request or for a // The transaction reference for a paypal purchase request or for a
// paypal create subscription request ends up in the execute URL // paypal create subscription request ends up in the execute URL
// in the links section of the response. // in the links section of the response.
$completeUrl = $this->getCompleteUrl(); // this has been HACKED. Paypal changed it's product offering & introduced
// smart buttons instead of checkout.js and it needs something different returned.
// notes here https://github.com/thephpleague/omnipay-paypal/issues/228 but I couldn't see a
// good answer here so I just hacked.
$completeUrl = $this->getRedirectUrl();
if (empty($completeUrl)) { if (empty($completeUrl)) {
return parent::getTransactionReference(); return parent::getTransactionReference();
} }
...@@ -70,6 +74,8 @@ class RestAuthorizeResponse extends RestResponse implements RedirectResponseInte ...@@ -70,6 +74,8 @@ class RestAuthorizeResponse extends RestResponse implements RedirectResponseInte
// The last element of the URL should be "execute" // The last element of the URL should be "execute"
$execute = end($urlParts); $execute = end($urlParts);
// part of hack above.
return (str_replace('webscr?cmd=_express-checkout&token=', '', $execute));
if (!in_array($execute, array('execute', 'agreement-execute'))) { if (!in_array($execute, array('execute', 'agreement-execute'))) {
return parent::getTransactionReference(); return parent::getTransactionReference();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment