3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_PaymentTest
extends CiviUnitTestCase
{
38 * Assume empty database with just civicrm_data.
40 protected $_individualId;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
48 public function setUp() {
51 $this->_apiversion
= 3;
52 $this->_individualId
= $this->individualCreate();
53 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array();
57 * Clean up after each test.
59 public function tearDown() {
60 $this->quickCleanUpFinancialEntities();
61 $this->quickCleanup(array('civicrm_uf_match'));
62 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
66 * Test Get Payment api.
68 public function testGetPayment() {
70 'contact_id' => $this->_individualId
,
71 'receive_date' => '2010-01-20',
72 'total_amount' => 100.00,
73 'financial_type_id' => $this->_financialTypeId
,
75 'contribution_status_id' => 1,
77 $contribution = $this->callAPISuccess('contribution', 'create', $p);
80 'contribution_id' => $contribution['id'],
81 'check_permissions' => TRUE,
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');
86 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviContribute');
87 $payment = $this->callAPISuccess('payment', 'get', $params);
89 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
90 $this->assertEquals(1, $payment['count']);
92 $expectedResult = array(
93 $contribution['id'] => array(
94 'total_amount' => 100,
96 'trxn_date' => '2010-01-20 00:00:00',
97 'contribution_id' => $contribution['id'],
101 $this->checkPaymentResult($payment, $expectedResult);
102 $this->callAPISuccess('Contribution', 'Delete', array(
103 'id' => $contribution['id'],
108 * Test create payment api with no line item in params
110 public function testCreatePaymentNoLineItems() {
111 list($lineItems, $contribution) = $this->createParticipantWithContribution();
113 //Create partial payment
115 'contribution_id' => $contribution['id'],
116 'total_amount' => 50,
118 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
119 $expectedResult = array(
120 $payment['id'] => array(
121 'from_financial_account_id' => 7,
122 'to_financial_account_id' => 6,
123 'total_amount' => 50,
128 $this->checkPaymentResult($payment, $expectedResult);
130 // Check entity financial trxn created properly
132 'entity_id' => $contribution['id'],
133 'entity_table' => 'civicrm_contribution',
134 'financial_trxn_id' => $payment['id'],
137 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
139 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
142 'entity_table' => 'civicrm_financial_item',
143 'financial_trxn_id' => $payment['id'],
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));
151 // Now create payment to complete total amount of contribution
153 'contribution_id' => $contribution['id'],
154 'total_amount' => 100,
156 $payment = $this->callAPISuccess('payment', 'create', $params);
157 $expectedResult = array(
158 $payment['id'] => array(
159 'from_financial_account_id' => 7,
160 'to_financial_account_id' => 6,
161 'total_amount' => 100,
166 $this->checkPaymentResult($payment, $expectedResult);
168 'entity_table' => 'civicrm_financial_item',
169 'financial_trxn_id' => $payment['id'],
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));
176 // Check contribution for completed status
177 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
179 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
180 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
181 $paymentParticipant = array(
182 'contribution_id' => $contribution['id'],
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');
187 $this->callAPISuccess('Contribution', 'Delete', array(
188 'id' => $contribution['id'],
193 * Function to assert db values
195 public function checkPaymentResult($payment, $expectedResult) {
196 foreach ($expectedResult[$payment['id']] as $key => $value) {
197 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
202 * Test create payment api with line item in params
204 public function testCreatePaymentLineItems() {
205 list($lineItems, $contribution) = $this->createParticipantWithContribution();
206 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
208 //Create partial payment by passing line item array is params
210 'contribution_id' => $contribution['id'],
211 'total_amount' => 50,
213 $amounts = array(40, 10);
214 foreach ($lineItems['values'] as $id => $ignore) {
215 $params['line_item'][] = array($id => array_pop($amounts));
217 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Payment with line item', 'CreatePaymentWithLineItems');
218 $expectedResult = array(
219 $payment['id'] => array(
220 'from_financial_account_id' => 7,
221 'to_financial_account_id' => 6,
222 'total_amount' => 50,
227 $this->checkPaymentResult($payment, $expectedResult);
229 // Check entity financial trxn created properly
231 'entity_id' => $contribution['id'],
232 'entity_table' => 'civicrm_contribution',
233 'financial_trxn_id' => $payment['id'],
236 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
238 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
241 'entity_table' => 'civicrm_financial_item',
242 'financial_trxn_id' => $payment['id'],
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));
250 // Now create payment to complete total amount of contribution
252 'contribution_id' => $contribution['id'],
253 'total_amount' => 100,
255 $amounts = array(80, 20);
256 foreach ($lineItems['values'] as $id => $ignore) {
257 $params['line_item'][] = array($id => array_pop($amounts));
259 $payment = $this->callAPISuccess('payment', 'create', $params);
260 $expectedResult = array(
261 $payment['id'] => array(
262 'from_financial_account_id' => 7,
263 'to_financial_account_id' => 6,
264 'total_amount' => 100,
269 $this->checkPaymentResult($payment, $expectedResult);
271 'entity_table' => 'civicrm_financial_item',
272 'financial_trxn_id' => $payment['id'],
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));
279 // Check contribution for completed status
280 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
282 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
283 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
284 $paymentParticipant = array(
285 'contribution_id' => $contribution['id'],
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');
290 $this->callAPISuccess('Contribution', 'Delete', array(
291 'id' => $contribution['id'],
296 * Test cancel payment api
298 public function testCancelPayment() {
299 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM', 'access CiviContribute');
300 list($lineItems, $contribution) = $this->createParticipantWithContribution();
303 'contribution_id' => $contribution['id'],
306 $payment = $this->callAPISuccess('payment', 'get', $params);
307 $this->assertEquals(1, $payment['count']);
309 $cancelParams = array(
310 'id' => $payment['id'],
311 'check_permissions' => TRUE,
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');
315 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
317 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
319 $payment = $this->callAPISuccess('payment', 'get', $params);
320 $this->assertEquals(2, $payment['count']);
321 $amounts = array(-150.00, 150.00);
322 foreach ($payment['values'] as $value) {
323 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
326 $this->callAPISuccess('Contribution', 'Delete', array(
327 'id' => $contribution['id'],
332 * Test delete payment api
334 public function testDeletePayment() {
335 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM', 'access CiviContribute');
336 list($lineItems, $contribution) = $this->createParticipantWithContribution();
339 'contribution_id' => $contribution['id'],
342 $payment = $this->callAPISuccess('payment', 'get', $params);
343 $this->assertEquals(1, $payment['count']);
345 $deleteParams = array(
346 'id' => $payment['id'],
347 'check_permissions' => TRUE,
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');
351 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'delete in CiviContribute');
352 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
354 $payment = $this->callAPISuccess('payment', 'get', $params);
355 $this->assertEquals(0, $payment['count']);
357 $this->callAPISuccess('Contribution', 'Delete', array(
358 'id' => $contribution['id'],
363 * Test update payment api
365 public function testUpdatePayment() {
366 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM', 'access CiviContribute', 'edit contributions');
367 list($lineItems, $contribution) = $this->createParticipantWithContribution();
369 //Create partial payment by passing line item array is params
371 'contribution_id' => $contribution['id'],
372 'total_amount' => 50,
375 $payment = $this->callAPISuccess('payment', 'create', $params);
376 $expectedResult = array(
377 $payment['id'] => array(
378 'from_financial_account_id' => 7,
379 'to_financial_account_id' => 6,
380 'total_amount' => 50,
385 $this->checkPaymentResult($payment, $expectedResult);
388 'entity_table' => 'civicrm_financial_item',
389 'financial_trxn_id' => $payment['id'],
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));
396 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM', 'access CiviContribute');
398 // update the amount for payment
400 'contribution_id' => $contribution['id'],
401 'total_amount' => 100,
402 'id' => $payment['id'],
403 'check_permissions' => TRUE,
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');
407 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
408 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Update Payment', 'UpdatePayment');
410 // Check for proportional cancelled payment against lineitems.
412 'entity_table' => 'civicrm_financial_item',
413 'financial_trxn_id' => $payment['id'] - 1,
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
417 $amounts = array(-33.33, -16.67);
419 foreach ($eft['values'] as $value) {
420 $this->assertEquals($value['amount'], array_pop($amounts));
423 // Check for proportional updated payment against lineitems.
425 'entity_table' => 'civicrm_financial_item',
426 'financial_trxn_id' => $payment['id'],
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));
435 'contribution_id' => $contribution['id'],
437 $payment = $this->callAPISuccess('payment', 'get', $params);
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');
443 // Check entity financial trxn created properly
445 'entity_id' => $contribution['id'],
446 'entity_table' => 'civicrm_contribution',
447 'financial_trxn_id' => $value['id'],
449 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
450 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
453 $this->callAPISuccess('Contribution', 'Delete', array(
454 'id' => $contribution['id'],
459 * Test create payment api for paylater contribution
461 public function testCreatePaymentPayLater() {
462 $this->createLoggedInUser();
463 $contributionParams = array(
464 'total_amount' => 100,
466 'contact_id' => $this->_individualId
,
467 'financial_type_id' => 1,
468 'contribution_status_id' => 2,
471 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
472 //add payment for pay later transaction
474 'contribution_id' => $contribution['id'],
475 'total_amount' => 100,
477 $payment = $this->callAPISuccess('Payment', 'create', $params);
478 $expectedResult = array(
479 $payment['id'] => array(
480 'from_financial_account_id' => 7,
481 'to_financial_account_id' => 6,
482 'total_amount' => 100,
487 $this->checkPaymentResult($payment, $expectedResult);
488 // Check entity financial trxn created properly
490 'entity_id' => $contribution['id'],
491 'entity_table' => 'civicrm_contribution',
492 'financial_trxn_id' => $payment['id'],
494 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
495 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
497 'entity_table' => 'civicrm_financial_item',
498 'financial_trxn_id' => $payment['id'],
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'],
512 * Test create payment api for paylater contribution with partial payment.
514 public function testCreatePaymentPayLaterPartialPayment() {
515 $this->createLoggedInUser();
516 $contributionParams = array(
517 'total_amount' => 100,
519 'contact_id' => $this->_individualId
,
520 'financial_type_id' => 1,
521 'contribution_status_id' => 2,
524 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
525 //Create partial payment
527 'contribution_id' => $contribution['id'],
528 'total_amount' => 60,
530 $payment = $this->callAPISuccess('Payment', 'create', $params);
531 $expectedResult = array(
532 $payment['id'] => array(
533 'total_amount' => 60,
538 $this->checkPaymentResult($payment, $expectedResult);
539 // Check entity financial trxn created properly
541 'entity_id' => $contribution['id'],
542 'entity_table' => 'civicrm_contribution',
543 'financial_trxn_id' => $payment['id'],
545 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
546 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
548 'entity_table' => 'civicrm_financial_item',
549 'financial_trxn_id' => $payment['id'],
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
558 'contribution_id' => $contribution['id'],
559 'total_amount' => 40,
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,
571 $this->checkPaymentResult($payment, $expectedResult);
572 // Check entity financial trxn created properly
574 'entity_id' => $contribution['id'],
575 'entity_table' => 'civicrm_contribution',
576 'financial_trxn_id' => $payment['id'],
578 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
579 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
581 'entity_table' => 'civicrm_financial_item',
582 'financial_trxn_id' => $payment['id'],
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'],