Skip to content

Commit

Permalink
Merge pull request #26589 from eileenmcnaughton/anet
Browse files Browse the repository at this point in the history
Simplify use of contribution - we are only looking at status
  • Loading branch information
seamuslee001 authored Jun 22, 2023
2 parents be659f9 + 11eb4f2 commit fdd168f
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions CRM/Core/Payment/AuthorizeNetIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contribution;
use Civi\Api4\PaymentProcessor;

/**
Expand Down Expand Up @@ -43,22 +44,7 @@ public function main() {
//we only get invoice num as a key player from payment gateway response.
//for ARB we get x_subscription_id and x_subscription_paynum
$x_subscription_id = $this->getRecurProcessorID();
// Check if the contribution exists
// make sure contribution exists and is valid
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->id = $contributionID = $this->getContributionID();
if (!$contribution->find(TRUE)) {
throw new CRM_Core_Exception('Failure: Could not find contribution record for ' . (int) $contribution->id, NULL, ['context' => "Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE)]);
}

$contributionRecur = $this->getContributionRecur();

// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($contribution->contribution_status_id == 1) {
$first = FALSE;
}
$this->recur($contributionRecur, $first);
$this->recur();
return TRUE;
}
catch (CRM_Core_Exception $e) {
Expand All @@ -68,12 +54,10 @@ public function main() {
}

/**
* @param \CRM_Contribute_BAO_ContributionRecur $recur
* @param bool $first
*
* @throws \CRM_Core_Exception
*/
public function recur($recur, $first) {
public function recur() {
$recur = $this->getContributionRecur();
$paymentProcessorID = $this->getPaymentProcessorID();
$input = $this->getInput();
$input['payment_processor_id'] = $paymentProcessorID;
Expand All @@ -84,7 +68,7 @@ public function recur($recur, $first) {
$isFirstOrLastRecurringPayment = FALSE;
if ($this->isSuccess()) {
// Approved
if ($first) {
if ($this->getContributionStatus() !== 'Completed') {
$recur->trxn_id = $recur->processor_id;
$isFirstOrLastRecurringPayment = CRM_Core_Payment::RECURRING_PAYMENT_START;
}
Expand Down Expand Up @@ -117,7 +101,7 @@ public function recur($recur, $first) {
return;
}

CRM_Contribute_BAO_Contribution::completeOrder($input, $recur->id, $first ? $this->getContributionID() : NULL);
CRM_Contribute_BAO_Contribution::completeOrder($input, $recur->id, $this->getContributionStatus() !== 'Completed' ? $this->getContributionID() : NULL);
if ($isFirstOrLastRecurringPayment) {
//send recurring Notification email for user
CRM_Contribute_BAO_ContributionPage::recurringNotify($this->getContributionID(), TRUE,
Expand Down Expand Up @@ -260,7 +244,7 @@ protected function getContributionRecurObject(string $processorID, int $contactI
throw new CRM_Core_Exception('Could not find contributionRecur id');
}
if ($contactID != $contRecur->contact_id) {
$message = ts("Recurring contribution appears to have been re-assigned from id %1 to %2, continuing with %2.", [1 => $contactID, 2 => $contRecur->contact_id]);
$message = ts('Recurring contribution appears to have been re-assigned from id %1 to %2, continuing with %2.', [1 => $contactID, 2 => $contRecur->contact_id]);
CRM_Core_Error::debug_log_message($message);
}
return $contRecur;
Expand Down Expand Up @@ -345,7 +329,7 @@ private function getContributionRecur(): CRM_Contribute_BAO_ContributionRecur {
$contributionRecur = new CRM_Contribute_BAO_ContributionRecur();
$contributionRecur->id = $this->getContributionRecurID();
if (!$contributionRecur->find(TRUE)) {
throw new CRM_Core_Exception('Could not find contribution recur record: ' . $this->getContributionRecurID() . " in IPN request: " . print_r($this->getInput(), TRUE));
throw new CRM_Core_Exception('Could not find contribution recur record: ' . $this->getContributionRecurID() . ' in IPN request: ' . print_r($this->getInput(), TRUE));
}
// do a subscription check
if ($contributionRecur->processor_id != $this->getRecurProcessorID()) {
Expand All @@ -354,4 +338,23 @@ private function getContributionRecur(): CRM_Contribute_BAO_ContributionRecur {
return $contributionRecur;
}

/**
* Get the relevant contribution status.
*
* @return string $status
*
* @throws \CRM_Core_Exception
*/
private function getContributionStatus(): string {
// Check if the contribution exists
// make sure contribution exists and is valid
$contribution = Contribution::get(FALSE)
->addWhere('id', '=', $this->getContributionID())
->addSelect('contribution_status_id:name')->execute()->first();
if (empty($contribution)) {
throw new CRM_Core_Exception('Failure: Could not find contribution record for ' . $this->getContributionID(), NULL, ['context' => 'Could not find contribution record: ' . $this->getContributionID() . ' in IPN request: ' . print_r($this->getInput(), TRUE)]);
}
return $contribution['contribution_status_id:name'];
}

}

0 comments on commit fdd168f

Please sign in to comment.