Merge pull request #13610 from eileenmcnaughton/payment_conf2
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / AdditionalPaymentTest.php
CommitLineData
61bfc595
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
61bfc595 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
61bfc595
PN
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 */
35class 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 131 public function testAddPaymentUsingCreditCardForPartialyPaidContribution() {
ecddfe70 132 $mut = new CiviMailUtils($this, TRUE);
61bfc595
PN
133 $this->createContribution('Partially paid');
134
135 // pay additional amount by using Credit Card
ecddfe70 136 $this->submitPayment(70, 'live', TRUE);
61bfc595 137 $this->checkResults(array(30, 70), 2);
ecddfe70 138 $mut->assertSubjects(['Payment Receipt -']);
139 $mut->checkMailLog([
140 'Dear Anthony Anderson',
141 'Payment Details',
142 'Total Fees: $ 100.00',
143 'This Payment Amount: $ 70.00',
144 'Balance Owed: $ 0.00 ',
145 ]);
146
147 $mut->stop();
61bfc595 148 }
61bfc595
PN
149
150 /**
151 * Test the submit function that completes the partially paid Contribution.
152 */
153 public function testAddPaymentForPartialyPaidContribution() {
154 $this->createContribution('Partially paid');
155
156 // pay additional amount
157 $this->submitPayment(70);
158 $this->checkResults(array(30, 70), 2);
159 }
160
161 /**
162 * Test the submit function that completes the partially paid Contribution with multiple payments.
163 */
164 public function testMultiplePaymentForPartialyPaidContribution() {
165 $this->createContribution('Partially paid');
166
167 // pay additional amount
168 $this->submitPayment(50);
169 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
170 $this->assertEquals('Partially paid', $contribution['contribution_status']);
171
172 // pay additional amount
173 $this->submitPayment(20);
174 $this->checkResults(array(30, 50, 20), 3);
175 }
80c9b98c 176
61bfc595
PN
177 /**
178 * Test the submit function that completes the partially paid Contribution with multiple payments.
80c9b98c
PN
179 */
180 public function testMultiplePaymentForPartiallyPaidContributionWithOneCreditCardPayment() {
61bfc595
PN
181 $this->createContribution('Partially paid');
182
183 // pay additional amount
184 $this->submitPayment(50);
185 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
186 $this->assertEquals('Partially paid', $contribution['contribution_status']);
187
188 // pay additional amount by using credit card
189 $this->submitPayment(20, 'live');
190 $this->checkResults(array(30, 50, 20), 3);
191 }
192
61bfc595
PN
193 /**
194 * Test the submit function that completes the pending pay later Contribution using Credit Card.
80c9b98c 195 */
61bfc595
PN
196 public function testAddPaymentUsingCreditCardForPendingPayLaterContribution() {
197 $this->createContribution('Pending');
198
199 // pay additional amount by using Credit Card
200 $this->submitPayment(100, 'live');
201 $this->checkResults(array(100), 1);
202 }
61bfc595
PN
203
204 /**
205 * Test the submit function that completes the pending pay later Contribution.
206 */
207 public function testAddPaymentForPendingPayLaterContribution() {
208 $this->createContribution('Pending');
209
210 // pay additional amount
211 $this->submitPayment(70);
212 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
213 $this->assertEquals('Partially paid', $contribution['contribution_status']);
214
215 // pay additional amount
216 $this->submitPayment(30);
217 $this->checkResults(array(30, 70), 2);
218 }
219
425f113a
AP
220 /**
221 * Test the Membership status after completing the pending pay later Contribution.
222 */
223 public function testMembershipStatusAfterCompletingPayLaterContribution() {
224 $this->createContribution('Pending');
225 $membership = $this->createPendingMembershipAndRecordContribution($this->_contributionId);
226 // pay additional amount
227 $this->submitPayment(100);
228 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
229 $contributionMembership = $this->callAPISuccessGetSingle('Membership', array('id' => $membership["id"]));
230 $membershipStatus = $this->callAPISuccessGetSingle('MembershipStatus', array('id' => $contributionMembership["status_id"]));
154bef4d 231 $this->assertEquals('New', $membershipStatus['name']);
425f113a
AP
232 }
233
234 private function createPendingMembershipAndRecordContribution($contributionId) {
235 $this->_individualId = $this->individualCreate();
236 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
237 'domain_id' => 1,
238 'name' => "AnnualFixed",
239 'member_of_contact_id' => 1,
240 'duration_unit' => "year",
241 'duration_interval' => 1,
242 'period_type' => "fixed",
243 'fixed_period_start_day' => "101",
244 'fixed_period_rollover_day' => "1231",
245 'relationship_type_id' => 20,
246 'financial_type_id' => 2,
247 ));
248 $membershipStatuses = CRM_Member_PseudoConstant::membershipStatus();
249 $pendingStatusId = array_search('Pending', $membershipStatuses);
250 $membership = $this->callAPISuccess('Membership', 'create', array(
251 'contact_id' => $this->_individualId,
252 'membership_type_id' => $membershipTypeAnnualFixed['id'],
253 ));
254 // Updating Membership status to Pending
255 $membership = $this->callAPISuccess('Membership', 'create', array(
256 'id' => $membership["id"],
257 'status_id' => $pendingStatusId,
258 ));
259 $membershipPayment = $this->callAPISuccess('MembershipPayment', 'create', array(
260 'membership_id' => $membership["id"],
261 'contribution_id' => $contributionId,
262 ));
263 return $membership;
264 }
265
61bfc595
PN
266 /**
267 * Test the submit function that completes the pending pay later Contribution with multiple payments.
268 */
269 public function testMultiplePaymentForPendingPayLaterContribution() {
270 $this->createContribution('Pending');
271
272 // pay additional amount
273 $this->submitPayment(40);
274 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
275 $this->assertEquals('Partially paid', $contribution['contribution_status']);
276
277 $this->submitPayment(20);
278 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
279 $this->assertEquals('Partially paid', $contribution['contribution_status']);
280
281 $this->submitPayment(30);
282 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
283 $this->assertEquals('Partially paid', $contribution['contribution_status']);
284
285 $this->submitPayment(10);
286 $this->checkResults(array(40, 20, 30, 10), 4);
287 }
288
289 /**
290 * Test the submit function that completes the pending pay later Contribution with multiple payments.
80c9b98c 291 */
61bfc595
PN
292 public function testMultiplePaymentForPendingPayLaterContributionWithOneCreditCardPayment() {
293 $this->createContribution('Pending');
294
295 // pay additional amount
296 $this->submitPayment(50);
297 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
298 $this->assertEquals('Partially paid', $contribution['contribution_status']);
299
300 $this->submitPayment(20, 'live');
301 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
302 $this->assertEquals('Partially paid', $contribution['contribution_status']);
303
304 $this->submitPayment(20);
305 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
306 $this->assertEquals('Partially paid', $contribution['contribution_status']);
307
308 $this->submitPayment(10, 'live');
309 $this->checkResults(array(50, 20, 20, 10), 4);
310 }
61bfc595
PN
311
312 /**
313 * Function to create pending pay later or partially paid conntribution.
314 *
315 * @param string $typeofContribution
316 *
317 */
318 public function createContribution($typeofContribution = 'Pending') {
319 if ($typeofContribution == 'Partially paid') {
320 $contributionParams = array_merge($this->_params, array(
321 'partial_payment_total' => 100.00,
f49cdeab 322 'partial_amount_to_pay' => 30,
61bfc595
PN
323 'contribution_status_id' => 1,
324 ));
325 }
326 elseif ($typeofContribution == 'Pending') {
327 $contributionParams = array_merge($this->_params, array(
328 'contribution_status_id' => 2,
329 'is_pay_later' => 1,
330 ));
331 }
332 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
333 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id']));
334 $this->assertNotEmpty($contribution);
335 $this->assertEquals($typeofContribution, $contribution['contribution_status']);
336 $this->_contributionId = $contribution['id'];
337 }
338
339 /**
340 * Function to submit payments for contribution.
341 *
342 * @param float $amount
343 * Payment Amount
344 * @param string $mode
345 * Mode of Payment
ecddfe70 346 * @param bool $isEmailReceipt
61bfc595 347 */
ecddfe70 348 public function submitPayment($amount, $mode = NULL, $isEmailReceipt = FALSE) {
61bfc595
PN
349 $form = new CRM_Contribute_Form_AdditionalPayment();
350
351 $submitParams = array(
352 'contribution_id' => $this->_contributionId,
353 'contact_id' => $this->_individualId,
354 'total_amount' => $amount,
355 'currency' => 'USD',
356 'financial_type_id' => 1,
357 'receive_date' => '04/21/2015',
358 'receive_date_time' => '11:27PM',
359 'trxn_date' => '2017-04-11 13:05:11',
80c9b98c 360 'payment_processor_id' => 0,
ecddfe70 361 'is_email_receipt' => $isEmailReceipt,
362 'from_email_address' => 'site@something.com',
61bfc595
PN
363 );
364 if ($mode) {
365 $submitParams += array(
ecddfe70 366 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
61bfc595
PN
367 'payment_processor_id' => $this->paymentProcessorID,
368 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
369 'credit_card_number' => '411111111111111',
370 'cvv2' => 234,
371 'credit_card_type' => 'Visa',
372 'billing_city-5' => 'Vancouver',
373 'billing_state_province_id-5' => 1059,
374 'billing_postal_code-5' => 1321312,
375 'billing_country_id-5' => 1228,
376 );
377 }
378 else {
379 $submitParams += array(
380 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
381 'check_number' => 'check-12345',
382 );
383 }
23cb875c 384 $form->cid = $this->_individualId;
61bfc595
PN
385 $form->testSubmit($submitParams, $mode);
386 }
387
388 /**
389 * Function to check result.
390 *
391 * @param array $amounts
392 * Array of payment amount for contribution
393 * @param int $count
394 * Number payment for contribution
395 *
396 */
397 public function checkResults($amounts, $count) {
398 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
399 $this->assertNotEmpty($contribution);
400 $this->assertEquals('Completed', $contribution['contribution_status']);
401
402 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
403 'entity_table' => "civicrm_contribution",
404 'entity_id' => $this->_contributionId,
405 'financial_trxn_id.is_payment' => 1,
406 'financial_trxn_id.total_amount' => array('IN' => $amounts),
407 ), $count);
408 }
409
410}