Merge pull request #11086 from agileware/CRM-21277
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentTest.php
CommitLineData
b7f554fe
E
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
52873538
PN
107 /**
108 * Test create payment api with no line item in params
109 */
b7f554fe 110 public function testCreatePaymentNoLineItems() {
52e3bed0 111 list($lineItems, $contribution) = $this->createParticipantWithContribution();
f5ec2569 112
b7f554fe
E
113 //Create partial payment
114 $params = array(
115 'contribution_id' => $contribution['id'],
52e3bed0 116 'total_amount' => 50,
b7f554fe 117 );
8ed3f575 118 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
52e3bed0 119 $expectedResult = array(
c60d3584
PN
120 $payment['id'] => array(
121 'from_financial_account_id' => 7,
122 'to_financial_account_id' => 6,
123 'total_amount' => 50,
124 'status_id' => 1,
125 'is_payment' => 1,
126 ),
52e3bed0
PN
127 );
128 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
129
130 // Check entity financial trxn created properly
131 $params = array(
132 'entity_id' => $contribution['id'],
133 'entity_table' => 'civicrm_contribution',
134 'financial_trxn_id' => $payment['id'],
135 );
136
137 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
138
52e3bed0 139 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
b7f554fe 140
577daeaa
PN
141 $params = array(
142 'entity_table' => 'civicrm_financial_item',
143 'financial_trxn_id' => $payment['id'],
144 );
145 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
146 $amounts = array(33.33, 16.67);
147 foreach ($eft['values'] as $value) {
148 $this->assertEquals($value['amount'], array_pop($amounts));
149 }
150
b7f554fe
E
151 // Now create payment to complete total amount of contribution
152 $params = array(
153 'contribution_id' => $contribution['id'],
52e3bed0 154 'total_amount' => 100,
b7f554fe 155 );
c60d3584 156 $payment = $this->callAPISuccess('payment', 'create', $params);
52e3bed0 157 $expectedResult = array(
c60d3584
PN
158 $payment['id'] => array(
159 'from_financial_account_id' => 7,
160 'to_financial_account_id' => 6,
161 'total_amount' => 100,
162 'status_id' => 1,
163 'is_payment' => 1,
164 ),
52e3bed0
PN
165 );
166 $this->checkPaymentResult($payment, $expectedResult);
577daeaa
PN
167 $params = array(
168 'entity_table' => 'civicrm_financial_item',
169 'financial_trxn_id' => $payment['id'],
170 );
171 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
172 $amounts = array(66.67, 33.33);
173 foreach ($eft['values'] as $value) {
174 $this->assertEquals($value['amount'], array_pop($amounts));
175 }
b7f554fe
E
176 // Check contribution for completed status
177 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
178
179 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
52e3bed0 180 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
577daeaa
PN
181 $paymentParticipant = array(
182 'contribution_id' => $contribution['id'],
183 );
184 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
185 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
186 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
b7f554fe
E
187 $this->callAPISuccess('Contribution', 'Delete', array(
188 'id' => $contribution['id'],
189 ));
190 }
f5ec2569 191
52e3bed0
PN
192 /**
193 * Function to assert db values
194 */
195 public function checkPaymentResult($payment, $expectedResult) {
c60d3584 196 foreach ($expectedResult[$payment['id']] as $key => $value) {
52e3bed0
PN
197 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
198 }
52e3bed0
PN
199 }
200
52873538
PN
201 /**
202 * Test create payment api with line item in params
203 */
b7f554fe 204 public function testCreatePaymentLineItems() {
d1f27fcf
PN
205 list($lineItems, $contribution) = $this->createParticipantWithContribution();
206 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
b7f554fe 207
d1f27fcf 208 //Create partial payment by passing line item array is params
b7f554fe
E
209 $params = array(
210 'contribution_id' => $contribution['id'],
d1f27fcf 211 'total_amount' => 50,
b7f554fe 212 );
d1f27fcf
PN
213 $amounts = array(40, 10);
214 foreach ($lineItems['values'] as $id => $ignore) {
215 $params['line_item'][] = array($id => array_pop($amounts));
216 }
8ed3f575 217 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems');
d1f27fcf 218 $expectedResult = array(
c60d3584
PN
219 $payment['id'] => array(
220 'from_financial_account_id' => 7,
221 'to_financial_account_id' => 6,
222 'total_amount' => 50,
223 'status_id' => 1,
224 'is_payment' => 1,
225 ),
d1f27fcf
PN
226 );
227 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
228
229 // Check entity financial trxn created properly
230 $params = array(
231 'entity_id' => $contribution['id'],
232 'entity_table' => 'civicrm_contribution',
233 'financial_trxn_id' => $payment['id'],
234 );
235
236 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
237
d1f27fcf
PN
238 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
239
240 $params = array(
241 'entity_table' => 'civicrm_financial_item',
242 'financial_trxn_id' => $payment['id'],
243 );
244 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
245 $amounts = array(40, 10);
246 foreach ($eft['values'] as $value) {
247 $this->assertEquals($value['amount'], array_pop($amounts));
248 }
b7f554fe
E
249
250 // Now create payment to complete total amount of contribution
251 $params = array(
252 'contribution_id' => $contribution['id'],
d1f27fcf 253 'total_amount' => 100,
b7f554fe 254 );
d1f27fcf
PN
255 $amounts = array(80, 20);
256 foreach ($lineItems['values'] as $id => $ignore) {
257 $params['line_item'][] = array($id => array_pop($amounts));
258 }
c60d3584 259 $payment = $this->callAPISuccess('payment', 'create', $params);
d1f27fcf 260 $expectedResult = array(
c60d3584
PN
261 $payment['id'] => array(
262 'from_financial_account_id' => 7,
263 'to_financial_account_id' => 6,
264 'total_amount' => 100,
265 'status_id' => 1,
266 'is_payment' => 1,
267 ),
d1f27fcf
PN
268 );
269 $this->checkPaymentResult($payment, $expectedResult);
270 $params = array(
271 'entity_table' => 'civicrm_financial_item',
272 'financial_trxn_id' => $payment['id'],
273 );
274 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
275 $amounts = array(80, 20);
276 foreach ($eft['values'] as $value) {
277 $this->assertEquals($value['amount'], array_pop($amounts));
278 }
b7f554fe
E
279 // Check contribution for completed status
280 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
281
282 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
d1f27fcf
PN
283 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
284 $paymentParticipant = array(
285 'contribution_id' => $contribution['id'],
286 );
287 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
288 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
289 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
b7f554fe
E
290 $this->callAPISuccess('Contribution', 'Delete', array(
291 'id' => $contribution['id'],
292 ));
293 }
294
db62fd2b
PN
295 /**
296 * Test cancel payment api
2fabb298 297 */
db62fd2b 298 public function testCancelPayment() {
979748a2 299 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
db62fd2b
PN
300 list($lineItems, $contribution) = $this->createParticipantWithContribution();
301
302 $params = array(
303 'contribution_id' => $contribution['id'],
304 );
305
979748a2 306 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b
PN
307 $this->assertEquals(1, $payment['count']);
308
309 $cancelParams = array(
310 'id' => $payment['id'],
979748a2 311 'check_permissions' => TRUE,
db62fd2b 312 );
4f94e3fa 313 $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 314
eba13f6d 315 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
979748a2 316
8ed3f575 317 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
db62fd2b 318
c60d3584 319 $payment = $this->callAPISuccess('payment', 'get', $params);
db62fd2b
PN
320 $this->assertEquals(2, $payment['count']);
321 $amounts = array(-150.00, 150.00);
2fabb298 322 foreach ($payment['values'] as $value) {
db62fd2b
PN
323 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
324 }
325
326 $this->callAPISuccess('Contribution', 'Delete', array(
327 'id' => $contribution['id'],
328 ));
329 }
330
ee1f482b
PN
331 /**
332 * Test delete payment api
333 */
334 public function testDeletePayment() {
979748a2 335 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
ee1f482b
PN
336 list($lineItems, $contribution) = $this->createParticipantWithContribution();
337
338 $params = array(
339 'contribution_id' => $contribution['id'],
340 );
341
c60d3584 342 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
343 $this->assertEquals(1, $payment['count']);
344
979748a2 345 $deleteParams = array(
ee1f482b 346 'id' => $payment['id'],
979748a2 347 'check_permissions' => TRUE,
ee1f482b 348 );
4f94e3fa 349 $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 350
eba13f6d 351 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute');
8ed3f575 352 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
ee1f482b 353
c60d3584 354 $payment = $this->callAPISuccess('payment', 'get', $params);
ee1f482b
PN
355 $this->assertEquals(0, $payment['count']);
356
357 $this->callAPISuccess('Contribution', 'Delete', array(
358 'id' => $contribution['id'],
359 ));
360 }
361
4cdb5e2f
PN
362 /**
363 * Test update payment api
364 */
365 public function testUpdatePayment() {
979748a2 366 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute', 'edit contributions');
4cdb5e2f
PN
367 list($lineItems, $contribution) = $this->createParticipantWithContribution();
368
369 //Create partial payment by passing line item array is params
370 $params = array(
371 'contribution_id' => $contribution['id'],
372 'total_amount' => 50,
373 );
374
c60d3584 375 $payment = $this->callAPISuccess('payment', 'create', $params);
4cdb5e2f 376 $expectedResult = array(
c60d3584
PN
377 $payment['id'] => array(
378 'from_financial_account_id' => 7,
379 'to_financial_account_id' => 6,
380 'total_amount' => 50,
381 'status_id' => 1,
382 'is_payment' => 1,
383 ),
4cdb5e2f
PN
384 );
385 $this->checkPaymentResult($payment, $expectedResult);
386
387 $params = array(
388 'entity_table' => 'civicrm_financial_item',
389 'financial_trxn_id' => $payment['id'],
390 );
391 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
392 $amounts = array(33.33, 16.67);
393 foreach ($eft['values'] as $value) {
394 $this->assertEquals($value['amount'], array_pop($amounts));
395 }
979748a2 396 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute');
4cdb5e2f
PN
397
398 // update the amount for payment
399 $params = array(
400 'contribution_id' => $contribution['id'],
401 'total_amount' => 100,
402 'id' => $payment['id'],
979748a2 403 'check_permissions' => TRUE,
4cdb5e2f 404 );
4f94e3fa 405 $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 406
eba13f6d 407 array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
8ed3f575 408 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');
4cdb5e2f 409
2a84219e
E
410 // Check for proportional cancelled payment against lineitems.
411 $minParams = array(
412 'entity_table' => 'civicrm_financial_item',
413 'financial_trxn_id' => $payment['id'] - 1,
414 );
415
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
417 $amounts = array(-33.33, -16.67);
418
419 foreach ($eft['values'] as $value) {
420 $this->assertEquals($value['amount'], array_pop($amounts));
421 }
422
423 // Check for proportional updated payment against lineitems.
4cdb5e2f
PN
424 $params = array(
425 'entity_table' => 'civicrm_financial_item',
426 'financial_trxn_id' => $payment['id'],
427 );
428 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
429 $amounts = array(66.67, 33.33);
430 foreach ($eft['values'] as $value) {
431 $this->assertEquals($value['amount'], array_pop($amounts));
432 }
433
434 $params = array(
435 'contribution_id' => $contribution['id'],
436 );
c60d3584 437 $payment = $this->callAPISuccess('payment', 'get', $params);
4cdb5e2f
PN
438 $amounts = array(100.00, -50.00, 50.00, 150.00);
439 foreach ($payment['values'] as $value) {
440 $amount = array_pop($amounts);
441 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
442
443 // Check entity financial trxn created properly
444 $params = array(
445 'entity_id' => $contribution['id'],
446 'entity_table' => 'civicrm_contribution',
447 'financial_trxn_id' => $value['id'],
448 );
449 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
450 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
451 }
452
453 $this->callAPISuccess('Contribution', 'Delete', array(
454 'id' => $contribution['id'],
455 ));
456 }
457
c60d3584
PN
458 /**
459 * Test create payment api for paylater contribution
460 */
461 public function testCreatePaymentPayLater() {
462 $this->createLoggedInUser();
463 $contributionParams = array(
464 'total_amount' => 100,
465 'currency' => 'USD',
466 'contact_id' => $this->_individualId,
467 'financial_type_id' => 1,
468 'contribution_status_id' => 2,
469 'is_pay_later' => 1,
470 );
471 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
8ed3f575 472 //add payment for pay later transaction
c60d3584
PN
473 $params = array(
474 'contribution_id' => $contribution['id'],
475 'total_amount' => 100,
476 );
477 $payment = $this->callAPISuccess('Payment', 'create', $params);
478 $expectedResult = array(
d5b39a17 479 $payment['id'] => array(
c60d3584
PN
480 'from_financial_account_id' => 7,
481 'to_financial_account_id' => 6,
482 'total_amount' => 100,
483 'status_id' => 1,
484 'is_payment' => 1,
485 ),
486 );
487 $this->checkPaymentResult($payment, $expectedResult);
488 // Check entity financial trxn created properly
489 $params = array(
490 'entity_id' => $contribution['id'],
491 'entity_table' => 'civicrm_contribution',
492 'financial_trxn_id' => $payment['id'],
493 );
494 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
495 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
496 $params = array(
497 'entity_table' => 'civicrm_financial_item',
498 'financial_trxn_id' => $payment['id'],
499 );
500 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
501 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
502 // Check contribution for completed status
503 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
504 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
505 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
506 $this->callAPISuccess('Contribution', 'Delete', array(
507 'id' => $contribution['id'],
508 ));
509 }
510
d5b39a17 511 /**
fa7a1ebd 512 * Test create payment api for paylater contribution with partial payment.
d5b39a17
PN
513 */
514 public function testCreatePaymentPayLaterPartialPayment() {
515 $this->createLoggedInUser();
516 $contributionParams = array(
517 'total_amount' => 100,
518 'currency' => 'USD',
519 'contact_id' => $this->_individualId,
520 'financial_type_id' => 1,
521 'contribution_status_id' => 2,
522 'is_pay_later' => 1,
523 );
524 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
525 //Create partial payment
526 $params = array(
527 'contribution_id' => $contribution['id'],
528 'total_amount' => 60,
529 );
530 $payment = $this->callAPISuccess('Payment', 'create', $params);
531 $expectedResult = array(
532 $payment['id'] => array(
533 'total_amount' => 60,
534 'status_id' => 1,
535 'is_payment' => 1,
536 ),
537 );
538 $this->checkPaymentResult($payment, $expectedResult);
539 // Check entity financial trxn created properly
540 $params = array(
541 'entity_id' => $contribution['id'],
542 'entity_table' => 'civicrm_contribution',
543 'financial_trxn_id' => $payment['id'],
544 );
545 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
546 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
547 $params = array(
548 'entity_table' => 'civicrm_financial_item',
549 'financial_trxn_id' => $payment['id'],
550 );
551 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
552 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
553 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
554 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
555 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
556 //Create full payment
557 $params = array(
558 'contribution_id' => $contribution['id'],
559 'total_amount' => 40,
560 );
561 $payment = $this->callAPISuccess('Payment', 'create', $params);
562 $expectedResult = array(
563 $payment['id'] => array(
564 'from_financial_account_id' => 7,
565 'to_financial_account_id' => 6,
566 'total_amount' => 40,
567 'status_id' => 1,
568 'is_payment' => 1,
569 ),
570 );
571 $this->checkPaymentResult($payment, $expectedResult);
572 // Check entity financial trxn created properly
573 $params = array(
574 'entity_id' => $contribution['id'],
575 'entity_table' => 'civicrm_contribution',
576 'financial_trxn_id' => $payment['id'],
577 );
578 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
579 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
580 $params = array(
581 'entity_table' => 'civicrm_financial_item',
582 'financial_trxn_id' => $payment['id'],
583 );
584 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
585 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
586 // Check contribution for completed status
587 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
588 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
589 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
590 $this->callAPISuccess('Contribution', 'Delete', array(
591 'id' => $contribution['id'],
592 ));
593 }
594
b7f554fe 595}