3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_PaymentTest
extends CiviUnitTestCase
{
37 protected $_individualId;
39 protected $_financialTypeId = 1;
41 protected $_apiversion;
48 public function setUp() {
51 $this->_apiversion
= 3;
52 $this->_individualId
= $this->individualCreate();
53 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= [];
57 * Clean up after each test.
61 public function tearDown() {
62 $this->quickCleanUpFinancialEntities();
63 $this->quickCleanup(['civicrm_uf_match']);
64 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
69 * Test Get Payment api.
71 public function testGetPayment() {
73 'contact_id' => $this->_individualId
,
74 'receive_date' => '2010-01-20',
75 'total_amount' => 100.00,
76 'financial_type_id' => $this->_financialTypeId
,
78 'contribution_status_id' => 1,
80 $contribution = $this->callAPISuccess('contribution', 'create', $p);
83 'contribution_id' => $contribution['id'],
84 'check_permissions' => TRUE,
86 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM'];
87 $payment = $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute');
89 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviContribute');
90 $payment = $this->callAPISuccess('payment', 'get', $params);
92 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
93 $this->assertEquals(1, $payment['count']);
96 $contribution['id'] => [
97 'total_amount' => 100,
99 'trxn_date' => '2010-01-20 00:00:00',
100 'contribution_id' => $contribution['id'],
104 $this->checkPaymentResult($payment, $expectedResult);
105 $this->callAPISuccess('Contribution', 'Delete', [
106 'id' => $contribution['id'],
111 * Retrieve Payment using trxn_id.
113 public function testGetPaymentWithTrxnID() {
114 $this->_individualId2
= $this->individualCreate();
116 'contact_id' => $this->_individualId
,
118 'total_amount' => 10,
120 $contributionID1 = $this->contributionCreate($params1);
123 'contact_id' => $this->_individualId2
,
125 'total_amount' => 20,
127 $contributionID2 = $this->contributionCreate($params2);
129 $paymentParams = ['trxn_id' => 111111];
130 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
133 'total_amount' => 10,
137 'contribution_id' => $contributionID1,
140 $this->checkPaymentResult($payment, $expectedResult);
142 $paymentParams = ['trxn_id' => 222222];
143 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
146 'total_amount' => 20,
150 'contribution_id' => $contributionID2,
153 $this->checkPaymentResult($payment, $expectedResult);
157 * Test email receipt for partial payment.
159 public function testPaymentEmailReceipt() {
160 $mut = new CiviMailUtils($this);
161 list($lineItems, $contribution) = $this->createParticipantWithContribution();
162 $event = $this->callAPISuccess('Event', 'get', []);
163 $this->addLocationToEvent($event['id']);
165 'contribution_id' => $contribution['id'],
166 'total_amount' => 50,
167 'check_number' => '345',
168 'trxn_date' => '2018-08-13 17:57:56',
170 $payment = $this->callAPISuccess('payment', 'create', $params);
171 $this->checkPaymentResult($payment, [
173 'from_financial_account_id' => 7,
174 'to_financial_account_id' => 6,
175 'total_amount' => 50,
181 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
182 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
184 'From: "FIXME" <info@EXAMPLE.ORG>',
186 'Total Fees: $ 300.00',
187 'This Payment Amount: $ 50.00',
188 //150 was paid in the 1st payment.
189 'Balance Owed: $ 100.00',
190 'Event Information and Location',
193 'Transaction Date: August 13th, 2018 5:57 PM',
198 $mut->clearMessages();
202 * Test email receipt for partial payment.
204 * @throws \CRM_Core_Exception
206 public function testPaymentEmailReceiptFullyPaid() {
207 $mut = new CiviMailUtils($this);
208 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
209 list($lineItems, $contribution) = $this->createParticipantWithContribution();
212 'contribution_id' => $contribution['id'],
213 'total_amount' => 150,
215 $payment = $this->callAPISuccess('payment', 'create', $params);
217 // Here we set the email to an invalid email & use check_permissions, domain email should be used.
218 $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
219 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
220 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet', 'Registration Confirmation - Annual CiviCRM meet']);
222 'From: "FIXME" <info@EXAMPLE.ORG>',
224 'A payment has been received.',
225 'Total Fees: $ 300.00',
226 'This Payment Amount: $ 150.00',
227 'Balance Owed: $ 0.00',
228 'Thank you for completing this payment.',
231 $mut->clearMessages();
235 * Test email receipt for partial payment.
237 * @dataProvider getThousandSeparators
239 * @param string $thousandSeparator
241 public function testRefundEmailReceipt($thousandSeparator) {
242 $this->setCurrencySeparators($thousandSeparator);
243 $decimalSeparator = ($thousandSeparator === ',' ?
'.' : ',');
244 $mut = new CiviMailUtils($this);
245 list($lineItems, $contribution) = $this->createParticipantWithContribution();
246 $this->callAPISuccess('payment', 'create', [
247 'contribution_id' => $contribution['id'],
248 'total_amount' => 50,
249 'check_number' => '345',
250 'trxn_date' => '2018-08-13 17:57:56',
253 $payment = $this->callAPISuccess('payment', 'create', [
254 'contribution_id' => $contribution['id'],
255 'total_amount' => -30,
256 'trxn_date' => '2018-11-13 12:01:56',
257 'sequential' => TRUE,
261 'from_financial_account_id' => 7,
262 'to_financial_account_id' => 6,
263 'total_amount' => -30,
264 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
267 foreach ($expected as $key => $value) {
268 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
271 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
272 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet']);
275 'A refund has been issued based on changes in your registration selections.',
276 'Total Fees: $ 300' . $decimalSeparator . '00',
277 'Refund Amount: $ -30' . $decimalSeparator . '00',
278 'Event Information and Location',
280 'Transaction Date: November 13th, 2018 12:01 PM',
281 'You Paid: $ 170' . $decimalSeparator . '00',
284 $mut->clearMessages();
288 * Test create payment api with no line item in params
290 public function testCreatePaymentNoLineItems() {
291 list($lineItems, $contribution) = $this->createParticipantWithContribution();
293 //Create partial payment
295 'contribution_id' => $contribution['id'],
296 'total_amount' => 50,
298 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
301 'from_financial_account_id' => 7,
302 'to_financial_account_id' => 6,
303 'total_amount' => 50,
308 $this->checkPaymentResult($payment, $expectedResult);
310 // Check entity financial trxn created properly
312 'entity_id' => $contribution['id'],
313 'entity_table' => 'civicrm_contribution',
314 'financial_trxn_id' => $payment['id'],
317 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
319 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
322 'entity_table' => 'civicrm_financial_item',
323 'financial_trxn_id' => $payment['id'],
325 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
326 $amounts = [33.33, 16.67];
327 foreach ($eft['values'] as $value) {
328 $this->assertEquals($value['amount'], array_pop($amounts));
331 // Now create payment to complete total amount of contribution
333 'contribution_id' => $contribution['id'],
334 'total_amount' => 100,
336 $payment = $this->callAPISuccess('payment', 'create', $params);
339 'from_financial_account_id' => 7,
340 'to_financial_account_id' => 6,
341 'total_amount' => 100,
346 $this->checkPaymentResult($payment, $expectedResult);
348 'entity_table' => 'civicrm_financial_item',
349 'financial_trxn_id' => $payment['id'],
351 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
352 $amounts = [66.67, 33.33];
353 foreach ($eft['values'] as $value) {
354 $this->assertEquals($value['amount'], array_pop($amounts));
356 // Check contribution for completed status
357 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
359 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
360 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
361 $paymentParticipant = [
362 'contribution_id' => $contribution['id'],
364 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
365 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
366 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
367 $this->callAPISuccess('Contribution', 'Delete', [
368 'id' => $contribution['id'],
373 * Function to assert db values
375 public function checkPaymentResult($payment, $expectedResult) {
376 foreach ($expectedResult[$payment['id']] as $key => $value) {
377 $this->assertEquals($payment['values'][$payment['id']][$key], $value, 'mismatch on ' . $key);
382 * Test create payment api with line item in params
384 public function testCreatePaymentLineItems() {
385 list($lineItems, $contribution) = $this->createParticipantWithContribution();
386 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]);
388 //Create partial payment by passing line item array is params
390 'contribution_id' => $contribution['id'],
391 'total_amount' => 50,
394 foreach ($lineItems['values'] as $id => $ignore) {
395 $params['line_item'][] = [$id => array_pop($amounts)];
397 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Payment with line item', 'CreatePaymentWithLineItems');
400 'from_financial_account_id' => 7,
401 'to_financial_account_id' => 6,
402 'total_amount' => 50,
407 $this->checkPaymentResult($payment, $expectedResult);
409 // Check entity financial trxn created properly
411 'entity_id' => $contribution['id'],
412 'entity_table' => 'civicrm_contribution',
413 'financial_trxn_id' => $payment['id'],
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
418 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
421 'entity_table' => 'civicrm_financial_item',
422 'financial_trxn_id' => $payment['id'],
424 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
426 foreach ($eft['values'] as $value) {
427 $this->assertEquals($value['amount'], array_pop($amounts));
430 // Now create payment to complete total amount of contribution
432 'contribution_id' => $contribution['id'],
433 'total_amount' => 100,
436 foreach ($lineItems['values'] as $id => $ignore) {
437 $params['line_item'][] = [$id => array_pop($amounts)];
439 $payment = $this->callAPISuccess('payment', 'create', $params);
442 'from_financial_account_id' => 7,
443 'to_financial_account_id' => 6,
444 'total_amount' => 100,
449 $this->checkPaymentResult($payment, $expectedResult);
451 'entity_table' => 'civicrm_financial_item',
452 'financial_trxn_id' => $payment['id'],
454 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
456 foreach ($eft['values'] as $value) {
457 $this->assertEquals($value['amount'], array_pop($amounts));
459 // Check contribution for completed status
460 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
462 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
463 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
464 $paymentParticipant = [
465 'contribution_id' => $contribution['id'],
467 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
468 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
469 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
470 $this->callAPISuccess('Contribution', 'Delete', [
471 'id' => $contribution['id'],
476 * Test cancel payment api
478 public function testCancelPayment() {
479 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
480 list($lineItems, $contribution) = $this->createParticipantWithContribution();
483 'contribution_id' => $contribution['id'],
486 $payment = $this->callAPISuccess('payment', 'get', $params);
487 $this->assertEquals(1, $payment['count']);
490 'id' => $payment['id'],
491 'check_permissions' => TRUE,
493 $payment = $this->callAPIFailure('payment', 'cancel', $cancelParams, 'API permission check failed for Payment/cancel call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
495 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
497 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
499 $payment = $this->callAPISuccess('payment', 'get', $params);
500 $this->assertEquals(2, $payment['count']);
501 $amounts = [-150.00, 150.00];
502 foreach ($payment['values'] as $value) {
503 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
506 $this->callAPISuccess('Contribution', 'Delete', [
507 'id' => $contribution['id'],
512 * Test delete payment api
514 public function testDeletePayment() {
515 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
516 list($lineItems, $contribution) = $this->createParticipantWithContribution();
519 'contribution_id' => $contribution['id'],
522 $payment = $this->callAPISuccess('payment', 'get', $params);
523 $this->assertEquals(1, $payment['count']);
526 'id' => $payment['id'],
527 'check_permissions' => TRUE,
529 $payment = $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');
531 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'delete in CiviContribute');
532 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
534 $payment = $this->callAPISuccess('payment', 'get', $params);
535 $this->assertEquals(0, $payment['count']);
537 $this->callAPISuccess('Contribution', 'Delete', [
538 'id' => $contribution['id'],
543 * Test update payment api.
545 * 1) create a contribution for $300 with a partial payment of $150
546 * - this results in 2 financial transactions. The accounts receivable transaction is linked
547 * via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
548 * so the line items are fully allocated even though they are only half paid.
550 * 2) add a payment of $50 -
551 * This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
554 * 3) update that payment to be $100
555 * This results in a negative and a positive payment ($50 & $100) - the negative payment results in
556 * financial_items but the positive payment does not.
558 * The final result is we have
559 * - 1 partly paid contribution of $300
560 * - payment financial_trxns totalling $250
561 * - 1 Accounts receivable financial_trxn totalling $300
562 * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
563 * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
564 * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
566 public function testUpdatePayment() {
567 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
568 list($lineItems, $contribution) = $this->createParticipantWithContribution();
570 //Create partial payment by passing line item array is params
572 'contribution_id' => $contribution['id'],
573 'total_amount' => 50,
576 $payment = $this->callAPISuccess('payment', 'create', $params);
579 'from_financial_account_id' => 7,
580 'to_financial_account_id' => 6,
581 'total_amount' => 50,
586 $this->checkPaymentResult($payment, $expectedResult);
589 'entity_table' => 'civicrm_financial_item',
590 'financial_trxn_id' => $payment['id'],
592 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
593 $amounts = [33.33, 16.67];
594 foreach ($eft['values'] as $value) {
595 $this->assertEquals($value['amount'], array_pop($amounts));
598 // update the amount for payment
600 'contribution_id' => $contribution['id'],
601 'total_amount' => 100,
602 'id' => $payment['id'],
603 'check_permissions' => TRUE,
605 // @todo - move this permissions test to it's own test - it just confuses here.
606 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
607 $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
609 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
610 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Update Payment', 'UpdatePayment');
612 // Check for proportional cancelled payment against lineitems.
614 'entity_table' => 'civicrm_financial_item',
615 'financial_trxn_id' => $payment['id'] - 1,
618 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
619 $amounts = [-33.33, -16.67];
621 foreach ($eft['values'] as $value) {
622 $this->assertEquals($value['amount'], array_pop($amounts));
625 // Check for proportional updated payment against lineitems.
627 'entity_table' => 'civicrm_financial_item',
628 'financial_trxn_id' => $payment['id'],
630 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
631 $amounts = [66.67, 33.33];
632 foreach ($eft['values'] as $value) {
633 $this->assertEquals($value['amount'], array_pop($amounts));
635 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
636 $this->assertCount(2, $items);
638 foreach ($items as $item) {
639 $this->assertEquals('civicrm_line_item', $item['entity_table']);
640 $itemSum +
= $item['amount'];
642 $this->assertEquals(300, $itemSum);
645 'contribution_id' => $contribution['id'],
647 $payment = $this->callAPISuccess('payment', 'get', $params);
648 $amounts = [100.00, -50.00, 50.00, 150.00];
649 foreach ($payment['values'] as $value) {
650 $amount = array_pop($amounts);
651 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
653 // Check entity financial trxn created properly
655 'entity_id' => $contribution['id'],
656 'entity_table' => 'civicrm_contribution',
657 'financial_trxn_id' => $value['id'],
659 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
660 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
663 $this->callAPISuccess('Contribution', 'Delete', [
664 'id' => $contribution['id'],
669 * Test create payment api for paylater contribution
671 public function testCreatePaymentPayLater() {
672 $this->createLoggedInUser();
673 $contributionParams = [
674 'total_amount' => 100,
676 'contact_id' => $this->_individualId
,
677 'financial_type_id' => 1,
678 'contribution_status_id' => 2,
681 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
682 //add payment for pay later transaction
684 'contribution_id' => $contribution['id'],
685 'total_amount' => 100,
687 $payment = $this->callAPISuccess('Payment', 'create', $params);
690 'from_financial_account_id' => 7,
691 'to_financial_account_id' => 6,
692 'total_amount' => 100,
697 $this->checkPaymentResult($payment, $expectedResult);
698 // Check entity financial trxn created properly
700 'entity_id' => $contribution['id'],
701 'entity_table' => 'civicrm_contribution',
702 'financial_trxn_id' => $payment['id'],
704 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
705 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
707 'entity_table' => 'civicrm_financial_item',
708 'financial_trxn_id' => $payment['id'],
710 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
711 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
712 // Check contribution for completed status
713 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
714 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
715 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
716 $this->callAPISuccess('Contribution', 'Delete', [
717 'id' => $contribution['id'],
722 * Test create payment api for pay later contribution with partial payment.
724 * https://lab.civicrm.org/dev/financial/issues/69
726 public function testCreatePaymentIncompletePaymentPartialPayment() {
727 $contributionParams = [
728 'total_amount' => 100,
730 'contact_id' => $this->_individualId
,
731 'financial_type_id' => 1,
732 'contribution_status_id' => 2,
734 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
735 $this->callAPISuccess('Payment', 'create', [
736 'contribution_id' => $contribution['id'],
737 'total_amount' => 50,
738 'payment_instrument_id' => 'Cash',
740 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
741 $this->assertCount(1, $payments);
745 * Test create payment api for pay later contribution with partial payment.
747 public function testCreatePaymentPayLaterPartialPayment() {
748 $this->createLoggedInUser();
749 $contributionParams = [
750 'total_amount' => 100,
752 'contact_id' => $this->_individualId
,
753 'financial_type_id' => 1,
754 'contribution_status_id' => 2,
757 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
758 //Create partial payment
760 'contribution_id' => $contribution['id'],
761 'total_amount' => 60,
763 $payment = $this->callAPISuccess('Payment', 'create', $params);
766 'total_amount' => 60,
771 $this->checkPaymentResult($payment, $expectedResult);
772 // Check entity financial trxn created properly
774 'entity_id' => $contribution['id'],
775 'entity_table' => 'civicrm_contribution',
776 'financial_trxn_id' => $payment['id'],
778 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
779 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
781 'entity_table' => 'civicrm_financial_item',
782 'financial_trxn_id' => $payment['id'],
784 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
785 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
786 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
787 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
788 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
789 //Create full payment
791 'contribution_id' => $contribution['id'],
792 'total_amount' => 40,
794 // Rename the 'completed' status label first to check that we are not using the labels!
795 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
796 $payment = $this->callAPISuccess('Payment', 'create', $params);
799 'from_financial_account_id' => 7,
800 'to_financial_account_id' => 6,
801 'total_amount' => 40,
806 $this->checkPaymentResult($payment, $expectedResult);
807 // Check entity financial trxn created properly
809 'entity_id' => $contribution['id'],
810 'entity_table' => 'civicrm_contribution',
811 'financial_trxn_id' => $payment['id'],
813 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
814 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
816 'entity_table' => 'civicrm_financial_item',
817 'financial_trxn_id' => $payment['id'],
819 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
820 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
821 // Check contribution for completed status
822 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
823 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn');
824 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
825 $this->callAPISuccess('Contribution', 'Delete', [
826 'id' => $contribution['id'],
828 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
829 $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId
, 'activity_type_id' => 'Payment'], 2);
833 * Add a location to our event.
835 * @param int $eventID
837 protected function addLocationToEvent($eventID) {
839 'name' => 'event place',
840 'street_address' => 'streety street',
841 'location_type_id' => 1,
844 // api requires contact_id - perhaps incorrectly but use add to get past that.
845 $address = CRM_Core_BAO_Address
::add($addressParams);
847 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id
]);
848 $this->callAPISuccess('Event', 'create', [
850 'loc_block_id' => $location['id'],
851 'is_show_location' => TRUE,