Merge pull request #15558 from eileenmcnaughton/report_page519
[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
b7f554fe 37 protected $_individualId;
d3b3ad06 38
b7f554fe 39 protected $_financialTypeId = 1;
d3b3ad06 40
b7f554fe 41 protected $_apiversion;
d3b3ad06 42
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();
d3b3ad06 53 CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
b7f554fe
E
54 }
55
56 /**
57 * Clean up after each test.
fda18dc3 58 *
59 * @throws \Exception
b7f554fe
E
60 */
61 public function tearDown() {
62 $this->quickCleanUpFinancialEntities();
d3b3ad06 63 $this->quickCleanup(['civicrm_uf_match']);
eba13f6d 64 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
fda18dc3 65 parent::tearDown();
b7f554fe
E
66 }
67
68 /**
52873538 69 * Test Get Payment api.
b7f554fe
E
70 */
71 public function testGetPayment() {
d3b3ad06 72 $p = [
b7f554fe
E
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,
d3b3ad06 79 ];
b7f554fe
E
80 $contribution = $this->callAPISuccess('contribution', 'create', $p);
81
d3b3ad06 82 $params = [
b7f554fe 83 'contribution_id' => $contribution['id'],
979748a2 84 'check_permissions' => TRUE,
d3b3ad06 85 ];
86 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'administer CiviCRM'];
eba13f6d 87 $payment = $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute');
b7f554fe 88
979748a2 89 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviContribute');
c60d3584 90 $payment = $this->callAPISuccess('payment', 'get', $params);
b7f554fe 91
979748a2 92 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
b7f554fe 93 $this->assertEquals(1, $payment['count']);
979748a2 94
d3b3ad06 95 $expectedResult = [
96 $contribution['id'] => [
c60d3584
PN
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,
d3b3ad06 102 ],
103 ];
a44499b4 104 $this->checkPaymentResult($payment, $expectedResult);
d3b3ad06 105 $this->callAPISuccess('Contribution', 'Delete', [
b7f554fe 106 'id' => $contribution['id'],
d3b3ad06 107 ]);
b7f554fe
E
108 }
109
6045f2b7
JP
110 /**
111 * Retrieve Payment using trxn_id.
112 */
113 public function testGetPaymentWithTrxnID() {
114 $this->_individualId2 = $this->individualCreate();
115 $params1 = [
116 'contact_id' => $this->_individualId,
117 'trxn_id' => 111111,
118 'total_amount' => 10,
119 ];
120 $contributionID1 = $this->contributionCreate($params1);
121
122 $params2 = [
123 'contact_id' => $this->_individualId2,
124 'trxn_id' => 222222,
125 'total_amount' => 20,
126 ];
127 $contributionID2 = $this->contributionCreate($params2);
128
129 $paymentParams = ['trxn_id' => 111111];
130 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
131 $expectedResult = [
132 $payment['id'] => [
133 'total_amount' => 10,
134 'trxn_id' => 111111,
135 'status_id' => 1,
136 'is_payment' => 1,
137 'contribution_id' => $contributionID1,
138 ],
139 ];
140 $this->checkPaymentResult($payment, $expectedResult);
141
142 $paymentParams = ['trxn_id' => 222222];
143 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
144 $expectedResult = [
145 $payment['id'] => [
146 'total_amount' => 20,
147 'trxn_id' => 222222,
148 'status_id' => 1,
149 'is_payment' => 1,
150 'contribution_id' => $contributionID2,
151 ],
152 ];
153 $this->checkPaymentResult($payment, $expectedResult);
154 }
155
a79d2ec2 156 /**
157 * Test email receipt for partial payment.
158 */
159 public function testPaymentEmailReceipt() {
160 $mut = new CiviMailUtils($this);
161 list($lineItems, $contribution) = $this->createParticipantWithContribution();
0fad34a0 162 $event = $this->callAPISuccess('Event', 'get', []);
163 $this->addLocationToEvent($event['id']);
b5a442ed 164 $params = [
a79d2ec2 165 'contribution_id' => $contribution['id'],
166 'total_amount' => 50,
434546ac 167 'check_number' => '345',
168 'trxn_date' => '2018-08-13 17:57:56',
b5a442ed 169 ];
a79d2ec2 170 $payment = $this->callAPISuccess('payment', 'create', $params);
171 $this->checkPaymentResult($payment, [
172 $payment['id'] => [
173 'from_financial_account_id' => 7,
174 'to_financial_account_id' => 6,
175 'total_amount' => 50,
176 'status_id' => 1,
177 'is_payment' => 1,
178 ],
179 ]);
180
181 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
182 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
d3b3ad06 183 $mut->checkMailLog([
44a2f017 184 'From: "FIXME" <info@EXAMPLE.ORG>',
1e477c5b 185 'Dear Anthony,',
a79d2ec2 186 'Total Fees: $ 300.00',
187 'This Payment Amount: $ 50.00',
39b959db
SL
188 //150 was paid in the 1st payment.
189 'Balance Owed: $ 100.00',
a79d2ec2 190 'Event Information and Location',
434546ac 191 'Paid By: Check',
192 'Check Number: 345',
193 'Transaction Date: August 13th, 2018 5:57 PM',
0fad34a0 194 'event place',
195 'streety street',
d3b3ad06 196 ]);
a79d2ec2 197 $mut->stop();
a7b9128b 198 $mut->clearMessages();
a79d2ec2 199 }
200
00ef9b01 201 /**
202 * Test email receipt for partial payment.
1e0f58c7 203 *
204 * @throws \CRM_Core_Exception
00ef9b01 205 */
206 public function testPaymentEmailReceiptFullyPaid() {
207 $mut = new CiviMailUtils($this);
44a2f017 208 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
00ef9b01 209 list($lineItems, $contribution) = $this->createParticipantWithContribution();
210
211 $params = [
212 'contribution_id' => $contribution['id'],
213 'total_amount' => 150,
214 ];
215 $payment = $this->callAPISuccess('payment', 'create', $params);
216
44a2f017 217 // Here we set the email to an invalid email & use check_permissions, domain email should be used.
218 $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
219 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
1604fa93 220 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet', 'Registration Confirmation - Annual CiviCRM meet']);
d3b3ad06 221 $mut->checkMailLog([
44a2f017 222 'From: "FIXME" <info@EXAMPLE.ORG>',
1e477c5b 223 'Dear Anthony,',
00ef9b01 224 'A payment has been received.',
225 'Total Fees: $ 300.00',
226 'This Payment Amount: $ 150.00',
227 'Balance Owed: $ 0.00',
228 'Thank you for completing payment.',
d3b3ad06 229 ]);
00ef9b01 230 $mut->stop();
231 $mut->clearMessages();
232 }
233
b5a442ed 234 /**
235 * Test email receipt for partial payment.
a7b9128b 236 *
237 * @dataProvider getThousandSeparators
238 *
239 * @param string $thousandSeparator
b5a442ed 240 */
a7b9128b 241 public function testRefundEmailReceipt($thousandSeparator) {
242 $this->setCurrencySeparators($thousandSeparator);
243 $decimalSeparator = ($thousandSeparator === ',' ? '.' : ',');
b5a442ed 244 $mut = new CiviMailUtils($this);
245 list($lineItems, $contribution) = $this->createParticipantWithContribution();
246 $this->callAPISuccess('payment', 'create', [
247 'contribution_id' => $contribution['id'],
248 'total_amount' => 50,
249 'check_number' => '345',
250 'trxn_date' => '2018-08-13 17:57:56',
251 ]);
252
253 $payment = $this->callAPISuccess('payment', 'create', [
254 'contribution_id' => $contribution['id'],
255 'total_amount' => -30,
256 'trxn_date' => '2018-11-13 12:01:56',
52746f6a 257 'sequential' => TRUE,
258 ])['values'][0];
b5a442ed 259
52746f6a 260 $expected = [
261 'from_financial_account_id' => 7,
262 'to_financial_account_id' => 6,
263 'total_amount' => -30,
2561fc11 264 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
52746f6a 265 'is_payment' => 1,
266 ];
267 foreach ($expected as $key => $value) {
268 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
269 }
b5a442ed 270
271 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
272 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet']);
d3b3ad06 273 $mut->checkMailLog([
1e477c5b 274 'Dear Anthony,',
00ef9b01 275 'A refund has been issued based on changes in your registration selections.',
a7b9128b 276 'Total Fees: $ 300' . $decimalSeparator . '00',
277 'Refund Amount: $ -30' . $decimalSeparator . '00',
b5a442ed 278 'Event Information and Location',
279 'Paid By: Check',
280 'Transaction Date: November 13th, 2018 12:01 PM',
a7b9128b 281 'You Paid: $ 170' . $decimalSeparator . '00',
d3b3ad06 282 ]);
b5a442ed 283 $mut->stop();
a7b9128b 284 $mut->clearMessages();
b5a442ed 285 }
286
52873538
PN
287 /**
288 * Test create payment api with no line item in params
289 */
b7f554fe 290 public function testCreatePaymentNoLineItems() {
52e3bed0 291 list($lineItems, $contribution) = $this->createParticipantWithContribution();
f5ec2569 292
b7f554fe 293 //Create partial payment
d3b3ad06 294 $params = [
b7f554fe 295 'contribution_id' => $contribution['id'],
52e3bed0 296 'total_amount' => 50,
d3b3ad06 297 ];
8ed3f575 298 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
d3b3ad06 299 $expectedResult = [
300 $payment['id'] => [
c60d3584
PN
301 'from_financial_account_id' => 7,
302 'to_financial_account_id' => 6,
303 'total_amount' => 50,
304 'status_id' => 1,
305 'is_payment' => 1,
d3b3ad06 306 ],
307 ];
52e3bed0 308 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
309
310 // Check entity financial trxn created properly
d3b3ad06 311 $params = [
b7f554fe
E
312 'entity_id' => $contribution['id'],
313 'entity_table' => 'civicrm_contribution',
314 'financial_trxn_id' => $payment['id'],
d3b3ad06 315 ];
b7f554fe
E
316
317 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
318
52e3bed0 319 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
b7f554fe 320
d3b3ad06 321 $params = [
577daeaa
PN
322 'entity_table' => 'civicrm_financial_item',
323 'financial_trxn_id' => $payment['id'],
d3b3ad06 324 ];
577daeaa 325 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 326 $amounts = [33.33, 16.67];
577daeaa
PN
327 foreach ($eft['values'] as $value) {
328 $this->assertEquals($value['amount'], array_pop($amounts));
329 }
330
b7f554fe 331 // Now create payment to complete total amount of contribution
d3b3ad06 332 $params = [
b7f554fe 333 'contribution_id' => $contribution['id'],
52e3bed0 334 'total_amount' => 100,
d3b3ad06 335 ];
c60d3584 336 $payment = $this->callAPISuccess('payment', 'create', $params);
d3b3ad06 337 $expectedResult = [
338 $payment['id'] => [
c60d3584
PN
339 'from_financial_account_id' => 7,
340 'to_financial_account_id' => 6,
341 'total_amount' => 100,
342 'status_id' => 1,
343 'is_payment' => 1,
d3b3ad06 344 ],
345 ];
52e3bed0 346 $this->checkPaymentResult($payment, $expectedResult);
d3b3ad06 347 $params = [
577daeaa
PN
348 'entity_table' => 'civicrm_financial_item',
349 'financial_trxn_id' => $payment['id'],
d3b3ad06 350 ];
577daeaa 351 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 352 $amounts = [66.67, 33.33];
577daeaa
PN
353 foreach ($eft['values'] as $value) {
354 $this->assertEquals($value['amount'], array_pop($amounts));
355 }
b7f554fe 356 // Check contribution for completed status
d3b3ad06 357 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
b7f554fe
E
358
359 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
52e3bed0 360 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
d3b3ad06 361 $paymentParticipant = [
577daeaa 362 'contribution_id' => $contribution['id'],
d3b3ad06 363 ];
577daeaa 364 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
d3b3ad06 365 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
577daeaa 366 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
d3b3ad06 367 $this->callAPISuccess('Contribution', 'Delete', [
b7f554fe 368 'id' => $contribution['id'],
d3b3ad06 369 ]);
b7f554fe 370 }
f5ec2569 371
52e3bed0
PN
372 /**
373 * Function to assert db values
374 */
375 public function checkPaymentResult($payment, $expectedResult) {
c60d3584 376 foreach ($expectedResult[$payment['id']] as $key => $value) {
2561fc11 377 $this->assertEquals($payment['values'][$payment['id']][$key], $value, 'mismatch on ' . $key);
52e3bed0 378 }
52e3bed0
PN
379 }
380
52873538
PN
381 /**
382 * Test create payment api with line item in params
383 */
b7f554fe 384 public function testCreatePaymentLineItems() {
d1f27fcf 385 list($lineItems, $contribution) = $this->createParticipantWithContribution();
d3b3ad06 386 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]);
b7f554fe 387
d1f27fcf 388 //Create partial payment by passing line item array is params
d3b3ad06 389 $params = [
b7f554fe 390 'contribution_id' => $contribution['id'],
d1f27fcf 391 'total_amount' => 50,
d3b3ad06 392 ];
393 $amounts = [40, 10];
d1f27fcf 394 foreach ($lineItems['values'] as $id => $ignore) {
d3b3ad06 395 $params['line_item'][] = [$id => array_pop($amounts)];
d1f27fcf 396 }
8ed3f575 397 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems');
d3b3ad06 398 $expectedResult = [
399 $payment['id'] => [
c60d3584
PN
400 'from_financial_account_id' => 7,
401 'to_financial_account_id' => 6,
402 'total_amount' => 50,
403 'status_id' => 1,
404 'is_payment' => 1,
d3b3ad06 405 ],
406 ];
d1f27fcf 407 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
408
409 // Check entity financial trxn created properly
d3b3ad06 410 $params = [
b7f554fe
E
411 'entity_id' => $contribution['id'],
412 'entity_table' => 'civicrm_contribution',
413 'financial_trxn_id' => $payment['id'],
d3b3ad06 414 ];
b7f554fe
E
415
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
417
d1f27fcf
PN
418 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
419
d3b3ad06 420 $params = [
d1f27fcf
PN
421 'entity_table' => 'civicrm_financial_item',
422 'financial_trxn_id' => $payment['id'],
d3b3ad06 423 ];
d1f27fcf 424 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 425 $amounts = [40, 10];
d1f27fcf
PN
426 foreach ($eft['values'] as $value) {
427 $this->assertEquals($value['amount'], array_pop($amounts));
428 }
b7f554fe
E
429
430 // Now create payment to complete total amount of contribution
d3b3ad06 431 $params = [
b7f554fe 432 'contribution_id' => $contribution['id'],
d1f27fcf 433 'total_amount' => 100,
d3b3ad06 434 ];
435 $amounts = [80, 20];
d1f27fcf 436 foreach ($lineItems['values'] as $id => $ignore) {
d3b3ad06 437 $params['line_item'][] = [$id => array_pop($amounts)];
d1f27fcf 438 }
c60d3584 439 $payment = $this->callAPISuccess('payment', 'create', $params);
d3b3ad06 440 $expectedResult = [
441 $payment['id'] => [
c60d3584
PN
442 'from_financial_account_id' => 7,
443 'to_financial_account_id' => 6,
444 'total_amount' => 100,
445 'status_id' => 1,
446 'is_payment' => 1,
d3b3ad06 447 ],
448 ];
d1f27fcf 449 $this->checkPaymentResult($payment, $expectedResult);
d3b3ad06 450 $params = [
d1f27fcf
PN
451 'entity_table' => 'civicrm_financial_item',
452 'financial_trxn_id' => $payment['id'],
d3b3ad06 453 ];
d1f27fcf 454 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 455 $amounts = [80, 20];
d1f27fcf
PN
456 foreach ($eft['values'] as $value) {
457 $this->assertEquals($value['amount'], array_pop($amounts));
458 }
b7f554fe 459 // Check contribution for completed status
d3b3ad06 460 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
b7f554fe
E
461
462 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
d1f27fcf 463 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
d3b3ad06 464 $paymentParticipant = [
d1f27fcf 465 'contribution_id' => $contribution['id'],
d3b3ad06 466 ];
d1f27fcf 467 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
d3b3ad06 468 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
d1f27fcf 469 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
d3b3ad06 470 $this->callAPISuccess('Contribution', 'Delete', [
b7f554fe 471 'id' => $contribution['id'],
d3b3ad06 472 ]);
b7f554fe
E
473 }
474
db62fd2b
PN
475 /**
476 * Test cancel payment api
2fabb298 477 */
db62fd2b 478 public function testCancelPayment() {
d3b3ad06 479 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
db62fd2b
PN
480 list($lineItems, $contribution) = $this->createParticipantWithContribution();
481
d3b3ad06 482 $params = [
db62fd2b 483 'contribution_id' => $contribution['id'],
d3b3ad06 484 ];
db62fd2b 485
979748a2 486 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b
PN
487 $this->assertEquals(1, $payment['count']);
488
d3b3ad06 489 $cancelParams = [
db62fd2b 490 'id' => $payment['id'],
979748a2 491 'check_permissions' => TRUE,
d3b3ad06 492 ];
4f94e3fa 493 $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 494
eba13f6d 495 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
979748a2 496
8ed3f575 497 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
db62fd2b 498
c60d3584 499 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b 500 $this->assertEquals(2, $payment['count']);
d3b3ad06 501 $amounts = [-150.00, 150.00];
2fabb298 502 foreach ($payment['values'] as $value) {
db62fd2b
PN
503 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
504 }
505
d3b3ad06 506 $this->callAPISuccess('Contribution', 'Delete', [
db62fd2b 507 'id' => $contribution['id'],
d3b3ad06 508 ]);
db62fd2b
PN
509 }
510
ee1f482b
PN
511 /**
512 * Test delete payment api
513 */
514 public function testDeletePayment() {
d3b3ad06 515 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
ee1f482b
PN
516 list($lineItems, $contribution) = $this->createParticipantWithContribution();
517
d3b3ad06 518 $params = [
ee1f482b 519 'contribution_id' => $contribution['id'],
d3b3ad06 520 ];
ee1f482b 521
c60d3584 522 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
523 $this->assertEquals(1, $payment['count']);
524
d3b3ad06 525 $deleteParams = [
ee1f482b 526 'id' => $payment['id'],
979748a2 527 'check_permissions' => TRUE,
d3b3ad06 528 ];
4f94e3fa 529 $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 530
eba13f6d 531 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute');
8ed3f575 532 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
ee1f482b 533
c60d3584 534 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
535 $this->assertEquals(0, $payment['count']);
536
d3b3ad06 537 $this->callAPISuccess('Contribution', 'Delete', [
ee1f482b 538 'id' => $contribution['id'],
d3b3ad06 539 ]);
ee1f482b
PN
540 }
541
4cdb5e2f 542 /**
36057c8d 543 * Test update payment api.
544 *
545 * 1) create a contribution for $300 with a partial payment of $150
546 * - this results in 2 financial transactions. The accounts receivable transaction is linked
547 * via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
548 * so the line items are fully allocated even though they are only half paid.
549 *
550 * 2) add a payment of $50 -
551 * This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
552 * but $200 is paid
553 *
554 * 3) update that payment to be $100
555 * This results in a negative and a positive payment ($50 & $100) - the negative payment results in
556 * financial_items but the positive payment does not.
557 *
558 * The final result is we have
559 * - 1 partly paid contribution of $300
560 * - payment financial_trxns totalling $250
561 * - 1 Accounts receivable financial_trxn totalling $300
562 * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
563 * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
564 * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
4cdb5e2f
PN
565 */
566 public function testUpdatePayment() {
d3b3ad06 567 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
4cdb5e2f
PN
568 list($lineItems, $contribution) = $this->createParticipantWithContribution();
569
570 //Create partial payment by passing line item array is params
d3b3ad06 571 $params = [
4cdb5e2f
PN
572 'contribution_id' => $contribution['id'],
573 'total_amount' => 50,
d3b3ad06 574 ];
4cdb5e2f 575
c60d3584 576 $payment = $this->callAPISuccess('payment', 'create', $params);
d3b3ad06 577 $expectedResult = [
578 $payment['id'] => [
c60d3584
PN
579 'from_financial_account_id' => 7,
580 'to_financial_account_id' => 6,
581 'total_amount' => 50,
582 'status_id' => 1,
583 'is_payment' => 1,
d3b3ad06 584 ],
585 ];
4cdb5e2f
PN
586 $this->checkPaymentResult($payment, $expectedResult);
587
d3b3ad06 588 $params = [
4cdb5e2f
PN
589 'entity_table' => 'civicrm_financial_item',
590 'financial_trxn_id' => $payment['id'],
d3b3ad06 591 ];
4cdb5e2f 592 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 593 $amounts = [33.33, 16.67];
4cdb5e2f
PN
594 foreach ($eft['values'] as $value) {
595 $this->assertEquals($value['amount'], array_pop($amounts));
596 }
597
598 // update the amount for payment
d3b3ad06 599 $params = [
4cdb5e2f
PN
600 'contribution_id' => $contribution['id'],
601 'total_amount' => 100,
602 'id' => $payment['id'],
979748a2 603 'check_permissions' => TRUE,
d3b3ad06 604 ];
36057c8d 605 // @todo - move this permissions test to it's own test - it just confuses here.
606 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
607 $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 608
36057c8d 609 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
8ed3f575 610 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');
4cdb5e2f 611
2a84219e 612 // Check for proportional cancelled payment against lineitems.
d3b3ad06 613 $minParams = [
2a84219e
E
614 'entity_table' => 'civicrm_financial_item',
615 'financial_trxn_id' => $payment['id'] - 1,
d3b3ad06 616 ];
2a84219e
E
617
618 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
d3b3ad06 619 $amounts = [-33.33, -16.67];
2a84219e
E
620
621 foreach ($eft['values'] as $value) {
622 $this->assertEquals($value['amount'], array_pop($amounts));
623 }
624
625 // Check for proportional updated payment against lineitems.
d3b3ad06 626 $params = [
4cdb5e2f
PN
627 'entity_table' => 'civicrm_financial_item',
628 'financial_trxn_id' => $payment['id'],
d3b3ad06 629 ];
4cdb5e2f 630 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
d3b3ad06 631 $amounts = [66.67, 33.33];
4cdb5e2f
PN
632 foreach ($eft['values'] as $value) {
633 $this->assertEquals($value['amount'], array_pop($amounts));
634 }
36057c8d 635 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
636 $this->assertCount(2, $items);
637 $itemSum = 0;
638 foreach ($items as $item) {
639 $this->assertEquals('civicrm_line_item', $item['entity_table']);
640 $itemSum += $item['amount'];
641 }
642 $this->assertEquals(300, $itemSum);
4cdb5e2f 643
d3b3ad06 644 $params = [
4cdb5e2f 645 'contribution_id' => $contribution['id'],
d3b3ad06 646 ];
c60d3584 647 $payment = $this->callAPISuccess('payment', 'get', $params);
d3b3ad06 648 $amounts = [100.00, -50.00, 50.00, 150.00];
4cdb5e2f
PN
649 foreach ($payment['values'] as $value) {
650 $amount = array_pop($amounts);
651 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
652
653 // Check entity financial trxn created properly
d3b3ad06 654 $params = [
4cdb5e2f
PN
655 'entity_id' => $contribution['id'],
656 'entity_table' => 'civicrm_contribution',
657 'financial_trxn_id' => $value['id'],
d3b3ad06 658 ];
4cdb5e2f
PN
659 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
660 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
661 }
662
d3b3ad06 663 $this->callAPISuccess('Contribution', 'Delete', [
4cdb5e2f 664 'id' => $contribution['id'],
d3b3ad06 665 ]);
4cdb5e2f
PN
666 }
667
c60d3584
PN
668 /**
669 * Test create payment api for paylater contribution
670 */
671 public function testCreatePaymentPayLater() {
672 $this->createLoggedInUser();
d3b3ad06 673 $contributionParams = [
c60d3584
PN
674 'total_amount' => 100,
675 'currency' => 'USD',
676 'contact_id' => $this->_individualId,
677 'financial_type_id' => 1,
678 'contribution_status_id' => 2,
679 'is_pay_later' => 1,
d3b3ad06 680 ];
c60d3584 681 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
8ed3f575 682 //add payment for pay later transaction
d3b3ad06 683 $params = [
c60d3584
PN
684 'contribution_id' => $contribution['id'],
685 'total_amount' => 100,
d3b3ad06 686 ];
c60d3584 687 $payment = $this->callAPISuccess('Payment', 'create', $params);
d3b3ad06 688 $expectedResult = [
689 $payment['id'] => [
c60d3584
PN
690 'from_financial_account_id' => 7,
691 'to_financial_account_id' => 6,
692 'total_amount' => 100,
693 'status_id' => 1,
694 'is_payment' => 1,
d3b3ad06 695 ],
696 ];
c60d3584
PN
697 $this->checkPaymentResult($payment, $expectedResult);
698 // Check entity financial trxn created properly
d3b3ad06 699 $params = [
c60d3584
PN
700 'entity_id' => $contribution['id'],
701 'entity_table' => 'civicrm_contribution',
702 'financial_trxn_id' => $payment['id'],
d3b3ad06 703 ];
c60d3584
PN
704 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
705 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
d3b3ad06 706 $params = [
c60d3584
PN
707 'entity_table' => 'civicrm_financial_item',
708 'financial_trxn_id' => $payment['id'],
d3b3ad06 709 ];
c60d3584
PN
710 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
711 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
712 // Check contribution for completed status
d3b3ad06 713 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
c60d3584
PN
714 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
715 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
d3b3ad06 716 $this->callAPISuccess('Contribution', 'Delete', [
c60d3584 717 'id' => $contribution['id'],
d3b3ad06 718 ]);
c60d3584
PN
719 }
720
d5b39a17 721 /**
8d54448e 722 * Test create payment api for pay later contribution with partial payment.
723 *
16b0233c 724 * https://lab.civicrm.org/dev/financial/issues/69
725 */
726 public function testCreatePaymentIncompletePaymentPartialPayment() {
727 $contributionParams = [
728 'total_amount' => 100,
729 'currency' => 'USD',
730 'contact_id' => $this->_individualId,
731 'financial_type_id' => 1,
732 'contribution_status_id' => 2,
733 ];
734 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
735 $this->callAPISuccess('Payment', 'create', [
736 'contribution_id' => $contribution['id'],
737 'total_amount' => 50,
738 'payment_instrument_id' => 'Cash',
739 ]);
740 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
741 $this->assertCount(1, $payments);
742 }
743
744 /**
745 * Test create payment api for pay later contribution with partial payment.
d5b39a17
PN
746 */
747 public function testCreatePaymentPayLaterPartialPayment() {
748 $this->createLoggedInUser();
d3b3ad06 749 $contributionParams = [
d5b39a17
PN
750 'total_amount' => 100,
751 'currency' => 'USD',
752 'contact_id' => $this->_individualId,
753 'financial_type_id' => 1,
754 'contribution_status_id' => 2,
755 'is_pay_later' => 1,
d3b3ad06 756 ];
d5b39a17
PN
757 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
758 //Create partial payment
d3b3ad06 759 $params = [
d5b39a17
PN
760 'contribution_id' => $contribution['id'],
761 'total_amount' => 60,
d3b3ad06 762 ];
d5b39a17 763 $payment = $this->callAPISuccess('Payment', 'create', $params);
d3b3ad06 764 $expectedResult = [
765 $payment['id'] => [
d5b39a17
PN
766 'total_amount' => 60,
767 'status_id' => 1,
768 'is_payment' => 1,
d3b3ad06 769 ],
770 ];
d5b39a17
PN
771 $this->checkPaymentResult($payment, $expectedResult);
772 // Check entity financial trxn created properly
d3b3ad06 773 $params = [
d5b39a17
PN
774 'entity_id' => $contribution['id'],
775 'entity_table' => 'civicrm_contribution',
776 'financial_trxn_id' => $payment['id'],
d3b3ad06 777 ];
d5b39a17
PN
778 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
779 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
d3b3ad06 780 $params = [
d5b39a17
PN
781 'entity_table' => 'civicrm_financial_item',
782 'financial_trxn_id' => $payment['id'],
d3b3ad06 783 ];
d5b39a17
PN
784 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
785 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
d3b3ad06 786 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
d5b39a17
PN
787 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
788 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
789 //Create full payment
d3b3ad06 790 $params = [
d5b39a17
PN
791 'contribution_id' => $contribution['id'],
792 'total_amount' => 40,
d3b3ad06 793 ];
0a201857 794 // Rename the 'completed' status label first to check that we are not using the labels!
795 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
d5b39a17 796 $payment = $this->callAPISuccess('Payment', 'create', $params);
d3b3ad06 797 $expectedResult = [
798 $payment['id'] => [
d5b39a17
PN
799 'from_financial_account_id' => 7,
800 'to_financial_account_id' => 6,
801 'total_amount' => 40,
802 'status_id' => 1,
803 'is_payment' => 1,
d3b3ad06 804 ],
805 ];
d5b39a17
PN
806 $this->checkPaymentResult($payment, $expectedResult);
807 // Check entity financial trxn created properly
d3b3ad06 808 $params = [
d5b39a17
PN
809 'entity_id' => $contribution['id'],
810 'entity_table' => 'civicrm_contribution',
811 'financial_trxn_id' => $payment['id'],
d3b3ad06 812 ];
d5b39a17
PN
813 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
814 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
d3b3ad06 815 $params = [
d5b39a17
PN
816 'entity_table' => 'civicrm_financial_item',
817 'financial_trxn_id' => $payment['id'],
d3b3ad06 818 ];
d5b39a17
PN
819 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
820 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
821 // Check contribution for completed status
d3b3ad06 822 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
0a201857 823 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn');
d5b39a17 824 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
d3b3ad06 825 $this->callAPISuccess('Contribution', 'Delete', [
d5b39a17 826 'id' => $contribution['id'],
d3b3ad06 827 ]);
0a201857 828 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
8d54448e 829 $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId, 'activity_type_id' => 'Payment'], 2);
d5b39a17
PN
830 }
831
0fad34a0 832 /**
833 * Add a location to our event.
834 *
835 * @param int $eventID
836 */
837 protected function addLocationToEvent($eventID) {
838 $addressParams = [
839 'name' => 'event place',
840 'street_address' => 'streety street',
841 'location_type_id' => 1,
842 'is_primary' => 1,
843 ];
844 // api requires contact_id - perhaps incorrectly but use add to get past that.
845 $address = CRM_Core_BAO_Address::add($addressParams);
846
847 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id]);
848 $this->callAPISuccess('Event', 'create', [
849 'id' => $eventID,
850 'loc_block_id' => $location['id'],
851 'is_show_location' => TRUE,
852 ]);
853 }
854
b7f554fe 855}