Merge pull request #15551 from civicrm/5.19
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class api_v3_PaymentTest extends CiviUnitTestCase {
36
37 protected $_individualId;
38
39 protected $_financialTypeId = 1;
40
41 protected $_apiversion;
42
43 public $debug = 0;
44
45 /**
46 * Setup function.
47 */
48 public function setUp() {
49 parent::setUp();
50
51 $this->_apiversion = 3;
52 $this->_individualId = $this->individualCreate();
53 CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
54 }
55
56 /**
57 * Clean up after each test.
58 *
59 * @throws \Exception
60 */
61 public function tearDown() {
62 $this->quickCleanUpFinancialEntities();
63 $this->quickCleanup(['civicrm_uf_match']);
64 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
65 parent::tearDown();
66 }
67
68 /**
69 * Test Get Payment api.
70 */
71 public function testGetPayment() {
72 $p = [
73 'contact_id' => $this->_individualId,
74 'receive_date' => '2010-01-20',
75 'total_amount' => 100.00,
76 'financial_type_id' => $this->_financialTypeId,
77 'trxn_id' => 23456,
78 'contribution_status_id' => 1,
79 ];
80 $contribution = $this->callAPISuccess('contribution', 'create', $p);
81
82 $params = [
83 'contribution_id' => $contribution['id'],
84 'check_permissions' => TRUE,
85 ];
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');
88
89 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviContribute');
90 $payment = $this->callAPISuccess('payment', 'get', $params);
91
92 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
93 $this->assertEquals(1, $payment['count']);
94
95 $expectedResult = [
96 $contribution['id'] => [
97 'total_amount' => 100,
98 'trxn_id' => 23456,
99 'trxn_date' => '2010-01-20 00:00:00',
100 'contribution_id' => $contribution['id'],
101 'is_payment' => 1,
102 ],
103 ];
104 $this->checkPaymentResult($payment, $expectedResult);
105 $this->callAPISuccess('Contribution', 'Delete', [
106 'id' => $contribution['id'],
107 ]);
108 }
109
110 /**
111 * Retrieve Payment using trxn_id.
112 */
113 public function testGetPaymentWithTrxnID() {
114 $this->_individualId2 = $this->individualCreate();
115 $params1 = [
116 'contact_id' => $this->_individualId,
117 'trxn_id' => 111111,
118 'total_amount' => 10,
119 ];
120 $contributionID1 = $this->contributionCreate($params1);
121
122 $params2 = [
123 'contact_id' => $this->_individualId2,
124 'trxn_id' => 222222,
125 'total_amount' => 20,
126 ];
127 $contributionID2 = $this->contributionCreate($params2);
128
129 $paymentParams = ['trxn_id' => 111111];
130 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
131 $expectedResult = [
132 $payment['id'] => [
133 'total_amount' => 10,
134 'trxn_id' => 111111,
135 'status_id' => 1,
136 'is_payment' => 1,
137 'contribution_id' => $contributionID1,
138 ],
139 ];
140 $this->checkPaymentResult($payment, $expectedResult);
141
142 $paymentParams = ['trxn_id' => 222222];
143 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
144 $expectedResult = [
145 $payment['id'] => [
146 'total_amount' => 20,
147 'trxn_id' => 222222,
148 'status_id' => 1,
149 'is_payment' => 1,
150 'contribution_id' => $contributionID2,
151 ],
152 ];
153 $this->checkPaymentResult($payment, $expectedResult);
154 }
155
156 /**
157 * Test email receipt for partial payment.
158 */
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']);
164 $params = [
165 'contribution_id' => $contribution['id'],
166 'total_amount' => 50,
167 'check_number' => '345',
168 'trxn_date' => '2018-08-13 17:57:56',
169 ];
170 $payment = $this->callAPISuccess('payment', 'create', $params);
171 $this->checkPaymentResult($payment, [
172 $payment['id'] => [
173 'from_financial_account_id' => 7,
174 'to_financial_account_id' => 6,
175 'total_amount' => 50,
176 'status_id' => 1,
177 'is_payment' => 1,
178 ],
179 ]);
180
181 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
182 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
183 $mut->checkMailLog([
184 'From: "FIXME" <info@EXAMPLE.ORG>',
185 'Dear Anthony,',
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',
191 'Paid By: Check',
192 'Check Number: 345',
193 'Transaction Date: August 13th, 2018 5:57 PM',
194 'event place',
195 'streety street',
196 ]);
197 $mut->stop();
198 $mut->clearMessages();
199 }
200
201 /**
202 * Test email receipt for partial payment.
203 *
204 * @throws \CRM_Core_Exception
205 */
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();
210
211 $params = [
212 'contribution_id' => $contribution['id'],
213 'total_amount' => 150,
214 ];
215 $payment = $this->callAPISuccess('payment', 'create', $params);
216
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']);
221 $mut->checkMailLog([
222 'From: "FIXME" <info@EXAMPLE.ORG>',
223 'Dear Anthony,',
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 payment.',
229 ]);
230 $mut->stop();
231 $mut->clearMessages();
232 }
233
234 /**
235 * Test email receipt for partial payment.
236 *
237 * @dataProvider getThousandSeparators
238 *
239 * @param string $thousandSeparator
240 */
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',
251 ]);
252
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,
258 ])['values'][0];
259
260 $expected = [
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'),
265 'is_payment' => 1,
266 ];
267 foreach ($expected as $key => $value) {
268 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
269 }
270
271 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
272 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet']);
273 $mut->checkMailLog([
274 'Dear Anthony,',
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',
279 'Paid By: Check',
280 'Transaction Date: November 13th, 2018 12:01 PM',
281 'You Paid: $ 170' . $decimalSeparator . '00',
282 ]);
283 $mut->stop();
284 $mut->clearMessages();
285 }
286
287 /**
288 * Test create payment api with no line item in params
289 */
290 public function testCreatePaymentNoLineItems() {
291 list($lineItems, $contribution) = $this->createParticipantWithContribution();
292
293 //Create partial payment
294 $params = [
295 'contribution_id' => $contribution['id'],
296 'total_amount' => 50,
297 ];
298 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
299 $expectedResult = [
300 $payment['id'] => [
301 'from_financial_account_id' => 7,
302 'to_financial_account_id' => 6,
303 'total_amount' => 50,
304 'status_id' => 1,
305 'is_payment' => 1,
306 ],
307 ];
308 $this->checkPaymentResult($payment, $expectedResult);
309
310 // Check entity financial trxn created properly
311 $params = [
312 'entity_id' => $contribution['id'],
313 'entity_table' => 'civicrm_contribution',
314 'financial_trxn_id' => $payment['id'],
315 ];
316
317 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
318
319 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
320
321 $params = [
322 'entity_table' => 'civicrm_financial_item',
323 'financial_trxn_id' => $payment['id'],
324 ];
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));
329 }
330
331 // Now create payment to complete total amount of contribution
332 $params = [
333 'contribution_id' => $contribution['id'],
334 'total_amount' => 100,
335 ];
336 $payment = $this->callAPISuccess('payment', 'create', $params);
337 $expectedResult = [
338 $payment['id'] => [
339 'from_financial_account_id' => 7,
340 'to_financial_account_id' => 6,
341 'total_amount' => 100,
342 'status_id' => 1,
343 'is_payment' => 1,
344 ],
345 ];
346 $this->checkPaymentResult($payment, $expectedResult);
347 $params = [
348 'entity_table' => 'civicrm_financial_item',
349 'financial_trxn_id' => $payment['id'],
350 ];
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));
355 }
356 // Check contribution for completed status
357 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
358
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'],
363 ];
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'],
369 ]);
370 }
371
372 /**
373 * Function to assert db values
374 */
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);
378 }
379 }
380
381 /**
382 * Test create payment api with line item in params
383 */
384 public function testCreatePaymentLineItems() {
385 list($lineItems, $contribution) = $this->createParticipantWithContribution();
386 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]);
387
388 //Create partial payment by passing line item array is params
389 $params = [
390 'contribution_id' => $contribution['id'],
391 'total_amount' => 50,
392 ];
393 $amounts = [40, 10];
394 foreach ($lineItems['values'] as $id => $ignore) {
395 $params['line_item'][] = [$id => array_pop($amounts)];
396 }
397 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems');
398 $expectedResult = [
399 $payment['id'] => [
400 'from_financial_account_id' => 7,
401 'to_financial_account_id' => 6,
402 'total_amount' => 50,
403 'status_id' => 1,
404 'is_payment' => 1,
405 ],
406 ];
407 $this->checkPaymentResult($payment, $expectedResult);
408
409 // Check entity financial trxn created properly
410 $params = [
411 'entity_id' => $contribution['id'],
412 'entity_table' => 'civicrm_contribution',
413 'financial_trxn_id' => $payment['id'],
414 ];
415
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
417
418 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
419
420 $params = [
421 'entity_table' => 'civicrm_financial_item',
422 'financial_trxn_id' => $payment['id'],
423 ];
424 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
425 $amounts = [40, 10];
426 foreach ($eft['values'] as $value) {
427 $this->assertEquals($value['amount'], array_pop($amounts));
428 }
429
430 // Now create payment to complete total amount of contribution
431 $params = [
432 'contribution_id' => $contribution['id'],
433 'total_amount' => 100,
434 ];
435 $amounts = [80, 20];
436 foreach ($lineItems['values'] as $id => $ignore) {
437 $params['line_item'][] = [$id => array_pop($amounts)];
438 }
439 $payment = $this->callAPISuccess('payment', 'create', $params);
440 $expectedResult = [
441 $payment['id'] => [
442 'from_financial_account_id' => 7,
443 'to_financial_account_id' => 6,
444 'total_amount' => 100,
445 'status_id' => 1,
446 'is_payment' => 1,
447 ],
448 ];
449 $this->checkPaymentResult($payment, $expectedResult);
450 $params = [
451 'entity_table' => 'civicrm_financial_item',
452 'financial_trxn_id' => $payment['id'],
453 ];
454 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
455 $amounts = [80, 20];
456 foreach ($eft['values'] as $value) {
457 $this->assertEquals($value['amount'], array_pop($amounts));
458 }
459 // Check contribution for completed status
460 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
461
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'],
466 ];
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'],
472 ]);
473 }
474
475 /**
476 * Test cancel payment api
477 */
478 public function testCancelPayment() {
479 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
480 list($lineItems, $contribution) = $this->createParticipantWithContribution();
481
482 $params = [
483 'contribution_id' => $contribution['id'],
484 ];
485
486 $payment = $this->callAPISuccess('payment', 'get', $params);
487 $this->assertEquals(1, $payment['count']);
488
489 $cancelParams = [
490 'id' => $payment['id'],
491 'check_permissions' => TRUE,
492 ];
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');
494
495 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
496
497 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
498
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');
504 }
505
506 $this->callAPISuccess('Contribution', 'Delete', [
507 'id' => $contribution['id'],
508 ]);
509 }
510
511 /**
512 * Test delete payment api
513 */
514 public function testDeletePayment() {
515 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
516 list($lineItems, $contribution) = $this->createParticipantWithContribution();
517
518 $params = [
519 'contribution_id' => $contribution['id'],
520 ];
521
522 $payment = $this->callAPISuccess('payment', 'get', $params);
523 $this->assertEquals(1, $payment['count']);
524
525 $deleteParams = [
526 'id' => $payment['id'],
527 'check_permissions' => TRUE,
528 ];
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');
530
531 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute');
532 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
533
534 $payment = $this->callAPISuccess('payment', 'get', $params);
535 $this->assertEquals(0, $payment['count']);
536
537 $this->callAPISuccess('Contribution', 'Delete', [
538 'id' => $contribution['id'],
539 ]);
540 }
541
542 /**
543 * Test update payment api.
544 *
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.
549 *
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
552 * but $200 is paid
553 *
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.
557 *
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.
565 */
566 public function testUpdatePayment() {
567 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
568 list($lineItems, $contribution) = $this->createParticipantWithContribution();
569
570 //Create partial payment by passing line item array is params
571 $params = [
572 'contribution_id' => $contribution['id'],
573 'total_amount' => 50,
574 ];
575
576 $payment = $this->callAPISuccess('payment', 'create', $params);
577 $expectedResult = [
578 $payment['id'] => [
579 'from_financial_account_id' => 7,
580 'to_financial_account_id' => 6,
581 'total_amount' => 50,
582 'status_id' => 1,
583 'is_payment' => 1,
584 ],
585 ];
586 $this->checkPaymentResult($payment, $expectedResult);
587
588 $params = [
589 'entity_table' => 'civicrm_financial_item',
590 'financial_trxn_id' => $payment['id'],
591 ];
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));
596 }
597
598 // update the amount for payment
599 $params = [
600 'contribution_id' => $contribution['id'],
601 'total_amount' => 100,
602 'id' => $payment['id'],
603 'check_permissions' => TRUE,
604 ];
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');
608
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');
611
612 // Check for proportional cancelled payment against lineitems.
613 $minParams = [
614 'entity_table' => 'civicrm_financial_item',
615 'financial_trxn_id' => $payment['id'] - 1,
616 ];
617
618 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
619 $amounts = [-33.33, -16.67];
620
621 foreach ($eft['values'] as $value) {
622 $this->assertEquals($value['amount'], array_pop($amounts));
623 }
624
625 // Check for proportional updated payment against lineitems.
626 $params = [
627 'entity_table' => 'civicrm_financial_item',
628 'financial_trxn_id' => $payment['id'],
629 ];
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));
634 }
635 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
636 $this->assertCount(2, $items);
637 $itemSum = 0;
638 foreach ($items as $item) {
639 $this->assertEquals('civicrm_line_item', $item['entity_table']);
640 $itemSum += $item['amount'];
641 }
642 $this->assertEquals(300, $itemSum);
643
644 $params = [
645 'contribution_id' => $contribution['id'],
646 ];
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');
652
653 // Check entity financial trxn created properly
654 $params = [
655 'entity_id' => $contribution['id'],
656 'entity_table' => 'civicrm_contribution',
657 'financial_trxn_id' => $value['id'],
658 ];
659 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
660 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
661 }
662
663 $this->callAPISuccess('Contribution', 'Delete', [
664 'id' => $contribution['id'],
665 ]);
666 }
667
668 /**
669 * Test create payment api for paylater contribution
670 */
671 public function testCreatePaymentPayLater() {
672 $this->createLoggedInUser();
673 $contributionParams = [
674 'total_amount' => 100,
675 'currency' => 'USD',
676 'contact_id' => $this->_individualId,
677 'financial_type_id' => 1,
678 'contribution_status_id' => 2,
679 'is_pay_later' => 1,
680 ];
681 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
682 //add payment for pay later transaction
683 $params = [
684 'contribution_id' => $contribution['id'],
685 'total_amount' => 100,
686 ];
687 $payment = $this->callAPISuccess('Payment', 'create', $params);
688 $expectedResult = [
689 $payment['id'] => [
690 'from_financial_account_id' => 7,
691 'to_financial_account_id' => 6,
692 'total_amount' => 100,
693 'status_id' => 1,
694 'is_payment' => 1,
695 ],
696 ];
697 $this->checkPaymentResult($payment, $expectedResult);
698 // Check entity financial trxn created properly
699 $params = [
700 'entity_id' => $contribution['id'],
701 'entity_table' => 'civicrm_contribution',
702 'financial_trxn_id' => $payment['id'],
703 ];
704 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
705 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
706 $params = [
707 'entity_table' => 'civicrm_financial_item',
708 'financial_trxn_id' => $payment['id'],
709 ];
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'],
718 ]);
719 }
720
721 /**
722 * Test create payment api for pay later contribution with partial payment.
723 *
724 * https://lab.civicrm.org/dev/financial/issues/69
725 */
726 public function testCreatePaymentIncompletePaymentPartialPayment() {
727 $contributionParams = [
728 'total_amount' => 100,
729 'currency' => 'USD',
730 'contact_id' => $this->_individualId,
731 'financial_type_id' => 1,
732 'contribution_status_id' => 2,
733 ];
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',
739 ]);
740 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
741 $this->assertCount(1, $payments);
742 }
743
744 /**
745 * Test create payment api for pay later contribution with partial payment.
746 */
747 public function testCreatePaymentPayLaterPartialPayment() {
748 $this->createLoggedInUser();
749 $contributionParams = [
750 'total_amount' => 100,
751 'currency' => 'USD',
752 'contact_id' => $this->_individualId,
753 'financial_type_id' => 1,
754 'contribution_status_id' => 2,
755 'is_pay_later' => 1,
756 ];
757 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
758 //Create partial payment
759 $params = [
760 'contribution_id' => $contribution['id'],
761 'total_amount' => 60,
762 ];
763 $payment = $this->callAPISuccess('Payment', 'create', $params);
764 $expectedResult = [
765 $payment['id'] => [
766 'total_amount' => 60,
767 'status_id' => 1,
768 'is_payment' => 1,
769 ],
770 ];
771 $this->checkPaymentResult($payment, $expectedResult);
772 // Check entity financial trxn created properly
773 $params = [
774 'entity_id' => $contribution['id'],
775 'entity_table' => 'civicrm_contribution',
776 'financial_trxn_id' => $payment['id'],
777 ];
778 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
779 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
780 $params = [
781 'entity_table' => 'civicrm_financial_item',
782 'financial_trxn_id' => $payment['id'],
783 ];
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
790 $params = [
791 'contribution_id' => $contribution['id'],
792 'total_amount' => 40,
793 ];
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);
797 $expectedResult = [
798 $payment['id'] => [
799 'from_financial_account_id' => 7,
800 'to_financial_account_id' => 6,
801 'total_amount' => 40,
802 'status_id' => 1,
803 'is_payment' => 1,
804 ],
805 ];
806 $this->checkPaymentResult($payment, $expectedResult);
807 // Check entity financial trxn created properly
808 $params = [
809 'entity_id' => $contribution['id'],
810 'entity_table' => 'civicrm_contribution',
811 'financial_trxn_id' => $payment['id'],
812 ];
813 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
814 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
815 $params = [
816 'entity_table' => 'civicrm_financial_item',
817 'financial_trxn_id' => $payment['id'],
818 ];
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'],
827 ]);
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);
830 }
831
832 /**
833 * Add a location to our event.
834 *
835 * @param int $eventID
836 */
837 protected function addLocationToEvent($eventID) {
838 $addressParams = [
839 'name' => 'event place',
840 'street_address' => 'streety street',
841 'location_type_id' => 1,
842 'is_primary' => 1,
843 ];
844 // api requires contact_id - perhaps incorrectly but use add to get past that.
845 $address = CRM_Core_BAO_Address::add($addressParams);
846
847 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id]);
848 $this->callAPISuccess('Event', 'create', [
849 'id' => $eventID,
850 'loc_block_id' => $location['id'],
851 'is_show_location' => TRUE,
852 ]);
853 }
854
855 }