Merge pull request #13926 from pradpnayak/NoticeErrorProfile
[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',
52746f6a 200 'sequential' => TRUE,
201 ])['values'][0];
b5a442ed 202
52746f6a 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 }
b5a442ed 213
214 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
215 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet']);
216 $mut->checkMailLog(array(
1e477c5b 217 'Dear Anthony,',
00ef9b01 218 'A refund has been issued based on changes in your registration selections.',
a7b9128b 219 'Total Fees: $ 300' . $decimalSeparator . '00',
220 'Refund Amount: $ -30' . $decimalSeparator . '00',
b5a442ed 221 'Event Information and Location',
222 'Paid By: Check',
223 'Transaction Date: November 13th, 2018 12:01 PM',
a7b9128b 224 'You Paid: $ 170' . $decimalSeparator . '00',
b5a442ed 225 ));
226 $mut->stop();
a7b9128b 227 $mut->clearMessages();
b5a442ed 228 }
229
52873538
PN
230 /**
231 * Test create payment api with no line item in params
232 */
b7f554fe 233 public function testCreatePaymentNoLineItems() {
52e3bed0 234 list($lineItems, $contribution) = $this->createParticipantWithContribution();
f5ec2569 235
b7f554fe
E
236 //Create partial payment
237 $params = array(
238 'contribution_id' => $contribution['id'],
52e3bed0 239 'total_amount' => 50,
b7f554fe 240 );
8ed3f575 241 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
52e3bed0 242 $expectedResult = array(
c60d3584
PN
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 ),
52e3bed0
PN
250 );
251 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
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
52e3bed0 262 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
b7f554fe 263
577daeaa
PN
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
b7f554fe
E
274 // Now create payment to complete total amount of contribution
275 $params = array(
276 'contribution_id' => $contribution['id'],
52e3bed0 277 'total_amount' => 100,
b7f554fe 278 );
c60d3584 279 $payment = $this->callAPISuccess('payment', 'create', $params);
52e3bed0 280 $expectedResult = array(
c60d3584
PN
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 ),
52e3bed0
PN
288 );
289 $this->checkPaymentResult($payment, $expectedResult);
577daeaa
PN
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 }
b7f554fe
E
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');
52e3bed0 303 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
577daeaa
PN
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');
b7f554fe
E
310 $this->callAPISuccess('Contribution', 'Delete', array(
311 'id' => $contribution['id'],
312 ));
313 }
f5ec2569 314
52e3bed0
PN
315 /**
316 * Function to assert db values
317 */
318 public function checkPaymentResult($payment, $expectedResult) {
c60d3584 319 foreach ($expectedResult[$payment['id']] as $key => $value) {
52e3bed0
PN
320 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
321 }
52e3bed0
PN
322 }
323
52873538
PN
324 /**
325 * Test create payment api with line item in params
326 */
b7f554fe 327 public function testCreatePaymentLineItems() {
d1f27fcf
PN
328 list($lineItems, $contribution) = $this->createParticipantWithContribution();
329 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
b7f554fe 330
d1f27fcf 331 //Create partial payment by passing line item array is params
b7f554fe
E
332 $params = array(
333 'contribution_id' => $contribution['id'],
d1f27fcf 334 'total_amount' => 50,
b7f554fe 335 );
d1f27fcf
PN
336 $amounts = array(40, 10);
337 foreach ($lineItems['values'] as $id => $ignore) {
338 $params['line_item'][] = array($id => array_pop($amounts));
339 }
8ed3f575 340 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems');
d1f27fcf 341 $expectedResult = array(
c60d3584
PN
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 ),
d1f27fcf
PN
349 );
350 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
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
d1f27fcf
PN
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 }
b7f554fe
E
372
373 // Now create payment to complete total amount of contribution
374 $params = array(
375 'contribution_id' => $contribution['id'],
d1f27fcf 376 'total_amount' => 100,
b7f554fe 377 );
d1f27fcf
PN
378 $amounts = array(80, 20);
379 foreach ($lineItems['values'] as $id => $ignore) {
380 $params['line_item'][] = array($id => array_pop($amounts));
381 }
c60d3584 382 $payment = $this->callAPISuccess('payment', 'create', $params);
d1f27fcf 383 $expectedResult = array(
c60d3584
PN
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 ),
d1f27fcf
PN
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 }
b7f554fe
E
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');
d1f27fcf
PN
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');
b7f554fe
E
413 $this->callAPISuccess('Contribution', 'Delete', array(
414 'id' => $contribution['id'],
415 ));
416 }
417
db62fd2b
PN
418 /**
419 * Test cancel payment api
2fabb298 420 */
db62fd2b 421 public function testCancelPayment() {
979748a2 422 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
db62fd2b
PN
423 list($lineItems, $contribution) = $this->createParticipantWithContribution();
424
425 $params = array(
426 'contribution_id' => $contribution['id'],
427 );
428
979748a2 429 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b
PN
430 $this->assertEquals(1, $payment['count']);
431
432 $cancelParams = array(
433 'id' => $payment['id'],
979748a2 434 'check_permissions' => TRUE,
db62fd2b 435 );
4f94e3fa 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');
979748a2 437
eba13f6d 438 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
979748a2 439
8ed3f575 440 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
db62fd2b 441
c60d3584 442 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b
PN
443 $this->assertEquals(2, $payment['count']);
444 $amounts = array(-150.00, 150.00);
2fabb298 445 foreach ($payment['values'] as $value) {
db62fd2b
PN
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
ee1f482b
PN
454 /**
455 * Test delete payment api
456 */
457 public function testDeletePayment() {
979748a2 458 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
ee1f482b
PN
459 list($lineItems, $contribution) = $this->createParticipantWithContribution();
460
461 $params = array(
462 'contribution_id' => $contribution['id'],
463 );
464
c60d3584 465 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
466 $this->assertEquals(1, $payment['count']);
467
979748a2 468 $deleteParams = array(
ee1f482b 469 'id' => $payment['id'],
979748a2 470 'check_permissions' => TRUE,
ee1f482b 471 );
4f94e3fa 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');
979748a2 473
eba13f6d 474 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute');
8ed3f575 475 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
ee1f482b 476
c60d3584 477 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
478 $this->assertEquals(0, $payment['count']);
479
480 $this->callAPISuccess('Contribution', 'Delete', array(
481 'id' => $contribution['id'],
482 ));
483 }
484
4cdb5e2f
PN
485 /**
486 * Test update payment api
487 */
488 public function testUpdatePayment() {
979748a2 489 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute', 'edit contributions');
4cdb5e2f
PN
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
c60d3584 498 $payment = $this->callAPISuccess('payment', 'create', $params);
4cdb5e2f 499 $expectedResult = array(
c60d3584
PN
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 ),
4cdb5e2f
PN
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 }
979748a2 519 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
4cdb5e2f
PN
520
521 // update the amount for payment
522 $params = array(
523 'contribution_id' => $contribution['id'],
524 'total_amount' => 100,
525 'id' => $payment['id'],
979748a2 526 'check_permissions' => TRUE,
4cdb5e2f 527 );
4f94e3fa 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');
979748a2 529
eba13f6d 530 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
8ed3f575 531 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');
4cdb5e2f 532
2a84219e
E
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.
4cdb5e2f
PN
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 );
c60d3584 560 $payment = $this->callAPISuccess('payment', 'get', $params);
4cdb5e2f
PN
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
c60d3584
PN
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);
8ed3f575 595 //add payment for pay later transaction
c60d3584
PN
596 $params = array(
597 'contribution_id' => $contribution['id'],
598 'total_amount' => 100,
599 );
600 $payment = $this->callAPISuccess('Payment', 'create', $params);
601 $expectedResult = array(
d5b39a17 602 $payment['id'] => array(
c60d3584
PN
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
d5b39a17 634 /**
fa7a1ebd 635 * Test create payment api for paylater contribution with partial payment.
d5b39a17
PN
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
0fad34a0 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
b7f554fe 741}