@@ -819,7 +819,7 @@ public function processPaymentNotification($params) {
819
819
]);
820
820
}
821
821
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 );
823
823
}
824
824
}
825
825
catch (CiviCRM_API3_Exception $ e ) {
@@ -988,19 +988,17 @@ protected function redirectOrExit($outcome, $response = NULL) {
988
988
*
989
989
* @throws \CiviCRM_API3_Exception
990
990
*/
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
+ ));
1000
998
$ contributionRecur = civicrm_api3 ('ContributionRecur ' , 'getsingle ' , ['id ' => $ contributionRecurID ]);
1001
999
civicrm_api3 ('contribution_recur ' , 'create ' , [
1002
1000
'id ' => $ contributionRecurID ,
1003
- 'payment_token_id ' => $ token [ ' id ' ] ,
1001
+ 'payment_token_id ' => $ tokenID ,
1004
1002
'is_transactional ' => FALSE ,
1005
1003
'next_sched_contribution_date ' => CRM_Utils_Date::isoToMysql (
1006
1004
date ('Y-m-d 00:00:00 ' , strtotime ('+ ' . $ contributionRecur ['frequency_interval ' ] . ' ' . $ contributionRecur ['frequency_unit ' ]))
@@ -1482,6 +1480,7 @@ protected function doTokenPayment(&$params) {
1482
1480
if (!empty ($ params ['is_recur ' ])) {
1483
1481
$ this ->doRecurPostApproval ($ params );
1484
1482
}
1483
+
1485
1484
// and, at least with Way rapid, the createCreditCard call ignores any attempt to authorise.
1486
1485
// that is likely to be a pattern.
1487
1486
$ action = CRM_Utils_Array::value ('payment_action ' , $ params , 'purchase ' );
@@ -1633,11 +1632,14 @@ protected function gatewayConfirmContribution($response): void {
1633
1632
* @throws \CiviCRM_API3_Exception
1634
1633
*/
1635
1634
protected function savePaymentToken (array $ params ): int {
1635
+ if (empty ($ params ['contact_id ' ])) {
1636
+ $ params ['contact_id ' ] = $ this ->getContactID ($ params );
1637
+ }
1636
1638
$ paymentToken = civicrm_api3 ('PaymentToken ' , 'create ' , [
1637
- 'contact_id ' => $ params ['contactID ' ],
1639
+ 'contact_id ' => $ params ['contact_id ' ],
1638
1640
'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 ' ] ,
1641
1643
'email ' => $ params ['email ' ],
1642
1644
'billing_first_name ' => $ params ['billing_first_name ' ] ?? NULL ,
1643
1645
'billing_middle_name ' => $ params ['billing_middle_name ' ] ?? NULL ,
@@ -1649,5 +1651,23 @@ protected function savePaymentToken(array $params): int {
1649
1651
return (int ) $ paymentToken ['id ' ];
1650
1652
}
1651
1653
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
+
1652
1672
}
1653
1673
0 commit comments