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