CRM-16259, added api test for delete action
[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';
29require_once 'CiviTest/CiviMailUtils.php';
30
31
32/**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38class api_v3_PaymentTest extends CiviUnitTestCase {
39
40 /**
41 * Assume empty database with just civicrm_data.
42 */
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
48 public $debug = 0;
49 protected $_params;
50 protected $_ids = array();
51 protected $_pageParams = array();
52
53 /**
54 * Parameters to create payment processor.
55 *
56 * @var array
57 */
58 protected $_processorParams = array();
59
60 /**
61 * Setup function.
62 */
63 public function setUp() {
64 parent::setUp();
65
66 $this->_apiversion = 3;
67 $this->_individualId = $this->individualCreate();
68 $this->_params = array(
69 'contact_id' => $this->_individualId,
70 'receive_date' => '20120511',
71 'total_amount' => 100.00,
72 'financial_type_id' => $this->_financialTypeId,
73 'non_deductible_amount' => 10.00,
74 'fee_amount' => 5.00,
75 'net_amount' => 95.00,
76 'source' => 'SSF',
77 'contribution_status_id' => 1,
78 );
79 $this->_processorParams = array(
80 'domain_id' => 1,
81 'name' => 'Dummy',
82 'payment_processor_type_id' => 10,
83 'financial_account_id' => 12,
84 'is_active' => 1,
85 'user_name' => '',
86 'url_site' => 'http://dummy.com',
87 'url_recur' => 'http://dummy.com',
88 'billing_mode' => 1,
89 );
90 }
91
92 /**
93 * Clean up after each test.
94 */
95 public function tearDown() {
96 $this->quickCleanUpFinancialEntities();
97 $this->quickCleanup(array('civicrm_uf_match'));
98 }
99
100 /**
52873538 101 * Test Get Payment api.
b7f554fe
E
102 */
103 public function testGetPayment() {
104 $p = array(
105 'contact_id' => $this->_individualId,
106 'receive_date' => '2010-01-20',
107 'total_amount' => 100.00,
108 'financial_type_id' => $this->_financialTypeId,
109 'trxn_id' => 23456,
110 'contribution_status_id' => 1,
111 );
112 $contribution = $this->callAPISuccess('contribution', 'create', $p);
113
114 $params = array(
115 'contribution_id' => $contribution['id'],
116 );
117
118 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
119
120 $this->assertEquals(1, $payment['count']);
121 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 100.00);
122 $this->assertEquals($payment['values'][$payment['id']]['trxn_id'], 23456);
123 $this->assertEquals($payment['values'][$payment['id']]['trxn_date'], '2010-01-20 00:00:00');
124 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
125 $this->assertEquals($payment['values'][$payment['id']]['contribution_id'], $contribution['id']);
126 $this->callAPISuccess('Contribution', 'Delete', array(
127 'id' => $contribution['id'],
128 ));
129 }
130
52873538
PN
131 /**
132 * Test create payment api with no line item in params
133 */
b7f554fe 134 public function testCreatePaymentNoLineItems() {
b7f554fe
E
135 $params = array(
136 'contact_id' => $this->_individualId,
137 'receive_date' => '20120511',
138 'total_amount' => 200.00,
139 'financial_type_id' => $this->_financialTypeId,
140 'payment_instrument_id' => 1,
141 'contribution_status_id' => 8,
142 );
143
144 $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution
f5ec2569 145
b7f554fe
E
146 //Create partial payment
147 $params = array(
148 'contribution_id' => $contribution['id'],
149 'total_amount' => 150,
150 );
151 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
152
153 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
154 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
155 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150);
156 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
157 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
158
159 // Check entity financial trxn created properly
160 $params = array(
161 'entity_id' => $contribution['id'],
162 'entity_table' => 'civicrm_contribution',
163 'financial_trxn_id' => $payment['id'],
164 );
165
166 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
167
168 $this->assertEquals($eft['values'][$eft['id']]['amount'], 150);
169
170 // Now create payment to complete total amount of contribution
171 $params = array(
172 'contribution_id' => $contribution['id'],
173 'total_amount' => 50,
174 );
175 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
176
177 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
178 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
179 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50);
180 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
181 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
182
183 // Check contribution for completed status
184 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
185
186 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
187 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
f5ec2569 188
b7f554fe
E
189 $this->callAPISuccess('Contribution', 'Delete', array(
190 'id' => $contribution['id'],
191 ));
192 }
f5ec2569 193
52873538
PN
194 /**
195 * Test create payment api with line item in params
196 */
b7f554fe 197 public function testCreatePaymentLineItems() {
b7f554fe
E
198 // Create priceset & price fields
199 $this->createPriceSet();
200
201 $params = array(
202 'contact_id' => $this->_individualId,
203 'receive_date' => '20120511',
204 'total_amount' => 200.00,
205 'financial_type_id' => $this->_financialTypeId,
206 'payment_instrument_id' => 1,
207 'contribution_status_id' => 8,
208 );
209
210 $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution
f5ec2569 211
b7f554fe
E
212 //Create partial payment
213 $params = array(
214 'contribution_id' => $contribution['id'],
215 'total_amount' => 150,
216 );
217 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
218
219 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
220 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
221 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150);
222 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
223 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
224
225 // Check entity financial trxn created properly
226 $params = array(
227 'entity_id' => $contribution['id'],
228 'entity_table' => 'civicrm_contribution',
229 'financial_trxn_id' => $payment['id'],
230 );
231
232 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
233
234 $this->assertEquals($eft['values'][$eft['id']]['amount'], 150);
235
236 // Now create payment to complete total amount of contribution
237 $params = array(
238 'contribution_id' => $contribution['id'],
239 'total_amount' => 50,
240 );
241 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
242
243 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
244 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
245 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50);
246 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
247 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
248
249 // Check contribution for completed status
250 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
251
252 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
253 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
f5ec2569 254
b7f554fe
E
255 $this->callAPISuccess('Contribution', 'Delete', array(
256 'id' => $contribution['id'],
257 ));
258 }
259
52873538
PN
260 /**
261 * function to delete data
262 */
b7f554fe
E
263 public function cleanUpAfterPriceSets() {
264 $this->quickCleanUpFinancialEntities();
265 $this->contactDelete($this->_ids['contact']);
266 }
267
268
269 /**
270 * Create price set.
271 *
b7f554fe
E
272 */
273 public function createPriceSet() {
274 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
275 'title' => "Test Contribution Page",
276 'financial_type_id' => 1,
277 'currency' => 'NZD',
278 'is_pay_later' => 1,
279 'is_monetary' => TRUE,
280 'is_email_receipt' => FALSE,
281 ));
282 $priceSet = $this->callAPISuccess('price_set', 'create', array(
283 'is_quick_config' => 0,
284 'extends' => 'CiviContribute',
285 'financial_type_id' => 1,
286 'title' => 'My Test Price Set',
287 ));
288 $priceSetID = $priceSet['id'];
289 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
290
291 $priceField = $this->callAPISuccess('price_field', 'create', array(
292 'price_set_id' => $priceSetID,
293 'label' => 'Goat Breed',
294 'html_type' => 'Radio',
295 ));
296 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
297 'price_set_id' => $priceSetID,
298 'price_field_id' => $priceField['id'],
299 'label' => 'Long Haired Goat',
300 'amount' => 50,
301 'financial_type_id' => 'Donation',
302 )
303 );
304 $this->_priceIds['price_field_value'] = array($priceFieldValue['id']);
305 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
306 'price_set_id' => $priceSetID,
307 'price_field_id' => $priceField['id'],
308 'label' => 'Shoe-eating Goat',
309 'amount' => 150,
310 'financial_type_id' => 'Donation',
311 )
312 );
313 $this->_priceIds['price_field_value'][] = $priceFieldValue['id'];
314 $this->_priceIds['price_set'] = $priceSetID;
315 $this->_priceIds['price_field'] = $priceField['id'];
316 }
317
db62fd2b
PN
318 /**
319 * Test cancel payment api
2fabb298 320 */
db62fd2b
PN
321 public function testCancelPayment() {
322 list($lineItems, $contribution) = $this->createParticipantWithContribution();
323
324 $params = array(
325 'contribution_id' => $contribution['id'],
326 );
327
328 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
329 $this->assertEquals(1, $payment['count']);
330
331 $cancelParams = array(
332 'id' => $payment['id'],
333 );
334 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__);
335
336 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
337 $this->assertEquals(2, $payment['count']);
338 $amounts = array(-150.00, 150.00);
2fabb298 339 foreach ($payment['values'] as $value) {
db62fd2b
PN
340 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
341 }
342
343 $this->callAPISuccess('Contribution', 'Delete', array(
344 'id' => $contribution['id'],
345 ));
346 }
347
ee1f482b
PN
348 /**
349 * Test delete payment api
350 */
351 public function testDeletePayment() {
352 list($lineItems, $contribution) = $this->createParticipantWithContribution();
353
354 $params = array(
355 'contribution_id' => $contribution['id'],
356 );
357
358 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
359 $this->assertEquals(1, $payment['count']);
360
361 $cancelParams = array(
362 'id' => $payment['id'],
363 );
364 $this->callAPIAndDocument('payment', 'delete', $cancelParams, __FUNCTION__, __FILE__);
365
366 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__);
367 $this->assertEquals(0, $payment['count']);
368
369 $this->callAPISuccess('Contribution', 'Delete', array(
370 'id' => $contribution['id'],
371 ));
372 }
373
b7f554fe 374}