Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-09-29-13-10-47
[civicrm-core.git] / CRM / Core / Payment.php
index a7f21fde65ced9332a25850779b9afd1318d88e2..9d143ea7ba3b64de517973b6f87c232825fbb896 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -78,17 +78,20 @@ abstract class CRM_Core_Payment {
 
   protected $_paymentProcessor;
 
+  /**
+   * @var CRM_Core_Form
+   */
   protected $_paymentForm = NULL;
 
   /**
    * singleton function used to manage this object
    *
    * @param string  $mode the mode of operation: live or test
-   * @param object  $paymentProcessor the details of the payment processor being invoked
+   * @param array  $paymentProcessor the details of the payment processor being invoked
    * @param object  $paymentForm      reference to the form object if available
    * @param boolean $force            should we force a reload of this payment object
    *
-   * @return object
+   * @return CRM_Core_Payment
    * @static
    *
    */
@@ -124,10 +127,28 @@ abstract class CRM_Core_Payment {
     return self::$_singleton[$cacheKey];
   }
 
+  /**
+   * @param $params
+   *
+   * @return mixed
+   */
+  public static function logPaymentNotification($params) {
+    $message = 'payment_notification ';
+    if (!empty($params['processor_name'])) {
+      $message .= 'processor_name=' . $params['processor_name'];
+    }
+    if (!empty($params['processor_id'])) {
+      $message .= 'processor_id=' . $params['processor_id'];
+    }
+
+    $log = new CRM_Utils_SystemLogger();
+    $log->alert($message, $_REQUEST);
+  }
+
   /**
    * Setter for the payment form that wants to use the processor
    *
-   * @param obj $paymentForm
+   * @param CRM_Core_Form $paymentForm
    *
    */
   function setForm(&$paymentForm) {
@@ -137,7 +158,7 @@ abstract class CRM_Core_Payment {
   /**
    * Getter for payment form that is using the processor
    *
-   * @return obj  A form object
+   * @return CRM_Core_Form  A form object
    */
   function getForm() {
     return $this->_paymentForm;
@@ -165,13 +186,18 @@ abstract class CRM_Core_Payment {
   /**
    * This function checks to see if we have the right config values
    *
-   * @param  string $mode the mode we are operating in (live or test)
+   * @internal param string $mode the mode we are operating in (live or test)
    *
    * @return string the error message if any
    * @public
    */
   abstract function checkConfig();
 
+  /**
+   * @param $paymentProcessor
+   *
+   * @return bool
+   */
   static function paypalRedirect(&$paymentProcessor) {
     if (!$paymentProcessor) {
       return FALSE;
@@ -210,11 +236,14 @@ abstract class CRM_Core_Payment {
    * Load requested payment processor and call that processor's handle<$method> method
    *
    * @public
+   * @param $method
+   * @param array $params
    */
-  static function handlePaymentMethod($method, $params = array( )) {
+  static function handlePaymentMethod($method, $params = array()) {
     if (!isset($params['processor_id']) && !isset($params['processor_name'])) {
       CRM_Core_Error::fatal("Either 'processor_id' or 'processor_name' param is required for payment callback");
     }
+    self::logPaymentNotification($params);
 
     // Query db for processor ..
     $mode = @$params['mode'];
@@ -260,7 +289,10 @@ abstract class CRM_Core_Payment {
       }
       else {
         // Legacy or extension as module instance
-        $paymentClass = 'CRM_Core_' . $dao->class_name;
+        if (empty($paymentClass)) {
+          $paymentClass = 'CRM_Core_' . $dao->class_name;
+
+        }
       }
 
       $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($dao->processor_id, $mode);
@@ -310,79 +342,78 @@ abstract class CRM_Core_Payment {
     return method_exists(CRM_Utils_System::getClassName($this), $method);
   }
 
+  /**
+   * @param null $entityID
+   * @param null $entity
+   * @param string $action
+   *
+   * @return string
+   */
   function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') {
-    if ($action == 'cancel') {
-      $url = 'civicrm/contribute/unsubscribe';
-    }
-    elseif ($action == 'billing') {
-      if (!$this->isSupported('updateSubscriptionBillingInfo')) {
-        return NULL;
-      }
-      $url = 'civicrm/contribute/updatebilling';
-    }
-    elseif ($action == 'update') {
-      $url = 'civicrm/contribute/updaterecur';
+    // Set URL
+    switch ($action) {
+      case 'cancel' :
+        $url = 'civicrm/contribute/unsubscribe';
+        break;
+      case 'billing' :
+        //in notify mode don't return the update billing url
+        if (!$this->isSupported('updateSubscriptionBillingInfo')) {
+          return NULL;
+        }
+        $url = 'civicrm/contribute/updatebilling';
+        break;
+      case 'update' :
+        $url = 'civicrm/contribute/updaterecur';
+        break;
     }
+
     $session       = CRM_Core_Session::singleton();
     $userId        = $session->get('userID');
-    $checksumValue = "";
-
-    if ($entityID && $entity == 'membership') {
-      if (!$userId) {
-        $contactID     = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id");
-        $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf');
-        $checksumValue = "&cs={$checksumValue}";
-      }
-      return CRM_Utils_System::url($url, "reset=1&mid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
-    }
-
-    if ($entityID && $entity == 'contribution') {
-      if (!$userId) {
-        $contactID     = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id");
-        $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf');
-        $checksumValue = "&cs={$checksumValue}";
-      }
-      return CRM_Utils_System::url($url, "reset=1&coid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
-    }
-
-    if ($entityID && $entity == 'recur') {
-      if (!$userId) {
-        $sql = "
+    $contactID     = 0;
+    $checksumValue = '';
+    $entityArg     = '';
+
+    // Find related Contact
+    if ($entityID) {
+      switch ($entity) {
+        case 'membership' :
+          $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id");
+          $entityArg = 'mid';
+          break;
+
+        case 'contribution' :
+          $contactID = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id");
+          $entityArg = 'coid';
+          break;
+
+        case 'recur' :
+          $sql = "
     SELECT con.contact_id
       FROM civicrm_contribution_recur rec
 INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id )
      WHERE rec.id = %1
   GROUP BY rec.id";
-        $contactID     = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($entityID, 'Integer')));
-        $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf');
-        $checksumValue = "&cs={$checksumValue}";
+          $contactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($entityID, 'Integer')));
+          $entityArg = 'crid';
+          break;
+      }
+    }
+
+    // Add entity arguments
+    if ($entityArg != '') {
+      // Add checksum argument
+      if ($contactID != 0 && $userId != $contactID) {
+        $checksumValue = '&cs=' . CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf');
       }
-      return CRM_Utils_System::url($url, "reset=1&crid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
+      return CRM_Utils_System::url($url, "reset=1&{$entityArg}={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
     }
 
+    // Else login URL
     if ($this->isSupported('accountLoginURL')) {
       return $this->accountLoginURL();
     }
-    return $this->_paymentProcessor['url_recur'];
-  }
 
-  /**
-   * Check for presence of type 1 or type 3 enabled processors (means we can do back-office submit credit/debit card trxns)
-   * @public
-   */
-  static function allowBackofficeCreditCard($template = NULL, $variableName = 'newCredit') {
-    $newCredit = FALSE;
-    // restrict to type=1 (credit card) payment processor payment_types and only include billing mode types 1 and 3
-    $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE,
-      "billing_mode IN ( 1, 3 ) AND payment_type = 1"
-    );
-    if (count($processors) > 0) {
-      $newCredit = TRUE;
-    }
-    if ($template) {
-      $template->assign($variableName, $newCredit);
-    }
-    return $newCredit;
+    // Else default
+    return $this->_paymentProcessor['url_recur'];
   }
-
 }