Skip to content

Commit a3b5ac0

Browse files
Initial support for Authorize -capture flow
1 parent f344113 commit a3b5ac0

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

CRM/Core/Payment/OmnipayMultiProcessor.php

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public function doDirectPayment(&$params, $component = 'contribute') {
143143
if (!empty($params['is_recur'])) {
144144
$response = $this->gateway->createCard($this->getCreditCardOptions(array_merge($params, array('action' => 'Purchase')), $component))->send();
145145
}
146+
elseif (!empty($params['token'])) {
147+
$params['transactionReference'] = ($params['token']);
148+
$response = $this->gateway->capture($this->getCreditCardOptions($params, $component))
149+
->send();
150+
}
146151
else {
147152
$response = $this->gateway->purchase($this->getCreditCardOptions($params, $component))
148153
->send();
@@ -195,7 +200,12 @@ public function setProcessorFields() {
195200
$this->gateway->$fn($value);
196201
}
197202
if (in_array('testMode', array_keys($this->gateway->getDefaultParameters()))) {
198-
$this->gateway->setTestMode($this->_is_test);
203+
if (method_exists($this->gateway, 'setDeveloperMode')) {
204+
$this->gateway->setDeveloperMode($this->_is_test);
205+
}
206+
else {
207+
$this->gateway->setTestMode($this->_is_test);
208+
}
199209
}
200210
}
201211
catch (Exception $e) {
@@ -231,6 +241,17 @@ function getProcessorFields() {
231241
return $result;
232242
}
233243

244+
/**
245+
* Get preapproval details that have been set.
246+
*
247+
* @param array $detail
248+
*
249+
* @return array mixed
250+
*/
251+
function getPreApprovalDetails($detail) {
252+
return $detail;
253+
}
254+
234255
/**
235256
* Core specifically won't save billing address if you use notify mode so we make up for that here.
236257
*
@@ -390,6 +411,7 @@ private function getCreditCardOptions($params, $component) {
390411
'notifyUrl' => $this->getNotifyUrl(),
391412
'card' => $this->getCreditCardObjectParams($params),
392413
'cardReference' => CRM_Utils_Array::value('token', $params),
414+
'transactionReference' => CRM_Utils_Array::value('token', $params),
393415
);
394416
if (!empty($params['action'])) {
395417
$creditCardOptions['action'] = 'Purchase';
@@ -831,6 +853,76 @@ protected function supportsFutureRecurStartDate() {
831853
return $this->_paymentProcessor['is_recur'];
832854
}
833855

856+
/**
857+
* Is an authorize-capture flow supported.
858+
*
859+
* @return bool
860+
*/
861+
protected function supportsPreApproval() {
862+
$entities = array();
863+
omnipaymultiprocessor_civicrm_managed($entities);
864+
foreach ($entities as $entity) {
865+
if ($entity['entity'] === 'payment_processor_type') {
866+
if (!empty($entity['params']['supports_preapproval'])) {
867+
return TRUE;
868+
}
869+
}
870+
}
871+
return FALSE;
872+
}
873+
874+
/**
875+
* Function to action pre-approval if supported
876+
*
877+
* @param array $params
878+
* Parameters from the form
879+
*
880+
* This function returns an array which should contain
881+
* - pre_approval_parameters (this will be stored on the calling form & available later)
882+
* - redirect_url (if set the browser will be redirected to this.
883+
*
884+
* @return array
885+
*/
886+
public function doPreApproval(&$params) {
887+
$this->_component = 'contribute';
888+
$this->ensurePaymentProcessorTypeIsSet();
889+
$this->gateway = Omnipay::create(str_replace('omnipay_', '', $this->_paymentProcessor['payment_processor_type']));
890+
$this->setProcessorFields();
891+
$this->setTransactionID(CRM_Utils_Array::value('contributionID', $params));
892+
$this->storeReturnUrls($params['qfKey'], CRM_Utils_Array::value('participantID', $params), CRM_Utils_Array::value('eventID', $params));
893+
$this->saveBillingAddressIfRequired($params);
894+
895+
try {
896+
$response = $this->gateway->authorize($this->getCreditCardOptions($params, 'contribute'))
897+
->send();
898+
if ($response->isSuccessful()) {
899+
$params['trxn_id'] = $params['token'] = $response->getTransactionReference();
900+
$creditCardPan = '************' . substr($params['credit_card_number'], -4);
901+
foreach ($_SESSION as $key => $value) {
902+
if (isset($value['values'])) {
903+
foreach ($value['values'] as $pageName => $pageValues) {
904+
if (isset($pageValues['credit_card_number'])) {
905+
unset($_SESSION[$key]['values'][$pageName]['cvv2']);
906+
$_SESSION[$key]['values'][$pageName]['credit_card_number'] = $creditCardPan;
907+
}
908+
}
909+
}
910+
}
911+
unset($params['credit_card_number']);
912+
unset($params['cvv2']);
913+
return array(
914+
'pre_approval_parameters' => array('token' => $response->getTransactionReference())
915+
);
916+
}
917+
else {
918+
return $this->handleError('alert', 'failed processor transaction ' . $this->_paymentProcessor['payment_processor_type'], (array) $response, 9001, $response->getMessage());
919+
}
920+
} catch (\Exception $e) {
921+
// internal error, log exception and display a generic message to the customer
922+
return $this->handleError('error', 'unknown processor error ' . $this->_paymentProcessor['payment_processor_type'], array($e->getCode() => $e->getMessage()), $e->getCode(), 'Sorry, there was an error processing your payment. Please try again later.');
923+
}
924+
}
925+
834926
/**
835927
* Get an array of the fields that can be edited on the recurring contribution.
836928
*

CRM/Core/Payment/processors.mgd.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@
423423
'url_api_default' => 'https://secure.authorize.net/gateway/transact.dll',
424424
'billing_mode' => 1,
425425
'payment_type' => 1,
426+
'supports_preapproval' => 1,
426427
),
427428
),
428429
);

0 commit comments

Comments
 (0)