Merge pull request #10006 from JMAConsulting/CRM-20022
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_contribute_recur* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ContributionPageTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37 protected $testAmount = 34567;
38 protected $params;
39 protected $id = 0;
40 protected $contactIds = array();
41 protected $_entity = 'contribution_page';
6c6e6187 42 protected $contribution_result = NULL;
f64a217a 43 protected $_priceSetParams = array();
f8fe0df6 44 /**
eceb18cc 45 * Payment processor details.
f8fe0df6 46 * @var array
47 */
48 protected $_paymentProcessor = array();
f64a217a
EM
49
50 /**
51 * @var array
16b10e64
CW
52 * - contribution_page
53 * - price_set
54 * - price_field
55 * - price_field_value
f64a217a
EM
56 */
57 protected $_ids = array();
58
b7c9bc4c 59
6a488035 60 public $DBResetRequired = TRUE;
5896d037 61
6a488035
TO
62 public function setUp() {
63 parent::setUp();
64 $this->contactIds[] = $this->individualCreate();
65 $this->params = array(
6a488035
TO
66 'title' => "Test Contribution Page",
67 'financial_type_id' => 1,
68 'currency' => 'NZD',
69 'goal_amount' => $this->testAmount,
6cdac50b 70 'is_pay_later' => 1,
f64a217a 71 'is_monetary' => TRUE,
dd1b539a 72 'is_email_receipt' => TRUE,
73 'receipt_from_email' => 'yourconscience@donate.com',
74 'receipt_from_name' => 'Ego Freud',
f64a217a
EM
75 );
76
77 $this->_priceSetParams = array(
78 'is_quick_config' => 1,
79 'extends' => 'CiviContribute',
80 'financial_type_id' => 'Donation',
21dfd5f5 81 'title' => 'my Page',
6a488035
TO
82 );
83 }
84
00be9182 85 public function tearDown() {
6a488035 86 foreach ($this->contactIds as $id) {
fc928539 87 $this->callAPISuccess('contact', 'delete', array('id' => $id));
611c7ece 88 }
f9342903 89 $this->quickCleanUpFinancialEntities();
6a488035
TO
90 }
91
92 public function testCreateContributionPage() {
fc928539 93 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
94 $this->assertEquals(1, $result['count']);
95 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
96 $this->getAndCheck($this->params, $result['id'], $this->_entity);
97 }
98
99 public function testGetBasicContributionPage() {
fc928539 100 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 101 $this->id = $createResult['id'];
6a488035 102 $getParams = array(
6a488035
TO
103 'currency' => 'NZD',
104 'financial_type_id' => 1,
105 );
fc928539 106 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
107 $this->assertEquals(1, $getResult['count']);
6a488035
TO
108 }
109
110 public function testGetContributionPageByAmount() {
fc928539 111 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 112 $this->id = $createResult['id'];
6a488035 113 $getParams = array(
5896d037 114 'amount' => '' . $this->testAmount, // 3456
6a488035
TO
115 'currency' => 'NZD',
116 'financial_type_id' => 1,
117 );
5be22f39 118 $getResult = $this->callAPISuccess($this->_entity, 'get', $getParams);
fc928539 119 $this->assertEquals(1, $getResult['count']);
6a488035
TO
120 }
121
122 public function testDeleteContributionPage() {
fc928539 123 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
124 $deleteParams = array('id' => $createResult['id']);
611c7ece 125 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
fc928539 126 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
127 $this->assertEquals(0, $checkDeleted['count']);
6a488035
TO
128 }
129
130 public function testGetFieldsContributionPage() {
fc928539 131 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
132 $this->assertEquals(12, $result['values']['start_date']['type']);
133 }
134
be26f3e0 135
d58b453e 136 /**
eceb18cc 137 * Test form submission with basic price set.
d58b453e 138 */
be26f3e0 139 public function testSubmit() {
f64a217a
EM
140 $this->setUpContributionPage();
141 $priceFieldID = reset($this->_ids['price_field']);
142 $priceFieldValueID = reset($this->_ids['price_field_value']);
143 $submitParams = array(
144 'price_' . $priceFieldID => $priceFieldValueID,
145 'id' => (int) $this->_ids['contribution_page'],
21dfd5f5 146 'amount' => 10,
be26f3e0
EM
147 );
148
f64a217a 149 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
05465712 150 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
151 //assert non-deductible amount
152 $this->assertEquals(5.00, $contribution['non_deductible_amount']);
f64a217a 153 }
5896d037 154
870156d6 155 /**
156 * Test form submission with billing first & last name where the contact does NOT
157 * otherwise have one.
158 */
159 public function testSubmitNewBillingNameData() {
160 $this->setUpContributionPage();
161 $contact = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com'));
162 $priceFieldID = reset($this->_ids['price_field']);
163 $priceFieldValueID = reset($this->_ids['price_field_value']);
164 $submitParams = array(
165 'price_' . $priceFieldID => $priceFieldValueID,
166 'id' => (int) $this->_ids['contribution_page'],
167 'amount' => 10,
168 'billing_first_name' => 'Wonder',
169 'billing_last_name' => 'Woman',
170 'contactID' => $contact['id'],
171 'email' => 'wonderwoman@amazon.com',
172 );
173
174 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
175 $contact = $this->callAPISuccess('Contact', 'get', array(
176 'id' => $contact['id'],
177 'return' => array(
178 'first_name',
179 'last_name',
180 'sort_name',
181 'display_name',
182 ),
183 ));
184 $this->assertEquals(array(
185 'first_name' => 'Wonder',
186 'last_name' => 'Woman',
187 'display_name' => 'Wonder Woman',
188 'sort_name' => 'Woman, Wonder',
189 'id' => $contact['id'],
190 'contact_id' => $contact['id'],
191 ), $contact['values'][$contact['id']]);
192
193 }
194
195 /**
196 * Test form submission with billing first & last name where the contact does
197 * otherwise have one and should not be overwritten.
198 */
199 public function testSubmitNewBillingNameDoNotOverwrite() {
200 $this->setUpContributionPage();
201 $contact = $this->callAPISuccess('Contact', 'create', array(
202 'contact_type' => 'Individual',
203 'email' => 'wonderwoman@amazon.com',
204 'first_name' => 'Super',
205 'last_name' => 'Boy',
206 ));
207 $priceFieldID = reset($this->_ids['price_field']);
208 $priceFieldValueID = reset($this->_ids['price_field_value']);
209 $submitParams = array(
210 'price_' . $priceFieldID => $priceFieldValueID,
211 'id' => (int) $this->_ids['contribution_page'],
212 'amount' => 10,
213 'billing_first_name' => 'Wonder',
214 'billing_last_name' => 'Woman',
215 'contactID' => $contact['id'],
216 'email' => 'wonderwoman@amazon.com',
217 );
218
219 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
220 $contact = $this->callAPISuccess('Contact', 'get', array(
221 'id' => $contact['id'],
222 'return' => array(
223 'first_name',
224 'last_name',
225 'sort_name',
226 'display_name',
227 ),
228 ));
229 $this->assertEquals(array(
230 'first_name' => 'Super',
231 'last_name' => 'Boy',
232 'display_name' => 'Super Boy',
233 'sort_name' => 'Boy, Super',
234 'id' => $contact['id'],
235 'contact_id' => $contact['id'],
236 ), $contact['values'][$contact['id']]);
237
238 }
239
7bc789a5 240 /**
241 * Test process with instant payment when more than one configured for the page.
242 *
243 * CRM-16923
244 */
245 public function testSubmitRecurMultiProcessorInstantPayment() {
246 $this->setUpContributionPage();
247 $this->setupPaymentProcessor();
d27635dc 248 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
7bc789a5 249 'payment_processor_type_id' => 'Dummy',
250 'name' => 'processor 2',
251 'class_name' => 'Payment_Dummy',
252 'billing_mode' => 1,
253 ));
254 $dummyPP = Civi\Payment\System::singleton()->getById($paymentProcessor2ID);
70cc8754 255 $dummyPP->setDoDirectPaymentResult(array(
256 'payment_status_id' => 1,
257 'trxn_id' => 'create_first_success',
258 'fee_amount' => .85,
259 ));
f69a9ac3 260 $processor = $dummyPP->getPaymentProcessor();
7bc789a5 261 $this->callAPISuccess('ContributionPage', 'create', array(
d27635dc 262 'id' => $this->_ids['contribution_page'],
263 'payment_processor' => array($paymentProcessor2ID, $this->_ids['payment_processor']),
7bc789a5 264 ));
265
266 $priceFieldID = reset($this->_ids['price_field']);
267 $priceFieldValueID = reset($this->_ids['price_field_value']);
268 $submitParams = array(
269 'price_' . $priceFieldID => $priceFieldValueID,
270 'id' => (int) $this->_ids['contribution_page'],
271 'amount' => 10,
272 'is_recur' => 1,
273 'frequency_interval' => 1,
274 'frequency_unit' => 'month',
275 'payment_processor_id' => $paymentProcessor2ID,
276 );
277
278 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
70cc8754 279 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
7bc789a5 280 'contribution_page_id' => $this->_ids['contribution_page'],
281 'contribution_status_id' => 1,
282 ));
70cc8754 283 $this->assertEquals('create_first_success', $contribution['trxn_id']);
284 $this->assertEquals(10, $contribution['total_amount']);
285 $this->assertEquals(.85, $contribution['fee_amount']);
286 $this->assertEquals(9.15, $contribution['net_amount']);
bf722049 287 $this->_checkFinancialRecords(array(
288 'id' => $contribution['id'],
289 'total_amount' => $contribution['total_amount'],
f69a9ac3 290 'payment_instrument_id' => $processor['payment_instrument_id'],
bf722049 291 ), 'online');
7bc789a5 292 }
70cc8754 293
f64a217a 294 /**
eceb18cc 295 * Test submit with a membership block in place.
f64a217a 296 */
f9342903
EM
297 public function testSubmitMembershipBlockNotSeparatePayment() {
298 $this->setUpMembershipContributionPage();
f64a217a 299 $submitParams = array(
3e28c791 300 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903 301 'id' => (int) $this->_ids['contribution_page'],
f64a217a
EM
302 'amount' => 10,
303 'billing_first_name' => 'Billy',
304 'billing_middle_name' => 'Goat',
305 'billing_last_name' => 'Gruff',
306 'selectMembership' => $this->_ids['membership_type'],
307
308 );
309
a828d7b8 310 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903 311 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
840e3907 312 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
313 $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']));
f9342903
EM
314 }
315
e04004fa 316 /**
317 * Test submit with a membership block in place works with renewal.
318 */
319 public function testSubmitMembershipBlockNotSeparatePaymentProcessorInstantRenew() {
320 $this->setUpMembershipContributionPage();
321 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
322 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1));
323 $submitParams = array(
324 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
325 'id' => (int) $this->_ids['contribution_page'],
326 'amount' => 10,
327 'billing_first_name' => 'Billy',
328 'billing_middle_name' => 'Goat',
329 'billing_last_name' => 'Gruff',
330 'selectMembership' => $this->_ids['membership_type'],
331 'payment_processor_id' => 1,
332 'credit_card_number' => '4111111111111111',
333 'credit_card_type' => 'Visa',
334 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
335 'cvv2' => 123,
336 );
337
338 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
339 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
340 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
341 $this->callAPISuccessGetCount('LineItem', array(
342 'entity_table' => 'civicrm_membership',
343 'entity_id' => $membershipPayment['id'],
344 ), 1);
345
346 $submitParams['contact_id'] = $contribution['contact_id'];
347
348 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
349 $this->callAPISuccessGetCount('LineItem', array(
350 'entity_table' => 'civicrm_membership',
351 'entity_id' => $membershipPayment['id'],
352 ), 2);
353 $membership = $this->callAPISuccessGetSingle('Membership', array(
354 'id' => $membershipPayment['membership_id'],
355 'return' => array('end_date', 'join_date', 'start_date'),
356 ));
357 $this->assertEquals(date('Y-m-d'), $membership['start_date']);
358 $this->assertEquals(date('Y-m-d'), $membership['join_date']);
359 $this->assertEquals(date('Y-m-d', strtotime('+ 2 year - 1 day')), $membership['end_date']);
360 }
361
2f59d010 362 /**
363 * Test submit with a membership block in place.
364 */
365 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
366 $mut = new CiviMailUtils($this, TRUE);
367 $this->setUpMembershipContributionPage();
368 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
369
370 $submitParams = array(
371 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
372 'id' => (int) $this->_ids['contribution_page'],
373 'amount' => 10,
374 'billing_first_name' => 'Billy',
375 'billing_middle_name' => 'Goat',
376 'billing_last_name' => 'Gruff',
377 'selectMembership' => $this->_ids['membership_type'],
378 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 379 'payment_processor_id' => $this->_paymentProcessor['id'],
380 'credit_card_number' => '4111111111111111',
381 'credit_card_type' => 'Visa',
382 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
383 'cvv2' => 123,
2f59d010 384 );
385
5be22f39 386 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
2f59d010 387 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
388 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
389 $mut->checkMailLog(array(
390 'Membership Type: General',
391 ));
392 $mut->stop();
393 $mut->clearMessages();
394 }
395
396 /**
397 * Test submit with a membership block in place.
398 */
399 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
400 $mut = new CiviMailUtils($this, TRUE);
401 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
402 $this->setUpMembershipContributionPage();
403 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
404
405 $submitParams = array(
406 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
407 'id' => (int) $this->_ids['contribution_page'],
408 'amount' => 0,
409 'billing_first_name' => 'Billy',
410 'billing_middle_name' => 'Goat',
411 'billing_last_name' => 'Gruffier',
412 'selectMembership' => $this->_ids['membership_type'],
413 'email-Primary' => 'billy-goat@the-new-bridge.net',
414 );
415
5be22f39 416 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
2f59d010 417 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
418 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
419 $mut->checkMailLog(array(
420 'Membership Type: General',
421 'Gruffier',
422 ),
423 array(
424 'Amount',
425 )
426 );
427 $mut->stop();
9daadfce 428 $mut->clearMessages(999);
2f59d010 429 }
430
f9342903 431 /**
eceb18cc 432 * Test submit with a membership block in place.
f9342903
EM
433 */
434 public function testSubmitMembershipBlockIsSeparatePayment() {
435 $this->setUpMembershipContributionPage(TRUE);
9daadfce 436 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 437 $submitParams = array(
3e28c791 438 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903
EM
439 'id' => (int) $this->_ids['contribution_page'],
440 'amount' => 10,
441 'billing_first_name' => 'Billy',
442 'billing_middle_name' => 'Goat',
443 'billing_last_name' => 'Gruff',
444 'selectMembership' => $this->_ids['membership_type'],
445 );
446
5be22f39 447 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
f9342903
EM
448 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
449 $this->assertCount(2, $contributions['values']);
8e8d287f 450 $lines = $this->callAPISuccess('LineItem', 'get', array('sequential' => 1));
451 $this->assertEquals(10, $lines['values'][0]['line_total']);
f9342903
EM
452 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
453 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
454 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
455 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
456 }
f64a217a 457
2f59d010 458 /**
459 * Test submit with a membership block in place.
460 */
461 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
462 $mut = new CiviMailUtils($this, TRUE);
463 $this->setUpMembershipContributionPage(TRUE);
464 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
465
466 $submitParams = array(
467 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
468 'id' => (int) $this->_ids['contribution_page'],
469 'amount' => 10,
470 'billing_first_name' => 'Billy',
471 'billing_middle_name' => 'Goat',
472 'billing_last_name' => 'Gruff',
473 'selectMembership' => $this->_ids['membership_type'],
474 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 475 'payment_processor_id' => $this->_paymentProcessor['id'],
476 'credit_card_number' => '4111111111111111',
477 'credit_card_type' => 'Visa',
478 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
479 'cvv2' => 123,
2f59d010 480 );
481
5be22f39 482 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
2f59d010 483 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
484 $this->assertCount(2, $contributions['values']);
485 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
486 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
487 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
488 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
de6a9861
JM
489 // We should have two separate email messages, each with their own amount
490 // line and no total line.
491 $mut->checkAllMailLog(
492 array(
493 'Amount: $ 2.00',
494 'Amount: $ 10.00',
495 'Membership Fee',
496 ),
497 array(
c2ce0c2a 498 'Total: $',
461ae02f 499 )
de6a9861 500 );
2f59d010 501 $mut->stop();
9daadfce 502 $mut->clearMessages(999);
2f59d010 503 }
504
505 /**
506 * Test submit with a membership block in place.
507 */
508 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
509 $mut = new CiviMailUtils($this, TRUE);
510 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
511 $this->setUpMembershipContributionPage(TRUE);
512 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
513
514 $submitParams = array(
515 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
516 'id' => (int) $this->_ids['contribution_page'],
517 'amount' => 0,
518 'billing_first_name' => 'Billy',
519 'billing_middle_name' => 'Goat',
520 'billing_last_name' => 'Gruffalo',
521 'selectMembership' => $this->_ids['membership_type'],
522 'payment_processor_id' => 0,
523 'email-Primary' => 'gruffalo@the-bridge.net',
524 );
525
526 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
527 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
528 $this->assertCount(2, $contributions['values']);
529 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
530 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
531 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
532 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
533 $mut->checkMailLog(array(
534 'Gruffalo',
535 'General Membership: $ 0.00',
536 'Membership Fee',
537 ));
538 $mut->stop();
539 $mut->clearMessages();
540 }
541
797c4f88 542 /**
eceb18cc 543 * Test submit with a membership block in place.
797c4f88
EM
544 */
545 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
546 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
547 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
548 $this->setUpMembershipContributionPage(TRUE);
549 $submitParams = array(
550 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
551 'id' => (int) $this->_ids['contribution_page'],
552 'amount' => 10,
553 'billing_first_name' => 'Billy',
554 'billing_middle_name' => 'Goat',
555 'billing_last_name' => 'Gruff',
556 'selectMembership' => $this->_ids['membership_type'][1],
557 );
558
a828d7b8 559 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
6c6e6187 560 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
797c4f88
EM
561 $this->assertCount(2, $contributions['values']);
562 $ids = array_keys($contributions['values']);
563 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
564 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
565 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
566 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
567 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
568 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
569 }
570
8ca611b7 571 /**
eceb18cc 572 * Test submit with a membership block in place.
329f3f66
EM
573 *
574 * We are expecting a separate payment for the membership vs the contribution.
8ca611b7 575 */
329f3f66 576 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
58d8c7e2 577 $mut = new CiviMailUtils($this, TRUE);
8ca611b7 578 $this->setUpMembershipContributionPage(TRUE);
0193ebdb 579 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
580 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
8ca611b7
EM
581 $submitParams = array(
582 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
583 'id' => (int) $this->_ids['contribution_page'],
584 'amount' => 10,
585 'billing_first_name' => 'Billy',
586 'billing_middle_name' => 'Goat',
587 'billing_last_name' => 'Gruff',
58d8c7e2 588 'email-Primary' => 'henry@8th.king',
8ca611b7 589 'selectMembership' => $this->_ids['membership_type'],
7bc789a5 590 'payment_processor_id' => $this->_paymentProcessor['id'],
8ca611b7
EM
591 'credit_card_number' => '4111111111111111',
592 'credit_card_type' => 'Visa',
5896d037 593 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
8ca611b7
EM
594 'cvv2' => 123,
595 );
05990684 596
a828d7b8 597 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 598 $contributions = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
599 'contribution_page_id' => $this->_ids['contribution_page'],
600 'contribution_status_id' => 1,
601 ));
8ca611b7
EM
602 $this->assertCount(2, $contributions['values']);
603 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
604 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
605 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
606 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
f2bca231 607 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('entity_table' => 'civicrm_membership'));
608 $this->assertEquals($lineItem['entity_id'], $membership['id']);
609 $this->assertEquals($lineItem['contribution_id'], $membershipPayment['contribution_id']);
610 $this->assertEquals($lineItem['qty'], 1);
611 $this->assertEquals($lineItem['unit_price'], 2);
612 $this->assertEquals($lineItem['line_total'], 2);
0193ebdb 613 foreach ($contributions['values'] as $contribution) {
614 $this->assertEquals(.72, $contribution['fee_amount']);
615 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
616 }
58d8c7e2 617 // The total string is currently absent & it seems worse with - although at some point
618 // it may have been intended
619 $mut->checkAllMailLog(array('$ 2.00', 'Contribution Amount', '$ 10.00'), array('Total:'));
620 $mut->stop();
621 $mut->clearMessages();
8ca611b7 622 }
f8fe0df6 623
5961bd47
EM
624 /**
625 * Test that when a transaction fails the pending contribution remains.
626 *
627 * An activity should also be created. CRM-16417.
628 */
629 public function testSubmitPaymentProcessorFailure() {
630 $this->setUpContributionPage();
631 $this->setupPaymentProcessor();
632 $this->createLoggedInUser();
633 $priceFieldID = reset($this->_ids['price_field']);
634 $priceFieldValueID = reset($this->_ids['price_field_value']);
635 $submitParams = array(
636 'price_' . $priceFieldID => $priceFieldValueID,
637 'id' => (int) $this->_ids['contribution_page'],
638 'amount' => 10,
639 'payment_processor_id' => 1,
640 'credit_card_number' => '4111111111111111',
641 'credit_card_type' => 'Visa',
642 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
643 'cvv2' => 123,
644 );
645
646 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
647 $contribution = $this->callAPISuccessGetSingle('contribution', array(
648 'contribution_page_id' => $this->_ids['contribution_page'],
382c5680 649 'contribution_status_id' => 'Failed',
5961bd47
EM
650 ));
651
652 $this->callAPISuccessGetSingle('activity', array(
653 'source_record_id' => $contribution['id'],
654 'activity_type_id' => 'Failed Payment',
655 ));
656
5961bd47
EM
657 }
658
f8fe0df6 659 /**
dd1b539a 660 * Test submit recurring membership with immediate confirmation (IATS style).
661 *
e6fa4056 662 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1fee3ad2 663 * processor (IATS style - denoted by returning trxn_id)
e6fa4056
EM
664 * - the first creates a new membership, completed contribution, in progress recurring. Check these
665 * - create another - end date should be extended
f8fe0df6 666 */
8bef2e38 667 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
f8fe0df6 668 $this->params['is_recur'] = 1;
f8fe0df6 669 $this->params['recur_frequency_unit'] = 'month';
670 $this->setUpMembershipContributionPage();
7c85dc65 671 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
8bef2e38 672 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
f69a9ac3 673 $processor = $dummyPP->getPaymentProcessor();
f8fe0df6 674
675 $submitParams = array(
676 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
677 'id' => (int) $this->_ids['contribution_page'],
678 'amount' => 10,
679 'billing_first_name' => 'Billy',
680 'billing_middle_name' => 'Goat',
681 'billing_last_name' => 'Gruff',
682 'email' => 'billy@goat.gruff',
683 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 684 'payment_processor_id' => 1,
f8fe0df6 685 'credit_card_number' => '4111111111111111',
686 'credit_card_type' => 'Visa',
5896d037 687 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 688 'cvv2' => 123,
689 'is_recur' => 1,
690 'frequency_interval' => 1,
691 'frequency_unit' => 'month',
692 );
693
a828d7b8 694 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 695 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
696 'contribution_page_id' => $this->_ids['contribution_page'],
697 'contribution_status_id' => 1,
698 ));
f69a9ac3 699 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
0739d6cf 700
701 $this->assertEquals('create_first_success', $contribution['trxn_id']);
f8fe0df6 702 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
703 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
704 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
705 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
706 $this->assertEquals(1, $membership['status_id']);
707 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
356bfcaf 708
709 $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
f8fe0df6 710 //renew it with processor setting completed - should extend membership
711 $submitParams['contact_id'] = $contribution['contact_id'];
8bef2e38 712 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
f8fe0df6 713 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
5896d037 714 $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
715 'id' => array('NOT IN' => array($contribution['id'])),
716 'contribution_page_id' => $this->_ids['contribution_page'],
717 'contribution_status_id' => 1,
718 ));
f8fe0df6 719 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
720 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
721 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
f69a9ac3 722 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
91259407 723 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
f8fe0df6 724 }
725
13a16f43 726 /**
727 * Test submit recurring membership with immediate confirmation (IATS style).
728 *
729 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
730 * processor (IATS style - denoted by returning trxn_id)
731 * - the first creates a new membership, completed contribution, in progress recurring. Check these
732 * - create another - end date should be extended
733 */
65e172a3 734 public function testSubmitMembershipComplexNonPriceSetPaymentPaymentProcessorRecurInstantPayment() {
13a16f43 735 $this->params['is_recur'] = 1;
736 $this->params['recur_frequency_unit'] = 'month';
737 // Add a membership so membership & contribution are not both 1.
738 $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
739 $this->setUpMembershipContributionPage();
740 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
741 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
742 $processor = $dummyPP->getPaymentProcessor();
743
744 $submitParams = array(
745 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
746 'price_' . $this->_ids['price_field']['cont'] => 88,
747 'id' => (int) $this->_ids['contribution_page'],
748 'amount' => 10,
749 'billing_first_name' => 'Billy',
750 'billing_middle_name' => 'Goat',
751 'billing_last_name' => 'Gruff',
752 'email' => 'billy@goat.gruff',
753 'selectMembership' => $this->_ids['membership_type'],
754 'payment_processor_id' => 1,
755 'credit_card_number' => '4111111111111111',
756 'credit_card_type' => 'Visa',
757 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
758 'cvv2' => 123,
759 'is_recur' => 1,
760 'frequency_interval' => 1,
761 'frequency_unit' => 'month',
762 );
763
764 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
765 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
766 'contribution_page_id' => $this->_ids['contribution_page'],
767 'contribution_status_id' => 1,
768 ));
769 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
770
771 $this->assertEquals('create_first_success', $contribution['trxn_id']);
772 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
773 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
774 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
775 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
776 $this->assertEquals(1, $membership['status_id']);
777 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
778
779 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $contribution['id']));
780 $this->assertEquals(2, $lines['count']);
781 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
782 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
783 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
784 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
785 $this->callAPISuccessGetSingle('MembershipPayment', array('contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID + 1));
786
787 //renew it with processor setting completed - should extend membership
788 $submitParams['contact_id'] = $contribution['contact_id'];
789 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
790 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
791 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', array(
792 'id' => array('NOT IN' => array($contribution['id'])),
793 'contribution_page_id' => $this->_ids['contribution_page'],
794 'contribution_status_id' => 1,
795 ));
796 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $renewContribution['id']));
797 $this->assertEquals(2, $lines['count']);
798 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
799 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
800 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
801 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
802
803 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
804 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
805 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
806 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
807 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
808 }
809
65e172a3 810 /**
811 * Test submit recurring membership with immediate confirmation (IATS style).
812 *
813 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
814 * processor (IATS style - denoted by returning trxn_id)
815 * - the first creates a new membership, completed contribution, in progress recurring. Check these
816 * - create another - end date should be extended
817 */
818 public function testSubmitMembershipComplexPriceSetPaymentPaymentProcessorRecurInstantPayment() {
819 $this->params['is_recur'] = 1;
820 $this->params['recur_frequency_unit'] = 'month';
821 // Add a membership so membership & contribution are not both 1.
822 $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
823 $this->createPriceSetWithPage();
824 $this->addSecondOrganizationMembershipToPriceSet();
825 $this->setupPaymentProcessor();
826
827 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
828 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
829 $processor = $dummyPP->getPaymentProcessor();
830
831 $submitParams = array(
832 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value']['cont'],
833 'price_' . $this->_ids['price_field']['org1'] => $this->_ids['price_field_value']['org1'],
834 'price_' . $this->_ids['price_field']['org2'] => $this->_ids['price_field_value']['org2'],
835 'id' => (int) $this->_ids['contribution_page'],
836 'amount' => 10,
837 'billing_first_name' => 'Billy',
838 'billing_middle_name' => 'Goat',
839 'billing_last_name' => 'Gruff',
840 'email' => 'billy@goat.gruff',
841 'selectMembership' => NULL,
842 'payment_processor_id' => 1,
843 'credit_card_number' => '4111111111111111',
844 'credit_card_type' => 'Visa',
845 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
846 'cvv2' => 123,
847 'frequency_interval' => 1,
848 'frequency_unit' => 'month',
849 );
850
851 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
852 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
853 'contribution_page_id' => $this->_ids['contribution_page'],
854 'contribution_status_id' => 1,
855 ));
856 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
857
858 $this->assertEquals('create_first_success', $contribution['trxn_id']);
859 $membershipPayments = $this->callAPISuccess('membership_payment', 'get', array(
860 'sequential' => 1,
861 'contribution_id' => $contribution['id'],
862 ));
863 $this->assertEquals(2, $membershipPayments['count']);
864 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $contribution['id']));
865 $this->assertEquals(3, $lines['count']);
866 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
867 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
868 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
869 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
870 $this->assertEquals('civicrm_membership', $lines['values'][2]['entity_table']);
871 $this->assertEquals($preExistingMembershipID + 2, $lines['values'][2]['entity_id']);
872
873 $this->callAPISuccessGetSingle('MembershipPayment', array('contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID + 1));
874 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $preExistingMembershipID + 1));
875
876 //renew it with processor setting completed - should extend membership
877 $submitParams['contact_id'] = $contribution['contact_id'];
878 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
879 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
880 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', array(
881 'id' => array('NOT IN' => array($contribution['id'])),
882 'contribution_page_id' => $this->_ids['contribution_page'],
883 'contribution_status_id' => 1,
884 ));
885 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $renewContribution['id']));
886 $this->assertEquals(3, $lines['count']);
887 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
888 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
889 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
890 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
891
892 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $preExistingMembershipID + 1));
893 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
894 }
895
896 /**
897 * Extend the price set with a second organisation's membership.
898 */
899 public function addSecondOrganizationMembershipToPriceSet() {
900 $organization2ID = $this->organizationCreate();
901 $membershipTypes = $this->callAPISuccess('MembershipType', 'get', array());
902 $this->_ids['membership_type'] = array_keys($membershipTypes['values']);
903 $this->_ids['membership_type']['org2'] = $this->membershipTypeCreate(array('contact_id' => $organization2ID, 'name' => 'Org 2'));
904 $priceField = $this->callAPISuccess('PriceField', 'create', array(
905 'price_set_id' => $this->_ids['price_set'],
906 'html_type' => 'Radio',
907 'name' => 'Org1 Price',
908 'label' => 'Org1Price',
909 ));
910 $this->_ids['price_field']['org1'] = $priceField['id'];
911
912 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
913 'name' => 'org1 amount',
914 'label' => 'org 1 Amount',
915 'amount' => 2,
916 'financial_type_id' => 'Member Dues',
917 'format.only_id' => TRUE,
918 'membership_type_id' => reset($this->_ids['membership_type']),
919 'price_field_id' => $priceField['id'],
920 ));
921 $this->_ids['price_field_value']['org1'] = $priceFieldValue;
922
923 $priceField = $this->callAPISuccess('PriceField', 'create', array(
924 'price_set_id' => $this->_ids['price_set'],
925 'html_type' => 'Radio',
926 'name' => 'Org2 Price',
927 'label' => 'Org2Price',
928 ));
929 $this->_ids['price_field']['org2'] = $priceField['id'];
930
931 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
932 'name' => 'org2 amount',
933 'label' => 'org 2 Amount',
934 'amount' => 200,
935 'financial_type_id' => 'Member Dues',
936 'format.only_id' => TRUE,
937 'membership_type_id' => $this->_ids['membership_type']['org2'],
938 'price_field_id' => $priceField['id'],
939 ));
940 $this->_ids['price_field_value']['org2'] = $priceFieldValue;
941
942 }
943
449f4c90 944 /**
945 * Test submit recurring membership with immediate confirmation (IATS style).
946 *
947 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
948 * processor (IATS style - denoted by returning trxn_id)
949 * - the first creates a new membership, completed contribution, in progress recurring. Check these
950 * - create another - end date should be extended
951 */
952 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
953
954 $this->setUpMembershipContributionPage(TRUE);
955 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
956 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
957
958 $submitParams = array(
959 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
960 'id' => (int) $this->_ids['contribution_page'],
961 'amount' => 10,
962 'billing_first_name' => 'Billy',
963 'billing_middle_name' => 'Goat',
964 'billing_last_name' => 'Gruff',
965 'email' => 'billy@goat.gruff',
966 'selectMembership' => $this->_ids['membership_type'],
967 'payment_processor_id' => 1,
968 'credit_card_number' => '4111111111111111',
969 'credit_card_type' => 'Visa',
970 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
971 'cvv2' => 123,
972 'is_recur' => 1,
973 'auto_renew' => TRUE,
974 'frequency_interval' => 1,
975 'frequency_unit' => 'month',
976 );
977
978 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
979 $contribution = $this->callAPISuccess('contribution', 'get', array(
980 'contribution_page_id' => $this->_ids['contribution_page'],
981 'contribution_status_id' => 1,
982 ));
983
984 $this->assertEquals(2, $contribution['count']);
985 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
986 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
987 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
988 $this->callAPISuccess('contribution_recur', 'getsingle', array());
989 }
990
f8fe0df6 991 /**
e6fa4056
EM
992 * Test submit recurring membership with delayed confirmation (Authorize.net style)
993 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
994 * processor (Authorize.net style - denoted by NOT returning trxn_id)
995 * - the first creates a pending membership, pending contribution, penging recurring. Check these
996 * - complete the transaction
997 * - create another - end date should NOT be extended
f8fe0df6 998 */
999 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
1000 $this->params['is_recur'] = 1;
f8fe0df6 1001 $this->params['recur_frequency_unit'] = 'month';
1002 $this->setUpMembershipContributionPage();
ab30e033 1003 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
b5eacf76 1004 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
356bfcaf 1005 $this->membershipTypeCreate(array('name' => 'Student'));
1006
1007 // Add a contribution & a couple of memberships so the id will not be 1 & will differ from membership id.
1008 // This saves us from 'accidental success'.
51f8c59c 1009 $this->contributionCreate(array('contact_id' => $this->contactIds[0]));
356bfcaf 1010 $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
1011 $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0], 'membership_type_id' => 'Student'));
f8fe0df6 1012
1013 $submitParams = array(
1014 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1015 'id' => (int) $this->_ids['contribution_page'],
1016 'amount' => 10,
1017 'billing_first_name' => 'Billy',
1018 'billing_middle_name' => 'Goat',
1019 'billing_last_name' => 'Gruff',
1020 'email' => 'billy@goat.gruff',
1021 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 1022 'payment_processor_id' => 1,
f8fe0df6 1023 'credit_card_number' => '4111111111111111',
1024 'credit_card_type' => 'Visa',
5896d037 1025 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 1026 'cvv2' => 123,
e6fa4056
EM
1027 'is_recur' => 1,
1028 'frequency_interval' => 1,
1029 'frequency_unit' => 'month',
f8fe0df6 1030 );
1031
a828d7b8 1032 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 1033 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1034 'contribution_page_id' => $this->_ids['contribution_page'],
1035 'contribution_status_id' => 2,
1036 ));
356bfcaf 1037
f8fe0df6 1038 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
1039 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
1040 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1041 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
e6fa4056 1042 $this->assertEquals(5, $membership['status_id']);
51f8c59c 1043
1044 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
1045 $this->assertEquals('civicrm_membership', $line['entity_table']);
1046 $this->assertEquals($membership['id'], $line['entity_id']);
1047
5896d037 1048 $this->callAPISuccess('contribution', 'completetransaction', array(
92915c55
TO
1049 'id' => $contribution['id'],
1050 'trxn_id' => 'ipn_called',
b396c447 1051 'payment_processor_id' => $this->_paymentProcessor['id'],
92915c55 1052 ));
51f8c59c 1053 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
1054 $this->assertEquals('civicrm_membership', $line['entity_table']);
1055 $this->assertEquals($membership['id'], $line['entity_id']);
1056
e6fa4056 1057 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
f8fe0df6 1058 //renew it with processor setting completed - should extend membership
1059 $submitParams = array_merge($submitParams, array(
5896d037
TO
1060 'contact_id' => $contribution['contact_id'],
1061 'is_recur' => 1,
1062 'frequency_interval' => 1,
1063 'frequency_unit' => 'month',
1064 )
f8fe0df6 1065 );
51f8c59c 1066
b5eacf76 1067 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 1068 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
6c6e6187 1069 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
5896d037 1070 'id' => array(
21dfd5f5 1071 'NOT IN' => array($contribution['id']),
5896d037
TO
1072 ),
1073 'contribution_page_id' => $this->_ids['contribution_page'],
21dfd5f5 1074 'contribution_status_id' => 2,
5896d037 1075 )
f8fe0df6 1076 );
51f8c59c 1077 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $newContribution['id']));
1078 $this->assertEquals('civicrm_membership', $line['entity_table']);
1079 $this->assertEquals($membership['id'], $line['entity_id']);
e6fa4056 1080
f8fe0df6 1081 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1082 //no renewal as the date hasn't changed
1083 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
e6fa4056
EM
1084 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
1085 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
f8fe0df6 1086 }
1087
f9342903 1088 /**
eceb18cc 1089 * Set up membership contribution page.
f9342903
EM
1090 * @param bool $isSeparatePayment
1091 */
00be9182 1092 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
f9342903 1093 $this->setUpMembershipBlockPriceSet();
a380f4a0 1094 $this->setupPaymentProcessor();
f9342903
EM
1095 $this->setUpContributionPage();
1096
1097 $this->callAPISuccess('membership_block', 'create', array(
1098 'entity_id' => $this->_ids['contribution_page'],
1099 'entity_table' => 'civicrm_contribution_page',
1100 'is_required' => TRUE,
1101 'is_active' => TRUE,
1102 'is_separate_payment' => $isSeparatePayment,
1103 'membership_type_default' => $this->_ids['membership_type'],
1104 ));
f64a217a
EM
1105 }
1106
dccd9f4f
ERL
1107 /**
1108 * Set up pledge block.
1109 */
1110 public function setUpPledgeBlock() {
1111 $params = array(
1112 'entity_table' => 'civicrm_contribution_page',
1113 'entity_id' => $this->_ids['contribution_page'],
1114 'pledge_frequency_unit' => 'week',
1115 'is_pledge_interval' => 0,
1116 'pledge_start_date' => json_encode(array('calendar_date' => date('Ymd', strtotime("+1 month")))),
1117 );
1118 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::create($params);
1119 $this->_ids['pledge_block_id'] = $pledgeBlock->id;
1120 }
1121
f9342903 1122 /**
dd1b539a 1123 * The default data set does not include a complete default membership price set - not quite sure why.
1124 *
f9342903
EM
1125 * This function ensures it exists & populates $this->_ids with it's data
1126 */
00be9182 1127 public function setUpMembershipBlockPriceSet() {
5896d037 1128 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
92915c55
TO
1129 'name' => 'default_membership_type_amount',
1130 'return' => 'id',
1131 ));
f9342903 1132 if (empty($this->_ids['membership_type'])) {
3e28c791 1133 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 1134 }
3e28c791
EM
1135 $priceField = $this->callAPISuccess('price_field', 'create', array(
1136 'price_set_id' => reset($this->_ids['price_set']),
1137 'name' => 'membership_amount',
1138 'label' => 'Membership Amount',
1139 'html_type' => 'Radio',
1140 'sequential' => 1,
1141 ));
1142 $this->_ids['price_field'][] = $priceField['id'];
dd1b539a 1143
3e28c791
EM
1144 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
1145 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
f9342903
EM
1146 'name' => 'membership_amount',
1147 'label' => 'Membership Amount',
65e172a3 1148 'amount' => 2,
43dbd988 1149 'financial_type_id' => 'Donation',
3e28c791
EM
1150 'format.only_id' => TRUE,
1151 'membership_type_id' => $membershipTypeID,
1152 'price_field_id' => $priceField['id'],
f9342903 1153 ));
840e3907 1154 $this->_ids['price_field_value'][] = $priceFieldValue;
f9342903 1155 }
65e172a3 1156 if (!empty($this->_ids['membership_type']['org2'])) {
1157 $priceField = $this->callAPISuccess('price_field', 'create', array(
1158 'price_set_id' => reset($this->_ids['price_set']),
1159 'name' => 'membership_org2',
1160 'label' => 'Membership Org2',
1161 'html_type' => 'Checkbox',
1162 'sequential' => 1,
1163 ));
1164 $this->_ids['price_field']['org2'] = $priceField['id'];
1165
1166 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1167 'name' => 'membership_org2',
1168 'label' => 'Membership org 2',
1169 'amount' => 55,
1170 'financial_type_id' => 'Member Dues',
1171 'format.only_id' => TRUE,
1172 'membership_type_id' => $this->_ids['membership_type']['org2'],
1173 'price_field_id' => $priceField['id'],
1174 ));
1175 $this->_ids['price_field_value']['org2'] = $priceFieldValue;
1176 }
13a16f43 1177 $priceField = $this->callAPISuccess('price_field', 'create', array(
1178 'price_set_id' => reset($this->_ids['price_set']),
1179 'name' => 'Contribution',
1180 'label' => 'Contribution',
1181 'html_type' => 'Text',
1182 'sequential' => 1,
1183 'is_enter_qty' => 1,
1184 ));
1185 $this->_ids['price_field']['cont'] = $priceField['id'];
1186 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1187 'name' => 'contribution',
1188 'label' => 'Give me money',
1189 'amount' => 88,
1190 'financial_type_id' => 'Donation',
1191 'format.only_id' => TRUE,
1192 'price_field_id' => $priceField['id'],
1193 ));
1194 $this->_ids['price_field_value'][] = $priceFieldValue;
f9342903 1195 }
3e28c791 1196
2f59d010 1197 /**
1198 * Add text field other amount to the price set.
1199 */
1200 public function addOtherAmountFieldToMembershipPriceSet() {
1201 $this->_ids['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
1202 'price_set_id' => reset($this->_ids['price_set']),
1203 'name' => 'other_amount',
1204 'label' => 'Other Amount',
1205 'html_type' => 'Text',
1206 'format.only_id' => TRUE,
1207 'sequential' => 1,
1208 ));
1209 $this->_ids['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
1210 'financial_type_id' => 'Donation',
1211 'format.only_id' => TRUE,
1212 'label' => 'Other Amount',
1213 'amount' => 1,
1214 'price_field_id' => $this->_ids['price_field']['other_amount'],
1215 ));
1216 }
1217
f64a217a 1218 /**
eceb18cc 1219 * Help function to set up contribution page with some defaults.
f64a217a 1220 */
00be9182 1221 public function setUpContributionPage() {
f64a217a
EM
1222 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
1223 if (empty($this->_ids['price_set'])) {
1224 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
1225 $this->_ids['price_set'][] = $priceSet['id'];
1226 }
1227 $priceSetID = reset($this->_ids['price_set']);
5896d037 1228 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
f9342903
EM
1229
1230 if (empty($this->_ids['price_field'])) {
1231 $priceField = $this->callAPISuccess('price_field', 'create', array(
1232 'price_set_id' => $priceSetID,
1233 'label' => 'Goat Breed',
1234 'html_type' => 'Radio',
1235 ));
1236 $this->_ids['price_field'] = array($priceField['id']);
1237 }
1238 if (empty($this->_ids['price_field_value'])) {
1239 $this->callAPISuccess('price_field_value', 'create', array(
1240 'price_set_id' => $priceSetID,
1241 'price_field_id' => $priceField['id'],
1242 'label' => 'Long Haired Goat',
43dbd988 1243 'financial_type_id' => 'Donation',
f9342903 1244 'amount' => 20,
05465712 1245 'non_deductible_amount' => 15,
f9342903
EM
1246 )
1247 );
1248 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1249 'price_set_id' => $priceSetID,
1250 'price_field_id' => $priceField['id'],
1251 'label' => 'Shoe-eating Goat',
43dbd988 1252 'financial_type_id' => 'Donation',
f9342903 1253 'amount' => 10,
05465712 1254 'non_deductible_amount' => 5,
f9342903
EM
1255 )
1256 );
1257 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1258 }
f64a217a 1259 $this->_ids['contribution_page'] = $contributionPageResult['id'];
be26f3e0
EM
1260 }
1261
6a488035 1262 public static function setUpBeforeClass() {
6c6e6187 1263 // put stuff here that should happen before all tests in this unit
6a488035
TO
1264 }
1265
5896d037 1266 public static function tearDownAfterClass() {
6a488035
TO
1267 $tablesToTruncate = array(
1268 'civicrm_contact',
1269 'civicrm_financial_type',
1270 'civicrm_contribution',
1271 'civicrm_contribution_page',
1272 );
1273 $unitTest = new CiviUnitTestCase();
1274 $unitTest->quickCleanup($tablesToTruncate);
1275 }
96025800 1276
a380f4a0
EM
1277 /**
1278 * Create a payment processor instance.
1279 */
1280 protected function setupPaymentProcessor() {
1281 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
1282 'payment_processor_type_id' => 'Dummy',
1283 'class_name' => 'Payment_Dummy',
1284 'billing_mode' => 1,
1285 ));
1286 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
1287 }
1288
dccd9f4f
ERL
1289 /**
1290 * Test submit recurring pledge.
1291 *
1292 * - we process 1 pledge with a future start date. A recur contribution and the pledge should be created with first payment date in the future.
1293 */
1294 public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() {
1295 $this->params['adjust_recur_start_date'] = TRUE;
1296 $this->params['is_pay_later'] = FALSE;
1297 $this->setUpContributionPage();
1298 $this->setUpPledgeBlock();
1299 $this->setupPaymentProcessor();
1300 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1301 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
1302
1303 $submitParams = array(
1304 'id' => (int) $this->_ids['contribution_page'],
1305 'amount' => 100,
1306 'billing_first_name' => 'Billy',
1307 'billing_middle_name' => 'Goat',
1308 'billing_last_name' => 'Gruff',
1309 'email' => 'billy@goat.gruff',
1310 'payment_processor_id' => 1,
1311 'credit_card_number' => '4111111111111111',
1312 'credit_card_type' => 'Visa',
1313 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1314 'cvv2' => 123,
1315 'pledge_frequency_interval' => 1,
1316 'pledge_frequency_unit' => 'week',
1317 'pledge_installments' => 3,
1318 'is_pledge' => TRUE,
1319 'pledge_block_id' => (int) $this->_ids['pledge_block_id'],
1320 );
1321
1322 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1323
1324 // Check if contribution created.
1325 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1326 'contribution_page_id' => $this->_ids['contribution_page'],
1327 'contribution_status_id' => 'Completed', // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1328 ));
1329
1330 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1331
1332 // Check if pledge created.
1333 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1334 $this->assertEquals(date('Ymd', strtotime($pledge['pledge_start_date'])), date('Ymd', strtotime("+1 month")));
1335 $this->assertEquals($pledge['pledge_amount'], 300.00);
1336
1337 // Check if pledge payments created.
1338 $params = array(
1339 'pledge_id' => $pledge['id'],
1340 );
1341 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1342 $this->assertEquals($pledgePayment['count'], 3);
1343 $this->assertEquals(date('Ymd', strtotime($pledgePayment['values'][1]['scheduled_date'])), date('Ymd', strtotime("+1 month")));
1344 $this->assertEquals($pledgePayment['values'][1]['scheduled_amount'], 100.00);
1345 $this->assertEquals($pledgePayment['values'][1]['status_id'], 1); // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1346
1347 // Check contribution recur record.
1348 $recur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
1349 $this->assertEquals(date('Ymd', strtotime($recur['start_date'])), date('Ymd', strtotime("+1 month")));
1350 $this->assertEquals($recur['amount'], 100.00);
1351 $this->assertEquals($recur['contribution_status_id'], 5); // In progress status.
1352 }
1353
603577b2
E
1354 /**
1355 * Test submit pledge payment.
1356 *
1357 * - test submitting a pledge payment using contribution form.
1358 */
1359 public function testSubmitPledgePayment() {
1360 $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment();
1361 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1362 $params = array(
1363 'pledge_id' => $pledge['id'],
1364 );
1365 $submitParams = array(
1366 'id' => (int) $pledge['pledge_contribution_page_id'],
1367 'pledge_amount' => array(2 => 1),
1368 'billing_first_name' => 'Billy',
1369 'billing_middle_name' => 'Goat',
1370 'billing_last_name' => 'Gruff',
1371 'email' => 'billy@goat.gruff',
1372 'payment_processor_id' => 1,
1373 'credit_card_number' => '4111111111111111',
1374 'credit_card_type' => 'Visa',
1375 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1376 'cvv2' => 123,
1377 'pledge_id' => $pledge['id'],
1378 'cid' => $pledge['contact_id'],
1379 'contact_id' => $pledge['contact_id'],
1380 'amount' => 100.00,
1381 'is_pledge' => TRUE,
1382 'pledge_block_id' => $this->_ids['pledge_block_id'],
1383 );
1384 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1385 $this->assertEquals($pledgePayment['values'][2]['status_id'], 2);
1386
1387 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1388
1389 // Check if contribution created.
1390 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1391 'contribution_page_id' => $pledge['pledge_contribution_page_id'],
1392 'contribution_status_id' => 'Completed',
1393 'contact_id' => $pledge['contact_id'],
1394 'contribution_recur_id' => array('IS NULL' => 1),
1395 ));
1396
1397 $this->assertEquals(100.00, $contribution['total_amount']);
1398 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1399 $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed");
1400 $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);
1401 }
1402
a93e9452
PN
1403 /**
1404 * Test form submission with multiple option price set.
1405 */
1406 public function testSubmitContributionPageWithPriceSet() {
1407 $this->_priceSetParams['is_quick_config'] = 0;
1408 $this->setUpContributionPage();
1409 $submitParams = array(
1410 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1411 'id' => (int) $this->_ids['contribution_page'],
1412 'amount' => 80,
1413 'first_name' => 'Billy',
1414 'last_name' => 'Gruff',
1415 'email' => 'billy@goat.gruff',
1416 'is_pay_later' => TRUE,
1417 );
1418 $this->addPriceFields($submitParams);
1419
1420 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1421 $contribution = $this->callAPISuccessGetSingle('contribution', array(
1422 'contribution_page_id' => $this->_ids['contribution_page'],
1423 'contribution_status_id' => 2,
1424 ));
1425 $this->callAPISuccessGetCount(
1426 'LineItem',
1427 array(
1428 'contribution_id' => $contribution['id'],
1429 ),
1430 3
1431 );
1432 }
1433
1434 /**
1435 * Function to add additional price fields to priceset.
1436 * @param array $params
1437 */
1438 public function addPriceFields(&$params) {
1439 $priceSetID = reset($this->_ids['price_set']);
1440 $priceField = $this->callAPISuccess('price_field', 'create', array(
1441 'price_set_id' => $priceSetID,
1442 'label' => 'Chicken Breed',
1443 'html_type' => 'CheckBox',
1444 ));
1445 $priceFieldValue1 = $this->callAPISuccess('price_field_value', 'create', array(
1446 'price_set_id' => $priceSetID,
1447 'price_field_id' => $priceField['id'],
1448 'label' => 'Shoe-eating chicken -1',
1449 'financial_type_id' => 'Donation',
1450 'amount' => 30,
1451 ));
1452 $priceFieldValue2 = $this->callAPISuccess('price_field_value', 'create', array(
1453 'price_set_id' => $priceSetID,
1454 'price_field_id' => $priceField['id'],
1455 'label' => 'Shoe-eating chicken -2',
1456 'financial_type_id' => 'Donation',
1457 'amount' => 40,
1458 ));
1459 $params['price_' . $priceField['id']] = array(
1460 $priceFieldValue1['id'] => 1,
1461 $priceFieldValue2['id'] => 1,
1462 );
1463 }
1464
57d395bb
K
1465 /**
1466 * Test Tax Amount is calculated properly when using PriceSet with Field Type = Text/Numeric Quantity
1467 */
1468 public function testSubmitContributionPageWithPriceSetQuantity() {
1469 $this->_priceSetParams['is_quick_config'] = 0;
1470 $this->enableTaxAndInvoicing();
1471 $financialType = $this->createFinancialType();
1472 $financialTypeId = $financialType['id'];
1473 // This function sets the Tax Rate at 10% - it currently has no way to pass Tax Rate into it - so let's work with 10%
1474 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5);
1475
1476 $this->setUpContributionPage();
1477 $submitParams = array(
1478 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1479 'id' => (int) $this->_ids['contribution_page'],
1480 'first_name' => 'J',
1481 'last_name' => 'T',
1482 'email' => 'JT@ohcanada.ca',
1483 'is_pay_later' => TRUE,
1484 );
1485
1486 // Create PriceSet/PriceField
1487 $priceSetID = reset($this->_ids['price_set']);
1488 $priceField = $this->callAPISuccess('price_field', 'create', array(
1489 'price_set_id' => $priceSetID,
1490 'label' => 'Printing Rights',
1491 'html_type' => 'Text',
1492 ));
1493 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1494 'price_set_id' => $priceSetID,
1495 'price_field_id' => $priceField['id'],
1496 'label' => 'Printing Rights',
1497 'financial_type_id' => $financialTypeId,
1498 'amount' => '16.95',
1499 ));
1500 $priceFieldId = $priceField['id'];
1501
1502 // Set quantity for our test
1503 $submitParams['price_' . $priceFieldId] = 180;
1504
1505 // contribution_page submit requires amount and tax_amount - and that's ok we're not testing that - we're testing at the LineItem level
1506 $submitParams['amount'] = 180 * 16.95;
1507 // This is the correct Tax Amount - use it later to compare to what the CiviCRM Core came up with at the LineItem level
1508 $submitParams['tax_amount'] = 180 * 16.95 * 0.10;
1509
1510 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1511 $contribution = $this->callAPISuccessGetSingle('contribution', array(
1512 'contribution_page_id' => $this->_ids['contribution_page'],
1513 ));
1514
1515 // Retrieve the lineItem that belongs to the Printing Rights and check the tax_amount CiviCRM Core calculated for it
1516 $lineItem = $this->callAPISuccess('LineItem', 'get', array(
1517 'contribution_id' => $contribution['id'],
1518 'label' => 'Printing Rights',
1519 ));
1520 $lineItemId = $lineItem['id'];
1521 $lineItem_TaxAmount = round($lineItem['values'][$lineItemId]['tax_amount'], 2);
1522
1523 // Compare this to what it should be!
1524 $this->assertEquals($lineItem_TaxAmount, round($submitParams['tax_amount'], 2), 'Wrong Sales Tax Amount is calculated and stored.');
1525 }
1526
6a488035 1527}