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