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 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_contribute_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
19 class api_v3_PaymentTest
extends CiviUnitTestCase
{
21 protected $_individualId;
23 protected $_financialTypeId = 1;
26 * Should financials be checked after the test but before tear down.
28 * Ideally all tests (or at least all that call any financial api calls ) should do this but there
29 * are some test data issues and some real bugs currently blocking.
33 protected $isValidateFinancialsOnPostAssert = TRUE;
38 * @throws \CiviCRM_API3_Exception
40 public function setUp(): void
{
43 $this->_apiversion
= 3;
44 $this->_individualId
= $this->individualCreate();
45 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= [];
49 * Clean up after each test.
53 public function tearDown(): void
{
54 $this->quickCleanUpFinancialEntities();
55 $this->quickCleanup(['civicrm_uf_match']);
56 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
61 * Test Get Payment api.
63 * @throws \CRM_Core_Exception
65 public function testGetPayment(): void
{
67 'contact_id' => $this->_individualId
,
68 'receive_date' => '2010-01-20',
69 'total_amount' => 100.00,
70 'financial_type_id' => $this->_financialTypeId
,
72 'contribution_status_id' => 1,
74 $contribution = $this->callAPISuccess('contribution', 'create', $p);
77 'contribution_id' => $contribution['id'],
78 'check_permissions' => TRUE,
80 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM'];
81 $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute');
83 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
[] = 'access CiviContribute';
84 $this->callAPISuccess('payment', 'get', $params);
86 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
87 $this->assertEquals(1, $payment['count']);
90 $contribution['id'] => [
91 'total_amount' => 100,
93 'trxn_date' => '2010-01-20 00:00:00',
94 'contribution_id' => $contribution['id'],
98 $this->checkPaymentResult($payment, $expectedResult);
99 $this->callAPISuccess('Contribution', 'Delete', [
100 'id' => $contribution['id'],
102 $this->validateAllPayments();
106 * Test multiple payments for contribution and assert if option
107 * and is_payment returns the correct list of payments.
109 * @throws \CRM_Core_Exception
111 public function testMultiplePaymentsForContribution(): void
{
113 'contact_id' => $this->_individualId
,
114 'total_amount' => 100,
115 'contribution_status_id' => 'Pending',
117 $contributionID = $this->contributionCreate($params);
119 'contribution_id' => $contributionID,
120 'total_amount' => 20,
121 'trxn_date' => date('Y-m-d'),
123 $this->callAPISuccess('payment', 'create', $paymentParams);
124 $paymentParams['total_amount'] = 30;
125 $this->callAPISuccess('payment', 'create', $paymentParams);
127 $paymentParams['total_amount'] = 50;
128 $this->callAPISuccess('payment', 'create', $paymentParams);
130 //check if contribution status is set to "Completed".
131 $contribution = $this->callAPISuccessGetSingle('Contribution', [
132 'id' => $contributionID,
134 $this->assertEquals('Completed', $contribution['contribution_status']);
136 //Get Payment using options
139 'contribution_id' => $contributionID,
141 'options' => ['limit' => 0, 'sort' => 'total_amount DESC'],
143 $payments = $this->callAPISuccess('Payment', 'get', $getParams);
144 $this->assertEquals(3, $payments['count']);
145 foreach ([50, 30, 20] as $key => $total_amount) {
146 $this->assertEquals($total_amount, $payments['values'][$key]['total_amount']);
151 * Retrieve Payment using trxn_id.
153 * @throws \CRM_Core_Exception
154 * @throws \CiviCRM_API3_Exception
156 public function testGetPaymentWithTrxnID(): void
{
157 $individual2 = $this->individualCreate();
159 'contact_id' => $this->_individualId
,
161 'total_amount' => 10,
163 $contributionID1 = $this->contributionCreate($params1);
166 'contact_id' => $individual2,
168 'total_amount' => 20,
170 $contributionID2 = $this->contributionCreate($params2);
172 $paymentParams = ['trxn_id' => 111111];
173 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
176 'total_amount' => 10,
180 'contribution_id' => $contributionID1,
183 $this->checkPaymentResult($payment, $expectedResult);
185 $paymentParams = ['trxn_id' => 222222];
186 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
189 'total_amount' => 20,
193 'contribution_id' => $contributionID2,
196 $this->checkPaymentResult($payment, $expectedResult);
197 $this->callAPISuccess('Payment', 'create', ['total_amount' => '-20', 'contribution_id' => $contributionID2]);
198 $this->validateAllPayments();
202 * Test contribution receipts triggered by Payment.create with is_send_contribution_notification = TRUE.
204 * @throws \CRM_Core_Exception
206 public function testPaymentSendContributionReceipt(): void
{
207 $mut = new CiviMailUtils($this);
208 $contribution = $this->createPartiallyPaidParticipantOrder();
209 $event = $this->callAPISuccess('Event', 'get', []);
210 $this->addLocationToEvent($event['id']);
212 'contribution_id' => $contribution['id'],
213 'total_amount' => 150,
214 'check_number' => '345',
215 'trxn_date' => '2018-08-13 17:57:56',
216 'is_send_contribution_notification' => TRUE,
218 $this->callAPISuccess('Payment', 'create', $params);
219 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);
220 $this->assertNotEmpty($contribution['receipt_date']);
222 'Price Field - Price Field 1 1 $ 100.00 $ 100.00',
229 * Test full refund when no payment has actually been record.
231 * @throws \CRM_Core_Exception
233 public function testFullRefundWithPaymentAlreadyRefunded(): void
{
235 'contact_id' => $this->_individualId
,
237 'total_amount' => 10,
239 $contributionID1 = $this->contributionCreate($params1);
240 $paymentParams = ['contribution_id' => $contributionID1];
241 $this->callAPISuccess('Payment', 'create', ['total_amount' => '-10', 'contribution_id' => $contributionID1]);
242 $this->callAPISuccess('payment', 'get', $paymentParams);
243 $this->callAPISuccess('Payment', 'create', ['total_amount' => '-10', 'contribution_id' => $contributionID1]);
244 $this->callAPISuccess('payment', 'get', $paymentParams);
245 $this->validateAllPayments();
249 * @throws \CRM_Core_Exception
251 public function testNegativePaymentWithNegativeContribution(): void
{
253 'contact_id' => $this->_individualId
,
255 'total_amount' => -10,
257 $contributionID1 = $this->contributionCreate($params1);
258 $this->callAPISuccess('Payment', 'create', ['total_amount' => '-20', 'contribution_id' => $contributionID1]);
259 $paymentParams = ['contribution_id' => $contributionID1];
260 $this->callAPISuccess('payment', 'get', $paymentParams);
261 $this->validateAllPayments();
265 * Test email receipt for partial payment.
267 * @throws \CRM_Core_Exception
269 public function testPaymentEmailReceipt(): void
{
270 $mut = new CiviMailUtils($this);
271 $contribution = $this->createPartiallyPaidParticipantOrder();
272 $event = $this->callAPISuccess('Event', 'get', []);
273 $this->addLocationToEvent($event['id']);
275 'contribution_id' => $contribution['id'],
276 'total_amount' => 50,
277 'check_number' => '345',
278 'trxn_date' => '2018-08-13 17:57:56',
280 $payment = $this->callAPISuccess('payment', 'create', $params);
281 $this->checkPaymentResult($payment, [
283 'from_financial_account_id' => 7,
284 'to_financial_account_id' => 6,
285 'total_amount' => 50,
291 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
292 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II']);
294 'From: "FIXME" <info@EXAMPLE.ORG>',
296 'Total Fee: $ 300.00',
297 'This Payment Amount: $ 50.00',
298 //150 was paid in the 1st payment.
299 'Balance Owed: $ 100.00',
300 'Event Information and Location',
303 'Transaction Date: August 13th, 2018 5:57 PM',
307 $this->validateAllPayments();
311 * Test email receipt for partial payment.
313 * @throws \CRM_Core_Exception
315 public function testPaymentEmailReceiptFullyPaid(): void
{
316 $mut = new CiviMailUtils($this);
317 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
318 $contribution = $this->createPartiallyPaidParticipantOrder();
321 'contribution_id' => $contribution['id'],
322 'total_amount' => 150,
324 $payment = $this->callAPISuccess('payment', 'create', $params);
326 // Here we set the email to an invalid email & use check_permissions, domain email should be used.
327 $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
328 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
329 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II', 'Registration Confirmation - Annual CiviCRM meet - Mr. Anthony Anderson II']);
331 'From: "FIXME" <info@EXAMPLE.ORG>',
333 'Below you will find a receipt for this payment.',
334 'Total Fee: $ 300.00',
335 'This Payment Amount: $ 150.00',
336 'Balance Owed: $ 0.00',
337 'Thank you for completing this payment.',
342 * Test email receipt for partial payment.
344 * @dataProvider getThousandSeparators
346 * @param string $thousandSeparator
348 * @throws \CRM_Core_Exception
350 public function testRefundEmailReceipt(string $thousandSeparator): void
{
351 $this->setCurrencySeparators($thousandSeparator);
352 $decimalSeparator = ($thousandSeparator === ',' ?
'.' : ',');
353 $mut = new CiviMailUtils($this);
354 $contribution = $this->createPartiallyPaidParticipantOrder();
355 $this->callAPISuccess('payment', 'create', [
356 'contribution_id' => $contribution['id'],
357 'total_amount' => 50,
358 'check_number' => '345',
359 'trxn_date' => '2018-08-13 17:57:56',
362 $payment = $this->callAPISuccess('payment', 'create', [
363 'contribution_id' => $contribution['id'],
364 'total_amount' => -30,
365 'trxn_date' => '2018-11-13 12:01:56',
366 'sequential' => TRUE,
370 'from_financial_account_id' => 7,
371 'to_financial_account_id' => 6,
372 'total_amount' => -30,
373 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
376 foreach ($expected as $key => $value) {
377 $this->assertEquals($value, $payment[$key], 'mismatch on key ' . $key);
380 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
381 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet - Mr. Anthony Anderson II']);
384 'A refund has been issued based on changes in your registration selections.',
385 'Total Fee: $ 300' . $decimalSeparator . '00',
386 'Refund Amount: $ -30' . $decimalSeparator . '00',
387 'Event Information and Location',
389 'Transaction Date: November 13th, 2018 12:01 PM',
390 'Total Paid: $ 170' . $decimalSeparator . '00',
395 * Test adding a payment to a pending multi-line order.
397 * @throws \CRM_Core_Exception
399 public function testCreatePaymentPendingOrderNoLineItems(): void
{
400 $order = $this->createPendingParticipantOrder();
401 $this->callAPISuccess('Payment', 'create', [
402 'order_id' => $order['id'],
403 'total_amount' => 50,
408 * Test that Payment.create does not fail if the line items are missing.
410 * In the original spec it was anticipated that financial items would not be created
411 * for pending contributions in some circumstances. We've backed away from this and
412 * I mostly could not find a way to do it through the UI. But I did seem to once &
413 * I want to be sure that if they ARE missing no fatal occurs so this tests
414 * that in an artificial way.
416 * @throws \CRM_Core_Exception
417 * @throws \CiviCRM_API3_Exception
419 public function testAddPaymentMissingFinancialItems(): void
{
420 $contribution = $this->callAPISuccess('Contribution', 'create', [
421 'total_amount' => 50,
422 'financial_type_id' => 'Donation',
423 'contact_id' => $this->individualCreate(),
424 'contribution_status_id' => 'Pending',
426 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_financial_item');
427 $this->callAPISuccess('Payment', 'create', ['contribution_id' => $contribution['id'], 'payment_instrument_id' => 'Check', 'total_amount' => 5]);
431 * Add participant with contribution
435 * @throws \CRM_Core_Exception
437 protected function createPendingParticipantOrder(): array {
438 return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams());
442 * Test create payment api with no line item in params
444 * @throws \CRM_Core_Exception
446 public function testCreatePaymentNoLineItems(): void
{
447 $contribution = $this->createPartiallyPaidParticipantOrder();
449 //Create partial payment
451 'contribution_id' => $contribution['id'],
452 'total_amount' => 50,
454 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
455 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
458 'entity_table' => 'civicrm_financial_item',
459 'financial_trxn_id' => $payment['id'],
461 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
462 $amounts = [33.33, 16.67];
463 foreach ($eft['values'] as $value) {
464 $this->assertEquals($value['amount'], array_pop($amounts));
467 // Now create payment to complete total amount of contribution
469 'contribution_id' => $contribution['id'],
470 'total_amount' => 100,
472 $payment = $this->callAPISuccess('payment', 'create', $params);
475 'from_financial_account_id' => 7,
476 'to_financial_account_id' => 6,
477 'total_amount' => 100,
482 $this->checkPaymentResult($payment, $expectedResult);
484 'entity_table' => 'civicrm_financial_item',
485 'financial_trxn_id' => $payment['id'],
487 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
488 $amounts = [66.67, 33.33];
489 foreach ($eft['values'] as $value) {
490 $this->assertEquals($value['amount'], array_pop($amounts));
492 // Check contribution for completed status
493 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
495 $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']);
496 $this->assertEquals(300.00, $contribution['values'][$contribution['id']]['total_amount']);
497 $paymentParticipant = [
498 'contribution_id' => $contribution['id'],
500 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
501 $this->callAPISuccessGetCount('Participant', ['status_id' => 'Registered'], 2);
505 * Function to assert db values
507 * @param array $payment
508 * @param array $expectedResult
510 * @throws \CRM_Core_Exception
512 public function checkPaymentResult(array $payment, array $expectedResult): void
{
513 $refreshedPayment = $this->callAPISuccessGetSingle('Payment', ['financial_trxn_id' => $payment['id']]);
514 foreach ($expectedResult[$payment['id']] as $key => $value) {
515 $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key); $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key);
520 * Test create payment api with line item in params
522 * @throws \CRM_Core_Exception
524 public function testCreatePaymentLineItems(): void
{
525 $contribution = $this->createPartiallyPaidParticipantOrder();
526 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
528 // Create partial payment by passing line item array is params.
530 'contribution_id' => $contribution['id'],
531 'total_amount' => 50,
534 foreach ($lineItems as $id => $ignore) {
535 $params['line_item'][] = [$id => array_pop($amounts)];
537 $payment = $this->callAPIAndDocument('Payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Payment with line item', 'CreatePaymentWithLineItems');
538 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
541 'entity_table' => 'civicrm_financial_item',
542 'financial_trxn_id' => $payment['id'],
543 'return' => ['entity_id.entity_id', 'amount'],
545 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
546 $this->assertCount(2, $eft);
548 foreach ($eft as $value) {
549 $this->assertEquals($value['amount'], array_pop($amounts));
552 // Now create payment to complete total amount of contribution
554 'contribution_id' => $contribution['id'],
555 'total_amount' => 100,
558 foreach ($lineItems as $id => $ignore) {
559 $params['line_item'][] = [$id => array_pop($amounts)];
561 $payment = $this->callAPISuccess('Payment', 'create', $params);
564 'from_financial_account_id' => 7,
565 'to_financial_account_id' => 6,
566 'total_amount' => 100,
571 $this->checkPaymentResult($payment, $expectedResult);
573 'entity_table' => 'civicrm_financial_item',
574 'financial_trxn_id' => $payment['id'],
576 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
577 $this->assertCount(2, $eft);
579 foreach ($eft as $value) {
580 $this->assertEquals($value['amount'], array_pop($amounts));
582 // Check contribution for completed status
583 $contribution = $this->callAPISuccess('Contribution', 'get', ['id' => $contribution['id']]);
585 $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']);
586 $this->assertEquals(300.00, $contribution['values'][$contribution['id']]['total_amount']);
587 $paymentParticipant = [
588 'contribution_id' => $contribution['id'],
590 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
591 $this->callAPISuccessGetCount('participant', ['status_id' => 'Registered'], 2);
595 * Test negative payment using create API.
597 * @throws \CRM_Core_Exception
599 public function testRefundPayment(): void
{
600 $result = $this->callAPISuccess('Contribution', 'create', [
601 'financial_type_id' => 'Donation',
602 'total_amount' => 100,
603 'contact_id' => $this->_individualId
,
605 $contributionID = $result['id'];
607 //Refund a part of the main amount.
608 $this->callAPISuccess('Payment', 'create', [
609 'contribution_id' => $contributionID,
610 'total_amount' => -10,
613 $contribution = $this->callAPISuccessGetSingle('Contribution', [
614 'return' => ['contribution_status_id'],
615 'id' => $contributionID,
617 //Still we've a status of Completed after refunding a partial amount.
618 $this->assertEquals('Completed', $contribution['contribution_status']);
620 //Refund the complete amount.
621 $this->callAPISuccess('Payment', 'create', [
622 'contribution_id' => $contributionID,
623 'total_amount' => -90,
625 $contribution = $this->callAPISuccessGetSingle('Contribution', [
626 'return' => ['contribution_status_id'],
627 'id' => $contributionID,
629 //Assert if main contribution status is updated to "Refunded".
630 $this->assertEquals('Refunded Label**', $contribution['contribution_status']);
634 * Test negative payment using create API when the "cancelled_payment_id" param is set.
636 * @throws \CRM_Core_Exception
638 public function testRefundPaymentWithCancelledPaymentId(): void
{
639 $result = $this->callAPISuccess('Contribution', 'create', [
640 'financial_type_id' => 'Donation',
641 'total_amount' => 100,
642 'contact_id' => $this->_individualId
,
644 $contributionID = $result['id'];
646 //Refund the complete amount.
647 $this->callAPISuccess('Payment', 'create', [
648 'contribution_id' => $contributionID,
649 'total_amount' => -100,
650 'cancelled_payment_id' => (int) $this->callAPISuccess('Payment', 'get', [])['id'],
652 $contribution = $this->callAPISuccessGetSingle('Contribution', [
653 'return' => ['contribution_status_id'],
654 'id' => $contributionID,
656 //Assert if main contribution status is updated to "Refunded".
657 $this->assertEquals('Refunded Label**', $contribution['contribution_status']);
661 * Test cancel payment api
663 * @throws \CRM_Core_Exception
665 public function testCancelPayment(): void
{
666 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
667 $contribution = $this->createPartiallyPaidParticipantOrder();
670 'contribution_id' => $contribution['id'],
673 $payment = $this->callAPISuccess('payment', 'get', $params);
674 $this->assertEquals(1, $payment['count']);
677 'id' => $payment['id'],
678 'check_permissions' => TRUE,
680 $this->callAPIFailure('payment', 'cancel', $cancelParams, 'API permission check failed for Payment/cancel call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
682 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
684 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
686 $payment = $this->callAPISuccess('payment', 'get', $params);
687 $this->assertEquals(2, $payment['count']);
688 $amounts = [-150.00, 150.00];
689 foreach ($payment['values'] as $value) {
690 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
695 * Test delete payment api
697 * @throws \CRM_Core_Exception
699 public function testDeletePayment(): void
{
700 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
701 $contribution = $this->createPartiallyPaidParticipantOrder();
704 'contribution_id' => $contribution['id'],
707 $payment = $this->callAPISuccessGetSingle('payment', $params);
710 'id' => $payment['id'],
711 'check_permissions' => TRUE,
713 $this->callAPIFailure('payment', 'delete', $deleteParams, 'API permission check failed for Payment/delete call; insufficient permission: require access CiviCRM and access CiviContribute and delete in CiviContribute');
715 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'delete in CiviContribute');
716 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
717 $this->callAPISuccessGetCount('payment', $params, 0);
719 $this->callAPISuccess('Contribution', 'Delete', ['id' => $contribution['id']]);
723 * Test update payment api.
725 * 1) create a contribution for $300 with a partial payment of $150
726 * - this results in 2 financial transactions. The accounts receivable transaction is linked
727 * via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
728 * so the line items are fully allocated even though they are only half paid.
730 * 2) add a payment of $50 -
731 * This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
734 * 3) update that payment to be $100
735 * This results in a negative and a positive payment ($50 & $100) - the negative payment results in
736 * financial_items but the positive payment does not.
738 * The final result is we have
739 * - 1 partly paid contribution of $300
740 * - payment financial_trxns totalling $250
741 * - 1 Accounts receivable financial_trxn totalling $300
742 * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
743 * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
744 * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
746 * @throws \CRM_Core_Exception
748 public function testUpdatePayment(): void
{
749 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
750 $contribution = $this->createPartiallyPaidParticipantOrder();
752 //Create partial payment by passing line item array is params
754 'contribution_id' => $contribution['id'],
755 'total_amount' => 50,
758 $payment = $this->callAPISuccess('payment', 'create', $params);
761 'from_financial_account_id' => 7,
762 'to_financial_account_id' => 6,
763 'total_amount' => 50,
768 $this->checkPaymentResult($payment, $expectedResult);
771 'entity_table' => 'civicrm_financial_item',
772 'financial_trxn_id' => $payment['id'],
774 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
775 $amounts = [33.33, 16.67];
776 foreach ($eft['values'] as $value) {
777 $this->assertEquals($value['amount'], array_pop($amounts));
780 // update the amount for payment
782 'contribution_id' => $contribution['id'],
783 'total_amount' => 100,
784 'id' => $payment['id'],
785 'check_permissions' => TRUE,
787 // @todo - move this permissions test to it's own test - it just confuses here.
788 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
789 $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
791 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
792 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Update Payment', 'UpdatePayment');
794 $this->validateAllPayments();
795 // Check for proportional cancelled payment against line items.
797 'entity_table' => 'civicrm_financial_item',
798 'financial_trxn_id' => $payment['id'] - 1,
801 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams)['values'];
802 $this->assertCount(2, $eft);
803 $amounts = [-33.33, -16.67];
805 foreach ($eft as $value) {
806 $this->assertEquals($value['amount'], array_pop($amounts));
809 // Check for proportional updated payment against line items.
811 'entity_table' => 'civicrm_financial_item',
812 'financial_trxn_id' => $payment['id'],
814 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
815 $amounts = [66.67, 33.33];
816 foreach ($eft as $value) {
817 $this->assertEquals($value['amount'], array_pop($amounts));
819 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
820 $this->assertCount(2, $items);
822 foreach ($items as $item) {
823 $this->assertEquals('civicrm_line_item', $item['entity_table']);
824 $itemSum +
= $item['amount'];
826 $this->assertEquals(300, $itemSum);
829 'contribution_id' => $contribution['id'],
831 $payment = $this->callAPISuccess('payment', 'get', $params);
832 $amounts = [100.00, -50.00, 50.00, 150.00];
833 foreach ($payment['values'] as $value) {
834 $amount = array_pop($amounts);
835 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
837 // Check entity financial trxn created properly
839 'entity_id' => $contribution['id'],
840 'entity_table' => 'civicrm_contribution',
841 'financial_trxn_id' => $value['id'],
843 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
844 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
849 * Test that a contribution can be overpaid with the payment api.
851 * @throws \API_Exception
852 * @throws \CRM_Core_Exception
853 * @throws \CiviCRM_API3_Exception
854 * @throws \Civi\API\Exception\UnauthorizedException
856 public function testCreatePaymentOverPay(): void
{
857 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
858 $payment = $this->callAPISuccess('Payment', 'create', ['total_amount' => 5, 'order_id' => $contributionID]);
859 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
860 $this->assertEquals('Completed', $contribution['contribution_status']);
861 $this->callAPISuccessGetCount('EntityFinancialTrxn', ['financial_trxn_id' => $payment['id'], 'entity_table' => 'civicrm_financial_item'], 0);
862 $this->validateAllPayments();
863 $this->validateAllContributions();
867 * Test create payment api for pay later contribution
869 * @throws \CRM_Core_Exception
870 * @throws \CiviCRM_API3_Exception
872 public function testCreatePaymentPayLater(): void
{
873 $this->createLoggedInUser();
874 $processorID = $this->paymentProcessorCreate();
875 $contributionParams = [
876 'total_amount' => 100,
878 'contact_id' => $this->_individualId
,
879 'financial_type_id' => 1,
880 'contribution_status_id' => 2,
883 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
884 //add payment for pay later transaction
886 'contribution_id' => $contribution['id'],
887 'total_amount' => 100,
888 'card_type_id' => 'Visa',
889 'pan_truncation' => '1234',
890 'trxn_result_code' => 'Startling success',
891 'payment_instrument_id' => $processorID,
894 $payment = $this->callAPISuccess('Payment', 'create', $params);
897 'from_financial_account_id' => 7,
898 'to_financial_account_id' => 6,
899 'total_amount' => 100,
903 'pan_truncation' => '1234',
904 'trxn_result_code' => 'Startling success',
906 'payment_instrument_id' => 1,
909 $this->checkPaymentResult($payment, $expectedResult);
910 // Check entity financial trxn created properly
912 'entity_id' => $contribution['id'],
913 'entity_table' => 'civicrm_contribution',
914 'financial_trxn_id' => $payment['id'],
916 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
917 $this->assertEquals(100, $eft['values'][$eft['id']]['amount']);
919 'entity_table' => 'civicrm_financial_item',
920 'financial_trxn_id' => $payment['id'],
922 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
923 $this->assertEquals(100, $eft['values'][$eft['id']]['amount']);
924 // Check contribution for completed status
925 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
926 $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']);
927 $this->assertEquals(100.00, $contribution['values'][$contribution['id']]['total_amount']);
928 $this->callAPISuccess('Contribution', 'Delete', [
929 'id' => $contribution['id'],
931 $this->validateAllPayments();
935 * Test net amount is set when fee amount is passed in.
937 * @throws \CRM_Core_Exception
939 public function testNetAmount(): void
{
940 $order = $this->createPendingParticipantOrder();
941 $payment = $this->callAPISuccess('Payment', 'create', ['order_id' => $order['id'], 'total_amount' => 10, 'fee_amount' => .25]);
942 $this->assertEquals('9.75', $this->callAPISuccessGetValue('Payment', ['id' => $payment['id'], 'return' => 'net_amount']));
946 * Test create payment api for pay later contribution with partial payment.
948 * https://lab.civicrm.org/dev/financial/issues/69
949 * @throws \CRM_Core_Exception
951 public function testCreatePaymentIncompletePaymentPartialPayment(): void
{
952 $contributionParams = [
953 'total_amount' => 100,
955 'contact_id' => $this->_individualId
,
956 'financial_type_id' => 1,
957 'contribution_status_id' => 2,
959 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
960 $checkNumber1 = 'C111';
961 $this->callAPISuccess('Payment', 'create', [
962 'contribution_id' => $contribution['id'],
963 'total_amount' => 50,
964 'payment_instrument_id' => 'Check',
965 'check_number' => $checkNumber1,
967 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
968 $this->assertCount(1, $payments);
969 $this->validateAllPayments();
971 $checkNumber2 = 'C222';
972 $this->callAPISuccess('Payment', 'create', [
973 'contribution_id' => $contribution['id'],
974 'total_amount' => 20,
975 'payment_instrument_id' => 'Check',
976 'check_number' => $checkNumber2,
978 $expectedConcatenatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]);
979 //Assert check number is concatenated on the main contribution.
980 $contributionValues = $this->callAPISuccess('Contribution', 'getsingle', ['id' => $contribution['id']]);
981 $this->assertEquals($expectedConcatenatedCheckNumbers, $contributionValues['check_number']);
985 * Test create payment api for failed contribution.
987 * @throws \CRM_Core_Exception
988 * @throws \CiviCRM_API3_Exception
990 public function testCreatePaymentOnFailedContribution(): void
{
991 $this->createLoggedInUser();
992 //Create a direct Failed Contribution (no ft record inserted).
993 $contributionParams = [
994 'total_amount' => 50,
996 'contact_id' => $this->_individualId
,
997 'financial_type_id' => 1,
998 'contribution_status_id' => 'Failed',
1000 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
1002 //Complete the payment in a single call.
1004 'contribution_id' => $contribution['id'],
1005 'total_amount' => 50,
1007 $this->callAPISuccess('Payment', 'create', $params);
1009 //Verify 2 rows are added to the financial trxn as payment is moved from
1010 //Failed -> Pending -> Completed, i.e, 0 -> 7(Account receivable) -> 6 (Deposit Bank).
1012 'entity_id' => $contribution['id'],
1013 'entity_table' => 'civicrm_contribution',
1015 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1016 $this->assertEquals(2, $eft['count']);
1019 //Create a Pending Contribution so an FT record is inserted.
1020 $contributionParams = [
1021 'total_amount' => 100,
1022 'currency' => 'USD',
1023 'contact_id' => $this->_individualId
,
1024 'financial_type_id' => 1,
1025 'contribution_status_id' => 'Pending',
1026 'is_pay_later' => 1,
1028 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
1030 //Mark it as failed. No FT record inserted on this update
1031 //so the payment is still in the account receivable account id 7.
1032 $this->callAPISuccess('Contribution', 'create', [
1033 'id' => $contribution['id'],
1034 'contribution_status_id' => 'Failed',
1036 $this->createPartialPaymentOnContribution($contribution['id'], 60, 100.00);
1038 //Call payment create on the failed contribution.
1040 'contribution_id' => $contribution['id'],
1041 'total_amount' => 40,
1043 $payment = $this->callAPISuccess('Payment', 'create', $params);
1046 'from_financial_account_id' => 7,
1047 'to_financial_account_id' => 6,
1048 'total_amount' => 40,
1053 $this->checkPaymentResult($payment, $expectedResult);
1055 //Check total ft rows are 4: 2 from initial pending + partial payment
1056 //+ 2 for failed -> completed transition.
1058 'entity_id' => $contribution['id'],
1059 'entity_table' => 'civicrm_contribution',
1061 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1062 $this->assertEquals(4, $eft['count']);
1064 $this->validateAllPayments();
1068 * Create partial payment for contribution
1070 * @param $contributionID
1071 * @param $partialAmount
1072 * @param $totalAmount
1074 * @throws \CRM_Core_Exception
1076 public function createPartialPaymentOnContribution($contributionID, $partialAmount, $totalAmount): void
{
1077 //Create partial payment
1079 'contribution_id' => $contributionID,
1080 'total_amount' => $partialAmount,
1082 $payment = $this->callAPISuccess('Payment', 'create', $params);
1085 'total_amount' => $partialAmount,
1090 $this->checkPaymentResult($payment, $expectedResult);
1091 // Check entity financial trxn created properly
1093 'entity_id' => $contributionID,
1094 'entity_table' => 'civicrm_contribution',
1095 'financial_trxn_id' => $payment['id'],
1097 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1098 $this->assertEquals($eft['values'][$eft['id']]['amount'], $partialAmount);
1100 'entity_table' => 'civicrm_financial_item',
1101 'financial_trxn_id' => $payment['id'],
1103 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1104 $this->assertEquals($eft['values'][$eft['id']]['amount'], $partialAmount);
1105 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contributionID]);
1106 $this->assertEquals('Partially paid', $contribution['values'][$contribution['id']]['contribution_status']);
1107 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], $totalAmount);
1111 * Test create payment api for pay later contribution with partial payment.
1113 * @throws \CRM_Core_Exception
1114 * @throws \CiviCRM_API3_Exception
1116 public function testCreatePaymentPayLaterPartialPayment(): void
{
1117 $this->createLoggedInUser();
1118 $contributionParams = [
1119 'total_amount' => 100,
1120 'currency' => 'USD',
1121 'contact_id' => $this->_individualId
,
1122 'financial_type_id' => 1,
1123 'contribution_status_id' => 2,
1124 'is_pay_later' => 1,
1126 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
1127 $this->createPartialPaymentOnContribution($contribution['id'], 60, 100.00);
1129 //Create full payment
1131 'contribution_id' => $contribution['id'],
1132 'total_amount' => 40,
1134 // Rename the 'completed' status label first to check that we are not using the labels!
1135 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
1136 $payment = $this->callAPISuccess('Payment', 'create', $params);
1139 'from_financial_account_id' => 7,
1140 'to_financial_account_id' => 6,
1141 'total_amount' => 40,
1146 $this->checkPaymentResult($payment, $expectedResult);
1147 // Check entity financial trxn created properly
1149 'entity_id' => $contribution['id'],
1150 'entity_table' => 'civicrm_contribution',
1151 'financial_trxn_id' => $payment['id'],
1153 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1154 $this->assertEquals(40, $eft['values'][$eft['id']]['amount']);
1156 'entity_table' => 'civicrm_financial_item',
1157 'financial_trxn_id' => $payment['id'],
1159 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
1160 $this->assertEquals(40, $eft['values'][$eft['id']]['amount']);
1161 // Check contribution for completed status
1162 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
1163 $this->assertEquals('Unicorn', $contribution['values'][$contribution['id']]['contribution_status']);
1164 $this->assertEquals(100.00, $contribution['values'][$contribution['id']]['total_amount']);
1165 $this->callAPISuccess('Contribution', 'Delete', [
1166 'id' => $contribution['id'],
1168 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
1169 $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId
, 'activity_type_id' => 'Payment'], 2);
1173 * Test that Payment.create uses the to_account of the payment processor.
1175 * @throws \CiviCRM_API3_Exception
1176 * @throws \CRM_Core_Exception
1178 public function testPaymentWithProcessorWithOddFinancialAccount(): void
{
1179 $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']);
1180 $processor2 = $this->dummyProcessorCreate(['financial_account_id' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id' => 'EFT']);
1181 $contributionParams = [
1182 'total_amount' => 100,
1183 'currency' => 'USD',
1184 'contact_id' => $this->_individualId
,
1185 'financial_type_id' => 1,
1186 'contribution_status_id' => 'Pending',
1188 $order = $this->callAPISuccess('Order', 'create', $contributionParams);
1189 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor->getID(), 'total_amount' => 6, 'contribution_id' => $order['id']]);
1190 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor2->getID(), 'total_amount' => 15, 'contribution_id' => $order['id']]);
1191 $payments = $this->callAPISuccess('Payment', 'get', ['sequential' => 1, 'contribution_id' => $order['id']])['values'];
1192 $this->assertEquals('Deposit Bank Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[0]['to_financial_account_id']));
1193 $this->assertEquals('Payment Processor Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[1]['to_financial_account_id']));
1194 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[0]['from_financial_account_id']));
1195 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[1]['from_financial_account_id']));
1196 $this->assertEquals('Cash', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[0]['payment_instrument_id']));
1197 $this->assertEquals('EFT', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[1]['payment_instrument_id']));
1198 // $order = $this->callAPISuccessGetSingle('Order', ['id' => $processor->getID()]);
1199 // $this->assertEquals('Cash', CRM_Core_PseudoConstant::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $order['payment_instrument_id']));
1203 * Add a location to our event.
1205 * @param int $eventID
1207 * @throws \CRM_Core_Exception
1209 protected function addLocationToEvent(int $eventID): void
{
1211 'name' => 'event place',
1212 'street_address' => 'streety street',
1213 'location_type_id' => 1,
1216 // api requires contact_id - perhaps incorrectly but use add to get past that.
1217 $address = CRM_Core_BAO_Address
::add($addressParams);
1219 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id
]);
1220 $this->callAPISuccess('Event', 'create', [
1222 'loc_block_id' => $location['id'],
1223 'is_show_location' => TRUE,
1228 * Check the created payment is valid.
1230 * This is probably over-testing really since we are repetitively checking a basic function...
1232 * @param int $paymentID
1233 * @param int $contributionID
1234 * @param int $amount
1236 * @throws \CRM_Core_Exception
1238 protected function checkPaymentIsValid(int $paymentID, int $contributionID, int $amount = 50): void
{
1239 $payment = $this->callAPISuccess('Payment', 'getsingle', ['financial_trxn_id' => $paymentID]);
1240 $this->assertEquals(7, $payment['from_financial_account_id']);
1241 $this->assertEquals(6, $payment['to_financial_account_id']);
1242 $this->assertEquals(1, $payment['status_id']);
1243 $this->assertEquals(1, $payment['is_payment']);
1244 $this->assertEquals($amount, $payment['total_amount']);
1246 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
1247 'entity_id' => $contributionID,
1248 'entity_table' => 'civicrm_contribution',
1249 'financial_trxn_id' => $payment['id'],
1252 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
1256 * This test was introduced in
1257 * https://github.com/civicrm/civicrm-core/pull/17688 to ensure that a
1258 * contribution's date is not set to today's date when a payment is received,
1259 * and that the contribution's trxn_id is set to that of the payment.
1261 * This tests the current behaviour, but there are questions about whether
1264 * The current behaviour is that when a payment is received that completes a
1265 * contribution: the contribution's receive_date is set to that of the
1266 * payment (passed to Payment.create as trxn_date).
1268 * But why *should* we update the receive_date at all?
1270 * If we decide that receive_date should not be touched, just change
1271 * $trxnDate for $trxnID as detailed in the code comment below, which will
1272 * still make sure we're not setting today's date, as well as confirming
1273 * that the original date is not changed.
1275 * @see https://github.com/civicrm/civicrm-core/pull/17688
1276 * @see https://lab.civicrm.org/dev/financial/-/issues/139
1278 * @throws \CRM_Core_Exception
1279 * @throws \CiviCRM_API3_Exception
1281 public function testPaymentCreateTrxnIdAndDates(): void
{
1283 $trxnDate = '2010-01-01 09:00:00';
1285 $originalReceiveDate = '2010-02-02 22:22:22';
1287 $contributionID = $this->contributionCreate([
1288 'contact_id' => $this->individualCreate(),
1289 'total_amount' => 100,
1290 'contribution_status_id' => 'Pending',
1291 'receive_date' => $originalReceiveDate,
1295 $this->callAPISuccess('Payment', 'create', [
1296 'total_amount' => 100,
1297 'order_id' => $contributionID,
1298 'trxn_date' => $trxnDate,
1299 'trxn_id' => $trxnID,
1303 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
1304 $this->assertEquals('Completed', $contribution['contribution_status']);
1305 $this->assertEquals(.2, $contribution['fee_amount']);
1306 $this->assertEquals(99.8, $contribution['net_amount']);
1308 $this->assertEquals($trxnID, $contribution['trxn_id'],
1309 'Contribution trxn_id should have been set to that of the payment.');
1311 $this->assertEquals($originalReceiveDate, $contribution['receive_date'],
1312 'Contribution receive date was changed, but should not have been.');