CRM-20158, added function to update credit card details like card type and pan trunc...
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 22 Feb 2017 23:31:05 +0000 (05:01 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Wed, 5 Apr 2017 09:44:07 +0000 (15:14 +0530)
----------------------------------------
* CRM-20158: Store card type and last 4 digits of credit card
  https://issues.civicrm.org/jira/browse/CRM-20158

CRM-20158, added test

----------------------------------------
* CRM-20158: Store card type and last 4 digits of credit card
  https://issues.civicrm.org/jira/browse/CRM-20158

CRM-20158, changed code to use api for update to financial trxn table and to retrieve data from financial trxn table

----------------------------------------
* CRM-20158: Store card type and last 4 digits of credit card
  https://issues.civicrm.org/jira/browse/CRM-20158

CRM-20158, placed function contribution BAO to update card details

----------------------------------------
* CRM-20158: Store card type and last 4 digits of credit card
  https://issues.civicrm.org/jira/browse/CRM-20158

CRM-20158, changed function defination and params and updated function calling

----------------------------------------
* CRM-20158: Store card type and last 4 digits of credit card
  https://issues.civicrm.org/jira/browse/CRM-20158

CRM/Contribute/BAO/Contribution.php
CRM/Core/BAO/FinancialTrxn.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php

index 39443bf8f45417a889d3717c5f1ac46ba56dfb99..a14b086cfa2cffccaba28b4397b2f977f59aa3d3 100644 (file)
@@ -3371,6 +3371,9 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
               civicrm_api3('FinancialTrxn', 'create', array('id' => $refundIDs['financialTrxnId'], 'trxn_id' => $params['refund_trxn_id']));
             }
           }
+          $cardType = CRM_Utils_Array::value('card_type', $params);
+          $panTruncation = CRM_Utils_Array::value('pan_truncation', $params);
+          CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($params['contribution']->id, $panTruncation, $cardType);
         }
       }
 
index 1f32835ab20c2450f60f0193cce56115a09eaf7b..3013845efe3f4b3b513bce74d456d6b1d0e6a2bd 100644 (file)
@@ -696,4 +696,43 @@ WHERE ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_ac
     }
   }
 
+  /**
+   * Update Credit Card Details in civicrm_financial_trxn table.
+   *
+   * @param int $contributionID
+   * @param int $panTruncation
+   * @param int $cardType
+   *
+   */
+  public static function updateCreditCardDetails($contributionID, $panTruncation, $cardType) {
+    $financialTrxn = civicrm_api3('EntityFinancialTrxn', 'get', array(
+      'return' => array('financial_trxn_id.payment_processor_id', 'financial_trxn_id'),
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $contributionID,
+      'financial_trxn_id.is_payment' => TRUE,
+      'options' => array('sort' => 'financial_trxn_id DESC', 'limit' => 1),
+    ));
+
+    if (!$financialTrxn['count']) {
+      return NULL;
+    }
+
+    $financialTrxn = $financialTrxn['values'][$financialTrxn['id']];
+    $paymentProcessorID = CRM_Utils_Array::value('financial_trxn_id.payment_processor_id', $financialTrxn);
+
+    if ($paymentProcessorID) {
+      return NULL;
+    }
+
+    $financialTrxnId = $financialTrxn['financial_trxn_id'];
+    $trxnparams = array('id' => $financialTrxnId);
+    if (isset($cardType)) {
+      $trxnparams['card_type'] = $cardType;
+    }
+    if (isset($panTruncation)) {
+      $trxnparams['pan_truncation'] = $panTruncation;
+    }
+    civicrm_api3('FinancialTrxn', 'create', $trxnparams);
+  }
+
 }
index da4596b7dc605e5304ed3625bb84c25d4372d6fb..5fdf40cebc9eec5047369d57779fd73fdcca9ded 100644 (file)
@@ -171,4 +171,77 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
     $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
   }
 
+  /**
+   * Test for updateCreditCardDetails().
+   */
+  public function testUpdateCreditCardDetailsUsingContributionAPI() {
+    $cid = $this->individualCreate();
+    $params = array(
+      'contact_id' => $cid,
+      'receive_date' => '2016-01-20',
+      'total_amount' => 100,
+      'financial_type_id' => 1,
+    );
+    $contribution = CRM_Contribute_BAO_Contribution::create($params);
+    $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
+    $financialTrxn = $this->callAPISuccessGetSingle(
+      'FinancialTrxn',
+      array(
+        'id' => $lastFinancialTrxnId['financialTrxnId'],
+        'return' => array('card_type', 'pan_truncation'),
+      )
+    );
+    $this->assertEquals(CRM_Utils_Array::value('card_type', $financialTrxn), NULL);
+    $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
+    $params = array(
+      'card_type' => 2,
+      'pan_truncation' => 4567,
+      'id' => $contribution->id,
+    );
+    $this->callAPISuccess("Contribution", "create", $params);
+    $financialTrxn = $this->callAPISuccessGetSingle(
+      'FinancialTrxn',
+      array(
+        'id' => $lastFinancialTrxnId['financialTrxnId'],
+        'return' => array('card_type', 'pan_truncation'),
+      )
+    );
+    $this->assertEquals($financialTrxn['card_type'], 2);
+    $this->assertEquals($financialTrxn['pan_truncation'], 4567);
+  }
+
+  /**
+   * Test for updateCreditCardDetails().
+   */
+  public function testUpdateCreditCardDetails() {
+    $cid = $this->individualCreate();
+    $params = array(
+      'contact_id' => $cid,
+      'receive_date' => '2016-01-20',
+      'total_amount' => 100,
+      'financial_type_id' => 1,
+    );
+    $contribution = CRM_Contribute_BAO_Contribution::create($params);
+    $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
+    $financialTrxn = $this->callAPISuccessGetSingle(
+      'FinancialTrxn',
+      array(
+        'id' => $lastFinancialTrxnId['financialTrxnId'],
+        'return' => array('card_type', 'pan_truncation'),
+      )
+    );
+    $this->assertEquals(CRM_Utils_Array::value('card_type', $financialTrxn), NULL);
+    $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
+    CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution->id, 4567, 2);
+    $financialTrxn = $this->callAPISuccessGetSingle(
+      'FinancialTrxn',
+      array(
+        'id' => $lastFinancialTrxnId['financialTrxnId'],
+        'return' => array('card_type', 'pan_truncation'),
+      )
+    );
+    $this->assertEquals($financialTrxn['card_type'], 2);
+    $this->assertEquals($financialTrxn['pan_truncation'], 4567);
+  }
+
 }