Skip to content

Commit bb41ebc

Browse files
Merge pull request #191 from eileenmcnaughton/ex
Improve signature on savePaymentToken
2 parents 838b784 + c81ece1 commit bb41ebc

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Diff for: CRM/Core/Payment/OmnipayMultiProcessor.php

+34-14
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public function processPaymentNotification($params) {
819819
]);
820820
}
821821
if (!empty($this->contribution['contribution_recur_id']) && ($tokenReference = $response->getCardReference()) != FALSE) {
822-
$this->storePaymentToken($params, $this->contribution, $tokenReference);
822+
$this->storePaymentToken(array_merge($params, ['contact_id' => $contribution['contact_id']]), $this->contribution['contribution_recur_id'], $tokenReference);
823823
}
824824
}
825825
catch (CiviCRM_API3_Exception $e) {
@@ -988,19 +988,17 @@ protected function redirectOrExit($outcome, $response = NULL) {
988988
*
989989
* @throws \CiviCRM_API3_Exception
990990
*/
991-
protected function storePaymentToken($params, $contribution, $tokenReference) {
992-
$contributionRecurID = $contribution['contribution_recur_id'];
993-
$token = civicrm_api3('payment_token', 'create', [
994-
'contact_id' => $contribution['contact_id'],
995-
'payment_processor_id' => $params['processor_id'],
996-
'token' => $tokenReference,
997-
'is_transactional' => FALSE,
998-
'created_id' => (CRM_Core_Session::singleton()->getLoggedInContactID() ?: $contribution['contact_id']),
999-
]);
991+
protected function storePaymentToken($params, $contributionRecurID, $tokenReference) {
992+
$tokenID = $this->savePaymentToken(array_merge($params, [
993+
'payment_processor_id' => $params['processor_id'] ?? $params['payment_processor_id'],
994+
'token' => $tokenReference,
995+
'is_transactional' => FALSE,
996+
]
997+
));
1000998
$contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', ['id' => $contributionRecurID]);
1001999
civicrm_api3('contribution_recur', 'create', [
10021000
'id' => $contributionRecurID,
1003-
'payment_token_id' => $token['id'],
1001+
'payment_token_id' => $tokenID,
10041002
'is_transactional' => FALSE,
10051003
'next_sched_contribution_date' => CRM_Utils_Date::isoToMysql(
10061004
date('Y-m-d 00:00:00', strtotime('+' . $contributionRecur['frequency_interval'] . ' ' . $contributionRecur['frequency_unit']))
@@ -1482,6 +1480,7 @@ protected function doTokenPayment(&$params) {
14821480
if (!empty($params['is_recur'])) {
14831481
$this->doRecurPostApproval($params);
14841482
}
1483+
14851484
// and, at least with Way rapid, the createCreditCard call ignores any attempt to authorise.
14861485
// that is likely to be a pattern.
14871486
$action = CRM_Utils_Array::value('payment_action', $params, 'purchase');
@@ -1633,11 +1632,14 @@ protected function gatewayConfirmContribution($response): void {
16331632
* @throws \CiviCRM_API3_Exception
16341633
*/
16351634
protected function savePaymentToken(array $params): int {
1635+
if (empty($params['contact_id'])) {
1636+
$params['contact_id'] = $this->getContactID($params);
1637+
}
16361638
$paymentToken = civicrm_api3('PaymentToken', 'create', [
1637-
'contact_id' => $params['contactID'],
1639+
'contact_id' => $params['contact_id'],
16381640
'token' => $params['token'],
1639-
'payment_processor_id' => $this->_paymentProcessor['id'],
1640-
'created_id' => CRM_Core_Session::getLoggedInContactID(),
1641+
'payment_processor_id' => $params['payment_processor_id'] ?? $this->_paymentProcessor['id'],
1642+
'created_id' => CRM_Core_Session::getLoggedInContactID() ?? $params['contact_id'],
16411643
'email' => $params['email'],
16421644
'billing_first_name' => $params['billing_first_name'] ?? NULL,
16431645
'billing_middle_name' => $params['billing_middle_name'] ?? NULL,
@@ -1649,5 +1651,23 @@ protected function savePaymentToken(array $params): int {
16491651
return (int) $paymentToken['id'];
16501652
}
16511653

1654+
/**
1655+
* @param array $params
1656+
*
1657+
* @return mixed
1658+
* @throws \API_Exception
1659+
* @throws \Civi\API\Exception\UnauthorizedException
1660+
*/
1661+
protected function getContactID() {
1662+
if ($this->propertyBag->has('contactID')) {
1663+
return $this->propertyBag->getContactID();
1664+
}
1665+
return (int) Contribution::get(FALSE)
1666+
->addSelect('contact_id')
1667+
->addWhere('id', '=', $this->propertyBag->getContributionID())
1668+
->execute()
1669+
->first()['contact_id'];
1670+
}
1671+
16521672
}
16531673

0 commit comments

Comments
 (0)