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