Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip]CRM-20158, added function to retrieve payment processor id #9879

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions CRM/Core/BAO/FinancialTrxn.php
Expand Up @@ -33,6 +33,14 @@
*
*/
class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {

/**
* Static field to hold financial trxn data.
*
* @var array
*/
static $_financialTrxnData = array();

/**
* Class constructor.
*
Expand Down Expand Up @@ -696,4 +704,36 @@ public static function createDeferredTrxn($lineItems, $contributionDetails, $upd
}
}

/**
* Check if financial trxn is via payment processor.
*
* @param int $contributionID
* Contribution ID
*
* @param string $returnField
*
* @return int
*/
public static function hasPaymentProcessorTrxn($contributionID, $returnField = 'payment_processor_id') {
if (empty(self::$_financialTrxnData[$contributionID])) {
$sql = "SELECT payment_processor_id, financial_trxn_id FROM civicrm_financial_trxn cft
INNER JOIN civicrm_entity_financial_trxn ceft ON ceft.financial_trxn_id = cft.id
WHERE ceft.entity_table = 'civicrm_contribution'
AND ceft.entity_id = %1
AND cft.is_payment = 1 ORDER BY cft.id DESC LIMIT 1";
$mysqlParams = array(1 => array($contributionID, 'Integer'));
$result = CRM_Core_DAO::executeQuery($sql, $mysqlParams);
if ($result->fetch()) {
self::$_financialTrxnData[$contributionID] = array(
'payment_processor_id' => $result->payment_processor_id,
'financial_trxn_id' => $result->financial_trxn_id,
);
}
else {
return NULL;
}
}
return self::$_financialTrxnData[$contributionID][$returnField];
}

}
21 changes: 21 additions & 0 deletions tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php
Expand Up @@ -171,4 +171,25 @@ public function testCreateDeferredTrxn() {
$this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
}

/**
* Test for hasPaymentProcessorTrxn().
*/
public function testHasPaymentProcessorTrxn() {
$cid = $this->individualCreate();
$paymentProcessorID = $this->processorCreate();
$params = array(
'contact_id' => $cid,
'receive_date' => '2016-01-20',
'total_amount' => 100,
'financial_type_id' => 1,
);
$contribution = CRM_Contribute_BAO_Contribution::create($params);
$paymentProcessorId = CRM_Core_BAO_FinancialTrxn::hasPaymentProcessorTrxn($contribution->id);
$this->assertEquals($paymentProcessorId, NULL);
$params['payment_processor'] = $paymentProcessorID;
$contribution = CRM_Contribute_BAO_Contribution::create($params);
$paymentProcessorId = CRM_Core_BAO_FinancialTrxn::hasPaymentProcessorTrxn($contribution->id);
$this->assertEquals($paymentProcessorId, $paymentProcessorID);
}

}