Fix storing of credit cards in database and add tests in
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 16 Sep 2016 06:23:08 +0000 (16:23 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 27 Sep 2016 07:49:05 +0000 (17:49 +1000)
CRM/Admin/Form/PaymentProcessor.php
CRM/Financial/BAO/PaymentProcessor.php
tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php [new file with mode: 0644]

index 7242cde62529dd1bdfb5a4c40568d405f5555fd5..a3bc30ae4fda22f9e6570309844a125eb45f9b04 100644 (file)
@@ -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,
index f5cb848d13971c0026ba30d420f6b6faffad8728..47ec34404babe4c6d38a2a8390703317715064b6 100644 (file)
@@ -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 (file)
index 0000000..761872d
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Financial_BAO_PaymentProcessorTypeTest
+ * @group headless
+ */
+class CRM_Financial_BAO_PaymentProcessorTypeTest extends CiviUnitTestCase {
+  public function setUp() {
+    parent::setUp();
+  }
+
+  /**
+   * Check method create()
+   */
+  public function testGetCreditCards() {
+    $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);
+    $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');
+  }
+
+}