Skip to content

Commit

Permalink
Merge pull request #19193 from eileenmcnaughton/ref
Browse files Browse the repository at this point in the history
[REF] Extract determination of subscription status information
  • Loading branch information
seamuslee001 authored Dec 12, 2020
2 parents 54a44bd + 7b7ccbe commit b3a7ec9
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions CRM/Core/Payment/PayPalIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ public function recur($input, $ids, $recur, $contribution, $first) {

$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':
Expand All @@ -112,15 +111,13 @@ public function recur($input, $ids, $recur, $contribution, $first) {
}
$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':
if ($recur->contribution_status_id != $contributionStatuses['Cancelled']) {
$recur->contribution_status_id = $contributionStatuses['Completed'];
}
$recur->end_date = $now;
$subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END;
break;

case 'subscr_cancel':
Expand Down Expand Up @@ -156,7 +153,7 @@ public function recur($input, $ids, $recur, $contribution, $first) {

$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']
Expand All @@ -165,7 +162,7 @@ public function recur($input, $ids, $recur, $contribution, $first) {
}

//send recurring Notification email for user
CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus,
CRM_Contribute_BAO_ContributionPage::recurringNotify($this->getFirstOrLastInSeriesStatus(),
$ids['contact'],
$ids['contributionPage'],
$recur,
Expand Down Expand Up @@ -502,4 +499,33 @@ public function getPayPalPaymentProcessorID($input, $ids) {
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;
}

}

0 comments on commit b3a7ec9

Please sign in to comment.