From 27b252afd777dc3e1691ce3146b005ff77bdb058 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 27 Sep 2016 07:00:34 +1000 Subject: [PATCH] Fix error found by Jitendra and add form validation on the credit card type checking against options for payment processor --- CRM/Admin/Form/PaymentProcessor.php | 6 ++++-- CRM/Contribute/Form/AbstractEditPayment.php | 2 +- CRM/Core/Payment.php | 2 +- CRM/Core/Payment/Form.php | 9 ++++++++- CRM/Core/Payment/PayPalImpl.php | 2 +- CRM/Utils/Rule.php | 3 --- templates/CRM/Core/BillingBlock.js | 12 +++++++++--- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CRM/Admin/Form/PaymentProcessor.php b/CRM/Admin/Form/PaymentProcessor.php index a3bc30ae4f..c29daa5b71 100644 --- a/CRM/Admin/Form/PaymentProcessor.php +++ b/CRM/Admin/Form/PaymentProcessor.php @@ -334,8 +334,10 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form { 'accepted_credit_cards' ), TRUE); $acceptedCards = array(); - foreach ($cards as $card => $val) { - $acceptedCards[$card] = 1; + if (!empty($cards)) { + foreach ($cards as $card => $val) { + $acceptedCards[$card] = 1; + } } $defaults['accept_credit_cards'] = $acceptedCards; unset($defaults['accepted_credit_cards']); diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index c5f8855dc0..cacb2db922 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -350,7 +350,7 @@ WHERE contribution_id = {$id} } } } - CRM_Financial_Form_Payment::addCreditCardJs(); + CRM_Financial_Form_Payment::addCreditCardJs($id); } $this->assign('recurringPaymentProcessorIds', empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 745bf1131e..bb4e62a906 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -393,7 +393,7 @@ abstract class CRM_Core_Payment { public function validatePaymentInstrument($values, &$errors) { CRM_Core_Form::validateMandatoryFields($this->getMandatoryFields(), $values, $errors); if ($this->_paymentProcessor['payment_type'] == 1) { - CRM_Core_Payment_Form::validateCreditCard($values, $errors); + CRM_Core_Payment_Form::validateCreditCard($this->_paymentProcessor['id'], $values, $errors); } } diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index 2134186568..8101efd0ba 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -326,11 +326,18 @@ class CRM_Core_Payment_Form { * Make sure that credit card number and cvv are valid. * Called within the scope of a QF formRule function * + * @param int $processorID * @param array $values * @param array $errors */ - public static function validateCreditCard($values, &$errors) { + public static function validateCreditCard($processorID = NULL, $values, &$errors) { if (!empty($values['credit_card_type']) || !empty($values['credit_card_number'])) { + if (!empty($values['credit_card_type'])) { + $processorCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($processorID); + if (!empty($processorCards) && !in_array($values['credit_card_type'], $processorCards)) { + $errors['credit_card_type'] = ts('This procesor does not support credit card type ' . $values['credit_card_type']); + } + } if (!empty($values['credit_card_number']) && !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) ) { diff --git a/CRM/Core/Payment/PayPalImpl.php b/CRM/Core/Payment/PayPalImpl.php index d71e9613d5..1634c93f1a 100644 --- a/CRM/Core/Payment/PayPalImpl.php +++ b/CRM/Core/Payment/PayPalImpl.php @@ -176,7 +176,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { */ public function validatePaymentInstrument($values, &$errors) { if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal' && !$this->isPaypalExpress($values)) { - CRM_Core_Payment_Form::validateCreditCard($values, $errors); + CRM_Core_Payment_Form::validateCreditCard($this->_paymentProcessor['id'], $values, $errors); CRM_Core_Form::validateMandatoryFields($this->getMandatoryFields(), $values, $errors); } } diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 0ea7c32e3c..88310ddd4a 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -725,7 +725,6 @@ class CRM_Utils_Rule { * @return bool */ public static function creditCardNumber($value, $type) { - require_once 'Validate/Finance/CreditCard.php'; return Validate_Finance_CreditCard::number($value, $type); } @@ -736,8 +735,6 @@ class CRM_Utils_Rule { * @return bool */ public static function cvv($value, $type) { - require_once 'Validate/Finance/CreditCard.php'; - return Validate_Finance_CreditCard::cvv($value, $type); } diff --git a/templates/CRM/Core/BillingBlock.js b/templates/CRM/Core/BillingBlock.js index 1802fb973b..4d75202f6e 100644 --- a/templates/CRM/Core/BillingBlock.js +++ b/templates/CRM/Core/BillingBlock.js @@ -67,9 +67,15 @@ $.each(card_types, function(key, pattern) { if (ccnumber.match('^' + pattern + '$')) { var value = card_values[key]; - $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + key).css('opacity', 1); - $('select#credit_card_type').val(value); - return false; + //$.each(CRM.config.creditCardTypes, function(key2, val) { + // if (value == val) { + $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + key).css('opacity', 1); + $('select#credit_card_type').val(value); + return false; + // } + // else { + // $ + // }); } }); } -- 2.25.1