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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_contribute_* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
37 class api_v3_PaymentTest
extends CiviUnitTestCase
{
40 * Assume empty database with just civicrm_data.
42 protected $_individualId;
43 protected $_financialTypeId = 1;
44 protected $_apiversion;
50 public function setUp() {
53 $this->_apiversion
= 3;
54 $this->_individualId
= $this->individualCreate();
58 * Clean up after each test.
60 public function tearDown() {
61 $this->quickCleanUpFinancialEntities();
62 $this->quickCleanup(array('civicrm_uf_match'));
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'],
83 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
85 $this->assertEquals(1, $payment['count']);
86 $expectedResult = array(
87 'total_amount' => 100,
89 'trxn_date' => '2010-01-20 00:00:00',
90 'contribution_id' => $contribution['id'],
93 $this->checkPaymentResult($payment, $expectedResult);
94 $this->callAPISuccess('Contribution', 'Delete', array(
95 'id' => $contribution['id'],
100 * Test create payment api with no line item in params
102 public function testCreatePaymentNoLineItems() {
103 list($lineItems, $contribution) = $this->createParticipantWithContribution();
105 //Create partial payment
107 'contribution_id' => $contribution['id'],
108 'total_amount' => 50,
110 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
111 $expectedResult = array(
112 'from_financial_account_id' => 7,
113 'to_financial_account_id' => 6,
114 'total_amount' => 50,
118 $this->checkPaymentResult($payment, $expectedResult);
120 // Check entity financial trxn created properly
122 'entity_id' => $contribution['id'],
123 'entity_table' => 'civicrm_contribution',
124 'financial_trxn_id' => $payment['id'],
127 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
129 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
132 'entity_table' => 'civicrm_financial_item',
133 'financial_trxn_id' => $payment['id'],
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));
141 // Now create payment to complete total amount of contribution
143 'contribution_id' => $contribution['id'],
144 'total_amount' => 100,
146 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
147 $expectedResult = array(
148 'from_financial_account_id' => 7,
149 'to_financial_account_id' => 6,
150 'total_amount' => 100,
154 $this->checkPaymentResult($payment, $expectedResult);
156 'entity_table' => 'civicrm_financial_item',
157 'financial_trxn_id' => $payment['id'],
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));
164 // Check contribution for completed status
165 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
167 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
168 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
169 $paymentParticipant = array(
170 'contribution_id' => $contribution['id'],
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');
175 $this->callAPISuccess('Contribution', 'Delete', array(
176 'id' => $contribution['id'],
181 * Function to assert db values
183 public function checkPaymentResult($payment, $expectedResult) {
184 foreach ($expectedResult as $key => $value) {
185 $this->assertEquals($payment['values'][$payment['id']][$key], $value);
190 * Test create payment api with line item in params
192 public function testCreatePaymentLineItems() {
193 list($lineItems, $contribution) = $this->createParticipantWithContribution();
194 $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id']));
196 //Create partial payment by passing line item array is params
198 'contribution_id' => $contribution['id'],
199 'total_amount' => 50,
201 $amounts = array(40, 10);
202 foreach ($lineItems['values'] as $id => $ignore) {
203 $params['line_item'][] = array($id => array_pop($amounts));
205 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
206 $expectedResult = array(
207 'from_financial_account_id' => 7,
208 'to_financial_account_id' => 6,
209 'total_amount' => 50,
213 $this->checkPaymentResult($payment, $expectedResult);
215 // Check entity financial trxn created properly
217 'entity_id' => $contribution['id'],
218 'entity_table' => 'civicrm_contribution',
219 'financial_trxn_id' => $payment['id'],
222 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
224 $this->assertEquals($eft['values'][$eft['id']]['amount'], 50);
227 'entity_table' => 'civicrm_financial_item',
228 'financial_trxn_id' => $payment['id'],
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));
236 // Now create payment to complete total amount of contribution
238 'contribution_id' => $contribution['id'],
239 'total_amount' => 100,
241 $amounts = array(80, 20);
242 foreach ($lineItems['values'] as $id => $ignore) {
243 $params['line_item'][] = array($id => array_pop($amounts));
245 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
246 $expectedResult = array(
247 'from_financial_account_id' => 7,
248 'to_financial_account_id' => 6,
249 'total_amount' => 100,
253 $this->checkPaymentResult($payment, $expectedResult);
255 'entity_table' => 'civicrm_financial_item',
256 'financial_trxn_id' => $payment['id'],
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));
263 // Check contribution for completed status
264 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
266 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
267 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
268 $paymentParticipant = array(
269 'contribution_id' => $contribution['id'],
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');
274 $this->callAPISuccess('Contribution', 'Delete', array(
275 'id' => $contribution['id'],
280 * Test cancel payment api
282 public function testCancelPayment() {
283 list($lineItems, $contribution) = $this->createParticipantWithContribution();
286 'contribution_id' => $contribution['id'],
289 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
290 $this->assertEquals(1, $payment['count']);
292 $cancelParams = array(
293 'id' => $payment['id'],
295 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
297 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
298 $this->assertEquals(2, $payment['count']);
299 $amounts = array(-150.00, 150.00);
300 foreach ($payment['values'] as $value) {
301 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
304 $this->callAPISuccess('Contribution', 'Delete', array(
305 'id' => $contribution['id'],
310 * Test delete payment api
312 public function testDeletePayment() {
313 list($lineItems, $contribution) = $this->createParticipantWithContribution();
316 'contribution_id' => $contribution['id'],
319 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
320 $this->assertEquals(1, $payment['count']);
322 $cancelParams = array(
323 'id' => $payment['id'],
325 $this->callAPIAndDocument('payment', 'delete', $cancelParams, __FUNCTION__
, __FILE__
);
327 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
328 $this->assertEquals(0, $payment['count']);
330 $this->callAPISuccess('Contribution', 'Delete', array(
331 'id' => $contribution['id'],
336 * Test update payment api
338 public function testUpdatePayment() {
339 list($lineItems, $contribution) = $this->createParticipantWithContribution();
341 //Create partial payment by passing line item array is params
343 'contribution_id' => $contribution['id'],
344 'total_amount' => 50,
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,
355 $this->checkPaymentResult($payment, $expectedResult);
358 'entity_table' => 'civicrm_financial_item',
359 'financial_trxn_id' => $payment['id'],
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));
367 // update the amount for payment
369 'contribution_id' => $contribution['id'],
370 'total_amount' => 100,
371 'id' => $payment['id'],
373 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
376 'entity_table' => 'civicrm_financial_item',
377 'financial_trxn_id' => $payment['id'],
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));
386 'contribution_id' => $contribution['id'],
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');
394 // Check entity financial trxn created properly
396 'entity_id' => $contribution['id'],
397 'entity_table' => 'civicrm_contribution',
398 'financial_trxn_id' => $value['id'],
400 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
401 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
404 $this->callAPISuccess('Contribution', 'Delete', array(
405 'id' => $contribution['id'],