From 7b7ccbe7657a5e838d0f26b57da717bf11c87011 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 12 Dec 2020 19:01:42 +1300 Subject: [PATCH] [REF] Extract determination of subscription status information This gets us away from it being buried in a switch --- CRM/Core/Payment/PayPalIPN.php | 38 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/CRM/Core/Payment/PayPalIPN.php b/CRM/Core/Payment/PayPalIPN.php index 5a26991b6b..789dbbf6a6 100644 --- a/CRM/Core/Payment/PayPalIPN.php +++ b/CRM/Core/Payment/PayPalIPN.php @@ -95,9 +95,8 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { $now = date('YmdHis'); - $subscriptionPaymentStatus = NULL; // set transaction type - $txnType = $this->retrieve('txn_type', 'String'); + $txnType = $this->getTrxnType(); $contributionStatuses = array_flip(CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate')); switch ($txnType) { case 'subscr_signup': @@ -112,7 +111,6 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { } $recur->processor_id = $this->retrieve('subscr_id', 'String'); $recur->trxn_id = $recur->processor_id; - $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; break; case 'subscr_eot': @@ -120,7 +118,6 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { $recur->contribution_status_id = $contributionStatuses['Completed']; } $recur->end_date = $now; - $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; break; case 'subscr_cancel': @@ -156,7 +153,7 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { $recur->save(); - if (in_array($this->retrieve('txn_type', 'String'), ['subscr_signup', 'subscr_eot'])) { + if ($this->getFirstOrLastInSeriesStatus()) { $autoRenewMembership = FALSE; if ($recur->id && isset($ids['membership']) && $ids['membership'] @@ -165,7 +162,7 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { } //send recurring Notification email for user - CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, + CRM_Contribute_BAO_ContributionPage::recurringNotify($this->getFirstOrLastInSeriesStatus(), $ids['contact'], $ids['contributionPage'], $recur, @@ -502,4 +499,33 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { return $paymentProcessorID; } + /** + * @return mixed + * @throws \CRM_Core_Exception + */ + protected function getTrxnType() { + $txnType = $this->retrieve('txn_type', 'String'); + return $txnType; + } + + /** + * Get status code for first or last recurring in the series. + * + * If this is the first or last then return the status code, else + * null. + * + * @return string|null + * @throws \CRM_Core_Exception + */ + protected function getFirstOrLastInSeriesStatus(): ?string { + $subscriptionPaymentStatus = NULL; + if ($this->getTrxnType() === 'subscr_signup') { + return CRM_Core_Payment::RECURRING_PAYMENT_START; + } + if ($this->getTrxnType() === 'subscr_eot') { + return CRM_Core_Payment::RECURRING_PAYMENT_END; + } + return NULL; + } + } -- 2.25.1