Skip to content

Commit

Permalink
Merge pull request #18747 from eileenmcnaughton/aip2
Browse files Browse the repository at this point in the history
[REF] Replace long if block with early return
  • Loading branch information
eileenmcnaughton authored Oct 13, 2020
2 parents 93e3518 + aafec05 commit 3ea17d1
Showing 1 changed file with 93 additions and 91 deletions.
184 changes: 93 additions & 91 deletions CRM/Core/Payment/AuthorizeNetIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,111 +39,113 @@ public function main($component = 'contribute') {
//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->retrieve('x_subscription_id', 'String');
if (!$x_subscription_id) {
// Presence of the id means it is approved.
return TRUE;
}
$ids = $objects = $input = [];

if ($x_subscription_id) {
// Presence of the id means it is approved.
$input['component'] = $component;
$input['component'] = $component;

// load post vars in $input
$this->getInput($input, $ids);
// load post vars in $input
$this->getInput($input, $ids);

// load post ids in $ids
$this->getIDs($ids, $input);
// load post ids in $ids
$this->getIDs($ids, $input);

// Attempt to get payment processor ID from URL
if (!empty($this->_inputParameters['processor_id'])) {
$paymentProcessorID = $this->_inputParameters['processor_id'];
}
else {
// This is an unreliable method as there could be more than one instance.
// Recommended approach is to use the civicrm/payment/ipn/xx url where xx is the payment
// processor id & the handleNotification function (which should call the completetransaction api & by-pass this
// entirely). The only thing the IPN class should really do is extract data from the request, validate it
// & call completetransaction or call fail? (which may not exist yet).
Civi::log()->warning('Unreliable method used to get payment_processor_id for AuthNet IPN - this will cause problems if you have more than one instance');
$paymentProcessorTypeID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
'AuthNet', 'id', 'name'
);
$paymentProcessorID = (int) civicrm_api3('PaymentProcessor', 'getvalue', [
'is_test' => 0,
'options' => ['limit' => 1],
'payment_processor_type_id' => $paymentProcessorTypeID,
'return' => 'id',
]);
}
// Attempt to get payment processor ID from URL
if (!empty($this->_inputParameters['processor_id'])) {
$paymentProcessorID = $this->_inputParameters['processor_id'];
}
else {
// This is an unreliable method as there could be more than one instance.
// Recommended approach is to use the civicrm/payment/ipn/xx url where xx is the payment
// processor id & the handleNotification function (which should call the completetransaction api & by-pass this
// entirely). The only thing the IPN class should really do is extract data from the request, validate it
// & call completetransaction or call fail? (which may not exist yet).
Civi::log()->warning('Unreliable method used to get payment_processor_id for AuthNet IPN - this will cause problems if you have more than one instance');
$paymentProcessorTypeID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
'AuthNet', 'id', 'name'
);
$paymentProcessorID = (int) civicrm_api3('PaymentProcessor', 'getvalue', [
'is_test' => 0,
'options' => ['limit' => 1],
'payment_processor_type_id' => $paymentProcessorTypeID,
'return' => 'id',
]);
}

// Check if the contribution exists
// make sure contribution exists and is valid
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->id = $ids['contribution'];
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)]);
}
$ids['contributionPage'] = $contribution->contribution_page_id;

// make sure contact exists and is valid
// use the contact id from the contribution record as the id in the IPN may not be valid anymore.
$contact = new CRM_Contact_BAO_Contact();
$contact->id = $contribution->contact_id;
$contact->find(TRUE);
if ($contact->id != $ids['contact']) {
// If the ids do not match then it is possible the contact id in the IPN has been merged into another contact which is why we use the contact_id from the contribution
CRM_Core_Error::debug_log_message("Contact ID in IPN {$ids['contact']} not found but contact_id found in contribution {$contribution->contact_id} used instead");
echo "WARNING: Could not find contact record: {$ids['contact']}<p>";
$ids['contact'] = $contribution->contact_id;
}
// Check if the contribution exists
// make sure contribution exists and is valid
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->id = $ids['contribution'];
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)]);
}
$ids['contributionPage'] = $contribution->contribution_page_id;

// make sure contact exists and is valid
// use the contact id from the contribution record as the id in the IPN may not be valid anymore.
$contact = new CRM_Contact_BAO_Contact();
$contact->id = $contribution->contact_id;
$contact->find(TRUE);
if ($contact->id != $ids['contact']) {
// If the ids do not match then it is possible the contact id in the IPN has been merged into another contact which is why we use the contact_id from the contribution
CRM_Core_Error::debug_log_message("Contact ID in IPN {$ids['contact']} not found but contact_id found in contribution {$contribution->contact_id} used instead");
echo "WARNING: Could not find contact record: {$ids['contact']}<p>";
$ids['contact'] = $contribution->contact_id;
}

if (!empty($ids['contributionRecur'])) {
$contributionRecur = new CRM_Contribute_BAO_ContributionRecur();
$contributionRecur->id = $ids['contributionRecur'];
if (!$contributionRecur->find(TRUE)) {
CRM_Core_Error::debug_log_message("Could not find contribution recur record: {$ids['ContributionRecur']} in IPN request: " . print_r($input, TRUE));
echo "Failure: Could not find contribution recur record: {$ids['ContributionRecur']}<p>";
return FALSE;
}
if (!empty($ids['contributionRecur'])) {
$contributionRecur = new CRM_Contribute_BAO_ContributionRecur();
$contributionRecur->id = $ids['contributionRecur'];
if (!$contributionRecur->find(TRUE)) {
CRM_Core_Error::debug_log_message("Could not find contribution recur record: {$ids['ContributionRecur']} in IPN request: " . print_r($input, TRUE));
echo "Failure: Could not find contribution recur record: {$ids['ContributionRecur']}<p>";
return FALSE;
}
}

$objects['contact'] = &$contact;
$objects['contribution'] = &$contribution;
$objects['contact'] = &$contact;
$objects['contribution'] = &$contribution;

$this->loadObjects($input, $ids, $objects, TRUE, $paymentProcessorID);
$this->loadObjects($input, $ids, $objects, TRUE, $paymentProcessorID);

if (!empty($ids['paymentProcessor']) && $objects['contributionRecur']->payment_processor_id != $ids['paymentProcessor']) {
Civi::log()->warning('Payment Processor does not match the recurring processor id.', ['civi.tag' => 'deprecated']);
}
if (!empty($ids['paymentProcessor']) && $objects['contributionRecur']->payment_processor_id != $ids['paymentProcessor']) {
Civi::log()->warning('Payment Processor does not match the recurring processor id.', ['civi.tag' => 'deprecated']);
}

if ($component == 'contribute' && $ids['contributionRecur']) {
// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($objects['contribution']->contribution_status_id == 1) {
$first = FALSE;
//load new contribution object if required.
// create a contribution and then get it processed
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->contact_id = $ids['contact'];
$contribution->financial_type_id = $objects['contributionType']->id;
$contribution->contribution_page_id = $ids['contributionPage'];
$contribution->contribution_recur_id = $ids['contributionRecur'];
$contribution->receive_date = $input['receive_date'];
$contribution->currency = $objects['contribution']->currency;
$contribution->amount_level = $objects['contribution']->amount_level;
$contribution->address_id = $objects['contribution']->address_id;
$contribution->campaign_id = $objects['contribution']->campaign_id;
$contribution->_relatedObjects = $objects['contribution']->_relatedObjects;

$objects['contribution'] = &$contribution;
}
$input['payment_processor_id'] = $paymentProcessorID;
return $this->recur($input, [
'related_contact' => $ids['related_contact'] ?? NULL,
'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
'contact' => $ids['contact'] ?? NULL,
'contributionPage' => $ids['contributionPage'] ?? NULL,
], $objects['contributionRecur'], $objects['contribution'], $first);
if ($component == 'contribute' && $ids['contributionRecur']) {
// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($objects['contribution']->contribution_status_id == 1) {
$first = FALSE;
//load new contribution object if required.
// create a contribution and then get it processed
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->contact_id = $ids['contact'];
$contribution->financial_type_id = $objects['contributionType']->id;
$contribution->contribution_page_id = $ids['contributionPage'];
$contribution->contribution_recur_id = $ids['contributionRecur'];
$contribution->receive_date = $input['receive_date'];
$contribution->currency = $objects['contribution']->currency;
$contribution->amount_level = $objects['contribution']->amount_level;
$contribution->address_id = $objects['contribution']->address_id;
$contribution->campaign_id = $objects['contribution']->campaign_id;
$contribution->_relatedObjects = $objects['contribution']->_relatedObjects;

$objects['contribution'] = &$contribution;
}
$input['payment_processor_id'] = $paymentProcessorID;
return $this->recur($input, [
'related_contact' => $ids['related_contact'] ?? NULL,
'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
'contact' => $ids['contact'] ?? NULL,
'contributionPage' => $ids['contributionPage'] ?? NULL,
], $objects['contributionRecur'], $objects['contribution'], $first);
}

return TRUE;
}
catch (CRM_Core_Exception $e) {
Expand Down

0 comments on commit 3ea17d1

Please sign in to comment.