Merge branch 'CRM-15714' of https://github.com/adixon/civicrm-core into adixon-CRM...
[civicrm-core.git] / CRM / Core / Payment.php
index 163971fd19d42131820ec8996d7c2c83eaed996b..da17493957244fbdc071e306feea8f46955cc66f 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 use Civi\Payment\System;
 
@@ -70,7 +70,7 @@ abstract class CRM_Core_Payment {
   protected $_paymentProcessor;
 
   /**
-   * Singleton function used to manage this object
+   * Singleton function used to manage this object.
    * We will migrate to calling Civi\Payment\System::singleton()->getByProcessor($paymentProcessor)
    * & Civi\Payment\System::singleton()->getById($paymentProcessor) directly as the main access methods & work
    * to remove this function all together
@@ -86,7 +86,6 @@ abstract class CRM_Core_Payment {
    *
    * @return CRM_Core_Payment
    * @throws \CRM_Core_Exception
-   * @static
    */
   public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
     // make sure paymentProcessor is not empty
@@ -119,7 +118,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Check if capability is supported
+   * Check if capability is supported.
    * @param string $capability
    *   E.g BackOffice, LiveMode, FutureRecurStartDate.
    *
@@ -156,7 +155,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Are test payments supported
+   * Are test payments supported.
    * @return bool
    */
   protected function supportsTestMode() {
@@ -164,7 +163,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Should the first payment date be configurable when setting up back office recurring payments
+   * Should the first payment date be configurable when setting up back office recurring payments.
    * We set this to false for historical consistency but in fact most new processors use tokens for recurring and can support this
    * @return bool
    */
@@ -172,6 +171,17 @@ abstract class CRM_Core_Payment {
     return FALSE;
   }
 
+  /**
+   * Default payment instrument validation
+   * Implement the usual Luhn algorithm via a static function in the CRM_Core_Payment_Form if it's a credit card
+   * Not a static function, because I need to check for payment_type
+   */
+  public function validatePaymentInstrument($values, &$errors) {
+    if ($this->_paymentProcessor['payment_type'] == 1) {
+      CRM_Core_Payment_Form::validateCreditCard($values, $errors);
+    }
+  }
+
   /**
    * Setter for the payment form that wants to use the processor
    * @deprecated
@@ -182,7 +192,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Getter for payment form that is using the processor
+   * Getter for payment form that is using the processor.
    * @deprecated
    * @return CRM_Core_Form
    *   A form object
@@ -192,7 +202,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Getter for accessing member vars
+   * Getter for accessing member vars.
    * @todo believe this is unused
    * @param string $name
    *
@@ -203,7 +213,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Get name for the payment information type
+   * Get name for the payment information type.
    * @todo - use option group + name field (like Omnipay does)
    * @return string
    */
@@ -212,7 +222,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Get label for the payment information type
+   * Get label for the payment information type.
    * @todo - use option group + labels (like Omnipay does)
    * @return string
    */
@@ -221,7 +231,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Get array of fields that should be displayed on the payment form
+   * Get array of fields that should be displayed on the payment form.
    * @todo make payment type an option value & use it in the function name - currently on debit & credit card work
    * @return array
    * @throws CiviCRM_API3_Exception
@@ -234,7 +244,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Get array of fields that should be displayed on the payment form for credit cards
+   * Get array of fields that should be displayed on the payment form for credit cards.
    *
    * @return array
    */
@@ -248,7 +258,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Get array of fields that should be displayed on the payment form for direct debits
+   * Get array of fields that should be displayed on the payment form for direct debits.
    *
    * @return array
    */
@@ -262,7 +272,7 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Return an array of all the details about the fields potentially required for payment fields
+   * Return an array of all the details about the fields potentially required for payment fields.
    * Only those determined by getPaymentFormFields will actually be assigned to the form
    *
    * @return array
@@ -304,7 +314,7 @@ abstract class CRM_Core_Payment {
             'rule_message' => ts('Please enter a valid value for your card security code. This is usually the last 3-4 digits on the card\'s signature panel.'),
             'rule_name' => 'integer',
             'rule_parameters' => NULL,
-          )
+          ),
         ),
       ),
       'credit_card_exp_date' => array(
@@ -319,7 +329,7 @@ abstract class CRM_Core_Payment {
             'rule_message' => ts('Card expiration date cannot be a past date.'),
             'rule_name' => 'currentDate',
             'rule_parameters' => TRUE,
-          )
+          ),
         ),
       ),
       'credit_card_type' => array(
@@ -358,7 +368,7 @@ abstract class CRM_Core_Payment {
             'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'),
             'rule_name' => 'nopunctuation',
             'rule_parameters' => NULL,
-          )
+          ),
         ),
         'is_required' => TRUE,
       ),
@@ -379,7 +389,7 @@ abstract class CRM_Core_Payment {
             'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'),
             'rule_name' => 'nopunctuation',
             'rule_parameters' => NULL,
-          )
+          ),
         ),
       ),
       'bank_name' => array(
@@ -399,8 +409,11 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * This function collects all the information from a web/api form and invokes
-   * the relevant payment processor specific functions to perform the transaction
+   * Calling this from outside the payment subsystem is deprecated - use doPayment.
+   *
+   * Does a server to server payment transaction.
+   *
+   * Note that doPayment will throw an exception so the code may need to be modified
    *
    * @param array $params
    *   Assoc array of input parameters for this transaction.
@@ -409,7 +422,7 @@ abstract class CRM_Core_Payment {
    *   the result in an nice formatted array (or an error object)
    * @abstract
    */
-  abstract function doDirectPayment(&$params);
+  abstract protected function doDirectPayment(&$params);
 
   /**
    * Process payment - this function wraps around both doTransferPayment and doDirectPayment
@@ -423,7 +436,7 @@ abstract class CRM_Core_Payment {
    *   (modified)
    * @throws CRM_Core_Exception
    */
-  public function doPayment(&$params, $component) {
+  public function doPayment(&$params, $component = 'contribute') {
     if ($this->_paymentProcessor['billing_mode'] == 4) {
       $result = $this->doTransferCheckout($params, $component);
     }
@@ -438,12 +451,12 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * This function checks to see if we have the right config values
+   * This function checks to see if we have the right config values.
    *
    * @return string
    *   the error message if any
    */
-  abstract function checkConfig();
+  abstract protected function checkConfig();
 
   /**
    * @param $paymentProcessor
@@ -590,7 +603,7 @@ abstract class CRM_Core_Payment {
    * @param string $method
    *   Method to check for.
    *
-   * @return boolean
+   * @return bool
    */
   public function isSupported($method = 'cancelSubscription') {
     return method_exists(CRM_Utils_System::getClassName($this), $method);
@@ -672,4 +685,5 @@ INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id )
     // Else default
     return $this->_paymentProcessor['url_recur'];
   }
+
 }