Skip to content

Commit

Permalink
Extract getContributionRecur, clean up input input...
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 21, 2023
1 parent 5af9324 commit 6aadf12
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions CRM/Core/Payment/AuthorizeNetIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ 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();
$input = [];

$input['component'] = 'contribute';

// load post vars in $input
$this->getInput($input);
$paymentProcessorID = $this->getPaymentProcessorID();

// Check if the contribution exists
// make sure contribution exists and is valid
$contribution = new CRM_Contribute_BAO_Contribution();
Expand All @@ -59,23 +51,14 @@ public function main() {
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 = 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($input, TRUE));
}
// do a subscription check
if ($contributionRecur->processor_id != $this->getRecurProcessorID()) {
throw new CRM_Core_Exception('Unrecognized subscription.');
}
$contributionRecur = $this->getContributionRecur();

// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($contribution->contribution_status_id == 1) {
$first = FALSE;
}
$input['payment_processor_id'] = $paymentProcessorID;
$isFirstOrLastRecurringPayment = $this->recur($input, $contributionRecur, $first);
$isFirstOrLastRecurringPayment = $this->recur($contributionRecur, $first);

if ($isFirstOrLastRecurringPayment) {
//send recurring Notification email for user
Expand All @@ -93,15 +76,16 @@ public function main() {
}

/**
* @param array $input
* @param \CRM_Contribute_BAO_ContributionRecur $recur
* @param bool $first
*
* @return bool
* @throws \CRM_Core_Exception
*/
public function recur($input, $recur, $first) {

public function recur($recur, $first) {
$paymentProcessorID = $this->getPaymentProcessorID();
$input = $this->getInput();
$input['payment_processor_id'] = $paymentProcessorID;
$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');

$now = date('YmdHis');
Expand Down Expand Up @@ -149,11 +133,12 @@ public function recur($input, $recur, $first) {
/**
* Get the input from passed in fields.
*
* @param array $input
*
* @throws \CRM_Core_Exception
*/
public function getInput(&$input) {
public function getInput(): array {
$input = [];
// This component is probably obsolete & will go once we stop calling completeOrder directly.
$input['component'] = 'contribute';
$input['amount'] = $this->retrieve('x_amount', 'String');
$input['subscription_id'] = $this->getRecurProcessorID();
$input['response_reason_code'] = $this->retrieve('x_response_reason_code', 'String', FALSE);
Expand Down Expand Up @@ -186,6 +171,7 @@ public function getInput(&$input) {
foreach ($params as $civiName => $resName) {
$input[$civiName] = $this->retrieve($resName, 'String', FALSE);
}
return $input;
}

/**
Expand Down Expand Up @@ -354,4 +340,23 @@ protected function getContributionRecurID(): int {
return (int) $contributionRecur->id;
}

/**
* @param array $input
*
* @return \CRM_Contribute_BAO_ContributionRecur
* @throws \CRM_Core_Exception
*/
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));
}
// do a subscription check
if ($contributionRecur->processor_id != $this->getRecurProcessorID()) {
throw new CRM_Core_Exception('Unrecognized subscription.');
}
return $contributionRecur;
}

}

0 comments on commit 6aadf12

Please sign in to comment.