Commit | Line | Data |
---|---|---|
61bfc595 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2017 | | |
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 | ||
28 | /** | |
29 | * Test APIv3 civicrm_contribute_* functions | |
30 | * | |
31 | * @package CiviCRM_APIv3 | |
32 | * @subpackage API_Contribution | |
33 | * @group headless | |
34 | */ | |
35 | class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { | |
36 | ||
37 | /** | |
38 | * Contact ID. | |
39 | * | |
40 | * @var int | |
41 | */ | |
42 | protected $_individualId; | |
43 | ||
44 | /** | |
45 | * Parameters to create contribution. | |
46 | * | |
47 | * @var array | |
48 | */ | |
49 | protected $_params; | |
50 | ||
51 | /** | |
52 | * Contribution ID. | |
53 | * | |
54 | * @var int | |
55 | */ | |
56 | protected $_contributionId; | |
57 | ||
58 | /** | |
59 | * Parameters to create payment processor. | |
60 | * | |
61 | * @var array | |
62 | */ | |
63 | protected $_processorParams = array(); | |
64 | ||
65 | /** | |
66 | * Payment instrument mapping. | |
67 | * | |
68 | * @var array | |
69 | */ | |
70 | protected $paymentInstruments = array(); | |
71 | ||
72 | /** | |
73 | * Dummy payment processor. | |
74 | * | |
75 | * @var CRM_Core_Payment_Dummy | |
76 | */ | |
77 | protected $paymentProcessor; | |
78 | ||
79 | /** | |
80 | * Payment processor ID. | |
81 | * | |
82 | * @var int | |
83 | */ | |
84 | protected $paymentProcessorID; | |
85 | ||
86 | /** | |
87 | * Setup function. | |
88 | */ | |
89 | public function setUp() { | |
90 | parent::setUp(); | |
91 | $this->createLoggedInUser(); | |
92 | ||
93 | $this->_individualId = $this->individualCreate(); | |
94 | $this->_params = array( | |
95 | 'total_amount' => 100, | |
96 | 'currency' => 'USD', | |
97 | 'contact_id' => $this->_individualId, | |
98 | 'financial_type_id' => 1, | |
99 | ); | |
100 | $this->_processorParams = array( | |
101 | 'domain_id' => 1, | |
102 | 'name' => 'Dummy', | |
103 | 'payment_processor_type_id' => 10, | |
104 | 'financial_account_id' => 12, | |
105 | 'is_active' => 1, | |
106 | 'user_name' => '', | |
107 | 'url_site' => 'http://dummy.com', | |
108 | 'url_recur' => 'http://dummy.com', | |
109 | 'billing_mode' => 1, | |
110 | ); | |
111 | ||
112 | $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); | |
113 | $this->paymentInstruments = $instruments['values']; | |
114 | ||
115 | $this->paymentProcessor = $this->dummyProcessorCreate(); | |
116 | $processor = $this->paymentProcessor->getPaymentProcessor(); | |
117 | $this->paymentProcessorID = $processor['id']; | |
118 | } | |
119 | ||
120 | /** | |
121 | * Clean up after each test. | |
122 | */ | |
123 | public function tearDown() { | |
124 | $this->quickCleanUpFinancialEntities(); | |
125 | parent::tearDown(); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test the submit function that completes the partially paid Contribution using Credit Card. | |
80c9b98c | 130 | */ |
61bfc595 PN |
131 | public function testAddPaymentUsingCreditCardForPartialyPaidContribution() { |
132 | $this->createContribution('Partially paid'); | |
133 | ||
134 | // pay additional amount by using Credit Card | |
135 | $this->submitPayment(70, 'live'); | |
136 | $this->checkResults(array(30, 70), 2); | |
137 | } | |
61bfc595 PN |
138 | |
139 | /** | |
140 | * Test the submit function that completes the partially paid Contribution. | |
141 | */ | |
142 | public function testAddPaymentForPartialyPaidContribution() { | |
143 | $this->createContribution('Partially paid'); | |
144 | ||
145 | // pay additional amount | |
146 | $this->submitPayment(70); | |
147 | $this->checkResults(array(30, 70), 2); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Test the submit function that completes the partially paid Contribution with multiple payments. | |
152 | */ | |
153 | public function testMultiplePaymentForPartialyPaidContribution() { | |
154 | $this->createContribution('Partially paid'); | |
155 | ||
156 | // pay additional amount | |
157 | $this->submitPayment(50); | |
158 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
159 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
160 | ||
161 | // pay additional amount | |
162 | $this->submitPayment(20); | |
163 | $this->checkResults(array(30, 50, 20), 3); | |
164 | } | |
80c9b98c | 165 | |
61bfc595 PN |
166 | /** |
167 | * Test the submit function that completes the partially paid Contribution with multiple payments. | |
80c9b98c PN |
168 | */ |
169 | public function testMultiplePaymentForPartiallyPaidContributionWithOneCreditCardPayment() { | |
61bfc595 PN |
170 | $this->createContribution('Partially paid'); |
171 | ||
172 | // pay additional amount | |
173 | $this->submitPayment(50); | |
174 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
175 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
176 | ||
177 | // pay additional amount by using credit card | |
178 | $this->submitPayment(20, 'live'); | |
179 | $this->checkResults(array(30, 50, 20), 3); | |
180 | } | |
181 | ||
61bfc595 PN |
182 | /** |
183 | * Test the submit function that completes the pending pay later Contribution using Credit Card. | |
80c9b98c | 184 | */ |
61bfc595 PN |
185 | public function testAddPaymentUsingCreditCardForPendingPayLaterContribution() { |
186 | $this->createContribution('Pending'); | |
187 | ||
188 | // pay additional amount by using Credit Card | |
189 | $this->submitPayment(100, 'live'); | |
190 | $this->checkResults(array(100), 1); | |
191 | } | |
61bfc595 PN |
192 | |
193 | /** | |
194 | * Test the submit function that completes the pending pay later Contribution. | |
195 | */ | |
196 | public function testAddPaymentForPendingPayLaterContribution() { | |
197 | $this->createContribution('Pending'); | |
198 | ||
199 | // pay additional amount | |
200 | $this->submitPayment(70); | |
201 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
202 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
203 | ||
204 | // pay additional amount | |
205 | $this->submitPayment(30); | |
206 | $this->checkResults(array(30, 70), 2); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Test the submit function that completes the pending pay later Contribution with multiple payments. | |
211 | */ | |
212 | public function testMultiplePaymentForPendingPayLaterContribution() { | |
213 | $this->createContribution('Pending'); | |
214 | ||
215 | // pay additional amount | |
216 | $this->submitPayment(40); | |
217 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
218 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
219 | ||
220 | $this->submitPayment(20); | |
221 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
222 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
223 | ||
224 | $this->submitPayment(30); | |
225 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
226 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
227 | ||
228 | $this->submitPayment(10); | |
229 | $this->checkResults(array(40, 20, 30, 10), 4); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Test the submit function that completes the pending pay later Contribution with multiple payments. | |
80c9b98c | 234 | */ |
61bfc595 PN |
235 | public function testMultiplePaymentForPendingPayLaterContributionWithOneCreditCardPayment() { |
236 | $this->createContribution('Pending'); | |
237 | ||
238 | // pay additional amount | |
239 | $this->submitPayment(50); | |
240 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
241 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
242 | ||
243 | $this->submitPayment(20, 'live'); | |
244 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
245 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
246 | ||
247 | $this->submitPayment(20); | |
248 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
249 | $this->assertEquals('Partially paid', $contribution['contribution_status']); | |
250 | ||
251 | $this->submitPayment(10, 'live'); | |
252 | $this->checkResults(array(50, 20, 20, 10), 4); | |
253 | } | |
61bfc595 PN |
254 | |
255 | /** | |
256 | * Function to create pending pay later or partially paid conntribution. | |
257 | * | |
258 | * @param string $typeofContribution | |
259 | * | |
260 | */ | |
261 | public function createContribution($typeofContribution = 'Pending') { | |
262 | if ($typeofContribution == 'Partially paid') { | |
263 | $contributionParams = array_merge($this->_params, array( | |
264 | 'partial_payment_total' => 100.00, | |
f49cdeab | 265 | 'partial_amount_to_pay' => 30, |
61bfc595 PN |
266 | 'contribution_status_id' => 1, |
267 | )); | |
268 | } | |
269 | elseif ($typeofContribution == 'Pending') { | |
270 | $contributionParams = array_merge($this->_params, array( | |
271 | 'contribution_status_id' => 2, | |
272 | 'is_pay_later' => 1, | |
273 | )); | |
274 | } | |
275 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); | |
276 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id'])); | |
277 | $this->assertNotEmpty($contribution); | |
278 | $this->assertEquals($typeofContribution, $contribution['contribution_status']); | |
279 | $this->_contributionId = $contribution['id']; | |
280 | } | |
281 | ||
282 | /** | |
283 | * Function to submit payments for contribution. | |
284 | * | |
285 | * @param float $amount | |
286 | * Payment Amount | |
287 | * @param string $mode | |
288 | * Mode of Payment | |
289 | * | |
290 | */ | |
291 | public function submitPayment($amount, $mode = NULL) { | |
292 | $form = new CRM_Contribute_Form_AdditionalPayment(); | |
293 | ||
294 | $submitParams = array( | |
295 | 'contribution_id' => $this->_contributionId, | |
296 | 'contact_id' => $this->_individualId, | |
297 | 'total_amount' => $amount, | |
298 | 'currency' => 'USD', | |
299 | 'financial_type_id' => 1, | |
300 | 'receive_date' => '04/21/2015', | |
301 | 'receive_date_time' => '11:27PM', | |
302 | 'trxn_date' => '2017-04-11 13:05:11', | |
80c9b98c | 303 | 'payment_processor_id' => 0, |
61bfc595 PN |
304 | ); |
305 | if ($mode) { | |
306 | $submitParams += array( | |
307 | 'payment_instrument_id' => array_search('Credit card', $this->paymentInstruments), | |
308 | 'payment_processor_id' => $this->paymentProcessorID, | |
309 | 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025), | |
310 | 'credit_card_number' => '411111111111111', | |
311 | 'cvv2' => 234, | |
312 | 'credit_card_type' => 'Visa', | |
313 | 'billing_city-5' => 'Vancouver', | |
314 | 'billing_state_province_id-5' => 1059, | |
315 | 'billing_postal_code-5' => 1321312, | |
316 | 'billing_country_id-5' => 1228, | |
317 | ); | |
318 | } | |
319 | else { | |
320 | $submitParams += array( | |
321 | 'payment_instrument_id' => array_search('Check', $this->paymentInstruments), | |
322 | 'check_number' => 'check-12345', | |
323 | ); | |
324 | } | |
23cb875c | 325 | $form->cid = $this->_individualId; |
61bfc595 PN |
326 | $form->testSubmit($submitParams, $mode); |
327 | } | |
328 | ||
329 | /** | |
330 | * Function to check result. | |
331 | * | |
332 | * @param array $amounts | |
333 | * Array of payment amount for contribution | |
334 | * @param int $count | |
335 | * Number payment for contribution | |
336 | * | |
337 | */ | |
338 | public function checkResults($amounts, $count) { | |
339 | $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); | |
340 | $this->assertNotEmpty($contribution); | |
341 | $this->assertEquals('Completed', $contribution['contribution_status']); | |
342 | ||
343 | $this->callAPISuccessGetCount('EntityFinancialTrxn', array( | |
344 | 'entity_table' => "civicrm_contribution", | |
345 | 'entity_id' => $this->_contributionId, | |
346 | 'financial_trxn_id.is_payment' => 1, | |
347 | 'financial_trxn_id.total_amount' => array('IN' => $amounts), | |
348 | ), $count); | |
349 | } | |
350 | ||
351 | } |