Merge pull request #7665 from julialongtin/safe_changes
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentTest.php
CommitLineData
b7f554fe
E
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
28require_once 'CiviTest/CiviUnitTestCase.php';
b7f554fe
E
29
30
31/**
32 * Test APIv3 civicrm_contribute_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
36 */
37class api_v3_PaymentTest extends CiviUnitTestCase {
38
39 /**
40 * Assume empty database with just civicrm_data.
41 */
42 protected $_individualId;
b7f554fe
E
43 protected $_financialTypeId = 1;
44 protected $_apiversion;
b7f554fe 45 public $debug = 0;
b7f554fe
E
46
47 /**
48 * Setup function.
49 */
50 public function setUp() {
51 parent::setUp();
52
53 $this->_apiversion = 3;
54 $this->_individualId = $this->individualCreate();
b7f554fe
E
55 }
56
57 /**
58 * Clean up after each test.
59 */
60 public function tearDown() {
61 $this->quickCleanUpFinancialEntities();
62 $this->quickCleanup(array('civicrm_uf_match'));
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'],
81 );
82
83 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
84
85 $this->assertEquals(1, $payment['count']);
a44499b4
PN
86 $expectedResult = array(
87 'total_amount' => 100,
88 'trxn_id' => 23456,
89 'trxn_date' => '2010-01-20 00:00:00',
90 'contribution_id' => $contribution['id'],
91 'is_payment' => 1,
92 );
93 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
94 $this->callAPISuccess('Contribution', 'Delete', array(
95 'id' => $contribution['id'],
96 ));
97 }
98
52873538
PN
99 /**
100 * Test create payment api with no line item in params
101 */
b7f554fe 102 public function testCreatePaymentNoLineItems() {
52e3bed0 103 list($lineItems, $contribution) = $this->createParticipantWithContribution();
f5ec2569 104
b7f554fe
E
105 //Create partial payment
106 $params = array(
107 'contribution_id' => $contribution['id'],
52e3bed0 108 'total_amount' => 50,
b7f554fe
E
109 );
110 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
52e3bed0
PN
111 $expectedResult = array(
112 'from_financial_account_id' => 7,
113 'to_financial_account_id' => 6,
114 'total_amount' => 50,
115 'status_id' => 1,
116 'is_payment' => 1,
117 );
118 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
119
120 // Check entity financial trxn created properly
121 $params = array(
122 'entity_id' => $contribution['id'],
123 'entity_table' => 'civicrm_contribution',
124 'financial_trxn_id' => $payment['id'],
125 );
126
127 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
128
52e3bed0 129 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
b7f554fe 130
577daeaa
PN
131 $params = array(
132 'entity_table' => 'civicrm_financial_item',
133 'financial_trxn_id' => $payment['id'],
134 );
135 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
136 $amounts = array(33.33, 16.67);
137 foreach ($eft['values'] as $value) {
138 $this->assertEquals($value['amount'], array_pop($amounts));
139 }
140
b7f554fe
E
141 // Now create payment to complete total amount of contribution
142 $params = array(
143 'contribution_id' => $contribution['id'],
52e3bed0 144 'total_amount' => 100,
b7f554fe
E
145 );
146 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
52e3bed0
PN
147 $expectedResult = array(
148 'from_financial_account_id' => 7,
149 'to_financial_account_id' => 6,
150 'total_amount' => 100,
151 'status_id' => 1,
152 'is_payment' => 1,
153 );
154 $this->checkPaymentResult($payment, $expectedResult);
577daeaa
PN
155 $params = array(
156 'entity_table' => 'civicrm_financial_item',
157 'financial_trxn_id' => $payment['id'],
158 );
159 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
160 $amounts = array(66.67, 33.33);
161 foreach ($eft['values'] as $value) {
162 $this->assertEquals($value['amount'], array_pop($amounts));
163 }
b7f554fe
E
164 // Check contribution for completed status
165 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
166
167 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
52e3bed0 168 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
577daeaa
PN
169 $paymentParticipant = array(
170 'contribution_id' => $contribution['id'],
171 );
172 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
173 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
174 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
b7f554fe
E
175 $this->callAPISuccess('Contribution', 'Delete', array(
176 'id' => $contribution['id'],
177 ));
178 }
f5ec2569 179
52e3bed0
PN
180 /**
181 * Function to assert db values
182 */
183 public function checkPaymentResult($payment, $expectedResult) {
184 foreach ($expectedResult as $key => $value) {
185 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
186 }
52e3bed0
PN
187 }
188
52873538
PN
189 /**
190 * Test create payment api with line item in params
191 */
b7f554fe 192 public function testCreatePaymentLineItems() {
d1f27fcf
PN
193 list($lineItems, $contribution) = $this->createParticipantWithContribution();
194 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
b7f554fe 195
d1f27fcf 196 //Create partial payment by passing line item array is params
b7f554fe
E
197 $params = array(
198 'contribution_id' => $contribution['id'],
d1f27fcf 199 'total_amount' => 50,
b7f554fe 200 );
d1f27fcf
PN
201 $amounts = array(40, 10);
202 foreach ($lineItems['values'] as $id => $ignore) {
203 $params['line_item'][] = array($id => array_pop($amounts));
204 }
b7f554fe 205 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
d1f27fcf
PN
206 $expectedResult = array(
207 'from_financial_account_id' => 7,
208 'to_financial_account_id' => 6,
209 'total_amount' => 50,
210 'status_id' => 1,
211 'is_payment' => 1,
212 );
213 $this->checkPaymentResult($payment, $expectedResult);
b7f554fe
E
214
215 // Check entity financial trxn created properly
216 $params = array(
217 'entity_id' => $contribution['id'],
218 'entity_table' => 'civicrm_contribution',
219 'financial_trxn_id' => $payment['id'],
220 );
221
222 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
223
d1f27fcf
PN
224 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
225
226 $params = array(
227 'entity_table' => 'civicrm_financial_item',
228 'financial_trxn_id' => $payment['id'],
229 );
230 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
231 $amounts = array(40, 10);
232 foreach ($eft['values'] as $value) {
233 $this->assertEquals($value['amount'], array_pop($amounts));
234 }
b7f554fe
E
235
236 // Now create payment to complete total amount of contribution
237 $params = array(
238 'contribution_id' => $contribution['id'],
d1f27fcf 239 'total_amount' => 100,
b7f554fe 240 );
d1f27fcf
PN
241 $amounts = array(80, 20);
242 foreach ($lineItems['values'] as $id => $ignore) {
243 $params['line_item'][] = array($id => array_pop($amounts));
244 }
b7f554fe 245 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
d1f27fcf
PN
246 $expectedResult = array(
247 'from_financial_account_id' => 7,
248 'to_financial_account_id' => 6,
249 'total_amount' => 100,
250 'status_id' => 1,
251 'is_payment' => 1,
252 );
253 $this->checkPaymentResult($payment, $expectedResult);
254 $params = array(
255 'entity_table' => 'civicrm_financial_item',
256 'financial_trxn_id' => $payment['id'],
257 );
258 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
259 $amounts = array(80, 20);
260 foreach ($eft['values'] as $value) {
261 $this->assertEquals($value['amount'], array_pop($amounts));
262 }
b7f554fe
E
263 // Check contribution for completed status
264 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
265
266 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
d1f27fcf
PN
267 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
268 $paymentParticipant = array(
269 'contribution_id' => $contribution['id'],
270 );
271 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
272 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
273 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
b7f554fe
E
274 $this->callAPISuccess('Contribution', 'Delete', array(
275 'id' => $contribution['id'],
276 ));
277 }
278
db62fd2b
PN
279 /**
280 * Test cancel payment api
2fabb298 281 */
db62fd2b
PN
282 public function testCancelPayment() {
283 list($lineItems, $contribution) = $this->createParticipantWithContribution();
284
285 $params = array(
286 'contribution_id' => $contribution['id'],
287 );
288
289 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
290 $this->assertEquals(1, $payment['count']);
291
292 $cancelParams = array(
293 'id' => $payment['id'],
294 );
295 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
296
297 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
298 $this->assertEquals(2, $payment['count']);
299 $amounts = array(-150.00, 150.00);
2fabb298 300 foreach ($payment['values'] as $value) {
db62fd2b
PN
301 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
302 }
303
304 $this->callAPISuccess('Contribution', 'Delete', array(
305 'id' => $contribution['id'],
306 ));
307 }
308
ee1f482b
PN
309 /**
310 * Test delete payment api
311 */
312 public function testDeletePayment() {
313 list($lineItems, $contribution) = $this->createParticipantWithContribution();
314
315 $params = array(
316 'contribution_id' => $contribution['id'],
317 );
318
319 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
320 $this->assertEquals(1, $payment['count']);
321
322 $cancelParams = array(
323 'id' => $payment['id'],
324 );
325 $this->callAPIAndDocument('payment', 'delete', $cancelParams, __FUNCTION__, __FILE__);
326
327 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
328 $this->assertEquals(0, $payment['count']);
329
330 $this->callAPISuccess('Contribution', 'Delete', array(
331 'id' => $contribution['id'],
332 ));
333 }
334
4cdb5e2f
PN
335 /**
336 * Test update payment api
337 */
338 public function testUpdatePayment() {
339 list($lineItems, $contribution) = $this->createParticipantWithContribution();
340
341 //Create partial payment by passing line item array is params
342 $params = array(
343 'contribution_id' => $contribution['id'],
344 'total_amount' => 50,
345 );
346
347 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
348 $expectedResult = array(
349 'from_financial_account_id' => 7,
350 'to_financial_account_id' => 6,
351 'total_amount' => 50,
352 'status_id' => 1,
353 'is_payment' => 1,
354 );
355 $this->checkPaymentResult($payment, $expectedResult);
356
357 $params = array(
358 'entity_table' => 'civicrm_financial_item',
359 'financial_trxn_id' => $payment['id'],
360 );
361 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
362 $amounts = array(33.33, 16.67);
363 foreach ($eft['values'] as $value) {
364 $this->assertEquals($value['amount'], array_pop($amounts));
365 }
366
367 // update the amount for payment
368 $params = array(
369 'contribution_id' => $contribution['id'],
370 'total_amount' => 100,
371 'id' => $payment['id'],
372 );
373 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
374
375 $params = array(
376 'entity_table' => 'civicrm_financial_item',
377 'financial_trxn_id' => $payment['id'],
378 );
379 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
380 $amounts = array(66.67, 33.33);
381 foreach ($eft['values'] as $value) {
382 $this->assertEquals($value['amount'], array_pop($amounts));
383 }
384
385 $params = array(
386 'contribution_id' => $contribution['id'],
387 );
388 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
389 $amounts = array(100.00, -50.00, 50.00, 150.00);
390 foreach ($payment['values'] as $value) {
391 $amount = array_pop($amounts);
392 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
393
394 // Check entity financial trxn created properly
395 $params = array(
396 'entity_id' => $contribution['id'],
397 'entity_table' => 'civicrm_contribution',
398 'financial_trxn_id' => $value['id'],
399 );
400 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
401 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
402 }
403
404 $this->callAPISuccess('Contribution', 'Delete', array(
405 'id' => $contribution['id'],
406 ));
407 }
408
b7f554fe 409}