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