3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 use Civi\Api4\FinancialItem
;
19 use Civi\Api4\LineItem
;
22 * This class contains payment related functions.
24 class CRM_Financial_BAO_Payment
{
27 * Function to process additional payment for partial and refund
30 * This function is called via API payment.create function. All forms that
31 * add payments should use this.
33 * @param array $params
38 * @return \CRM_Financial_DAO_FinancialTrxn
40 * @throws \CRM_Core_Exception
41 * @throws \CiviCRM_API3_Exception
42 * @throws \API_Exception
44 public static function create(array $params): CRM_Financial_DAO_FinancialTrxn
{
45 $contribution = civicrm_api3('Contribution', 'getsingle', ['id' => $params['contribution_id']]);
46 $contributionStatus = CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution['contribution_status_id']);
47 $isPaymentCompletesContribution = self
::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount'], $contributionStatus);
48 $lineItems = self
::getPayableLineItems($params);
50 $whiteList = ['check_number', 'payment_processor_id', 'fee_amount', 'total_amount', 'contribution_id', 'net_amount', 'card_type_id', 'pan_truncation', 'trxn_result_code', 'payment_instrument_id', 'trxn_id', 'trxn_date', 'order_reference'];
51 $paymentTrxnParams = array_intersect_key($params, array_fill_keys($whiteList, 1));
52 $paymentTrxnParams['is_payment'] = 1;
53 // Really we should have a DB default.
54 $paymentTrxnParams['fee_amount'] = $paymentTrxnParams['fee_amount'] ??
0;
56 if (isset($paymentTrxnParams['payment_processor_id']) && empty($paymentTrxnParams['payment_processor_id'])) {
57 // Don't pass 0 - ie the Pay Later processor as it is a pseudo-processor.
58 unset($paymentTrxnParams['payment_processor_id']);
60 if (empty($paymentTrxnParams['payment_instrument_id'])) {
61 if (!empty($params['payment_processor_id'])) {
62 $paymentTrxnParams['payment_instrument_id'] = civicrm_api3('PaymentProcessor', 'getvalue', ['return' => 'payment_instrument_id', 'id' => $paymentTrxnParams['payment_processor_id']]);
65 // Fall back on the payment instrument already used - should we deprecate this?
66 $paymentTrxnParams['payment_instrument_id'] = $contribution['payment_instrument_id'];
70 $paymentTrxnParams['currency'] = $contribution['currency'];
72 $accountsReceivableAccount = CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship($contribution['financial_type_id'], 'Accounts Receivable Account is');
73 $paymentTrxnParams['to_financial_account_id'] = CRM_Contribute_BAO_Contribution
::getToFinancialAccount($contribution, $params);
74 $paymentTrxnParams['from_financial_account_id'] = $accountsReceivableAccount;
76 if ($params['total_amount'] > 0) {
77 $paymentTrxnParams['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Completed');
79 elseif ($params['total_amount'] < 0) {
80 $paymentTrxnParams['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
83 //If Payment is recorded on Failed contribution, update it to Pending.
84 if ($contributionStatus === 'Failed' && $params['total_amount'] > 0) {
85 //Enter a financial trxn to record a payment in receivable account
86 //as failed transaction does not insert any trxn values. Hence, if Payment is
87 //recorded on a failed contribution, the transition happens from Failed -> Pending -> Completed.
88 $ftParams = array_merge($paymentTrxnParams, [
89 'from_financial_account_id' => NULL,
90 'to_financial_account_id' => $accountsReceivableAccount,
92 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
94 CRM_Core_BAO_FinancialTrxn
::create($ftParams);
95 $contributionStatus = 'Pending';
96 self
::updateContributionStatus($contribution['id'], $contributionStatus);
98 $trxn = CRM_Core_BAO_FinancialTrxn
::create($paymentTrxnParams);
100 if ($params['total_amount'] < 0 && !empty($params['cancelled_payment_id'])) {
101 self
::reverseAllocationsFromPreviousPayment($params, $trxn->id
);
104 $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution
::getSalesTaxFinancialAccounts();
105 $financialItems = LineItem
::get(FALSE)
106 ->addSelect('tax_amount', 'price_field_value_id', 'financial_item.id', 'financial_item.status_id:name', 'financial_item.financial_account_id')
108 'FinancialItem AS financial_item',
111 ['financial_item.entity_table', '=', '"civicrm_line_item"'],
112 ['financial_item.entity_id', '=', 'id']
114 ->addOrderBy('financial_item.id', 'DESC')
115 ->addWhere('contribution_id', '=', (int) $params['contribution_id'])->execute();
117 foreach ($lineItems as $key => $value) {
118 if ($value['allocation'] === (float) 0) {
121 $financialItemID = NULL;
122 $currentFinancialItemStatus = NULL;
123 foreach ($financialItems as $item) {
124 // We check against price_field_value_id rather than line item
125 // id because that is what the code did previously - but it's
126 // unclear whether this is for good reason or bad coding.
127 if ($item['price_field_value_id'] === (int) $value['price_field_value_id']
128 && !in_array($item['financial_item.financial_account_id'], $salesTaxFinancialAccount, TRUE)
130 $financialItemID = $item['financial_item.id'];
131 $currentFinancialItemStatus = $item['financial_item.status_id:name'];
134 if (!$financialItemID) {
135 $financialItemID = self
::getNewFinancialItemID($value, $params['trxn_date'], $contribution['contact_id'], $paymentTrxnParams['currency']);
139 'entity_table' => 'civicrm_financial_item',
140 'financial_trxn_id' => $trxn->id
,
141 'entity_id' => $financialItemID,
142 'amount' => $value['allocation'],
145 civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
146 if ($currentFinancialItemStatus && 'Paid' !== $currentFinancialItemStatus) {
147 $newStatus = $value['allocation'] < $value['balance'] ?
'Partially paid' : 'Paid';
148 FinancialItem
::update(FALSE)
149 ->addValue('status_id:name', $newStatus)
150 ->addWhere('id', '=', $financialItemID)
154 foreach ($financialItems as $item) {
155 if ($item['price_field_value_id'] === (int) $value['price_field_value_id']
156 && in_array($item['financial_item.financial_account_id'], $salesTaxFinancialAccount, TRUE)
158 // @todo - this is expected to be broken - it should be fixed to
159 // a) have the getPayableLineItems add the amount to allocate for tax
160 // b) call EntityFinancialTrxn directly - per above.
161 // - see https://github.com/civicrm/civicrm-core/pull/14763
163 'contribution_total_amount' => $contribution['total_amount'],
164 'trxn_total_amount' => $params['total_amount'],
165 'trxn_id' => $trxn->id
,
166 'line_item_amount' => $item['tax_amount'],
168 $eftParams['entity_id'] = $item['financial_item.id'];
169 CRM_Contribute_BAO_Contribution
::createProportionalEntry($entityParams, $eftParams);
175 if ($isPaymentCompletesContribution) {
176 if ($contributionStatus === 'Pending refund') {
177 // Ideally we could still call completetransaction as non-payment related actions should
178 // be outside this class. However, for now we just update the contribution here.
179 // Unit test cover in CRM_Event_BAO_AdditionalPaymentTest::testTransactionInfo.
180 civicrm_api3('Contribution', 'create',
182 'id' => $contribution['id'],
183 'contribution_status_id' => 'Completed',
188 civicrm_api3('Contribution', 'completetransaction', [
189 'id' => $contribution['id'],
190 'is_post_payment_create' => TRUE,
191 'is_email_receipt' => $params['is_send_contribution_notification'],
192 'trxn_date' => $params['trxn_date'],
193 'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'],
194 'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ??
NULL,
197 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
198 $ftParams = ['id' => $trxnId['financialTrxnId']];
199 $trxn = CRM_Core_BAO_FinancialTrxn
::retrieve($ftParams);
202 elseif ($contributionStatus === 'Pending' && $params['total_amount'] > 0) {
203 self
::updateContributionStatus($contribution['id'], 'Partially Paid');
204 $participantPayments = civicrm_api3('ParticipantPayment', 'get', [
205 'contribution_id' => $contribution['id'],
206 'participant_id.status_id' => ['IN' => ['Pending from pay later', 'Pending from incomplete transaction']],
208 foreach ($participantPayments as $participantPayment) {
209 civicrm_api3('Participant', 'create', ['id' => $participantPayment['participant_id'], 'status_id' => 'Partially paid']);
212 elseif ($contributionStatus === 'Completed' && ((float) CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id'], TRUE) === 0.0)) {
213 // If the contribution has previously been completed (fully paid) and now has total payments adding up to 0
214 // change status to refunded.
215 self
::updateContributionStatus($contribution['id'], 'Refunded');
217 self
::updateRelatedContribution($params, $params['contribution_id']);
218 CRM_Contribute_BAO_Contribution
::recordPaymentActivity($params['contribution_id'], CRM_Utils_Array
::value('participant_id', $params), $params['total_amount'], $trxn->currency
, $trxn->trxn_date
);
223 * Function to update contribution's check_number and trxn_id by
224 * concatenating values from financial trxn's check_number and trxn_id
227 * @param array $params
228 * @param int $contributionID
230 * @throws \CiviCRM_API3_Exception
232 public static function updateRelatedContribution(array $params, int $contributionID): void
{
233 $contributionDAO = new CRM_Contribute_DAO_Contribution();
234 $contributionDAO->id
= $contributionID;
235 $contributionDAO->find(TRUE);
236 if (isset($params['fee_amount'])) {
237 // Update contribution.fee_amount to be be the total of all fees
238 // since the payment is already saved the total here will be right.
239 $payments = civicrm_api3('Payment', 'get', [
240 'contribution_id' => $contributionID,
241 'return' => 'fee_amount',
244 foreach ($payments as $payment) {
245 $totalFees +
= $payment['fee_amount'] ??
0;
247 $contributionDAO->fee_amount
= $totalFees;
248 $contributionDAO->net_amount
= $contributionDAO->total_amount
- $contributionDAO->fee_amount
;
251 foreach (['trxn_id', 'check_number'] as $fieldName) {
252 if (!empty($params[$fieldName])) {
254 if (!empty($contributionDAO->$fieldName)) {
255 $values = explode(',', $contributionDAO->$fieldName);
257 // if submitted check_number or trxn_id value is
258 // already present then ignore else add to $values array
259 if (!in_array($params[$fieldName], $values)) {
260 $values[] = $params[$fieldName];
262 $contributionDAO->$fieldName = implode(',', $values);
266 $contributionDAO->save();
270 * Send an email confirming a payment that has been received.
272 * @param array $params
276 * @throws \CiviCRM_API3_Exception
278 public static function sendConfirmation($params) {
280 $entities = self
::loadRelatedEntities($params['id']);
282 if (!empty($entities['event'])) {
283 CRM_Event_BAO_Event
::setOutputTimeZone($entities['event']);
286 $sendTemplateParams = [
287 'groupName' => 'msg_tpl_workflow_contribution',
288 'valueName' => 'payment_or_refund_notification',
289 'PDFFilename' => ts('notification') . '.pdf',
290 'contactId' => $entities['contact']['id'],
291 'toName' => $entities['contact']['display_name'],
292 'toEmail' => $entities['contact']['email'],
293 'tplParams' => self
::getConfirmationTemplateParameters($entities),
295 if (!empty($params['from']) && !empty($params['check_permissions'])) {
296 // Filter from against permitted emails.
297 $validEmails = self
::getValidFromEmailsForPayment($entities['event']['id'] ??
NULL);
298 if (!isset($validEmails[$params['from']])) {
299 // Ignore unpermitted parameter.
300 unset($params['from']);
303 $sendTemplateParams['from'] = $params['from'] ??
key(CRM_Core_BAO_Email
::domainEmails());
304 return CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
308 * Get valid from emails for payment.
310 * @param int $eventID
314 public static function getValidFromEmailsForPayment($eventID = NULL) {
316 $emails = CRM_Event_BAO_Event
::getFromEmailIds($eventID);
319 $emails['from_email_id'] = CRM_Core_BAO_Email
::getFromEmail();
321 return $emails['from_email_id'];
325 * Load entities related to the current payment id.
327 * This gives us all the data we need to send an email confirmation but avoiding
328 * getting anything not tested for the confirmations. We retrieve the 'full' event as
329 * it has been traditionally assigned in full.
334 * - contact = ['id' => x, 'display_name' => y, 'email' => z]
335 * - event = [.... full event details......]
336 * - contribution = ['id' => x],
337 * - payment = [payment info + payment summary info]
338 * @throws \CiviCRM_API3_Exception
340 protected static function loadRelatedEntities($id) {
342 $contributionID = (int) civicrm_api3('EntityFinancialTrxn', 'getvalue', [
343 'financial_trxn_id' => $id,
344 'entity_table' => 'civicrm_contribution',
345 'return' => 'entity_id',
347 $entities['contribution'] = ['id' => $contributionID];
348 $entities['payment'] = array_merge(civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $id]),
349 CRM_Contribute_BAO_Contribution
::getPaymentInfo($contributionID)
352 $contactID = self
::getPaymentContactID($contributionID);
353 [$displayName, $email] = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
354 $entities['contact'] = ['id' => $contactID, 'display_name' => $displayName, 'email' => $email];
355 $contact = civicrm_api3('Contact', 'getsingle', ['id' => $contactID, 'return' => 'email_greeting']);
356 $entities['contact']['email_greeting'] = $contact['email_greeting_display'];
358 $participantRecords = civicrm_api3('ParticipantPayment', 'get', [
359 'contribution_id' => $contributionID,
360 'api.Participant.get' => ['return' => 'event_id'],
363 if (!empty($participantRecords)) {
364 $entities['event'] = civicrm_api3('Event', 'getsingle', ['id' => $participantRecords[0]['api.Participant.get']['values'][0]['event_id']]);
365 if (!empty($entities['event']['is_show_location'])) {
367 'entity_id' => $entities['event']['id'],
368 'entity_table' => 'civicrm_event',
370 $entities['location'] = CRM_Core_BAO_Location
::getValues($locationParams, TRUE);
378 * @param int $contributionID
381 * @throws \CiviCRM_API3_Exception
382 * @throws \CiviCRM_API3_Exception
384 public static function getPaymentContactID($contributionID) {
385 $contribution = civicrm_api3('Contribution', 'getsingle', [
386 'id' => $contributionID ,
387 'return' => ['contact_id'],
389 return (int) $contribution['contact_id'];
393 * @param array $entities
394 * Related entities as an array keyed by the various entities.
397 * Values required for the notification
399 * - template_variables
400 * - event (DAO of event if relevant)
402 public static function getConfirmationTemplateParameters($entities) {
403 $templateVariables = [
404 'contactDisplayName' => $entities['contact']['display_name'],
405 'emailGreeting' => $entities['contact']['email_greeting'],
406 'totalAmount' => $entities['payment']['total'],
407 'amountOwed' => $entities['payment']['balance'],
408 'totalPaid' => $entities['payment']['paid'],
409 'paymentAmount' => $entities['payment']['total_amount'],
410 'checkNumber' => $entities['payment']['check_number'] ??
NULL,
411 'receive_date' => $entities['payment']['trxn_date'],
412 'paidBy' => CRM_Core_PseudoConstant
::getLabel('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $entities['payment']['payment_instrument_id']),
413 'isShowLocation' => (!empty($entities['event']) ?
$entities['event']['is_show_location'] : FALSE),
414 'location' => $entities['location'] ??
NULL,
415 'event' => $entities['event'] ??
NULL,
416 'component' => (!empty($entities['event']) ?
'event' : 'contribution'),
417 'isRefund' => $entities['payment']['total_amount'] < 0,
418 'isAmountzero' => $entities['payment']['total_amount'] === 0,
419 'refundAmount' => ($entities['payment']['total_amount'] < 0 ?
$entities['payment']['total_amount'] : NULL),
420 'paymentsComplete' => ($entities['payment']['balance'] == 0),
423 return self
::filterUntestedTemplateVariables($templateVariables);
427 * Filter out any untested variables.
429 * This just serves to highlight if any variables are added without a unit test also being added.
431 * (if hit then add a unit test for the param & add to this array).
433 * @param array $params
437 public static function filterUntestedTemplateVariables($params) {
438 $testedTemplateVariables = [
439 'contactDisplayName',
457 // These are assigned by the payment form - they still 'get through' from the
458 // form for now without being in here but we should ideally load
459 // and assign. Note we should update the tpl to use {if $billingName}
460 // and ditch contributeMode - although it might need to be deprecated rather than removed.
466 'credit_card_number',
467 'credit_card_exp_date',
469 $filteredParams = [];
470 foreach ($testedTemplateVariables as $templateVariable) {
471 // This will cause an a-notice if any are NOT set - by design. Ensuring
472 // they are set prevents leakage.
473 $filteredParams[$templateVariable] = $params[$templateVariable];
475 return $filteredParams;
479 * Does this payment complete the contribution.
481 * @param int $contributionID
482 * @param float $paymentAmount
483 * @param string $previousStatus
487 protected static function isPaymentCompletesContribution($contributionID, $paymentAmount, $previousStatus) {
488 if ($previousStatus === 'Completed') {
491 $outstandingBalance = CRM_Contribute_BAO_Contribution
::getContributionBalance($contributionID);
492 $cmp = bccomp($paymentAmount, $outstandingBalance, 5);
493 return ($cmp == 0 ||
$cmp == 1);
497 * Update the status of the contribution.
499 * We pass the is_post_payment_create as we have already created the line items
501 * @param int $contributionID
502 * @param string $status
504 * @throws \CiviCRM_API3_Exception
506 private static function updateContributionStatus(int $contributionID, string $status) {
507 civicrm_api3('Contribution', 'create',
509 'id' => $contributionID,
510 'is_post_payment_create' => TRUE,
511 'contribution_status_id' => $status,
517 * Get the line items for the contribution.
519 * Retrieve the line items and wrangle the following
521 * - get the outstanding balance on a line item basis.
522 * - determine what amount is being paid on this line item - we get the total being paid
523 * for the whole contribution and determine the ratio of the balance that is being paid
524 * and then assign apply that ratio to each line item.
525 * - if overrides have been passed in we use those amounts instead.
530 * @throws \CiviCRM_API3_Exception
532 protected static function getPayableLineItems($params): array {
533 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($params['contribution_id']);
534 $lineItemOverrides = [];
535 if (!empty($params['line_item'])) {
536 // The format is a bit weird here - $params['line_item'] => [[1 => 10], [2 => 40]]
537 // Squash to [1 => 10, 2 => 40]
538 foreach ($params['line_item'] as $lineItem) {
539 $lineItemOverrides +
= $lineItem;
542 $outstandingBalance = CRM_Contribute_BAO_Contribution
::getContributionBalance($params['contribution_id']);
543 if ($outstandingBalance !== 0.0) {
544 $ratio = $params['total_amount'] / $outstandingBalance;
546 elseif ($params['total_amount'] < 0) {
547 $ratio = $params['total_amount'] / (float) CRM_Core_BAO_FinancialTrxn
::getTotalPayments($params['contribution_id'], TRUE);
550 // Help we are making a payment but no money is owed. We won't allocate the overpayment to any line item.
553 foreach ($lineItems as $lineItemID => $lineItem) {
554 // Ideally id would be set deeper but for now just add in here.
555 $lineItems[$lineItemID]['id'] = $lineItemID;
556 $lineItems[$lineItemID]['paid'] = self
::getAmountOfLineItemPaid($lineItemID);
557 $lineItems[$lineItemID]['balance'] = $lineItem['subTotal'] - $lineItems[$lineItemID]['paid'];
558 if (!empty($lineItemOverrides)) {
559 $lineItems[$lineItemID]['allocation'] = $lineItemOverrides[$lineItemID] ??
NULL;
562 if (empty($lineItems[$lineItemID]['balance']) && !empty($ratio) && $params['total_amount'] < 0) {
563 $lineItems[$lineItemID]['allocation'] = $lineItem['subTotal'] * $ratio;
566 $lineItems[$lineItemID]['allocation'] = $lineItems[$lineItemID]['balance'] * $ratio;
574 * Get the amount paid so far against this line item.
576 * @param int $lineItemID
580 * @throws \CiviCRM_API3_Exception
582 protected static function getAmountOfLineItemPaid($lineItemID) {
584 $financialItems = civicrm_api3('FinancialItem', 'get', [
585 'entity_id' => $lineItemID,
586 'entity_table' => 'civicrm_line_item',
587 'options' => ['sort' => 'id DESC', 'limit' => 0],
589 if (!empty($financialItems)) {
590 $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [
591 'entity_table' => 'civicrm_financial_item',
592 'entity_id' => ['IN' => array_keys($financialItems)],
593 'options' => ['limit' => 0],
594 'financial_trxn_id.is_payment' => 1,
596 foreach ($entityFinancialTrxns as $entityFinancialTrxn) {
597 $paid +
= $entityFinancialTrxn['amount'];
600 return (float) $paid;
604 * Reverse the entity financial transactions associated with the cancelled payment.
606 * The reversals are linked to the new payemnt.
608 * @param array $params
611 * @throws \CiviCRM_API3_Exception
613 protected static function reverseAllocationsFromPreviousPayment($params, $trxnID) {
614 // Do a direct reversal of any entity_financial_trxn records being cancelled.
615 $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [
616 'entity_table' => 'civicrm_financial_item',
617 'options' => ['limit' => 0],
618 'financial_trxn_id.id' => $params['cancelled_payment_id'],
620 foreach ($entityFinancialTrxns as $entityFinancialTrxn) {
621 civicrm_api3('EntityFinancialTrxn', 'create', [
622 'entity_table' => 'civicrm_financial_item',
623 'entity_id' => $entityFinancialTrxn['entity_id'],
624 'amount' => -$entityFinancialTrxn['amount'],
625 'financial_trxn_id' => $trxnID,
631 * Create a financial items & return the ID.
633 * Ideally this will never be called.
635 * However, I hit a scenario in testing where 'something' had created a pending payment with
636 * no financial items and that would result in a fatal error without handling here. I failed
637 * to replicate & am not investigating via a new test methodology
638 * https://github.com/civicrm/civicrm-core/pull/15706
640 * After this is in I will do more digging & once I feel confident new instances are not being
641 * created I will add deprecation notices into this function with a view to removing.
643 * However, I think we want to add it in 5.20 as there is a risk of users experiencing an error
644 * if there is incorrect data & we need time to ensure that what I hit was not a 'thing.
645 * (it might be the demo site data is a bit flawed & that was the issue).
647 * @param array $lineItem
648 * @param string $trxn_date
649 * @param int $contactID
650 * @param string $currency
654 * @throws \CiviCRM_API3_Exception
656 protected static function getNewFinancialItemID($lineItem, $trxn_date, $contactID, $currency): int {
657 $financialAccount = CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship(
658 $lineItem['financial_type_id'],
662 'transaction_date' => $trxn_date,
663 'contact_id' => $contactID,
664 'currency' => $currency,
665 'amount' => $lineItem['line_total'],
666 'description' => $lineItem['label'],
667 'status_id' => 'Unpaid',
668 'financial_account_id' => $financialAccount,
669 'entity_table' => 'civicrm_line_item',
670 'entity_id' => $lineItem['id'],
672 return (int) civicrm_api3('FinancialItem', 'create', $itemParams)['id'];