INFRA-132 - CRM/Core - phpcbf
[civicrm-core.git] / CRM / Core / Payment / AuthorizeNetIPN.php
index 93c43e7fe2c94822f1bc3c955e53aa9af9eda68f..6257cc4d6af274b53b56691ffe06ed7f1b76d561 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  *
  */
 class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
+
   /**
-   * Constructor
+   * Constructor function
+   *
+   * @param $inputData
+   *   Array contents of HTTP REQUEST.
+   *
+   * @throws CRM_Core_Exception
    */
-  function __construct() {
+  public function __construct($inputData) {
+    $this->setInputParameters($inputData);
     parent::__construct();
   }
 
@@ -45,11 +52,11 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
    *
    * @return bool|void
    */
-  function main($component = 'contribute') {
+  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 = self::retrieve('x_subscription_id', 'String');
+    $x_subscription_id = $this->retrieve('x_subscription_id', 'String');
 
     if ($x_subscription_id) {
       //Approved
@@ -80,17 +87,19 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
         return $this->recur($input, $ids, $objects, $first);
       }
     }
+    return TRUE;
   }
 
   /**
    * @param array $input
    * @param array $ids
-   * @param $objects
+   * @param array $objects
    * @param $first
    *
    * @return bool
    */
-  function recur(&$input, &$ids, &$objects, $first) {
+  public function recur(&$input, &$ids, &$objects, $first) {
+    $this->_isRecurring = TRUE;
     $recur = &$objects['contributionRecur'];
 
     // do a subscription check
@@ -147,14 +156,12 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
     // check and validate gateway MD5 response if present
     $this->checkMD5($ids, $input);
 
-    $sendNotification = FALSE;
     if ($input['response_code'] == 1) {
       // Approved
       if ($first) {
         $recur->start_date = $now;
         $recur->trxn_id = $recur->processor_id;
-        $sendNotification = TRUE;
-        $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START;
+        $this->_isFirstOrLastRecurringPayment = CRM_Core_Payment::RECURRING_PAYMENT_START;
       }
       $statusName = 'In Progress';
       if (($recur->installments > 0) &&
@@ -163,8 +170,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
         // this is the last payment
         $statusName = 'Completed';
         $recur->end_date = $now;
-        $sendNotification = TRUE;
-        $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END;
+        $this->_isFirstOrLastRecurringPayment = CRM_Core_Payment::RECURRING_PAYMENT_END;
       }
       $recur->modified_date = $now;
       $recur->contribution_status_id = array_search($statusName, $contributionStatus);
@@ -195,38 +201,22 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
     }
 
     $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
-
-    if ($sendNotification) {
-      $autoRenewMembership = FALSE;
-      if ($recur->id &&
-        isset($ids['membership']) && $ids['membership']
-      ) {
-        $autoRenewMembership = TRUE;
-      }
-
-      //send recurring Notification email for user
-      CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus,
-        $ids['contact'],
-        $ids['contributionPage'],
-        $recur,
-        $autoRenewMembership
-      );
-    }
   }
 
   /**
    * @param $input
    * @param $ids
    */
-  function getInput(&$input, &$ids) {
-    $input['amount'] = self::retrieve('x_amount', 'String');
-    $input['subscription_id'] = self::retrieve('x_subscription_id', 'Integer');
-    $input['response_code'] = self::retrieve('x_response_code', 'Integer');
-    $input['MD5_Hash'] = self::retrieve('x_MD5_Hash', 'String', FALSE, '');
-    $input['response_reason_code'] = self::retrieve('x_response_reason_code', 'String', FALSE);
-    $input['response_reason_text'] = self::retrieve('x_response_reason_text', 'String', FALSE);
-    $input['subscription_paynum'] = self::retrieve('x_subscription_paynum', 'Integer', FALSE, 0);
-    $input['trxn_id'] = self::retrieve('x_trans_id', 'String', FALSE);
+  public function getInput(&$input, &$ids) {
+    $input['amount'] = $this->retrieve('x_amount', 'String');
+    $input['subscription_id'] = $this->retrieve('x_subscription_id', 'Integer');
+    $input['response_code'] = $this->retrieve('x_response_code', 'Integer');
+    $input['MD5_Hash'] = $this->retrieve('x_MD5_Hash', 'String', FALSE, '');
+    $input['response_reason_code'] = $this->retrieve('x_response_reason_code', 'String', FALSE);
+    $input['response_reason_text'] = $this->retrieve('x_response_reason_text', 'String', FALSE);
+    $input['subscription_paynum'] = $this->retrieve('x_subscription_paynum', 'Integer', FALSE, 0);
+    $input['trxn_id'] = $this->retrieve('x_trans_id', 'String', FALSE);
+
     if ($input['trxn_id']) {
       $input['is_test'] = 0;
     }
@@ -250,7 +240,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
       "email-{$billingID}" => 'x_email',
     );
     foreach ($params as $civiName => $resName) {
-      $input[$civiName] = self::retrieve($resName, 'String', FALSE);
+      $input[$civiName] = $this->retrieve($resName, 'String', FALSE);
     }
   }
 
@@ -258,9 +248,9 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
    * @param $ids
    * @param $input
    */
-  function getIDs(&$ids, &$input) {
-    $ids['contact'] = self::retrieve('x_cust_id', 'Integer', FALSE, 0);
-    $ids['contribution'] = self::retrieve('x_invoice_num', 'Integer');
+  public function getIDs(&$ids, &$input) {
+    $ids['contact'] = $this->retrieve('x_cust_id', 'Integer', FALSE, 0);
+    $ids['contribution'] = $this->retrieve('x_invoice_num', 'Integer');
 
     // joining with contribution table for extra checks
     $sql = "
@@ -313,25 +303,26 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr
   }
 
   /**
-   * @param $name
-   * @param $type
+   * @param string $name
+   *   Parameter name.
+   * @param string $type
+   *   Parameter type.
    * @param bool $abort
+   *   Abort if not present.
    * @param null $default
-   * @param string $location
+   *   Default value.
    *
+   * @throws CRM_Core_Exception
    * @return mixed
    */
-  static function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'POST') {
-    static $store = NULL;
-    $value = CRM_Utils_Request::retrieve($name, $type, $store,
-      FALSE, $default, $location
+  public function retrieve($name, $type, $abort = TRUE, $default = NULL) {
+    $value = CRM_Utils_Type::validate(
+      empty($this->_inputParameters[$name]) ? $default : $this->_inputParameters[$name],
+      $type,
+      FALSE
     );
     if ($abort && $value === NULL) {
-      CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location");
-      CRM_Core_Error::debug_var('POST', $_POST);
-      CRM_Core_Error::debug_var('REQUEST', $_REQUEST);
-      echo "Failure: Missing Parameter<p>";
-      exit();
+      throw new CRM_Core_Exception("Could not find an entry for $name");
     }
     return $value;
   }
@@ -342,7 +333,7 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr
    *
    * @return bool
    */
-  function checkMD5($ids, $input) {
+  public function checkMD5($ids, $input) {
     $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ids['paymentProcessor'],
       $input['is_test'] ? 'test' : 'live'
     );
@@ -356,4 +347,3 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr
     return TRUE;
   }
 }
-