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;
44 * @throws \CRM_Core_Exception
46 public function setUp() {
49 $this->_apiversion
= 3;
50 $this->_individualId
= $this->individualCreate();
51 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= [];
55 * Clean up after each test.
59 public function tearDown() {
60 $this->quickCleanUpFinancialEntities();
61 $this->quickCleanup(['civicrm_uf_match']);
62 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
67 * Test Get Payment api.
69 * @throws \CRM_Core_Exception
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'],
108 $this->validateAllPayments();
112 * Retrieve Payment using trxn_id.
114 * @throws \CRM_Core_Exception
116 public function testGetPaymentWithTrxnID() {
117 $this->_individualId2
= $this->individualCreate();
119 'contact_id' => $this->_individualId
,
121 'total_amount' => 10,
123 $contributionID1 = $this->contributionCreate($params1);
126 'contact_id' => $this->_individualId2
,
128 'total_amount' => 20,
130 $contributionID2 = $this->contributionCreate($params2);
132 $paymentParams = ['trxn_id' => 111111];
133 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
136 'total_amount' => 10,
140 'contribution_id' => $contributionID1,
143 $this->checkPaymentResult($payment, $expectedResult);
145 $paymentParams = ['trxn_id' => 222222];
146 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
149 'total_amount' => 20,
153 'contribution_id' => $contributionID2,
156 $this->checkPaymentResult($payment, $expectedResult);
157 $this->validateAllPayments();
161 * Test email receipt for partial payment.
163 * @throws \CRM_Core_Exception
165 public function testPaymentEmailReceipt() {
166 $mut = new CiviMailUtils($this);
167 $contribution = $this->createPartiallyPaidParticipantOrder();
168 $event = $this->callAPISuccess('Event', 'get', []);
169 $this->addLocationToEvent($event['id']);
171 'contribution_id' => $contribution['id'],
172 'total_amount' => 50,
173 'check_number' => '345',
174 'trxn_date' => '2018-08-13 17:57:56',
176 $payment = $this->callAPISuccess('payment', 'create', $params);
177 $this->checkPaymentResult($payment, [
179 'from_financial_account_id' => 7,
180 'to_financial_account_id' => 6,
181 'total_amount' => 50,
187 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
188 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II']);
190 'From: "FIXME" <info@EXAMPLE.ORG>',
192 'Total Fees: $ 300.00',
193 'This Payment Amount: $ 50.00',
194 //150 was paid in the 1st payment.
195 'Balance Owed: $ 100.00',
196 'Event Information and Location',
199 'Transaction Date: August 13th, 2018 5:57 PM',
204 $mut->clearMessages();
205 $this->validateAllPayments();
209 * Test email receipt for partial payment.
211 * @throws \CRM_Core_Exception
213 public function testPaymentEmailReceiptFullyPaid() {
214 $mut = new CiviMailUtils($this);
215 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
216 $contribution = $this->createPartiallyPaidParticipantOrder();
219 'contribution_id' => $contribution['id'],
220 'total_amount' => 150,
222 $payment = $this->callAPISuccess('payment', 'create', $params);
224 // Here we set the email to an invalid email & use check_permissions, domain email should be used.
225 $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
226 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
227 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II', 'Registration Confirmation - Annual CiviCRM meet - Mr. Anthony Anderson II']);
229 'From: "FIXME" <info@EXAMPLE.ORG>',
231 'A payment has been received.',
232 'Total Fees: $ 300.00',
233 'This Payment Amount: $ 150.00',
234 'Balance Owed: $ 0.00',
235 'Thank you for completing this payment.',
238 $mut->clearMessages();
239 $this->validateAllPayments();
243 * Test email receipt for partial payment.
245 * @dataProvider getThousandSeparators
247 * @param string $thousandSeparator
249 * @throws \CRM_Core_Exception
251 public function testRefundEmailReceipt($thousandSeparator) {
252 $this->setCurrencySeparators($thousandSeparator);
253 $decimalSeparator = ($thousandSeparator === ',' ?
'.' : ',');
254 $mut = new CiviMailUtils($this);
255 $contribution = $this->createPartiallyPaidParticipantOrder();
256 $this->callAPISuccess('payment', 'create', [
257 'contribution_id' => $contribution['id'],
258 'total_amount' => 50,
259 'check_number' => '345',
260 'trxn_date' => '2018-08-13 17:57:56',
263 $payment = $this->callAPISuccess('payment', 'create', [
264 'contribution_id' => $contribution['id'],
265 'total_amount' => -30,
266 'trxn_date' => '2018-11-13 12:01:56',
267 'sequential' => TRUE,
271 'from_financial_account_id' => 7,
272 'to_financial_account_id' => 6,
273 'total_amount' => -30,
274 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
277 foreach ($expected as $key => $value) {
278 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
281 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
282 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet - Mr. Anthony Anderson II']);
285 'A refund has been issued based on changes in your registration selections.',
286 'Total Fees: $ 300' . $decimalSeparator . '00',
287 'Refund Amount: $ -30' . $decimalSeparator . '00',
288 'Event Information and Location',
290 'Transaction Date: November 13th, 2018 12:01 PM',
291 'You Paid: $ 170' . $decimalSeparator . '00',
294 $mut->clearMessages();
295 $this->validateAllPayments();
299 * Test adding a payment to a pending multi-line order.
301 * @throws \CRM_Core_Exception
303 public function testCreatePaymentPendingOrderNoLineItems() {
304 $order = $this->createPendingParticipantOrder();
305 $this->callAPISuccess('Payment', 'create', [
306 'order_id' => $order['id'],
307 'total_amount' => 50,
309 $this->validateAllPayments();
313 * Add participant with contribution
317 * @throws \CRM_Core_Exception
319 protected function createPendingParticipantOrder() {
320 return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams());
324 * Test create payment api with no line item in params
326 * @throws \CRM_Core_Exception
328 public function testCreatePaymentNoLineItems() {
329 $contribution = $this->createPartiallyPaidParticipantOrder();
331 //Create partial payment
333 'contribution_id' => $contribution['id'],
334 'total_amount' => 50,
336 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
337 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
340 'entity_table' => 'civicrm_financial_item',
341 'financial_trxn_id' => $payment['id'],
343 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
344 $amounts = [33.33, 16.67];
345 foreach ($eft['values'] as $value) {
346 $this->assertEquals($value['amount'], array_pop($amounts));
349 // Now create payment to complete total amount of contribution
351 'contribution_id' => $contribution['id'],
352 'total_amount' => 100,
354 $payment = $this->callAPISuccess('payment', 'create', $params);
357 'from_financial_account_id' => 7,
358 'to_financial_account_id' => 6,
359 'total_amount' => 100,
364 $this->checkPaymentResult($payment, $expectedResult);
366 'entity_table' => 'civicrm_financial_item',
367 'financial_trxn_id' => $payment['id'],
369 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
370 $amounts = [66.67, 33.33];
371 foreach ($eft['values'] as $value) {
372 $this->assertEquals($value['amount'], array_pop($amounts));
374 // Check contribution for completed status
375 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
377 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
378 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
379 $paymentParticipant = [
380 'contribution_id' => $contribution['id'],
382 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
383 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
384 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
385 $this->callAPISuccess('Contribution', 'Delete', [
386 'id' => $contribution['id'],
388 $this->validateAllPayments();
392 * Function to assert db values
394 * @throws \CRM_Core_Exception
396 public function checkPaymentResult($payment, $expectedResult) {
397 $refreshedPayment = $this->callAPISuccessGetSingle('Payment', ['financial_trxn_id' => $payment['id']]);
398 foreach ($expectedResult[$payment['id']] as $key => $value) {
399 $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key); $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key);
404 * Test create payment api with line item in params
406 * @throws \CRM_Core_Exception
408 public function testCreatePaymentLineItems() {
409 $contribution = $this->createPartiallyPaidParticipantOrder();
410 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
412 // Create partial payment by passing line item array is params.
414 'contribution_id' => $contribution['id'],
415 'total_amount' => 50,
418 foreach ($lineItems as $id => $ignore) {
419 $params['line_item'][] = [$id => array_pop($amounts)];
421 $payment = $this->callAPIAndDocument('Payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Payment with line item', 'CreatePaymentWithLineItems');
422 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
425 'entity_table' => 'civicrm_financial_item',
426 'financial_trxn_id' => $payment['id'],
427 'return' => ['entity_id.entity_id', 'amount'],
429 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
430 $this->assertCount(2, $eft);
432 foreach ($eft as $value) {
433 $this->assertEquals($value['amount'], array_pop($amounts));
436 // Now create payment to complete total amount of contribution
438 'contribution_id' => $contribution['id'],
439 'total_amount' => 100,
442 foreach ($lineItems as $id => $ignore) {
443 $params['line_item'][] = [$id => array_pop($amounts)];
445 $payment = $this->callAPISuccess('Payment', 'create', $params);
448 'from_financial_account_id' => 7,
449 'to_financial_account_id' => 6,
450 'total_amount' => 100,
455 $this->checkPaymentResult($payment, $expectedResult);
457 'entity_table' => 'civicrm_financial_item',
458 'financial_trxn_id' => $payment['id'],
460 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
461 $this->assertCount(2, $eft);
463 foreach ($eft as $value) {
464 $this->assertEquals($value['amount'], array_pop($amounts));
466 // Check contribution for completed status
467 $contribution = $this->callAPISuccess('Contribution', 'get', ['id' => $contribution['id']]);
469 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
470 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
471 $paymentParticipant = [
472 'contribution_id' => $contribution['id'],
474 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
475 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
476 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
477 $this->validateAllPayments();
481 * Test cancel payment api
483 * @throws \CRM_Core_Exception
485 public function testCancelPayment() {
486 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
487 $contribution = $this->createPartiallyPaidParticipantOrder();
490 'contribution_id' => $contribution['id'],
493 $payment = $this->callAPISuccess('payment', 'get', $params);
494 $this->assertEquals(1, $payment['count']);
497 'id' => $payment['id'],
498 'check_permissions' => TRUE,
500 $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');
502 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
504 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
506 $payment = $this->callAPISuccess('payment', 'get', $params);
507 $this->assertEquals(2, $payment['count']);
508 $amounts = [-150.00, 150.00];
509 foreach ($payment['values'] as $value) {
510 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
513 $this->callAPISuccess('Contribution', 'Delete', [
514 'id' => $contribution['id'],
516 $this->validateAllPayments();
520 * Test delete payment api
522 * @throws \CRM_Core_Exception
524 public function testDeletePayment() {
525 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
526 $contribution = $this->createPartiallyPaidParticipantOrder();
529 'contribution_id' => $contribution['id'],
532 $payment = $this->callAPISuccess('payment', 'get', $params);
533 $this->assertEquals(1, $payment['count']);
536 'id' => $payment['id'],
537 'check_permissions' => TRUE,
539 $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');
541 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'delete in CiviContribute');
542 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
544 $payment = $this->callAPISuccess('payment', 'get', $params);
545 $this->assertEquals(0, $payment['count']);
547 $this->callAPISuccess('Contribution', 'Delete', [
548 'id' => $contribution['id'],
553 * Test update payment api.
555 * 1) create a contribution for $300 with a partial payment of $150
556 * - this results in 2 financial transactions. The accounts receivable transaction is linked
557 * via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
558 * so the line items are fully allocated even though they are only half paid.
560 * 2) add a payment of $50 -
561 * This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
564 * 3) update that payment to be $100
565 * This results in a negative and a positive payment ($50 & $100) - the negative payment results in
566 * financial_items but the positive payment does not.
568 * The final result is we have
569 * - 1 partly paid contribution of $300
570 * - payment financial_trxns totalling $250
571 * - 1 Accounts receivable financial_trxn totalling $300
572 * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
573 * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
574 * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
576 * @throws \CRM_Core_Exception
578 public function testUpdatePayment() {
579 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
580 $contribution = $this->createPartiallyPaidParticipantOrder();
582 //Create partial payment by passing line item array is params
584 'contribution_id' => $contribution['id'],
585 'total_amount' => 50,
588 $payment = $this->callAPISuccess('payment', 'create', $params);
591 'from_financial_account_id' => 7,
592 'to_financial_account_id' => 6,
593 'total_amount' => 50,
598 $this->checkPaymentResult($payment, $expectedResult);
601 'entity_table' => 'civicrm_financial_item',
602 'financial_trxn_id' => $payment['id'],
604 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
605 $amounts = [33.33, 16.67];
606 foreach ($eft['values'] as $value) {
607 $this->assertEquals($value['amount'], array_pop($amounts));
610 // update the amount for payment
612 'contribution_id' => $contribution['id'],
613 'total_amount' => 100,
614 'id' => $payment['id'],
615 'check_permissions' => TRUE,
617 // @todo - move this permissions test to it's own test - it just confuses here.
618 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
619 $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
621 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
622 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Update Payment', 'UpdatePayment');
624 $this->validateAllPayments();
625 // Check for proportional cancelled payment against lineitems.
627 'entity_table' => 'civicrm_financial_item',
628 'financial_trxn_id' => $payment['id'] - 1,
631 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams)['values'];
632 $this->assertCount(2, $eft);
633 $amounts = [-33.33, -16.67];
635 foreach ($eft as $value) {
636 $this->assertEquals($value['amount'], array_pop($amounts));
639 // Check for proportional updated payment against lineitems.
641 'entity_table' => 'civicrm_financial_item',
642 'financial_trxn_id' => $payment['id'],
644 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
645 $amounts = [66.67, 33.33];
646 foreach ($eft as $value) {
647 $this->assertEquals($value['amount'], array_pop($amounts));
649 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
650 $this->assertCount(2, $items);
652 foreach ($items as $item) {
653 $this->assertEquals('civicrm_line_item', $item['entity_table']);
654 $itemSum +
= $item['amount'];
656 $this->assertEquals(300, $itemSum);
659 'contribution_id' => $contribution['id'],
661 $payment = $this->callAPISuccess('payment', 'get', $params);
662 $amounts = [100.00, -50.00, 50.00, 150.00];
663 foreach ($payment['values'] as $value) {
664 $amount = array_pop($amounts);
665 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
667 // Check entity financial trxn created properly
669 'entity_id' => $contribution['id'],
670 'entity_table' => 'civicrm_contribution',
671 'financial_trxn_id' => $value['id'],
673 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
674 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
677 $this->callAPISuccess('Contribution', 'Delete', [
678 'id' => $contribution['id'],
680 $this->validateAllPayments();
684 * Test create payment api for paylater contribution
686 * @throws \CRM_Core_Exception
688 public function testCreatePaymentPayLater() {
689 $this->createLoggedInUser();
690 $processorID = $this->paymentProcessorCreate();
691 $contributionParams = [
692 'total_amount' => 100,
694 'contact_id' => $this->_individualId
,
695 'financial_type_id' => 1,
696 'contribution_status_id' => 2,
699 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
700 //add payment for pay later transaction
702 'contribution_id' => $contribution['id'],
703 'total_amount' => 100,
704 'card_type_id' => 'Visa',
705 'pan_truncation' => '1234',
706 'trxn_result_code' => 'Startling success',
707 'payment_instrument_id' => $processorID,
710 $payment = $this->callAPISuccess('Payment', 'create', $params);
713 'from_financial_account_id' => 7,
714 'to_financial_account_id' => 6,
715 'total_amount' => 100,
719 'pan_truncation' => '1234',
720 'trxn_result_code' => 'Startling success',
722 'payment_instrument_id' => 1,
725 $this->checkPaymentResult($payment, $expectedResult);
726 // Check entity financial trxn created properly
728 'entity_id' => $contribution['id'],
729 'entity_table' => 'civicrm_contribution',
730 'financial_trxn_id' => $payment['id'],
732 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
733 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
735 'entity_table' => 'civicrm_financial_item',
736 'financial_trxn_id' => $payment['id'],
738 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
739 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
740 // Check contribution for completed status
741 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
742 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
743 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
744 $this->callAPISuccess('Contribution', 'Delete', [
745 'id' => $contribution['id'],
747 $this->validateAllPayments();
751 * Test create payment api for pay later contribution with partial payment.
753 * https://lab.civicrm.org/dev/financial/issues/69
754 * @throws \CRM_Core_Exception
756 public function testCreatePaymentIncompletePaymentPartialPayment() {
757 $contributionParams = [
758 'total_amount' => 100,
760 'contact_id' => $this->_individualId
,
761 'financial_type_id' => 1,
762 'contribution_status_id' => 2,
764 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
765 $this->callAPISuccess('Payment', 'create', [
766 'contribution_id' => $contribution['id'],
767 'total_amount' => 50,
768 'payment_instrument_id' => 'Cash',
770 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
771 $this->assertCount(1, $payments);
772 $this->validateAllPayments();
776 * Test create payment api for pay later contribution with partial payment.
778 * @throws \CRM_Core_Exception
780 public function testCreatePaymentPayLaterPartialPayment() {
781 $this->createLoggedInUser();
782 $contributionParams = [
783 'total_amount' => 100,
785 'contact_id' => $this->_individualId
,
786 'financial_type_id' => 1,
787 'contribution_status_id' => 2,
790 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
791 //Create partial payment
793 'contribution_id' => $contribution['id'],
794 'total_amount' => 60,
796 $payment = $this->callAPISuccess('Payment', 'create', $params);
799 'total_amount' => 60,
804 $this->checkPaymentResult($payment, $expectedResult);
805 // Check entity financial trxn created properly
807 'entity_id' => $contribution['id'],
808 'entity_table' => 'civicrm_contribution',
809 'financial_trxn_id' => $payment['id'],
811 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
812 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
814 'entity_table' => 'civicrm_financial_item',
815 'financial_trxn_id' => $payment['id'],
817 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
818 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
819 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
820 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
821 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
822 //Create full payment
824 'contribution_id' => $contribution['id'],
825 'total_amount' => 40,
827 // Rename the 'completed' status label first to check that we are not using the labels!
828 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
829 $payment = $this->callAPISuccess('Payment', 'create', $params);
832 'from_financial_account_id' => 7,
833 'to_financial_account_id' => 6,
834 'total_amount' => 40,
839 $this->checkPaymentResult($payment, $expectedResult);
840 // Check entity financial trxn created properly
842 'entity_id' => $contribution['id'],
843 'entity_table' => 'civicrm_contribution',
844 'financial_trxn_id' => $payment['id'],
846 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
847 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
849 'entity_table' => 'civicrm_financial_item',
850 'financial_trxn_id' => $payment['id'],
852 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
853 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
854 // Check contribution for completed status
855 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
856 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn');
857 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
858 $this->callAPISuccess('Contribution', 'Delete', [
859 'id' => $contribution['id'],
861 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
862 $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId
, 'activity_type_id' => 'Payment'], 2);
863 $this->validateAllPayments();
867 * Test that Payment.create uses the to_account of the payment processor.
869 * @throws \CiviCRM_API3_Exception
870 * @throws \CRM_Core_Exception
872 public function testPaymentWithProcessorWithOddFinancialAccount() {
873 $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']);
874 $processor2 = $this->dummyProcessorCreate(['financial_account_id' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id' => 'EFT']);
875 $contributionParams = [
876 'total_amount' => 100,
878 'contact_id' => $this->_individualId
,
879 'financial_type_id' => 1,
880 'contribution_status_id' => 'Pending',
882 $order = $this->callAPISuccess('Order', 'create', $contributionParams);
883 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor->getID(), 'total_amount' => 6, 'contribution_id' => $order['id']]);
884 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor2->getID(), 'total_amount' => 15, 'contribution_id' => $order['id']]);
885 $payments = $this->callAPISuccess('Payment', 'get', ['sequential' => 1, 'contribution_id' => $order['id']])['values'];
886 $this->assertEquals('Deposit Bank Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[0]['to_financial_account_id']));
887 $this->assertEquals('Payment Processor Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[1]['to_financial_account_id']));
888 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[0]['from_financial_account_id']));
889 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[1]['from_financial_account_id']));
890 $this->assertEquals('Cash', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[0]['payment_instrument_id']));
891 $this->assertEquals('EFT', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[1]['payment_instrument_id']));
892 // $order = $this->callAPISuccessGetSingle('Order', ['id' => $processor->getID()]);
893 // $this->assertEquals('Cash', CRM_Core_PseudoConstant::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $order['payment_instrument_id']));
897 * Add a location to our event.
899 * @param int $eventID
901 * @throws \CRM_Core_Exception
903 protected function addLocationToEvent($eventID) {
905 'name' => 'event place',
906 'street_address' => 'streety street',
907 'location_type_id' => 1,
910 // api requires contact_id - perhaps incorrectly but use add to get past that.
911 $address = CRM_Core_BAO_Address
::add($addressParams);
913 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id
]);
914 $this->callAPISuccess('Event', 'create', [
916 'loc_block_id' => $location['id'],
917 'is_show_location' => TRUE,
919 $this->validateAllPayments();
923 * Check the created payment is valid.
925 * This is probably over-testing really since we are repetitively checking a basic function...
927 * @param int $paymentID
928 * @param int $contributionID
931 * @throws \CRM_Core_Exception
933 protected function checkPaymentIsValid($paymentID, $contributionID, $amount = 50) {
934 $payment = $this->callAPISuccess('Payment', 'getsingle', ['financial_trxn_id' => $paymentID]);
935 $this->assertEquals(7, $payment['from_financial_account_id']);
936 $this->assertEquals(6, $payment['to_financial_account_id']);
937 $this->assertEquals(1, $payment['status_id']);
938 $this->assertEquals(1, $payment['is_payment']);
939 $this->assertEquals($amount, $payment['total_amount']);
941 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
942 'entity_id' => $contributionID,
943 'entity_table' => 'civicrm_contribution',
944 'financial_trxn_id' => $payment['id'],
947 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
948 $this->validateAllPayments();