CRM-15714 CiviContribute convert validateCreditCard to validatePaymentInstrument
authoradixon <alan.g.dixon@gmail.com>
Mon, 2 Feb 2015 21:11:39 +0000 (16:11 -0500)
committeradixon <alan.g.dixon@gmail.com>
Mon, 2 Feb 2015 21:11:39 +0000 (16:11 -0500)
The validateCreditCard function is too restrictive for both SWIPE and DirectDebit payment processors. Convert those
calls to a new validatePaymentInstrument that only invokes validateCreditCard for credit card payment processors,
and also allows SWIPE processors to override it.

CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/UpdateBilling.php
CRM/Core/Payment.php
CRM/Core/Payment/Form.php
CRM/Event/Cart/Form/Checkout/Payment.php
CRM/Event/Form/Participant.php
CRM/Event/Form/Registration/AdditionalParticipant.php
CRM/Event/Form/Registration/Register.php
CRM/Member/Form/Membership.php

index 243daceabab84b8ecbdd3a7acf558a3fb44f58b6..5ccaac7b18a3d6fcf783682056a4f9f17916bb53 100644 (file)
@@ -1083,7 +1083,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     if (!empty($self->_paymentFields)) {
       CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors);
     }
-    CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
+    CRM_Core_Payment_Form::validatePaymentInstrument($fields['payment_processor'], $fields, $errors, $self);
 
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
       if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
index aa7070cb086beb7373119a183e8e37a82576a862..9d2705d3c9e2d4f89cbfaea8825fc539382c26e5 100644 (file)
@@ -234,8 +234,8 @@ class CRM_Contribute_Form_UpdateBilling extends CRM_Core_Form {
     $errors = array();
     CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
 
-    // make sure that credit card number and cvv are valid
-    CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
+    // validate the payment instrument values (e.g. credit card number)
+    CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, $self);
 
     return empty($errors) ? TRUE : $errors;
   }
index 899c83709779711c0088ddc99441aa1f32c95de3..a6b6a03c47654113054bb97c9870abfb9950c7e7 100644 (file)
@@ -171,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
index 78d5413742972070d3780047ae6f33a235c4deea..d682e9800270aeb9db35ba428a526ee3af1d0d01 100644 (file)
@@ -316,6 +316,20 @@ class CRM_Core_Payment_Form {
     }
   }
 
+  /**
+   * Validate the payment instrument values before passing it to the payment processor
+   * We want this to be overrideable by the payment processor, and default to using
+   * this object's validCreditCard for credit cards (implemented as the default in the Payment class).
+   */
+  public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $form) {
+    // ignore if we don't have a payment instrument to validate (e.g. backend payments)
+    if ($payment_processor_id > 0) {
+      $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id,'live');
+      $payment = CRM_Core_Payment::singleton('live', $paymentProcessor, $form);
+      $payment->validatePaymentInstrument($values, $errors);
+    }
+  }
+
   /**
    * The credit card pseudo constant results only the CC label, not the key ID
    * So we normalize the name to use it as a CSS class.
index 7777e51750ac14e8aeda4b4b596c7df56fe4a02d..b646e1290946cc57e69828c4ff64326448324630 100644 (file)
@@ -398,8 +398,8 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       }
       CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
 
-      // make sure that credit card number and cvv are valid
-      CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
+      // validate payment instrument values (e.g. credit card number)
+      CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, $self);
     }
 
     return empty($errors) ? TRUE : $errors;
index 3ee04fe61c43c0aba6479f81e8d2fe4f61dc7f5a..881ad5731da7f61773f82acf748c5f9d82d4755e 100644 (file)
@@ -814,8 +814,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     $errorMsg = array();
 
     if (!empty($values['payment_processor_id'])) {
-      // make sure that credit card number and cvv are valid
-      CRM_Core_Payment_Form::validateCreditCard($values, $errorMsg);
+      // make sure that payment instrument values (e.g. credit card number and cvv) are valid
+      CRM_Core_Payment_Form::validatePaymentInstrument($values['payment_processor_id'], $values, $errorMsg, $self);
     }
 
     if (!empty($values['record_contribution'])) {
index 90200122dfcc33af90638acccab4b504352423b2..7eaa956eea7c4b829df2e8466615bc51840a5896 100644 (file)
@@ -565,8 +565,9 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
 
     CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
 
-    // make sure that credit card number and cvv are valid
-    CRM_Core_Payment_Form::validateCreditCard($self->_params[0], $errors);
+    // validate supplied payment instrument values (e.g. credit card number and cvv)
+    $payment_processor_id = $self->params[0]['payment_processor_id'];
+    CRM_Core_Payment_Form::validatePaymentInstrument($payment_processor_id, $self->_params[0], $errors, $self);
 
     if ($errors) {
       return FALSE;
index 968ec60b339ae616b5c00d109de24c68306c3bb0..0abef5da29e61397e342496416877650a6410268 100644 (file)
@@ -930,7 +930,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       if (!empty($self->_paymentFields)) {
         CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors);
       }
-      CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
+      CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessorID, $fields, $errors, $self);
     }
 
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
index 315efb07dbff6fbff2f24d1c42fd79c72567a0a5..eea3547eecfd3c83e646af31d6f7ef3d862bb577 100644 (file)
@@ -859,8 +859,8 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     }
 
     if (!empty($params['payment_processor_id'])) {
-      // make sure that credit card number and cvv are valid
-      CRM_Core_Payment_Form::validateCreditCard($params, $errors);
+      // validate payment instrument (e.g. credit card number)
+      CRM_Core_Payment_Form::validatePaymentInstrument($params['payment_processor_id'], $params, $errors, $self);
     }
 
     $joinDate = NULL;