From: Seamus Lee Date: Fri, 16 Sep 2016 06:23:08 +0000 (+1000) Subject: Fix storing of credit cards in database and add tests in X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c294dbbc8ada2c0c34c8231062d3807aa8aded7d;p=civicrm-core.git Fix storing of credit cards in database and add tests in --- diff --git a/CRM/Admin/Form/PaymentProcessor.php b/CRM/Admin/Form/PaymentProcessor.php index 7242cde625..a3bc30ae4f 100644 --- a/CRM/Admin/Form/PaymentProcessor.php +++ b/CRM/Admin/Form/PaymentProcessor.php @@ -329,10 +329,15 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form { if ($this->_ppType) { $defaults['payment_processor_type_id'] = $this->_ppType; } - $defaults['accept_credit_cards'] = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', + $cards = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', $this->_id, 'accepted_credit_cards' ), TRUE); + $acceptedCards = array(); + foreach ($cards as $card => $val) { + $acceptedCards[$card] = 1; + } + $defaults['accept_credit_cards'] = $acceptedCards; unset($defaults['accepted_credit_cards']); // now get testID $testDAO = new CRM_Financial_DAO_PaymentProcessor(); @@ -389,7 +394,20 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form { $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array::value($field, $values) : $values["test_{$field}"]; } } - $creditCards = empty($values['accept_credit_cards']) ? "NULL" : json_encode($values['accept_credit_cards']); + if (!empty($values['accept_credit_cards'])) { + $creditCards = array(); + $accptedCards = array_keys($values['accept_credit_cards']); + $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard(); + foreach ($creditCardTypes as $type => $val) { + if (in_array($type, $accptedCards)) { + $creditCards[$type] = $creditCardTypes[$type]; + } + } + $creditCards = json_encode($creditCards); + } + else { + $creditCards = "NULL"; + } $params = array_merge(array( 'id' => $test ? $this->_testID : $this->_id, 'domain_id' => $domainID, diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index f5cb848d13..47ec34404b 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -91,7 +91,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } /** - * Retieve array of allowed credit cards for this payment processor. + * Retrieve array of allowed credit cards for this payment processor. * @param interger|null $paymentProcessorID id of processor. * @return array */ diff --git a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php new file mode 100644 index 0000000000..761872d777 --- /dev/null +++ b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php @@ -0,0 +1,99 @@ + 'API_Test_PP_Type', + 'title' => 'API Test Payment Processor Type', + 'class_name' => 'CRM_Core_Payment_APITest', + 'billing_mode' => 'form', + 'payment_processor_type_id' => 1, + 'is_recur' => 0, + 'domain_id' => 1, + 'accepted_credit_cards' => json_encode(array( + 'Visa' => 'Visa', + 'Mastercard' => 'Mastercard', + 'Amex' => 'Amex', + )), + ); + $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($params); + $expectedCards = array( + 'Visa' => 'Visa', + 'Mastercard' => 'Mastercard', + 'Amex' => 'Amex', + ); + $cards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessor->id); + $this->assertEquals($cards, $expectedCards, 'Verify correct credit card types are returned'); + } + + public function testCreditCardCSSName() { + $params = array( + 'name' => 'API_Test_PP_Type', + 'title' => 'API Test Payment Processor Type', + 'class_name' => 'CRM_Core_Payment_APITest', + 'billing_mode' => 'form', + 'payment_processor_type_id' => 1, + 'is_recur' => 0, + 'domain_id' => 1, + 'accepted_credit_cards' => json_encode(array( + 'Visa' => 'Visa', + 'Mastercard' => 'Mastercard', + 'Amex' => 'Amex', + )), + ); + $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($params); + $cards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessor->id); + $CSSCards = CRM_Core_Payment_Form::getCreditCardCSSNames($cards); + $expectedCSSCards = array( + 'visa' => 'Visa', + 'mastercard' => 'Mastercard', + 'amex' => 'Amex', + ); + $this->assertEquals($CSSCards, $expectedCSSCards, 'Verify correct credit card types are returned'); + $CSSCards2 = CRM_Core_Payment_Form::getCreditCardCSSNames(array()); + $allCards = array( + 'visa' => 'Visa', + 'mastercard' => 'MasterCard', + 'amex' => 'Amex', + 'discover' => 'Discover', + ); + $this->assertEquals($CSSCards2, $allCards, 'Verify correct credit card types are returned'); + } + +}