3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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();
56 * Clean up after each test.
58 public function tearDown() {
59 $this->quickCleanUpFinancialEntities();
60 $this->quickCleanup(array('civicrm_uf_match'));
64 * Test Get Payment api.
66 public function testGetPayment() {
68 'contact_id' => $this->_individualId
,
69 'receive_date' => '2010-01-20',
70 'total_amount' => 100.00,
71 'financial_type_id' => $this->_financialTypeId
,
73 'contribution_status_id' => 1,
75 $contribution = $this->callAPISuccess('contribution', 'create', $p);
78 'contribution_id' => $contribution['id'],
81 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
83 $this->assertEquals(1, $payment['count']);
84 $expectedResult = array(
85 'total_amount' => 100,
87 'trxn_date' => '2010-01-20 00:00:00',
88 'contribution_id' => $contribution['id'],
91 $this->checkPaymentResult($payment, $expectedResult);
92 $this->callAPISuccess('Contribution', 'Delete', array(
93 'id' => $contribution['id'],
98 * Test create payment api with no line item in params
100 public function testCreatePaymentNoLineItems() {
101 list($lineItems, $contribution) = $this->createParticipantWithContribution();
103 //Create partial payment
105 'contribution_id' => $contribution['id'],
106 'total_amount' => 50,
108 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
109 $expectedResult = array(
110 'from_financial_account_id' => 7,
111 'to_financial_account_id' => 6,
112 'total_amount' => 50,
116 $this->checkPaymentResult($payment, $expectedResult);
118 // Check entity financial trxn created properly
120 'entity_id' => $contribution['id'],
121 'entity_table' => 'civicrm_contribution',
122 'financial_trxn_id' => $payment['id'],
125 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
127 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
130 'entity_table' => 'civicrm_financial_item',
131 'financial_trxn_id' => $payment['id'],
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));
139 // Now create payment to complete total amount of contribution
141 'contribution_id' => $contribution['id'],
142 'total_amount' => 100,
144 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
145 $expectedResult = array(
146 'from_financial_account_id' => 7,
147 'to_financial_account_id' => 6,
148 'total_amount' => 100,
152 $this->checkPaymentResult($payment, $expectedResult);
154 'entity_table' => 'civicrm_financial_item',
155 'financial_trxn_id' => $payment['id'],
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));
162 // Check contribution for completed status
163 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
165 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
166 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
167 $paymentParticipant = array(
168 'contribution_id' => $contribution['id'],
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');
173 $this->callAPISuccess('Contribution', 'Delete', array(
174 'id' => $contribution['id'],
179 * Function to assert db values
181 public function checkPaymentResult($payment, $expectedResult) {
182 foreach ($expectedResult as $key => $value) {
183 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
188 * Test create payment api with line item in params
190 public function testCreatePaymentLineItems() {
191 list($lineItems, $contribution) = $this->createParticipantWithContribution();
192 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
194 //Create partial payment by passing line item array is params
196 'contribution_id' => $contribution['id'],
197 'total_amount' => 50,
199 $amounts = array(40, 10);
200 foreach ($lineItems['values'] as $id => $ignore) {
201 $params['line_item'][] = array($id => array_pop($amounts));
203 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
204 $expectedResult = array(
205 'from_financial_account_id' => 7,
206 'to_financial_account_id' => 6,
207 'total_amount' => 50,
211 $this->checkPaymentResult($payment, $expectedResult);
213 // Check entity financial trxn created properly
215 'entity_id' => $contribution['id'],
216 'entity_table' => 'civicrm_contribution',
217 'financial_trxn_id' => $payment['id'],
220 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
222 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
225 'entity_table' => 'civicrm_financial_item',
226 'financial_trxn_id' => $payment['id'],
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));
234 // Now create payment to complete total amount of contribution
236 'contribution_id' => $contribution['id'],
237 'total_amount' => 100,
239 $amounts = array(80, 20);
240 foreach ($lineItems['values'] as $id => $ignore) {
241 $params['line_item'][] = array($id => array_pop($amounts));
243 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
244 $expectedResult = array(
245 'from_financial_account_id' => 7,
246 'to_financial_account_id' => 6,
247 'total_amount' => 100,
251 $this->checkPaymentResult($payment, $expectedResult);
253 'entity_table' => 'civicrm_financial_item',
254 'financial_trxn_id' => $payment['id'],
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));
261 // Check contribution for completed status
262 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
264 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
265 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
266 $paymentParticipant = array(
267 'contribution_id' => $contribution['id'],
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');
272 $this->callAPISuccess('Contribution', 'Delete', array(
273 'id' => $contribution['id'],
278 * Test cancel payment api
280 public function testCancelPayment() {
281 list($lineItems, $contribution) = $this->createParticipantWithContribution();
284 'contribution_id' => $contribution['id'],
287 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
288 $this->assertEquals(1, $payment['count']);
290 $cancelParams = array(
291 'id' => $payment['id'],
293 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
295 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
296 $this->assertEquals(2, $payment['count']);
297 $amounts = array(-150.00, 150.00);
298 foreach ($payment['values'] as $value) {
299 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
302 $this->callAPISuccess('Contribution', 'Delete', array(
303 'id' => $contribution['id'],
308 * Test delete payment api
310 public function testDeletePayment() {
311 list($lineItems, $contribution) = $this->createParticipantWithContribution();
314 'contribution_id' => $contribution['id'],
317 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
318 $this->assertEquals(1, $payment['count']);
320 $cancelParams = array(
321 'id' => $payment['id'],
323 $this->callAPIAndDocument('payment', 'delete', $cancelParams, __FUNCTION__
, __FILE__
);
325 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
326 $this->assertEquals(0, $payment['count']);
328 $this->callAPISuccess('Contribution', 'Delete', array(
329 'id' => $contribution['id'],
334 * Test update payment api
336 public function testUpdatePayment() {
337 list($lineItems, $contribution) = $this->createParticipantWithContribution();
339 //Create partial payment by passing line item array is params
341 'contribution_id' => $contribution['id'],
342 'total_amount' => 50,
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,
353 $this->checkPaymentResult($payment, $expectedResult);
356 'entity_table' => 'civicrm_financial_item',
357 'financial_trxn_id' => $payment['id'],
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));
365 // update the amount for payment
367 'contribution_id' => $contribution['id'],
368 'total_amount' => 100,
369 'id' => $payment['id'],
371 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
374 'entity_table' => 'civicrm_financial_item',
375 'financial_trxn_id' => $payment['id'],
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));
384 'contribution_id' => $contribution['id'],
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');
392 // Check entity financial trxn created properly
394 'entity_id' => $contribution['id'],
395 'entity_table' => 'civicrm_contribution',
396 'financial_trxn_id' => $value['id'],
398 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
399 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
402 $this->callAPISuccess('Contribution', 'Delete', array(
403 'id' => $contribution['id'],