From: Pradeep Nayak Date: Wed, 22 Feb 2017 23:31:05 +0000 (+0530) Subject: CRM-20158, added function to update credit card details like card type and pan trunc... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2c4a6dc814a064249206fe7a591a8fa6d17ae16e;p=civicrm-core.git CRM-20158, added function to update credit card details like card type and pan truncation for contribution ---------------------------------------- * 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 --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 39443bf8f4..a14b086cfa 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -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); } } diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 1f32835ab2..3013845efe 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -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); + } + } diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index da4596b7dc..5fdf40cebc 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -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); + } + }