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