avoid fetching processor ID from IPN reponse and added unit test
authordeb.monish <monish.deb@webaccessglobal.com>
Thu, 18 Aug 2016 09:21:04 +0000 (14:51 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Thu, 18 Aug 2016 09:21:04 +0000 (14:51 +0530)
CRM/Core/Payment/PayPalIPN.php
tests/phpunit/CRM/Core/Payment/PayPalPNTest.php

index b90ccd67db576aef264b1d95fab7b6e30678b347..b5bbcbe0af4a8595a1039d9d7409157d8ae08de5 100644 (file)
@@ -333,18 +333,22 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
       $ids['onbehalf_dupe_alert'] = $this->retrieve('onBehalfDupeAlert', 'Integer', FALSE);
     }
 
-    $processorParams = array(
-      'user_name' => $this->retrieve('receiver_email', 'String', FALSE),
-      'payment_processor_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', 'PayPal_Standard', 'id', 'name'),
-      'is_test' => empty($input['is_test']) ? 0 : 1,
-    );
+    $paymentProcessorID = $this->retrieve('processor_id', 'Integer', FALSE);
+    if (!empty($paymentProcessorID)) {
+      $processorParams = array(
+        'user_name' => $this->retrieve('receiver_email', 'String', FALSE),
+        'payment_processor_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', 'PayPal_Standard', 'id', 'name'),
+        'is_test' => empty($input['is_test']) ? 0 : 1,
+      );
 
-    $processorInfo = array();
-    if (!CRM_Financial_BAO_PaymentProcessor::retrieve($processorParams, $processorInfo)) {
-      return FALSE;
+      $processorInfo = array();
+      if (!CRM_Financial_BAO_PaymentProcessor::retrieve($processorParams, $processorInfo)) {
+        return FALSE;
+      }
+      $paymentProcessorID = $processorInfo['id'];
     }
 
-    if (!$this->validateData($input, $ids, $objects, TRUE, $processorInfo['id'])) {
+    if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
       return FALSE;
     }
 
index fb2fe9080638a2fa9f98195484c9bcb624354a31..b8c5f507e78689a4f6e500ecbc411d9702dc4881 100644 (file)
@@ -52,12 +52,11 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase {
     $this->_paymentProcessorID = $this->paymentProcessorCreate(array('is_test' => 0, 'payment_processor_type_id' => 'PayPal_Standard'));
     $this->_contactID = $this->individualCreate();
     $contributionPage = $this->callAPISuccess('contribution_page', 'create', array(
-        'title' => "Test Contribution Page",
-        'financial_type_id' => $this->_financialTypeID,
-        'currency' => 'USD',
-        'payment_processor' => $this->_paymentProcessorID,
-      )
-    );
+      'title' => "Test Contribution Page",
+      'financial_type_id' => $this->_financialTypeID,
+      'currency' => 'USD',
+      'payment_processor' => $this->_paymentProcessorID,
+    ));
     $this->_contributionPageID = $contributionPage['id'];
   }
 
@@ -68,6 +67,35 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase {
     $this->quickCleanUpFinancialEntities();
   }
 
+  public function testIPNPaymentSuccess() {
+    $pendingStatusID = CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name');
+    $completedStatusID = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
+    $params = array(
+      'payment_processor_id' => $this->_paymentProcessorID,
+      'contact_id' => $this->_contactID,
+      'trxn_id' => NULL,
+      'invoice_id' => $this->_invoiceID,
+      'contribution_status_id' => $pendingStatusID,
+    );
+    $this->_contributionID = $this->contributionCreate($params);
+    $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $this->_contributionID, 'sequential' => 1));
+    // assert that contribution created before handling payment via paypal standard has no transaction id set and pending status
+    $this->assertEquals(NULL, $contribution['values'][0]['trxn_id']);
+    $this->assertEquals($pendingStatusID, $contribution['values'][0]['contribution_status_id']);
+
+    global $_REQUEST;
+    $_REQUEST = array('q' => CRM_Utils_System::url('civicrm/payment/ipn/' . $this->_paymentProcessorID)) + $this->getPaypalTransaction();
+
+    $paymentProcesors = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $this->_paymentProcessorID));
+    $payment = Civi\Payment\System::singleton()->getByProcessor($paymentProcesors);
+    $payment->handlePaymentNotification();
+
+    $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $this->_contributionID, 'sequential' => 1));
+    // assert that contribution is completed after getting response from paypal standard which has transaction id set and completed status
+    $this->assertEquals($_REQUEST['txn_id'], $contribution['values'][0]['trxn_id']);
+    $this->assertEquals($completedStatusID, $contribution['values'][0]['contribution_status_id']);
+  }
+
   /**
    * Test IPN response updates contribution_recur & contribution for first & second contribution.
    *
@@ -155,6 +183,30 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * Get IPN style details for an incoming paypal standard transaction.
+   */
+  public function getPaypalTransaction() {
+    return array(
+      'contactID' => $this->_contactID,
+      'contributionID' => $this->_contributionID,
+      'invoice' => $this->_invoiceID,
+      'mc_gross' => '100.00',
+      'mc_fee' => '5.00',
+      'settle_amount' => '95.00',
+      'module' => 'contribute',
+      'payer_id' => 'FV5ZW7TLMQ874',
+      'payment_status' => 'Completed',
+      'receiver_email' => 'sunil._1183377782_biz_api1.webaccess.co.in',
+      'txn_type' => 'web_accept',
+      'last_name' => 'Roberty',
+      'payment_fee' => '0.63',
+      'first_name' => 'Robert',
+      'txn_id' => '8XA571746W2698126',
+      'residence_country' => 'US',
+    );
+  }
+
   /**
    * Get IPN-style details for a second incoming transaction.
    *