CRM-16259, fixed notice error
[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 /**
101 * Test Get.
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
131 public function testCreatePaymentNoLineItems() {
132
133 $params = array(
134 'contact_id' => $this->_individualId,
135 'receive_date' => '20120511',
136 'total_amount' => 200.00,
137 'financial_type_id' => $this->_financialTypeId,
138 'payment_instrument_id' => 1,
139 'contribution_status_id' => 8,
140 );
141
142 $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution
f5ec2569 143
b7f554fe
E
144 //Create partial payment
145 $params = array(
146 'contribution_id' => $contribution['id'],
147 'total_amount' => 150,
148 );
149 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
150
151 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
152 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
153 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150);
154 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
155 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
156
157 // Check entity financial trxn created properly
158 $params = array(
159 'entity_id' => $contribution['id'],
160 'entity_table' => 'civicrm_contribution',
161 'financial_trxn_id' => $payment['id'],
162 );
163
164 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
165
166 $this->assertEquals($eft['values'][$eft['id']]['amount'], 150);
167
168 // Now create payment to complete total amount of contribution
169 $params = array(
170 'contribution_id' => $contribution['id'],
171 'total_amount' => 50,
172 );
173 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
174
175 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
176 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
177 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50);
178 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
179 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
180
181 // Check contribution for completed status
182 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
183
184 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
185 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
f5ec2569 186
b7f554fe
E
187 $this->callAPISuccess('Contribution', 'Delete', array(
188 'id' => $contribution['id'],
189 ));
190 }
f5ec2569 191
b7f554fe
E
192 public function testCreatePaymentLineItems() {
193
194 // Create priceset & price fields
195 $this->createPriceSet();
196
197 $params = array(
198 'contact_id' => $this->_individualId,
199 'receive_date' => '20120511',
200 'total_amount' => 200.00,
201 'financial_type_id' => $this->_financialTypeId,
202 'payment_instrument_id' => 1,
203 'contribution_status_id' => 8,
204 );
205
206 $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution
f5ec2569 207
b7f554fe
E
208 //Create partial payment
209 $params = array(
210 'contribution_id' => $contribution['id'],
211 'total_amount' => 150,
212 );
213 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
214
215 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
216 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
217 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150);
218 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
219 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
220
221 // Check entity financial trxn created properly
222 $params = array(
223 'entity_id' => $contribution['id'],
224 'entity_table' => 'civicrm_contribution',
225 'financial_trxn_id' => $payment['id'],
226 );
227
228 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
229
230 $this->assertEquals($eft['values'][$eft['id']]['amount'], 150);
231
232 // Now create payment to complete total amount of contribution
233 $params = array(
234 'contribution_id' => $contribution['id'],
235 'total_amount' => 50,
236 );
237 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__);
238
239 $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7);
240 $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6);
241 $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50);
242 $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1);
243 $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1);
244
245 // Check contribution for completed status
246 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id']));
247
248 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
249 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
f5ec2569 250
b7f554fe
E
251 $this->callAPISuccess('Contribution', 'Delete', array(
252 'id' => $contribution['id'],
253 ));
254 }
255
256 public function cleanUpAfterPriceSets() {
257 $this->quickCleanUpFinancialEntities();
258 $this->contactDelete($this->_ids['contact']);
259 }
260
261
262 /**
263 * Create price set.
264 *
b7f554fe
E
265 */
266 public function createPriceSet() {
267 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
268 'title' => "Test Contribution Page",
269 'financial_type_id' => 1,
270 'currency' => 'NZD',
271 'is_pay_later' => 1,
272 'is_monetary' => TRUE,
273 'is_email_receipt' => FALSE,
274 ));
275 $priceSet = $this->callAPISuccess('price_set', 'create', array(
276 'is_quick_config' => 0,
277 'extends' => 'CiviContribute',
278 'financial_type_id' => 1,
279 'title' => 'My Test Price Set',
280 ));
281 $priceSetID = $priceSet['id'];
282 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
283
284 $priceField = $this->callAPISuccess('price_field', 'create', array(
285 'price_set_id' => $priceSetID,
286 'label' => 'Goat Breed',
287 'html_type' => 'Radio',
288 ));
289 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
290 'price_set_id' => $priceSetID,
291 'price_field_id' => $priceField['id'],
292 'label' => 'Long Haired Goat',
293 'amount' => 50,
294 'financial_type_id' => 'Donation',
295 )
296 );
297 $this->_priceIds['price_field_value'] = array($priceFieldValue['id']);
298 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
299 'price_set_id' => $priceSetID,
300 'price_field_id' => $priceField['id'],
301 'label' => 'Shoe-eating Goat',
302 'amount' => 150,
303 'financial_type_id' => 'Donation',
304 )
305 );
306 $this->_priceIds['price_field_value'][] = $priceFieldValue['id'];
307 $this->_priceIds['price_set'] = $priceSetID;
308 $this->_priceIds['price_field'] = $priceField['id'];
309 }
310
311}