Merge pull request #5786 from mallezie/crm-16283-duplicaterelationcheck
[civicrm-core.git] / CRM / Core / Payment.php
index 8b211b4e42c8463f0f06186d583306f8c3d5753a..63c2ce9033cf3feb07c51cdb39f9451f54ca02c7 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -89,7 +89,7 @@ abstract class CRM_Core_Payment {
    * @return CRM_Core_Payment
    * @throws \CRM_Core_Exception
    */
-  public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
+  public static function singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
     // make sure paymentProcessor is not empty
     // CRM-7424
     if (empty($paymentProcessor)) {
@@ -206,6 +206,27 @@ abstract class CRM_Core_Payment {
     }
   }
 
+  /**
+   * Getter for the payment processor.
+   *
+   * The payment processor array is based on the civicrm_payment_processor table entry.
+   *
+   * @return array
+   *   Payment processor array.
+   */
+  public function getPaymentProcessor() {
+    return $this->_paymentProcessor;
+  }
+
+  /**
+   * Setter for the payment processor.
+   *
+   * @param array $processor
+   */
+  public function setPaymentProcessor($processor) {
+    $this->_paymentProcessor = $processor;
+  }
+
   /**
    * Setter for the payment form that wants to use the processor.
    *
@@ -229,7 +250,9 @@ abstract class CRM_Core_Payment {
 
   /**
    * Getter for accessing member vars.
+   *
    * @todo believe this is unused
+   *
    * @param string $name
    *
    * @return null
@@ -318,6 +341,7 @@ abstract class CRM_Core_Payment {
           'size' => 20,
           'maxlength' => 20,
           'autocomplete' => 'off',
+          'class' => 'creditcard',
         ),
         'is_required' => TRUE,
       ),
@@ -452,8 +476,10 @@ abstract class CRM_Core_Payment {
   abstract protected function doDirectPayment(&$params);
 
   /**
-   * Process payment - this function wraps around both doTransferPayment and doDirectPayment
-   * it ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms more agnostic
+   * Process payment - this function wraps around both doTransferPayment and doDirectPayment.
+   *
+   * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms
+   * more agnostic.
    *
    * @param array $params
    *
@@ -486,8 +512,12 @@ abstract class CRM_Core_Payment {
   abstract protected function checkConfig();
 
   /**
+   * Redirect for paypal.
+   *
+   * @todo move to paypal class or remove
+   *
    * @param $paymentProcessor
-   * @todo move to paypal class or remover
+   *
    * @return bool
    */
   public static function paypalRedirect(&$paymentProcessor) {
@@ -507,6 +537,10 @@ abstract class CRM_Core_Payment {
   }
 
   /**
+   * Handle incoming payment notification.
+   *
+   * IPNs, also called silent posts are notifications of payment outcomes or activity on an external site.
+   *
    * @todo move to0 \Civi\Payment\System factory method
    * Page callback for civicrm/payment/ipn
    */
@@ -519,6 +553,7 @@ abstract class CRM_Core_Payment {
         'mode' => @$_GET['mode'],
       )
     );
+    CRM_Utils_System::civiExit();
   }
 
   /**
@@ -541,16 +576,11 @@ abstract class CRM_Core_Payment {
     }
     self::logPaymentNotification($params);
 
-    // Query db for processor ..
-    $mode = @$params['mode'];
-
     $sql = "SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id
               FROM civicrm_payment_processor_type ppt
         INNER JOIN civicrm_payment_processor pp
                 ON pp.payment_processor_type_id = ppt.id
-               AND pp.is_active
-               AND pp.is_test = %1";
-    $args[1] = array($mode == 'test' ? 1 : 0, 'Integer');
+               AND pp.is_active";
 
     if (isset($params['processor_id'])) {
       $sql .= " WHERE pp.id = %2";
@@ -558,7 +588,12 @@ abstract class CRM_Core_Payment {
       $notFound = "No active instances of payment processor ID#'{$params['processor_id']}'  were found.";
     }
     else {
-      $sql .= " WHERE ppt.name = %2";
+      // This is called when processor_name is passed - passing processor_id instead is recommended.
+      $sql .= " WHERE ppt.name = %2 AND pp.is_test = %1";
+      $args[1] = array(
+        (CRM_Utils_Array::value('mode', $params) == 'test') ? 1 : 0,
+        'Integer',
+      );
       $args[2] = array($params['processor_name'], 'String');
       $notFound = "No active instances of the '{$params['processor_name']}' payment processor were found.";
     }
@@ -585,22 +620,16 @@ abstract class CRM_Core_Payment {
       }
       else {
         // Legacy or extension as module instance
-        if (empty($paymentClass)) {
-          $paymentClass = 'CRM_Core_' . $dao->class_name;
-
-        }
+        $paymentClass = 'CRM_Core_' . $dao->class_name;
       }
 
-      $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($dao->processor_id, $mode);
+      $processorInstance = Civi\Payment\System::singleton()->getById($dao->processor_id);
 
       // Should never be empty - we already established this processor_id exists and is active.
-      if (empty($paymentProcessor)) {
+      if (empty($processorInstance)) {
         continue;
       }
 
-      // Instantiate PP
-      $processorInstance = new $paymentClass($mode, $paymentProcessor);
-
       // Does PP implement this method, and can we call it?
       if (!method_exists($processorInstance, $method) ||
         !is_callable(array($processorInstance, $method))
@@ -621,11 +650,6 @@ abstract class CRM_Core_Payment {
         "$method method is unsupported in legacy payment processors."
       );
     }
-
-    // Exit here on web requests, allowing just the plain text response to be echoed
-    if ($method == 'handlePaymentNotification') {
-      CRM_Utils_System::civiExit();
-    }
   }
 
   /**