[NFC] array formatting in api_v3_paymentTest class
[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 public function tearDown() {
60 $this->quickCleanUpFinancialEntities();
61 $this->quickCleanup(['civicrm_uf_match']);
62 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
63 }
64
65 /**
66 * Test Get Payment api.
67 */
68 public function testGetPayment() {
69 $p = [
70 'contact_id' => $this->_individualId,
71 'receive_date' => '2010-01-20',
72 'total_amount' => 100.00,
73 'financial_type_id' => $this->_financialTypeId,
74 'trxn_id' => 23456,
75 'contribution_status_id' => 1,
76 ];
77 $contribution = $this->callAPISuccess('contribution', 'create', $p);
78
79 $params = [
80 'contribution_id' => $contribution['id'],
81 'check_permissions' => TRUE,
82 ];
83 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'administer CiviCRM'];
84 $payment = $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute');
85
86 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviContribute');
87 $payment = $this->callAPISuccess('payment', 'get', $params);
88
89 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
90 $this->assertEquals(1, $payment['count']);
91
92 $expectedResult = [
93 $contribution['id'] => [
94 'total_amount' => 100,
95 'trxn_id' => 23456,
96 'trxn_date' => '2010-01-20 00:00:00',
97 'contribution_id' => $contribution['id'],
98 'is_payment' => 1,
99 ],
100 ];
101 $this->checkPaymentResult($payment, $expectedResult);
102 $this->callAPISuccess('Contribution', 'Delete', [
103 'id' => $contribution['id'],
104 ]);
105 }
106
107 /**
108 * Test email receipt for partial payment.
109 */
110 public function testPaymentEmailReceipt() {
111 $mut = new CiviMailUtils($this);
112 list($lineItems, $contribution) = $this->createParticipantWithContribution();
113 $event = $this->callAPISuccess('Event', 'get', []);
114 $this->addLocationToEvent($event['id']);
115 $params = [
116 'contribution_id' => $contribution['id'],
117 'total_amount' => 50,
118 'check_number' => '345',
119 'trxn_date' => '2018-08-13 17:57:56',
120 ];
121 $payment = $this->callAPISuccess('payment', 'create', $params);
122 $this->checkPaymentResult($payment, [
123 $payment['id'] => [
124 'from_financial_account_id' => 7,
125 'to_financial_account_id' => 6,
126 'total_amount' => 50,
127 'status_id' => 1,
128 'is_payment' => 1,
129 ],
130 ]);
131
132 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
133 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
134 $mut->checkMailLog([
135 'Dear Anthony,',
136 'Total Fees: $ 300.00',
137 'This Payment Amount: $ 50.00',
138 //150 was paid in the 1st payment.
139 'Balance Owed: $ 100.00',
140 'Event Information and Location',
141 'Paid By: Check',
142 'Check Number: 345',
143 'Transaction Date: August 13th, 2018 5:57 PM',
144 'event place',
145 'streety street',
146 ]);
147 $mut->stop();
148 $mut->clearMessages();
149 }
150
151 /**
152 * Test email receipt for partial payment.
153 */
154 public function testPaymentEmailReceiptFullyPaid() {
155 $mut = new CiviMailUtils($this);
156 list($lineItems, $contribution) = $this->createParticipantWithContribution();
157
158 $params = [
159 'contribution_id' => $contribution['id'],
160 'total_amount' => 150,
161 ];
162 $payment = $this->callAPISuccess('payment', 'create', $params);
163
164 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
165 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
166 $mut->checkMailLog([
167 'Dear Anthony,',
168 'A payment has been received.',
169 'Total Fees: $ 300.00',
170 'This Payment Amount: $ 150.00',
171 'Balance Owed: $ 0.00',
172 'Thank you for completing payment.',
173 ]);
174 $mut->stop();
175 $mut->clearMessages();
176 }
177
178 /**
179 * Test email receipt for partial payment.
180 *
181 * @dataProvider getThousandSeparators
182 *
183 * @param string $thousandSeparator
184 */
185 public function testRefundEmailReceipt($thousandSeparator) {
186 $this->setCurrencySeparators($thousandSeparator);
187 $decimalSeparator = ($thousandSeparator === ',' ? '.' : ',');
188 $mut = new CiviMailUtils($this);
189 list($lineItems, $contribution) = $this->createParticipantWithContribution();
190 $this->callAPISuccess('payment', 'create', [
191 'contribution_id' => $contribution['id'],
192 'total_amount' => 50,
193 'check_number' => '345',
194 'trxn_date' => '2018-08-13 17:57:56',
195 ]);
196
197 $payment = $this->callAPISuccess('payment', 'create', [
198 'contribution_id' => $contribution['id'],
199 'total_amount' => -30,
200 'trxn_date' => '2018-11-13 12:01:56',
201 'sequential' => TRUE,
202 ])['values'][0];
203
204 $expected = [
205 'from_financial_account_id' => 7,
206 'to_financial_account_id' => 6,
207 'total_amount' => -30,
208 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
209 'is_payment' => 1,
210 ];
211 foreach ($expected as $key => $value) {
212 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
213 }
214
215 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
216 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet']);
217 $mut->checkMailLog([
218 'Dear Anthony,',
219 'A refund has been issued based on changes in your registration selections.',
220 'Total Fees: $ 300' . $decimalSeparator . '00',
221 'Refund Amount: $ -30' . $decimalSeparator . '00',
222 'Event Information and Location',
223 'Paid By: Check',
224 'Transaction Date: November 13th, 2018 12:01 PM',
225 'You Paid: $ 170' . $decimalSeparator . '00',
226 ]);
227 $mut->stop();
228 $mut->clearMessages();
229 }
230
231 /**
232 * Test create payment api with no line item in params
233 */
234 public function testCreatePaymentNoLineItems() {
235 list($lineItems, $contribution) = $this->createParticipantWithContribution();
236
237 //Create partial payment
238 $params = [
239 'contribution_id' => $contribution['id'],
240 'total_amount' => 50,
241 ];
242 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
243 $expectedResult = [
244 $payment['id'] => [
245 'from_financial_account_id' => 7,
246 'to_financial_account_id' => 6,
247 'total_amount' => 50,
248 'status_id' => 1,
249 'is_payment' => 1,
250 ],
251 ];
252 $this->checkPaymentResult($payment, $expectedResult);
253
254 // Check entity financial trxn created properly
255 $params = [
256 'entity_id' => $contribution['id'],
257 'entity_table' => 'civicrm_contribution',
258 'financial_trxn_id' => $payment['id'],
259 ];
260
261 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
262
263 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
264
265 $params = [
266 'entity_table' => 'civicrm_financial_item',
267 'financial_trxn_id' => $payment['id'],
268 ];
269 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
270 $amounts = [33.33, 16.67];
271 foreach ($eft['values'] as $value) {
272 $this->assertEquals($value['amount'], array_pop($amounts));
273 }
274
275 // Now create payment to complete total amount of contribution
276 $params = [
277 'contribution_id' => $contribution['id'],
278 'total_amount' => 100,
279 ];
280 $payment = $this->callAPISuccess('payment', 'create', $params);
281 $expectedResult = [
282 $payment['id'] => [
283 'from_financial_account_id' => 7,
284 'to_financial_account_id' => 6,
285 'total_amount' => 100,
286 'status_id' => 1,
287 'is_payment' => 1,
288 ],
289 ];
290 $this->checkPaymentResult($payment, $expectedResult);
291 $params = [
292 'entity_table' => 'civicrm_financial_item',
293 'financial_trxn_id' => $payment['id'],
294 ];
295 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
296 $amounts = [66.67, 33.33];
297 foreach ($eft['values'] as $value) {
298 $this->assertEquals($value['amount'], array_pop($amounts));
299 }
300 // Check contribution for completed status
301 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
302
303 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
304 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
305 $paymentParticipant = [
306 'contribution_id' => $contribution['id'],
307 ];
308 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
309 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
310 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
311 $this->callAPISuccess('Contribution', 'Delete', [
312 'id' => $contribution['id'],
313 ]);
314 }
315
316 /**
317 * Function to assert db values
318 */
319 public function checkPaymentResult($payment, $expectedResult) {
320 foreach ($expectedResult[$payment['id']] as $key => $value) {
321 $this->assertEquals($payment['values'][$payment['id']][$key], $value, 'mismatch on ' . $key);
322 }
323 }
324
325 /**
326 * Test create payment api with line item in params
327 */
328 public function testCreatePaymentLineItems() {
329 list($lineItems, $contribution) = $this->createParticipantWithContribution();
330 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]);
331
332 //Create partial payment by passing line item array is params
333 $params = [
334 'contribution_id' => $contribution['id'],
335 'total_amount' => 50,
336 ];
337 $amounts = [40, 10];
338 foreach ($lineItems['values'] as $id => $ignore) {
339 $params['line_item'][] = [$id => array_pop($amounts)];
340 }
341 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems');
342 $expectedResult = [
343 $payment['id'] => [
344 'from_financial_account_id' => 7,
345 'to_financial_account_id' => 6,
346 'total_amount' => 50,
347 'status_id' => 1,
348 'is_payment' => 1,
349 ],
350 ];
351 $this->checkPaymentResult($payment, $expectedResult);
352
353 // Check entity financial trxn created properly
354 $params = [
355 'entity_id' => $contribution['id'],
356 'entity_table' => 'civicrm_contribution',
357 'financial_trxn_id' => $payment['id'],
358 ];
359
360 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
361
362 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
363
364 $params = [
365 'entity_table' => 'civicrm_financial_item',
366 'financial_trxn_id' => $payment['id'],
367 ];
368 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
369 $amounts = [40, 10];
370 foreach ($eft['values'] as $value) {
371 $this->assertEquals($value['amount'], array_pop($amounts));
372 }
373
374 // Now create payment to complete total amount of contribution
375 $params = [
376 'contribution_id' => $contribution['id'],
377 'total_amount' => 100,
378 ];
379 $amounts = [80, 20];
380 foreach ($lineItems['values'] as $id => $ignore) {
381 $params['line_item'][] = [$id => array_pop($amounts)];
382 }
383 $payment = $this->callAPISuccess('payment', 'create', $params);
384 $expectedResult = [
385 $payment['id'] => [
386 'from_financial_account_id' => 7,
387 'to_financial_account_id' => 6,
388 'total_amount' => 100,
389 'status_id' => 1,
390 'is_payment' => 1,
391 ],
392 ];
393 $this->checkPaymentResult($payment, $expectedResult);
394 $params = [
395 'entity_table' => 'civicrm_financial_item',
396 'financial_trxn_id' => $payment['id'],
397 ];
398 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
399 $amounts = [80, 20];
400 foreach ($eft['values'] as $value) {
401 $this->assertEquals($value['amount'], array_pop($amounts));
402 }
403 // Check contribution for completed status
404 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
405
406 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
407 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
408 $paymentParticipant = [
409 'contribution_id' => $contribution['id'],
410 ];
411 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
412 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
413 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
414 $this->callAPISuccess('Contribution', 'Delete', [
415 'id' => $contribution['id'],
416 ]);
417 }
418
419 /**
420 * Test cancel payment api
421 */
422 public function testCancelPayment() {
423 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
424 list($lineItems, $contribution) = $this->createParticipantWithContribution();
425
426 $params = [
427 'contribution_id' => $contribution['id'],
428 ];
429
430 $payment = $this->callAPISuccess('payment', 'get', $params);
431 $this->assertEquals(1, $payment['count']);
432
433 $cancelParams = [
434 'id' => $payment['id'],
435 'check_permissions' => TRUE,
436 ];
437 $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');
438
439 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
440
441 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
442
443 $payment = $this->callAPISuccess('payment', 'get', $params);
444 $this->assertEquals(2, $payment['count']);
445 $amounts = [-150.00, 150.00];
446 foreach ($payment['values'] as $value) {
447 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
448 }
449
450 $this->callAPISuccess('Contribution', 'Delete', [
451 'id' => $contribution['id'],
452 ]);
453 }
454
455 /**
456 * Test delete payment api
457 */
458 public function testDeletePayment() {
459 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
460 list($lineItems, $contribution) = $this->createParticipantWithContribution();
461
462 $params = [
463 'contribution_id' => $contribution['id'],
464 ];
465
466 $payment = $this->callAPISuccess('payment', 'get', $params);
467 $this->assertEquals(1, $payment['count']);
468
469 $deleteParams = [
470 'id' => $payment['id'],
471 'check_permissions' => TRUE,
472 ];
473 $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');
474
475 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute');
476 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
477
478 $payment = $this->callAPISuccess('payment', 'get', $params);
479 $this->assertEquals(0, $payment['count']);
480
481 $this->callAPISuccess('Contribution', 'Delete', [
482 'id' => $contribution['id'],
483 ]);
484 }
485
486 /**
487 * Test update payment api
488 */
489 public function testUpdatePayment() {
490 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
491 list($lineItems, $contribution) = $this->createParticipantWithContribution();
492
493 //Create partial payment by passing line item array is params
494 $params = [
495 'contribution_id' => $contribution['id'],
496 'total_amount' => 50,
497 ];
498
499 $payment = $this->callAPISuccess('payment', 'create', $params);
500 $expectedResult = [
501 $payment['id'] => [
502 'from_financial_account_id' => 7,
503 'to_financial_account_id' => 6,
504 'total_amount' => 50,
505 'status_id' => 1,
506 'is_payment' => 1,
507 ],
508 ];
509 $this->checkPaymentResult($payment, $expectedResult);
510
511 $params = [
512 'entity_table' => 'civicrm_financial_item',
513 'financial_trxn_id' => $payment['id'],
514 ];
515 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
516 $amounts = [33.33, 16.67];
517 foreach ($eft['values'] as $value) {
518 $this->assertEquals($value['amount'], array_pop($amounts));
519 }
520 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
521
522 // update the amount for payment
523 $params = [
524 'contribution_id' => $contribution['id'],
525 'total_amount' => 100,
526 'id' => $payment['id'],
527 'check_permissions' => TRUE,
528 ];
529 $payment = $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
530
531 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
532 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');
533
534 // Check for proportional cancelled payment against lineitems.
535 $minParams = [
536 'entity_table' => 'civicrm_financial_item',
537 'financial_trxn_id' => $payment['id'] - 1,
538 ];
539
540 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
541 $amounts = [-33.33, -16.67];
542
543 foreach ($eft['values'] as $value) {
544 $this->assertEquals($value['amount'], array_pop($amounts));
545 }
546
547 // Check for proportional updated payment against lineitems.
548 $params = [
549 'entity_table' => 'civicrm_financial_item',
550 'financial_trxn_id' => $payment['id'],
551 ];
552 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
553 $amounts = [66.67, 33.33];
554 foreach ($eft['values'] as $value) {
555 $this->assertEquals($value['amount'], array_pop($amounts));
556 }
557
558 $params = [
559 'contribution_id' => $contribution['id'],
560 ];
561 $payment = $this->callAPISuccess('payment', 'get', $params);
562 $amounts = [100.00, -50.00, 50.00, 150.00];
563 foreach ($payment['values'] as $value) {
564 $amount = array_pop($amounts);
565 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
566
567 // Check entity financial trxn created properly
568 $params = [
569 'entity_id' => $contribution['id'],
570 'entity_table' => 'civicrm_contribution',
571 'financial_trxn_id' => $value['id'],
572 ];
573 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
574 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
575 }
576
577 $this->callAPISuccess('Contribution', 'Delete', [
578 'id' => $contribution['id'],
579 ]);
580 }
581
582 /**
583 * Test create payment api for paylater contribution
584 */
585 public function testCreatePaymentPayLater() {
586 $this->createLoggedInUser();
587 $contributionParams = [
588 'total_amount' => 100,
589 'currency' => 'USD',
590 'contact_id' => $this->_individualId,
591 'financial_type_id' => 1,
592 'contribution_status_id' => 2,
593 'is_pay_later' => 1,
594 ];
595 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
596 //add payment for pay later transaction
597 $params = [
598 'contribution_id' => $contribution['id'],
599 'total_amount' => 100,
600 ];
601 $payment = $this->callAPISuccess('Payment', 'create', $params);
602 $expectedResult = [
603 $payment['id'] => [
604 'from_financial_account_id' => 7,
605 'to_financial_account_id' => 6,
606 'total_amount' => 100,
607 'status_id' => 1,
608 'is_payment' => 1,
609 ],
610 ];
611 $this->checkPaymentResult($payment, $expectedResult);
612 // Check entity financial trxn created properly
613 $params = [
614 'entity_id' => $contribution['id'],
615 'entity_table' => 'civicrm_contribution',
616 'financial_trxn_id' => $payment['id'],
617 ];
618 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
619 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
620 $params = [
621 'entity_table' => 'civicrm_financial_item',
622 'financial_trxn_id' => $payment['id'],
623 ];
624 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
625 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
626 // Check contribution for completed status
627 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
628 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
629 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
630 $this->callAPISuccess('Contribution', 'Delete', [
631 'id' => $contribution['id'],
632 ]);
633 }
634
635 /**
636 * Test create payment api for paylater contribution with partial payment.
637 */
638 public function testCreatePaymentPayLaterPartialPayment() {
639 $this->createLoggedInUser();
640 $contributionParams = [
641 'total_amount' => 100,
642 'currency' => 'USD',
643 'contact_id' => $this->_individualId,
644 'financial_type_id' => 1,
645 'contribution_status_id' => 2,
646 'is_pay_later' => 1,
647 ];
648 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
649 //Create partial payment
650 $params = [
651 'contribution_id' => $contribution['id'],
652 'total_amount' => 60,
653 ];
654 $payment = $this->callAPISuccess('Payment', 'create', $params);
655 $expectedResult = [
656 $payment['id'] => [
657 'total_amount' => 60,
658 'status_id' => 1,
659 'is_payment' => 1,
660 ],
661 ];
662 $this->checkPaymentResult($payment, $expectedResult);
663 // Check entity financial trxn created properly
664 $params = [
665 'entity_id' => $contribution['id'],
666 'entity_table' => 'civicrm_contribution',
667 'financial_trxn_id' => $payment['id'],
668 ];
669 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
670 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
671 $params = [
672 'entity_table' => 'civicrm_financial_item',
673 'financial_trxn_id' => $payment['id'],
674 ];
675 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
676 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
677 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
678 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
679 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
680 //Create full payment
681 $params = [
682 'contribution_id' => $contribution['id'],
683 'total_amount' => 40,
684 ];
685 // Rename the 'completed' status label first to check that we are not using the labels!
686 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
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' => 40,
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'], 40);
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'], 40);
712 // Check contribution for completed status
713 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
714 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn');
715 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
716 $this->callAPISuccess('Contribution', 'Delete', [
717 'id' => $contribution['id'],
718 ]);
719 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
720
721 }
722
723 /**
724 * Add a location to our event.
725 *
726 * @param int $eventID
727 */
728 protected function addLocationToEvent($eventID) {
729 $addressParams = [
730 'name' => 'event place',
731 'street_address' => 'streety street',
732 'location_type_id' => 1,
733 'is_primary' => 1,
734 ];
735 // api requires contact_id - perhaps incorrectly but use add to get past that.
736 $address = CRM_Core_BAO_Address::add($addressParams);
737
738 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id]);
739 $this->callAPISuccess('Event', 'create', [
740 'id' => $eventID,
741 'loc_block_id' => $location['id'],
742 'is_show_location' => TRUE,
743 ]);
744 }
745
746 }