Skip to content

Commit

Permalink
Merge pull request #12386 from mattwire/IPN_NFC
Browse files Browse the repository at this point in the history
NFC code cleanup for AuthNet, Paypal, PaypalPro IPNs
  • Loading branch information
eileenmcnaughton authored Jul 2, 2018
2 parents 72e875c + d280385 commit 6936f14
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 133 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/Payment/AuthorizeNetIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function main($component = 'contribute') {
// 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 for AuthNet IPN - this will cause problems if you have more than one instance');
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'
);
Expand Down
139 changes: 66 additions & 73 deletions CRM/Core/Payment/PayPalIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
* $Id$
*
*/
class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
Expand All @@ -52,7 +51,7 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
* @throws CRM_Core_Exception
*/
public function __construct($inputData) {
//CRM-19676
// CRM-19676
$params = (!empty($inputData['custom'])) ?
array_merge($inputData, json_decode($inputData['custom'], TRUE)) :
$inputData;
Expand All @@ -62,57 +61,56 @@ public function __construct($inputData) {

/**
* @param string $name
* @param $type
* @param string $type
* @param bool $abort
*
* @return mixed
* @throws \CRM_Core_Exception
*/
public function retrieve($name, $type, $abort = TRUE) {
static $store = NULL;
$value = CRM_Utils_Type::validate(
CRM_Utils_Array::value($name, $this->_inputParameters),
$type,
FALSE
);
$value = CRM_Utils_Type::validate(CRM_Utils_Array::value($name, $this->_inputParameters), $type, FALSE);
if ($abort && $value === NULL) {
CRM_Core_Error::debug_log_message("Could not find an entry for $name");
Civi::log()->debug("PayPalIPN: Could not find an entry for $name");
echo "Failure: Missing Parameter<p>" . CRM_Utils_Type::escape($name, 'String');
exit();
throw new CRM_Core_Exception("PayPalIPN: Could not find an entry for $name");
}
return $value;
}

/**
* @param $input
* @param $ids
* @param $objects
* @param $first
* @param array $input
* @param array $ids
* @param array $objects
* @param bool $first
*
* @return void
*
* @return bool
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function recur(&$input, &$ids, &$objects, $first) {
if (!isset($input['txnType'])) {
CRM_Core_Error::debug_log_message("Could not find txn_type in input request");
Civi::log()->debug('PayPalIPN: Could not find txn_type in input request');
echo "Failure: Invalid parameters<p>";
return FALSE;
return;
}

if ($input['txnType'] == 'subscr_payment' &&
$input['paymentStatus'] != 'Completed'
) {
CRM_Core_Error::debug_log_message("Ignore all IPN payments that are not completed");
Civi::log()->debug('PayPalIPN: Ignore all IPN payments that are not completed');
echo "Failure: Invalid parameters<p>";
return FALSE;
return;
}

$recur = &$objects['contributionRecur'];

// make sure the invoice ids match
// make sure the invoice is valid and matches what we have in the contribution record
if ($recur->invoice_id != $input['invoice']) {
CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request");
Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request (RecurID: ' . $recur->id . ').');
echo "Failure: Invoice values dont match between database and IPN request<p>";
return FALSE;
return;
}

$now = date('YmdHis');
Expand All @@ -127,18 +125,19 @@ public function recur(&$input, &$ids, &$objects, $first) {
}
$sendNotification = FALSE;
$subscriptionPaymentStatus = NULL;
//set transaction type
// set transaction type
$txnType = $this->retrieve('txn_type', 'String');
$contributionStatuses = array_flip(CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'));
switch ($txnType) {
case 'subscr_signup':
$recur->create_date = $now;
//some times subscr_signup response come after the
//subscr_payment and set to pending mode.
// sometimes subscr_signup response come after the subscr_payment and set to pending mode.

$statusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur',
$recur->id, 'contribution_status_id'
);
if ($statusID != 5) {
$recur->contribution_status_id = 2;
if ($statusID != $contributionStatuses['In Progress']) {
$recur->contribution_status_id = $contributionStatuses['Pending'];
}
$recur->processor_id = $this->retrieve('subscr_id', 'String');
$recur->trxn_id = $recur->processor_id;
Expand All @@ -147,28 +146,28 @@ public function recur(&$input, &$ids, &$objects, $first) {
break;

case 'subscr_eot':
if ($recur->contribution_status_id != 3) {
$recur->contribution_status_id = 1;
if ($recur->contribution_status_id != $contributionStatuses['Cancelled']) {
$recur->contribution_status_id = $contributionStatuses['Completed'];
}
$recur->end_date = $now;
$sendNotification = TRUE;
$subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END;
break;

case 'subscr_cancel':
$recur->contribution_status_id = 3;
$recur->contribution_status_id = $contributionStatuses['Cancelled'];
$recur->cancel_date = $now;
break;

case 'subscr_failed':
$recur->contribution_status_id = 4;
$recur->contribution_status_id = $contributionStatuses['Failed'];
$recur->modified_date = $now;
break;

case 'subscr_modify':
CRM_Core_Error::debug_log_message("We do not handle modifications to subscriptions right now");
Civi::log()->debug('PayPalIPN: We do not handle modifications to subscriptions right now (RecurID: ' . $recur->id . ').');
echo "Failure: We do not handle modifications to subscriptions right now<p>";
return FALSE;
return;

case 'subscr_payment':
if ($first) {
Expand All @@ -180,16 +179,15 @@ public function recur(&$input, &$ids, &$objects, $first) {

// make sure the contribution status is not done
// since order of ipn's is unknown
if ($recur->contribution_status_id != 1) {
$recur->contribution_status_id = 5;
if ($recur->contribution_status_id != $contributionStatuses['Completed']) {
$recur->contribution_status_id = $contributionStatuses['In Progress'];
}
break;
}

$recur->save();

if ($sendNotification) {

$autoRenewMembership = FALSE;
if ($recur->id &&
isset($ids['membership']) && $ids['membership']
Expand All @@ -211,14 +209,18 @@ public function recur(&$input, &$ids, &$objects, $first) {
}

if (!$first) {
//check if this contribution transaction is already processed
//if not create a contribution and then get it processed
// check if this contribution transaction is already processed
// if not create a contribution and then get it processed
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->trxn_id = $input['trxn_id'];
if ($contribution->trxn_id && $contribution->find()) {
CRM_Core_Error::debug_log_message("returning since contribution has already been handled");
Civi::log()->debug('PayPalIPN: Returning since contribution has already been handled (trxn_id: ' . $contribution->trxn_id . ')');
echo "Success: Contribution has already been handled<p>";
return TRUE;
return;
}

if ($input['paymentStatus'] != 'Completed') {
throw new CRM_Core_Exception("Ignore all IPN payments that are not completed");
}

$contribution->contact_id = $ids['contact'];
Expand All @@ -240,27 +242,23 @@ public function recur(&$input, &$ids, &$objects, $first) {
}

/**
* @param $input
* @param $ids
* @param $objects
* @param array $input
* @param array $ids
* @param array $objects
* @param bool $recur
* @param bool $first
*
* @return bool
* @return void
*/
public function single(
&$input, &$ids, &$objects,
$recur = FALSE,
$first = FALSE
) {
public function single(&$input, &$ids, &$objects, $recur = FALSE, $first = FALSE) {
$contribution = &$objects['contribution'];

// make sure the invoice is valid and matches what we have in the contribution record
if ((!$recur) || ($recur && $first)) {
if ($contribution->invoice_id != $input['invoice']) {
CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request");
Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request. (ID: ' . $contribution->id . ').');
echo "Failure: Invoice values dont match between database and IPN request<p>";
return FALSE;
return;
}
}
else {
Expand All @@ -269,9 +267,9 @@ public function single(

if (!$recur) {
if ($contribution->total_amount != $input['amount']) {
CRM_Core_Error::debug_log_message("Amount values dont match between database and IPN request");
Civi::log()->debug('PayPalIPN: Amount values dont match between database and IPN request. (ID: ' . $contribution->id . ').');
echo "Failure: Amount values dont match between database and IPN request<p>";
return FALSE;
return;
}
}
else {
Expand All @@ -280,9 +278,6 @@ public function single(

$transaction = new CRM_Core_Transaction();

$participant = &$objects['participant'];
$membership = &$objects['membership'];

$status = $input['paymentStatus'];
if ($status == 'Denied' || $status == 'Failed' || $status == 'Voided') {
return $this->failed($objects, $transaction);
Expand All @@ -298,11 +293,12 @@ public function single(
}

// check if contribution is already completed, if so we ignore this ipn
if ($contribution->contribution_status_id == 1) {
$completedStatusId = CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
if ($contribution->contribution_status_id == $completedStatusId) {
$transaction->commit();
CRM_Core_Error::debug_log_message("returning since contribution has already been handled");
Civi::log()->debug('PayPalIPN: Returning since contribution has already been handled. (ID: ' . $contribution->id . ').');
echo "Success: Contribution has already been handled<p>";
return TRUE;
return;
}

$this->completeTransaction($input, $ids, $objects, $transaction, $recur);
Expand All @@ -311,10 +307,10 @@ public function single(
/**
* Main function.
*
* @return bool
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function main() {

$objects = $ids = $input = array();
$component = $this->retrieve('module', 'String');
$input['component'] = $component;
Expand Down Expand Up @@ -361,29 +357,26 @@ public function main() {
if ($ids['contributionRecur']) {
// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($objects['contribution']->contribution_status_id == 1) {
$completedStatusId = CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
if ($objects['contribution']->contribution_status_id == $completedStatusId) {
$first = FALSE;
}
return $this->recur($input, $ids, $objects, $first);
}
else {
return $this->single($input, $ids, $objects, FALSE, FALSE);
$this->recur($input, $ids, $objects, $first);
return;
}
}
else {
return $this->single($input, $ids, $objects, FALSE, FALSE);
}
$this->single($input, $ids, $objects, FALSE, FALSE);
}

/**
* @param $input
* @param $ids
* @param array $input
* @param array $ids
*
* @return bool
* @throws \CRM_Core_Exception
*/
public function getInput(&$input, &$ids) {
if (!$this->getBillingID($ids)) {
return FALSE;
return;
}

$input['txnType'] = $this->retrieve('txn_type', 'String', FALSE);
Expand Down
Loading

0 comments on commit 6936f14

Please sign in to comment.