Merge pull request #10946 from mattwire/CRM-21037_activity_sendsms_unittests
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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_recur* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class 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';
42 protected $contribution_result = NULL;
43 protected $_priceSetParams = array();
44 protected $_membershipBlockAmount = 2;
45 /**
46 * Payment processor details.
47 * @var array
48 */
49 protected $_paymentProcessor = array();
50
51 /**
52 * @var array
53 * - contribution_page
54 * - price_set
55 * - price_field
56 * - price_field_value
57 */
58 protected $_ids = array();
59
60
61 public $DBResetRequired = TRUE;
62
63 public function setUp() {
64 parent::setUp();
65 $this->contactIds[] = $this->individualCreate();
66 $this->params = array(
67 'title' => "Test Contribution Page",
68 'financial_type_id' => 1,
69 'currency' => 'NZD',
70 'goal_amount' => $this->testAmount,
71 'is_pay_later' => 1,
72 'is_monetary' => TRUE,
73 'is_email_receipt' => TRUE,
74 'receipt_from_email' => 'yourconscience@donate.com',
75 'receipt_from_name' => 'Ego Freud',
76 );
77
78 $this->_priceSetParams = array(
79 'is_quick_config' => 1,
80 'extends' => 'CiviContribute',
81 'financial_type_id' => 'Donation',
82 'title' => 'my Page',
83 );
84 }
85
86 public function tearDown() {
87 foreach ($this->contactIds as $id) {
88 $this->callAPISuccess('contact', 'delete', array('id' => $id));
89 }
90 $this->quickCleanUpFinancialEntities();
91 }
92
93 public function testCreateContributionPage() {
94 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
95 $this->assertEquals(1, $result['count']);
96 $this->assertNotNull($result['values'][$result['id']]['id']);
97 $this->getAndCheck($this->params, $result['id'], $this->_entity);
98 }
99
100 public function testGetBasicContributionPage() {
101 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
102 $this->id = $createResult['id'];
103 $getParams = array(
104 'currency' => 'NZD',
105 'financial_type_id' => 1,
106 );
107 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
108 $this->assertEquals(1, $getResult['count']);
109 }
110
111 public function testGetContributionPageByAmount() {
112 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
113 $this->id = $createResult['id'];
114 $getParams = array(
115 'amount' => '' . $this->testAmount, // 3456
116 'currency' => 'NZD',
117 'financial_type_id' => 1,
118 );
119 $getResult = $this->callAPISuccess($this->_entity, 'get', $getParams);
120 $this->assertEquals(1, $getResult['count']);
121 }
122
123 public function testDeleteContributionPage() {
124 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
125 $deleteParams = array('id' => $createResult['id']);
126 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
127 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
128 $this->assertEquals(0, $checkDeleted['count']);
129 }
130
131 public function testGetFieldsContributionPage() {
132 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
133 $this->assertEquals(12, $result['values']['start_date']['type']);
134 }
135
136
137 /**
138 * Test form submission with basic price set.
139 */
140 public function testSubmit() {
141 $this->setUpContributionPage();
142 $priceFieldID = reset($this->_ids['price_field']);
143 $priceFieldValueID = reset($this->_ids['price_field_value']);
144 $submitParams = array(
145 'price_' . $priceFieldID => $priceFieldValueID,
146 'id' => (int) $this->_ids['contribution_page'],
147 'amount' => 10,
148 );
149
150 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
151 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
152 //assert non-deductible amount
153 $this->assertEquals(5.00, $contribution['non_deductible_amount']);
154 }
155
156 /**
157 * Test form submission with billing first & last name where the contact does NOT
158 * otherwise have one.
159 */
160 public function testSubmitNewBillingNameData() {
161 $this->setUpContributionPage();
162 $contact = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com'));
163 $priceFieldID = reset($this->_ids['price_field']);
164 $priceFieldValueID = reset($this->_ids['price_field_value']);
165 $submitParams = array(
166 'price_' . $priceFieldID => $priceFieldValueID,
167 'id' => (int) $this->_ids['contribution_page'],
168 'amount' => 10,
169 'billing_first_name' => 'Wonder',
170 'billing_last_name' => 'Woman',
171 'contactID' => $contact['id'],
172 'email' => 'wonderwoman@amazon.com',
173 );
174
175 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
176 $contact = $this->callAPISuccess('Contact', 'get', array(
177 'id' => $contact['id'],
178 'return' => array(
179 'first_name',
180 'last_name',
181 'sort_name',
182 'display_name',
183 ),
184 ));
185 $this->assertEquals(array(
186 'first_name' => 'Wonder',
187 'last_name' => 'Woman',
188 'display_name' => 'Wonder Woman',
189 'sort_name' => 'Woman, Wonder',
190 'id' => $contact['id'],
191 'contact_id' => $contact['id'],
192 ), $contact['values'][$contact['id']]);
193
194 }
195
196 /**
197 * Test form submission with billing first & last name where the contact does
198 * otherwise have one and should not be overwritten.
199 */
200 public function testSubmitNewBillingNameDoNotOverwrite() {
201 $this->setUpContributionPage();
202 $contact = $this->callAPISuccess('Contact', 'create', array(
203 'contact_type' => 'Individual',
204 'email' => 'wonderwoman@amazon.com',
205 'first_name' => 'Super',
206 'last_name' => 'Boy',
207 ));
208 $priceFieldID = reset($this->_ids['price_field']);
209 $priceFieldValueID = reset($this->_ids['price_field_value']);
210 $submitParams = array(
211 'price_' . $priceFieldID => $priceFieldValueID,
212 'id' => (int) $this->_ids['contribution_page'],
213 'amount' => 10,
214 'billing_first_name' => 'Wonder',
215 'billing_last_name' => 'Woman',
216 'contactID' => $contact['id'],
217 'email' => 'wonderwoman@amazon.com',
218 );
219
220 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
221 $contact = $this->callAPISuccess('Contact', 'get', array(
222 'id' => $contact['id'],
223 'return' => array(
224 'first_name',
225 'last_name',
226 'sort_name',
227 'display_name',
228 ),
229 ));
230 $this->assertEquals(array(
231 'first_name' => 'Super',
232 'last_name' => 'Boy',
233 'display_name' => 'Super Boy',
234 'sort_name' => 'Boy, Super',
235 'id' => $contact['id'],
236 'contact_id' => $contact['id'],
237 ), $contact['values'][$contact['id']]);
238
239 }
240
241 /**
242 * Test process with instant payment when more than one configured for the page.
243 *
244 * CRM-16923
245 */
246 public function testSubmitRecurMultiProcessorInstantPayment() {
247 $this->setUpContributionPage();
248 $this->setupPaymentProcessor();
249 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
250 'payment_processor_type_id' => 'Dummy',
251 'name' => 'processor 2',
252 'class_name' => 'Payment_Dummy',
253 'billing_mode' => 1,
254 ));
255 $dummyPP = Civi\Payment\System::singleton()->getById($paymentProcessor2ID);
256 $dummyPP->setDoDirectPaymentResult(array(
257 'payment_status_id' => 1,
258 'trxn_id' => 'create_first_success',
259 'fee_amount' => .85,
260 ));
261 $processor = $dummyPP->getPaymentProcessor();
262 $this->callAPISuccess('ContributionPage', 'create', array(
263 'id' => $this->_ids['contribution_page'],
264 'payment_processor' => array($paymentProcessor2ID, $this->_ids['payment_processor']),
265 ));
266
267 $priceFieldID = reset($this->_ids['price_field']);
268 $priceFieldValueID = reset($this->_ids['price_field_value']);
269 $submitParams = array(
270 'price_' . $priceFieldID => $priceFieldValueID,
271 'id' => (int) $this->_ids['contribution_page'],
272 'amount' => 10,
273 'is_recur' => 1,
274 'frequency_interval' => 1,
275 'frequency_unit' => 'month',
276 'payment_processor_id' => $paymentProcessor2ID,
277 );
278
279 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
280 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
281 'contribution_page_id' => $this->_ids['contribution_page'],
282 'contribution_status_id' => 1,
283 ));
284 $this->assertEquals('create_first_success', $contribution['trxn_id']);
285 $this->assertEquals(10, $contribution['total_amount']);
286 $this->assertEquals(.85, $contribution['fee_amount']);
287 $this->assertEquals(9.15, $contribution['net_amount']);
288 $this->_checkFinancialRecords(array(
289 'id' => $contribution['id'],
290 'total_amount' => $contribution['total_amount'],
291 'payment_instrument_id' => $processor['payment_instrument_id'],
292 ), 'online');
293 }
294
295 /**
296 * Test submit with a membership block in place.
297 */
298 public function testSubmitMembershipBlockNotSeparatePayment() {
299 $this->setUpMembershipContributionPage();
300 $submitParams = array(
301 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
302 'id' => (int) $this->_ids['contribution_page'],
303 'amount' => 10,
304 'billing_first_name' => 'Billy',
305 'billing_middle_name' => 'Goat',
306 'billing_last_name' => 'Gruff',
307 'selectMembership' => $this->_ids['membership_type'],
308
309 );
310
311 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
312 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
313 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
314 $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']));
315 }
316
317 /**
318 * Test submit with a membership block in place works with renewal.
319 */
320 public function testSubmitMembershipBlockNotSeparatePaymentProcessorInstantRenew() {
321 $this->setUpMembershipContributionPage();
322 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
323 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1));
324 $submitParams = array(
325 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
326 'id' => (int) $this->_ids['contribution_page'],
327 'amount' => 10,
328 'billing_first_name' => 'Billy',
329 'billing_middle_name' => 'Goat',
330 'billing_last_name' => 'Gruff',
331 'selectMembership' => $this->_ids['membership_type'],
332 'payment_processor_id' => 1,
333 'credit_card_number' => '4111111111111111',
334 'credit_card_type' => 'Visa',
335 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
336 'cvv2' => 123,
337 );
338
339 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
340 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
341 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
342 $this->callAPISuccessGetCount('LineItem', array(
343 'entity_table' => 'civicrm_membership',
344 'entity_id' => $membershipPayment['id'],
345 ), 1);
346
347 $submitParams['contact_id'] = $contribution['contact_id'];
348
349 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
350 $this->callAPISuccessGetCount('LineItem', array(
351 'entity_table' => 'civicrm_membership',
352 'entity_id' => $membershipPayment['id'],
353 ), 2);
354 $membership = $this->callAPISuccessGetSingle('Membership', array(
355 'id' => $membershipPayment['membership_id'],
356 'return' => array('end_date', 'join_date', 'start_date'),
357 ));
358 $this->assertEquals(date('Y-m-d'), $membership['start_date']);
359 $this->assertEquals(date('Y-m-d'), $membership['join_date']);
360 $this->assertEquals(date('Y-m-d', strtotime('+ 2 year - 1 day')), $membership['end_date']);
361 }
362
363 /**
364 * Test submit with a membership block in place.
365 */
366 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
367 $mut = new CiviMailUtils($this, TRUE);
368 $this->setUpMembershipContributionPage();
369 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
370
371 $submitParams = array(
372 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
373 'id' => (int) $this->_ids['contribution_page'],
374 'amount' => 10,
375 'billing_first_name' => 'Billy',
376 'billing_middle_name' => 'Goat',
377 'billing_last_name' => 'Gruff',
378 'selectMembership' => $this->_ids['membership_type'],
379 'email-Primary' => 'billy-goat@the-bridge.net',
380 'payment_processor_id' => $this->_paymentProcessor['id'],
381 'credit_card_number' => '4111111111111111',
382 'credit_card_type' => 'Visa',
383 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
384 'cvv2' => 123,
385 );
386
387 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
388 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
389 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
390 $mut->checkMailLog(array(
391 'Membership Type: General',
392 ));
393 $mut->stop();
394 $mut->clearMessages();
395 }
396
397 /**
398 * Test submit with a membership block in place.
399 */
400 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
401 $mut = new CiviMailUtils($this, TRUE);
402 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
403 $this->setUpMembershipContributionPage();
404 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
405
406 $submitParams = array(
407 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
408 'id' => (int) $this->_ids['contribution_page'],
409 'amount' => 0,
410 'billing_first_name' => 'Billy',
411 'billing_middle_name' => 'Goat',
412 'billing_last_name' => 'Gruffier',
413 'selectMembership' => $this->_ids['membership_type'],
414 'email-Primary' => 'billy-goat@the-new-bridge.net',
415 'payment_processor_id' => $this->params['payment_processor_id'],
416 );
417
418 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
419 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
420 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
421 //Assert only one mail is being sent.
422 $msgs = $mut->getAllMessages();
423 $this->assertCount(1, $msgs);
424
425 $mut->checkMailLog(array(
426 'Membership Type: General',
427 'Gruffier',
428 ),
429 array(
430 'Amount',
431 )
432 );
433 $mut->stop();
434 $mut->clearMessages();
435 }
436
437 /**
438 * Test submit with a pay later abnd check line item in mails.
439 */
440 public function testSubmitMembershipBlockIsSeparatePaymentPayLaterWithEmail() {
441 $mut = new CiviMailUtils($this, TRUE);
442 $this->setUpMembershipContributionPage();
443 $submitParams = array(
444 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
445 'id' => (int) $this->_ids['contribution_page'],
446 'amount' => 10,
447 'billing_first_name' => 'Billy',
448 'billing_middle_name' => 'Goat',
449 'billing_last_name' => 'Gruff',
450 'is_pay_later' => 1,
451 'selectMembership' => $this->_ids['membership_type'],
452 'email-Primary' => 'billy-goat@the-bridge.net',
453 );
454
455 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
456 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
457 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
458 $mut->checkMailLog(array(
459 'Membership Amount -... $ 2.00',
460 ));
461 $mut->stop();
462 $mut->clearMessages();
463 }
464
465 /**
466 * Test submit with a membership block in place.
467 */
468 public function testSubmitMembershipBlockIsSeparatePayment() {
469 $this->setUpMembershipContributionPage(TRUE);
470 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
471 $submitParams = array(
472 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
473 'id' => (int) $this->_ids['contribution_page'],
474 'amount' => 10,
475 'billing_first_name' => 'Billy',
476 'billing_middle_name' => 'Goat',
477 'billing_last_name' => 'Gruff',
478 'selectMembership' => $this->_ids['membership_type'],
479 );
480
481 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
482 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
483 $this->assertCount(2, $contributions['values']);
484 $lines = $this->callAPISuccess('LineItem', 'get', array('sequential' => 1));
485 $this->assertEquals(10, $lines['values'][0]['line_total']);
486 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
487 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
488 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
489 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
490 }
491
492
493 /**
494 * Test submit with a membership block in place.
495 */
496 public function testSubmitMembershipBlockIsSeparatePaymentWithPayLater() {
497 $this->setUpMembershipContributionPage(TRUE);
498 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
499 //Pay later
500 $submitParams = array(
501 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
502 'id' => (int) $this->_ids['contribution_page'],
503 'amount' => 0,
504 'billing_first_name' => 'Billy',
505 'billing_middle_name' => 'Goat',
506 'billing_last_name' => 'Gruff',
507 'is_pay_later' => 1,
508 'selectMembership' => $this->_ids['membership_type'],
509 );
510
511 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
512 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
513 $this->assertCount(2, $contributions['values']);
514 foreach ($contributions['values'] as $val) {
515 $this->assertEquals('Pending', $val['contribution_status']);
516 }
517
518 //Membership should be in Pending state.
519 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
520 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
521 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
522 $pendingStatus = $this->callAPISuccessGetSingle('MembershipStatus', array(
523 'return' => array("id"),
524 'name' => "Pending",
525 ));
526 $this->assertEquals($membership['status_id'], $pendingStatus['id']);
527 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
528 }
529
530 /**
531 * Test submit with a membership block in place.
532 */
533 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
534 $mut = new CiviMailUtils($this, TRUE);
535 $this->setUpMembershipContributionPage(TRUE);
536 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
537
538 $submitParams = array(
539 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
540 'id' => (int) $this->_ids['contribution_page'],
541 'amount' => 10,
542 'billing_first_name' => 'Billy',
543 'billing_middle_name' => 'Goat',
544 'billing_last_name' => 'Gruff',
545 'selectMembership' => $this->_ids['membership_type'],
546 'email-Primary' => 'billy-goat@the-bridge.net',
547 'payment_processor_id' => $this->_paymentProcessor['id'],
548 'credit_card_number' => '4111111111111111',
549 'credit_card_type' => 'Visa',
550 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
551 'cvv2' => 123,
552 );
553
554 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
555 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
556 $this->assertCount(2, $contributions['values']);
557 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
558 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
559 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
560 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
561 // We should have two separate email messages, each with their own amount
562 // line and no total line.
563 $mut->checkAllMailLog(
564 array(
565 'Amount: $ 2.00',
566 'Amount: $ 10.00',
567 'Membership Fee',
568 ),
569 array(
570 'Total: $',
571 )
572 );
573 $mut->stop();
574 $mut->clearMessages();
575 }
576
577 /**
578 * Test submit with a membership block in place.
579 */
580 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
581 $mut = new CiviMailUtils($this, TRUE);
582 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
583 $this->setUpMembershipContributionPage(TRUE);
584 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
585
586 $submitParams = array(
587 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
588 'id' => (int) $this->_ids['contribution_page'],
589 'amount' => 0,
590 'billing_first_name' => 'Billy',
591 'billing_middle_name' => 'Goat',
592 'billing_last_name' => 'Gruffalo',
593 'selectMembership' => $this->_ids['membership_type'],
594 'payment_processor_id' => 0,
595 'email-Primary' => 'gruffalo@the-bridge.net',
596 );
597
598 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
599 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
600 $this->assertCount(2, $contributions['values']);
601 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
602 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
603 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
604 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
605 $mut->checkMailLog(array(
606 'Gruffalo',
607 'General Membership: $ 0.00',
608 'Membership Fee',
609 ));
610 $mut->stop();
611 $mut->clearMessages();
612 }
613
614 /**
615 * Test submit with a membership block in place.
616 */
617 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
618 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
619 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
620 $this->setUpMembershipContributionPage(TRUE);
621 $submitParams = array(
622 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
623 'id' => (int) $this->_ids['contribution_page'],
624 'amount' => 10,
625 'billing_first_name' => 'Billy',
626 'billing_middle_name' => 'Goat',
627 'billing_last_name' => 'Gruff',
628 'selectMembership' => $this->_ids['membership_type'][1],
629 );
630
631 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
632 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
633 $this->assertCount(2, $contributions['values']);
634 $ids = array_keys($contributions['values']);
635 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
636 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
637 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
638 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
639 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
640 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
641 }
642
643 /**
644 * Test submit with a membership block in place.
645 *
646 * We are expecting a separate payment for the membership vs the contribution.
647 */
648 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
649 $mut = new CiviMailUtils($this, TRUE);
650 $this->setUpMembershipContributionPage(TRUE);
651 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
652 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
653 $submitParams = array(
654 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
655 'id' => (int) $this->_ids['contribution_page'],
656 'amount' => 10,
657 'billing_first_name' => 'Billy',
658 'billing_middle_name' => 'Goat',
659 'billing_last_name' => 'Gruff',
660 'email-Primary' => 'henry@8th.king',
661 'selectMembership' => $this->_ids['membership_type'],
662 'payment_processor_id' => $this->_paymentProcessor['id'],
663 'credit_card_number' => '4111111111111111',
664 'credit_card_type' => 'Visa',
665 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
666 'cvv2' => 123,
667 );
668
669 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
670 $contributions = $this->callAPISuccess('contribution', 'get', array(
671 'contribution_page_id' => $this->_ids['contribution_page'],
672 'contribution_status_id' => 1,
673 ));
674 $this->assertCount(2, $contributions['values']);
675 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
676 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
677 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
678 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
679 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('entity_table' => 'civicrm_membership'));
680 $this->assertEquals($lineItem['entity_id'], $membership['id']);
681 $this->assertEquals($lineItem['contribution_id'], $membershipPayment['contribution_id']);
682 $this->assertEquals($lineItem['qty'], 1);
683 $this->assertEquals($lineItem['unit_price'], 2);
684 $this->assertEquals($lineItem['line_total'], 2);
685 foreach ($contributions['values'] as $contribution) {
686 $this->assertEquals(.72, $contribution['fee_amount']);
687 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
688 }
689 // The total string is currently absent & it seems worse with - although at some point
690 // it may have been intended
691 $mut->checkAllMailLog(array('$ 2.00', 'Contribution Amount', '$ 10.00'), array('Total:'));
692 $mut->stop();
693 $mut->clearMessages();
694 }
695
696 /**
697 * Test submit with a membership block in place.
698 *
699 * Ensure a separate payment for the membership vs the contribution, with
700 * correct amounts.
701 *
702 * @param string $thousandSeparator
703 * punctuation used to refer to thousands.
704 *
705 * @dataProvider getThousandSeparators
706 */
707 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNowChargesCorrectAmounts($thousandSeparator) {
708 $this->setCurrencySeparators($thousandSeparator);
709 $this->setUpMembershipContributionPage(TRUE);
710 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
711 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
712 $test_uniq = uniqid();
713 $contributionPageAmount = 10;
714 $submitParams = array(
715 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
716 'id' => (int) $this->_ids['contribution_page'],
717 'amount' => $contributionPageAmount,
718 'billing_first_name' => 'Billy',
719 'billing_middle_name' => 'Goat',
720 'billing_last_name' => 'Gruff',
721 'email-Primary' => 'henry@8th.king',
722 'selectMembership' => $this->_ids['membership_type'],
723 'payment_processor_id' => $this->_paymentProcessor['id'],
724 'credit_card_number' => '4111111111111111',
725 'credit_card_type' => 'Visa',
726 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
727 'cvv2' => 123,
728 'TEST_UNIQ' => $test_uniq,
729 );
730
731 // set custom hook
732 $this->hookClass->setHook('civicrm_alterPaymentProcessorParams', array($this, 'hook_civicrm_alterPaymentProcessorParams'));
733
734 $this->callAPISuccess('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
735 $contributions = $this->callAPISuccess('contribution', 'get', array(
736 'contribution_page_id' => $this->_ids['contribution_page'],
737 'contribution_status_id' => 1,
738 ));
739
740 $result = civicrm_api3('SystemLog', 'get', array(
741 'sequential' => 1,
742 'message' => array('LIKE' => "%{$test_uniq}%"),
743 ));
744 $this->assertCount(2, $result['values'], "Expected exactly 2 log entries matching {$test_uniq}.");
745
746 // Examine logged entries to ensure correct values.
747 $contribution_ids = array();
748 $found_membership_amount = $found_contribution_amount = FALSE;
749 foreach ($result['values'] as $value) {
750 list($junk, $json) = explode("$test_uniq:", $value['message']);
751 $logged_contribution = json_decode($json, TRUE);
752 $contribution_ids[] = $logged_contribution['contributionID'];
753 if (!empty($logged_contribution['total_amount'])) {
754 $amount = $logged_contribution['total_amount'];
755 }
756 else {
757 $amount = $logged_contribution['amount'];
758 }
759
760 if ($amount == $this->_membershipBlockAmount) {
761 $found_membership_amount = TRUE;
762 }
763 if ($amount == $contributionPageAmount) {
764 $found_contribution_amount = TRUE;
765 }
766 }
767
768 $distinct_contribution_ids = array_unique($contribution_ids);
769 $this->assertCount(2, $distinct_contribution_ids, "Expected exactly 2 log contributions with distinct contributionIDs.");
770 $this->assertTrue($found_contribution_amount, "Expected one log contribution with amount '$contributionPageAmount' (the contribution page amount)");
771 $this->assertTrue($found_membership_amount, "Expected one log contribution with amount '$this->_membershipBlockAmount' (the membership amount)");
772 }
773
774 /**
775 * Test that when a transaction fails the pending contribution remains.
776 *
777 * An activity should also be created. CRM-16417.
778 */
779 public function testSubmitPaymentProcessorFailure() {
780 $this->setUpContributionPage();
781 $this->setupPaymentProcessor();
782 $this->createLoggedInUser();
783 $priceFieldID = reset($this->_ids['price_field']);
784 $priceFieldValueID = reset($this->_ids['price_field_value']);
785 $submitParams = array(
786 'price_' . $priceFieldID => $priceFieldValueID,
787 'id' => (int) $this->_ids['contribution_page'],
788 'amount' => 10,
789 'payment_processor_id' => 1,
790 'credit_card_number' => '4111111111111111',
791 'credit_card_type' => 'Visa',
792 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
793 'cvv2' => 123,
794 );
795
796 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
797 $contribution = $this->callAPISuccessGetSingle('contribution', array(
798 'contribution_page_id' => $this->_ids['contribution_page'],
799 'contribution_status_id' => 'Failed',
800 ));
801
802 $this->callAPISuccessGetSingle('activity', array(
803 'source_record_id' => $contribution['id'],
804 'activity_type_id' => 'Failed Payment',
805 ));
806
807 }
808
809 /**
810 * Test submit recurring (yearly) membership with immediate confirmation (IATS style).
811 *
812 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
813 * processor (IATS style - denoted by returning trxn_id)
814 * - the first creates a new membership, completed contribution, in progress recurring. Check these
815 * - create another - end date should be extended
816 */
817 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentYear() {
818 $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'year'));
819 }
820
821 /**
822 * Test submit recurring (monthly) membership with immediate confirmation (IATS style).
823 *
824 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
825 * processor (IATS style - denoted by returning trxn_id)
826 * - the first creates a new membership, completed contribution, in progress recurring. Check these
827 * - create another - end date should be extended
828 */
829 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth() {
830 $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'month', 'recur_frequency_unit' => 'month'));
831 }
832
833 /**
834 * Test submit recurring (mismatched frequency unit) membership with immediate confirmation (IATS style).
835 *
836 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
837 * processor (IATS style - denoted by returning trxn_id)
838 * - the first creates a new membership, completed contribution, in progress recurring. Check these
839 * - create another - end date should be extended
840 */
841 //public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentDifferentFrequency() {
842 // $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'month'));
843 //}
844
845 /**
846 * Helper function for testSubmitMembershipPriceSetPaymentProcessorRecurInstantPayment*
847 * @param array $params
848 *
849 * @throws \CRM_Core_Exception
850 * @throws \Exception
851 */
852 public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment($params = array()) {
853 $this->params['is_recur'] = 1;
854 $this->params['recur_frequency_unit'] = $params['recur_frequency_unit'];
855 $membershipTypeParams['duration_unit'] = $params['duration_unit'];
856 if ($params['recur_frequency_unit'] === $params['duration_unit']) {
857 $durationUnit = $params['duration_unit'];
858 }
859 else {
860 $durationUnit = NULL;
861 }
862 $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
863 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
864 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
865 $processor = $dummyPP->getPaymentProcessor();
866
867 if ($params['recur_frequency_unit'] === $params['duration_unit']) {
868 // Membership will be in "New" state because it will get confirmed as payment matches
869 $expectedMembershipStatus = 1;
870 }
871 else {
872 // Membership will still be in "Pending" state as it won't get confirmed as payment doesn't match
873 $expectedMembershipStatus = 5;
874 }
875
876 $submitParams = array(
877 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
878 'id' => (int) $this->_ids['contribution_page'],
879 'amount' => 10,
880 'billing_first_name' => 'Billy',
881 'billing_middle_name' => 'Goat',
882 'billing_last_name' => 'Gruff',
883 'email' => 'billy@goat.gruff',
884 'selectMembership' => $this->_ids['membership_type'],
885 'payment_processor_id' => 1,
886 'credit_card_number' => '4111111111111111',
887 'credit_card_type' => 'Visa',
888 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
889 'cvv2' => 123,
890 'is_recur' => 1,
891 'frequency_interval' => 1,
892 'frequency_unit' => $this->params['recur_frequency_unit'],
893 );
894
895 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
896 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
897 'contribution_page_id' => $this->_ids['contribution_page'],
898 'contribution_status_id' => 1,
899 ));
900 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
901
902 $this->assertEquals('create_first_success', $contribution['trxn_id']);
903 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
904 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
905 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
906 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
907 $this->assertEquals($expectedMembershipStatus, $membership['status_id']);
908 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
909
910 $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
911 //renew it with processor setting completed - should extend membership
912 $submitParams['contact_id'] = $contribution['contact_id'];
913 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
914 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
915 $this->callAPISuccess('contribution', 'getsingle', array(
916 'id' => array('NOT IN' => array($contribution['id'])),
917 'contribution_page_id' => $this->_ids['contribution_page'],
918 'contribution_status_id' => 1,
919 ));
920 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
921 if ($durationUnit) {
922 // We only have an end_date if frequency units match, otherwise membership won't be autorenewed and dates won't be calculated.
923 $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membership['end_date']);
924 $this->assertEquals($renewedMembershipEndDate, $renewedMembership['end_date']);
925 }
926 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
927 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
928 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
929 }
930
931 /**
932 * Test submit recurring membership with immediate confirmation (IATS style).
933 *
934 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
935 * processor (IATS style - denoted by returning trxn_id)
936 * - the first creates a new membership, completed contribution, in progress recurring. Check these
937 * - create another - end date should be extended
938 */
939 public function testSubmitMembershipComplexNonPriceSetPaymentPaymentProcessorRecurInstantPayment() {
940 $this->params['is_recur'] = 1;
941 $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
942 // Add a membership so membership & contribution are not both 1.
943 $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
944 $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
945 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
946 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
947 $processor = $dummyPP->getPaymentProcessor();
948
949 $submitParams = array(
950 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
951 'price_' . $this->_ids['price_field']['cont'] => 88,
952 'id' => (int) $this->_ids['contribution_page'],
953 'amount' => 10,
954 'billing_first_name' => 'Billy',
955 'billing_middle_name' => 'Goat',
956 'billing_last_name' => 'Gruff',
957 'email' => 'billy@goat.gruff',
958 'selectMembership' => $this->_ids['membership_type'],
959 'payment_processor_id' => 1,
960 'credit_card_number' => '4111111111111111',
961 'credit_card_type' => 'Visa',
962 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
963 'cvv2' => 123,
964 'is_recur' => 1,
965 'frequency_interval' => 1,
966 'frequency_unit' => $this->params['recur_frequency_unit'],
967 );
968
969 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
970 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
971 'contribution_page_id' => $this->_ids['contribution_page'],
972 'contribution_status_id' => 1,
973 ));
974 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
975
976 $this->assertEquals('create_first_success', $contribution['trxn_id']);
977 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
978 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
979 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
980 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
981 $this->assertEquals(1, $membership['status_id']);
982 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
983
984 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $contribution['id']));
985 $this->assertEquals(2, $lines['count']);
986 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
987 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
988 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
989 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
990 $this->callAPISuccessGetSingle('MembershipPayment', array('contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID + 1));
991
992 //renew it with processor setting completed - should extend membership
993 $submitParams['contact_id'] = $contribution['contact_id'];
994 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
995 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
996 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', array(
997 'id' => array('NOT IN' => array($contribution['id'])),
998 'contribution_page_id' => $this->_ids['contribution_page'],
999 'contribution_status_id' => 1,
1000 ));
1001 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $renewContribution['id']));
1002 $this->assertEquals(2, $lines['count']);
1003 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1004 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
1005 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1006 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
1007
1008 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1009 $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
1010 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
1011 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
1012 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
1013 }
1014
1015 /**
1016 * Test submit recurring membership with immediate confirmation (IATS style).
1017 *
1018 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1019 * processor (IATS style - denoted by returning trxn_id)
1020 * - the first creates a new membership, completed contribution, in progress recurring. Check these
1021 * - create another - end date should be extended
1022 */
1023 public function testSubmitMembershipComplexPriceSetPaymentPaymentProcessorRecurInstantPayment() {
1024 $this->params['is_recur'] = 1;
1025 $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
1026 // Add a membership so membership & contribution are not both 1.
1027 $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
1028 $this->createPriceSetWithPage();
1029 $this->addSecondOrganizationMembershipToPriceSet();
1030 $this->setupPaymentProcessor();
1031
1032 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1033 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
1034 $processor = $dummyPP->getPaymentProcessor();
1035
1036 $submitParams = array(
1037 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value']['cont'],
1038 'price_' . $this->_ids['price_field']['org1'] => $this->_ids['price_field_value']['org1'],
1039 'price_' . $this->_ids['price_field']['org2'] => $this->_ids['price_field_value']['org2'],
1040 'id' => (int) $this->_ids['contribution_page'],
1041 'amount' => 10,
1042 'billing_first_name' => 'Billy',
1043 'billing_middle_name' => 'Goat',
1044 'billing_last_name' => 'Gruff',
1045 'email' => 'billy@goat.gruff',
1046 'selectMembership' => NULL,
1047 'payment_processor_id' => 1,
1048 'credit_card_number' => '4111111111111111',
1049 'credit_card_type' => 'Visa',
1050 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1051 'cvv2' => 123,
1052 'frequency_interval' => 1,
1053 'frequency_unit' => $this->params['recur_frequency_unit'],
1054 );
1055
1056 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1057 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1058 'contribution_page_id' => $this->_ids['contribution_page'],
1059 'contribution_status_id' => 1,
1060 ));
1061 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
1062
1063 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1064 $membershipPayments = $this->callAPISuccess('membership_payment', 'get', array(
1065 'sequential' => 1,
1066 'contribution_id' => $contribution['id'],
1067 ));
1068 $this->assertEquals(2, $membershipPayments['count']);
1069 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $contribution['id']));
1070 $this->assertEquals(3, $lines['count']);
1071 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1072 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
1073 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1074 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
1075 $this->assertEquals('civicrm_membership', $lines['values'][2]['entity_table']);
1076 $this->assertEquals($preExistingMembershipID + 2, $lines['values'][2]['entity_id']);
1077
1078 $this->callAPISuccessGetSingle('MembershipPayment', array('contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID + 1));
1079 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $preExistingMembershipID + 1));
1080
1081 //renew it with processor setting completed - should extend membership
1082 $submitParams['contact_id'] = $contribution['contact_id'];
1083 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
1084 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1085 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', array(
1086 'id' => array('NOT IN' => array($contribution['id'])),
1087 'contribution_page_id' => $this->_ids['contribution_page'],
1088 'contribution_status_id' => 1,
1089 ));
1090 $lines = $this->callAPISuccess('line_item', 'get', array('sequential' => 1, 'contribution_id' => $renewContribution['id']));
1091 $this->assertEquals(3, $lines['count']);
1092 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1093 $this->assertEquals($preExistingMembershipID + 1, $lines['values'][0]['entity_id']);
1094 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1095 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
1096
1097 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $preExistingMembershipID + 1));
1098 $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
1099 }
1100
1101 /**
1102 * Extend the price set with a second organisation's membership.
1103 */
1104 public function addSecondOrganizationMembershipToPriceSet() {
1105 $organization2ID = $this->organizationCreate();
1106 $membershipTypes = $this->callAPISuccess('MembershipType', 'get', array());
1107 $this->_ids['membership_type'] = array_keys($membershipTypes['values']);
1108 $this->_ids['membership_type']['org2'] = $this->membershipTypeCreate(array('contact_id' => $organization2ID, 'name' => 'Org 2'));
1109 $priceField = $this->callAPISuccess('PriceField', 'create', array(
1110 'price_set_id' => $this->_ids['price_set'],
1111 'html_type' => 'Radio',
1112 'name' => 'Org1 Price',
1113 'label' => 'Org1Price',
1114 ));
1115 $this->_ids['price_field']['org1'] = $priceField['id'];
1116
1117 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1118 'name' => 'org1 amount',
1119 'label' => 'org 1 Amount',
1120 'amount' => 2,
1121 'financial_type_id' => 'Member Dues',
1122 'format.only_id' => TRUE,
1123 'membership_type_id' => reset($this->_ids['membership_type']),
1124 'price_field_id' => $priceField['id'],
1125 ));
1126 $this->_ids['price_field_value']['org1'] = $priceFieldValue;
1127
1128 $priceField = $this->callAPISuccess('PriceField', 'create', array(
1129 'price_set_id' => $this->_ids['price_set'],
1130 'html_type' => 'Radio',
1131 'name' => 'Org2 Price',
1132 'label' => 'Org2Price',
1133 ));
1134 $this->_ids['price_field']['org2'] = $priceField['id'];
1135
1136 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1137 'name' => 'org2 amount',
1138 'label' => 'org 2 Amount',
1139 'amount' => 200,
1140 'financial_type_id' => 'Member Dues',
1141 'format.only_id' => TRUE,
1142 'membership_type_id' => $this->_ids['membership_type']['org2'],
1143 'price_field_id' => $priceField['id'],
1144 ));
1145 $this->_ids['price_field_value']['org2'] = $priceFieldValue;
1146
1147 }
1148
1149 /**
1150 * Test submit recurring membership with immediate confirmation (IATS style).
1151 *
1152 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1153 * processor (IATS style - denoted by returning trxn_id)
1154 * - the first creates a new membership, completed contribution, in progress recurring. Check these
1155 * - create another - end date should be extended
1156 */
1157 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
1158
1159 $this->setUpMembershipContributionPage(TRUE);
1160 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1161 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
1162
1163 $submitParams = array(
1164 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1165 'id' => (int) $this->_ids['contribution_page'],
1166 'amount' => 10,
1167 'billing_first_name' => 'Billy',
1168 'billing_middle_name' => 'Goat',
1169 'billing_last_name' => 'Gruff',
1170 'email' => 'billy@goat.gruff',
1171 'selectMembership' => $this->_ids['membership_type'],
1172 'payment_processor_id' => 1,
1173 'credit_card_number' => '4111111111111111',
1174 'credit_card_type' => 'Visa',
1175 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1176 'cvv2' => 123,
1177 'is_recur' => 1,
1178 'auto_renew' => TRUE,
1179 'frequency_interval' => 1,
1180 'frequency_unit' => 'month',
1181 );
1182
1183 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1184 $contribution = $this->callAPISuccess('contribution', 'get', array(
1185 'contribution_page_id' => $this->_ids['contribution_page'],
1186 'contribution_status_id' => 1,
1187 ));
1188
1189 $this->assertEquals(2, $contribution['count']);
1190 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
1191 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1192 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
1193 $this->callAPISuccess('contribution_recur', 'getsingle', array());
1194 }
1195
1196 /**
1197 * Test submit recurring membership with delayed confirmation (Authorize.net style)
1198 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
1199 * processor (Authorize.net style - denoted by NOT returning trxn_id)
1200 * - the first creates a pending membership, pending contribution, penging recurring. Check these
1201 * - complete the transaction
1202 * - create another - end date should NOT be extended
1203 */
1204 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
1205 $this->params['is_recur'] = 1;
1206 $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
1207 $this->setUpMembershipContributionPage();
1208 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1209 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
1210 $this->membershipTypeCreate(array('name' => 'Student'));
1211
1212 // Add a contribution & a couple of memberships so the id will not be 1 & will differ from membership id.
1213 // This saves us from 'accidental success'.
1214 $this->contributionCreate(array('contact_id' => $this->contactIds[0]));
1215 $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
1216 $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0], 'membership_type_id' => 'Student'));
1217
1218 $submitParams = array(
1219 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1220 'id' => (int) $this->_ids['contribution_page'],
1221 'amount' => 10,
1222 'billing_first_name' => 'Billy',
1223 'billing_middle_name' => 'Goat',
1224 'billing_last_name' => 'Gruff',
1225 'email' => 'billy@goat.gruff',
1226 'selectMembership' => $this->_ids['membership_type'],
1227 'payment_processor_id' => 1,
1228 'credit_card_number' => '4111111111111111',
1229 'credit_card_type' => 'Visa',
1230 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1231 'cvv2' => 123,
1232 'is_recur' => 1,
1233 'frequency_interval' => 1,
1234 'frequency_unit' => $this->params['recur_frequency_unit'],
1235 );
1236
1237 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1238 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1239 'contribution_page_id' => $this->_ids['contribution_page'],
1240 'contribution_status_id' => 2,
1241 ));
1242
1243 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
1244 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
1245 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1246 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
1247 $this->assertEquals(5, $membership['status_id']);
1248
1249 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
1250 $this->assertEquals('civicrm_membership', $line['entity_table']);
1251 $this->assertEquals($membership['id'], $line['entity_id']);
1252
1253 $this->callAPISuccess('contribution', 'completetransaction', array(
1254 'id' => $contribution['id'],
1255 'trxn_id' => 'ipn_called',
1256 'payment_processor_id' => $this->_paymentProcessor['id'],
1257 ));
1258 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
1259 $this->assertEquals('civicrm_membership', $line['entity_table']);
1260 $this->assertEquals($membership['id'], $line['entity_id']);
1261
1262 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1263 //renew it with processor setting completed - should extend membership
1264 $submitParams = array_merge($submitParams, array(
1265 'contact_id' => $contribution['contact_id'],
1266 'is_recur' => 1,
1267 'frequency_interval' => 1,
1268 'frequency_unit' => $this->params['recur_frequency_unit'],
1269 )
1270 );
1271
1272 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
1273 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1274 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
1275 'id' => array(
1276 'NOT IN' => array($contribution['id']),
1277 ),
1278 'contribution_page_id' => $this->_ids['contribution_page'],
1279 'contribution_status_id' => 2,
1280 )
1281 );
1282 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $newContribution['id']));
1283 $this->assertEquals('civicrm_membership', $line['entity_table']);
1284 $this->assertEquals($membership['id'], $line['entity_id']);
1285
1286 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
1287 //no renewal as the date hasn't changed
1288 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
1289 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
1290 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
1291 }
1292
1293 /**
1294 * Test non-recur contribution with membership payment
1295 */
1296 public function testSubmitMembershipIsSeparatePaymentNotRecur() {
1297 //Create recur contribution page.
1298 $this->setUpMembershipContributionPage(TRUE, TRUE);
1299 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1300 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
1301
1302 //Sumbit payment with recur disabled.
1303 $submitParams = array(
1304 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1305 'id' => (int) $this->_ids['contribution_page'],
1306 'amount' => 10,
1307 'frequency_interval' => 1,
1308 'frequency_unit' => 'month',
1309 'billing_first_name' => 'Billy',
1310 'billing_middle_name' => 'Goat',
1311 'billing_last_name' => 'Gruff',
1312 'email' => 'billy@goat.gruff',
1313 'selectMembership' => $this->_ids['membership_type'],
1314 'payment_processor_id' => 1,
1315 'credit_card_number' => '4111111111111111',
1316 'credit_card_type' => 'Visa',
1317 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1318 'cvv2' => 123,
1319 );
1320
1321 //Assert if recur contribution is created.
1322 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1323 $recur = $this->callAPISuccess('contribution_recur', 'get', array());
1324 $this->assertEmpty($recur['count']);
1325 }
1326
1327
1328 /**
1329 * Set up membership contribution page.
1330 * @param bool $isSeparatePayment
1331 * @param bool $isRecur
1332 * @param array $membershipTypeParams Parameters to pass to membershiptype.create API
1333 */
1334 public function setUpMembershipContributionPage($isSeparatePayment = FALSE, $isRecur = FALSE, $membershipTypeParams = array()) {
1335 $this->setUpMembershipBlockPriceSet($membershipTypeParams);
1336 $this->setupPaymentProcessor();
1337 $this->setUpContributionPage($isRecur);
1338
1339 $this->callAPISuccess('membership_block', 'create', array(
1340 'entity_id' => $this->_ids['contribution_page'],
1341 'entity_table' => 'civicrm_contribution_page',
1342 'is_required' => TRUE,
1343 'is_active' => TRUE,
1344 'is_separate_payment' => $isSeparatePayment,
1345 'membership_type_default' => $this->_ids['membership_type'],
1346 ));
1347 }
1348
1349 /**
1350 * Set up pledge block.
1351 */
1352 public function setUpPledgeBlock() {
1353 $params = array(
1354 'entity_table' => 'civicrm_contribution_page',
1355 'entity_id' => $this->_ids['contribution_page'],
1356 'pledge_frequency_unit' => 'week',
1357 'is_pledge_interval' => 0,
1358 'pledge_start_date' => json_encode(array('calendar_date' => date('Ymd', strtotime("+1 month")))),
1359 );
1360 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::create($params);
1361 $this->_ids['pledge_block_id'] = $pledgeBlock->id;
1362 }
1363
1364 /**
1365 * The default data set does not include a complete default membership price set - not quite sure why.
1366 *
1367 * This function ensures it exists & populates $this->_ids with it's data
1368 */
1369 public function setUpMembershipBlockPriceSet($membershipTypeParams = array()) {
1370 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
1371 'name' => 'default_membership_type_amount',
1372 'return' => 'id',
1373 ));
1374 if (empty($this->_ids['membership_type'])) {
1375 $membershipTypeParams = array_merge(array(
1376 'minimum_fee' => 2,
1377 ), $membershipTypeParams);
1378 $this->_ids['membership_type'] = array($this->membershipTypeCreate($membershipTypeParams));
1379 }
1380 $priceField = $this->callAPISuccess('price_field', 'create', array(
1381 'price_set_id' => reset($this->_ids['price_set']),
1382 'name' => 'membership_amount',
1383 'label' => 'Membership Amount',
1384 'html_type' => 'Radio',
1385 'sequential' => 1,
1386 ));
1387 $this->_ids['price_field'][] = $priceField['id'];
1388
1389 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
1390 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1391 'name' => 'membership_amount',
1392 'label' => 'Membership Amount',
1393 'amount' => $this->_membershipBlockAmount,
1394 'financial_type_id' => 'Donation',
1395 'format.only_id' => TRUE,
1396 'membership_type_id' => $membershipTypeID,
1397 'price_field_id' => $priceField['id'],
1398 ));
1399 $this->_ids['price_field_value'][] = $priceFieldValue;
1400 }
1401 if (!empty($this->_ids['membership_type']['org2'])) {
1402 $priceField = $this->callAPISuccess('price_field', 'create', array(
1403 'price_set_id' => reset($this->_ids['price_set']),
1404 'name' => 'membership_org2',
1405 'label' => 'Membership Org2',
1406 'html_type' => 'Checkbox',
1407 'sequential' => 1,
1408 ));
1409 $this->_ids['price_field']['org2'] = $priceField['id'];
1410
1411 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1412 'name' => 'membership_org2',
1413 'label' => 'Membership org 2',
1414 'amount' => 55,
1415 'financial_type_id' => 'Member Dues',
1416 'format.only_id' => TRUE,
1417 'membership_type_id' => $this->_ids['membership_type']['org2'],
1418 'price_field_id' => $priceField['id'],
1419 ));
1420 $this->_ids['price_field_value']['org2'] = $priceFieldValue;
1421 }
1422 $priceField = $this->callAPISuccess('price_field', 'create', array(
1423 'price_set_id' => reset($this->_ids['price_set']),
1424 'name' => 'Contribution',
1425 'label' => 'Contribution',
1426 'html_type' => 'Text',
1427 'sequential' => 1,
1428 'is_enter_qty' => 1,
1429 ));
1430 $this->_ids['price_field']['cont'] = $priceField['id'];
1431 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1432 'name' => 'contribution',
1433 'label' => 'Give me money',
1434 'amount' => 88,
1435 'financial_type_id' => 'Donation',
1436 'format.only_id' => TRUE,
1437 'price_field_id' => $priceField['id'],
1438 ));
1439 $this->_ids['price_field_value'][] = $priceFieldValue;
1440 }
1441
1442 /**
1443 * Add text field other amount to the price set.
1444 */
1445 public function addOtherAmountFieldToMembershipPriceSet() {
1446 $this->_ids['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
1447 'price_set_id' => reset($this->_ids['price_set']),
1448 'name' => 'other_amount',
1449 'label' => 'Other Amount',
1450 'html_type' => 'Text',
1451 'format.only_id' => TRUE,
1452 'sequential' => 1,
1453 ));
1454 $this->_ids['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
1455 'financial_type_id' => 'Donation',
1456 'format.only_id' => TRUE,
1457 'label' => 'Other Amount',
1458 'amount' => 1,
1459 'price_field_id' => $this->_ids['price_field']['other_amount'],
1460 ));
1461 }
1462
1463 /**
1464 * Help function to set up contribution page with some defaults.
1465 * @param bool $isRecur
1466 */
1467 public function setUpContributionPage($isRecur = FALSE) {
1468 if ($isRecur) {
1469 $this->params['is_recur'] = 1;
1470 $this->params['recur_frequency_unit'] = 'month';
1471 }
1472 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
1473 if (empty($this->_ids['price_set'])) {
1474 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
1475 $this->_ids['price_set'][] = $priceSet['id'];
1476 }
1477 $priceSetID = reset($this->_ids['price_set']);
1478 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1479
1480 if (empty($this->_ids['price_field'])) {
1481 $priceField = $this->callAPISuccess('price_field', 'create', array(
1482 'price_set_id' => $priceSetID,
1483 'label' => 'Goat Breed',
1484 'html_type' => 'Radio',
1485 ));
1486 $this->_ids['price_field'] = array($priceField['id']);
1487 }
1488 if (empty($this->_ids['price_field_value'])) {
1489 $this->callAPISuccess('price_field_value', 'create', array(
1490 'price_set_id' => $priceSetID,
1491 'price_field_id' => $priceField['id'],
1492 'label' => 'Long Haired Goat',
1493 'financial_type_id' => 'Donation',
1494 'amount' => 20,
1495 'non_deductible_amount' => 15,
1496 )
1497 );
1498 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1499 'price_set_id' => $priceSetID,
1500 'price_field_id' => $priceField['id'],
1501 'label' => 'Shoe-eating Goat',
1502 'financial_type_id' => 'Donation',
1503 'amount' => 10,
1504 'non_deductible_amount' => 5,
1505 )
1506 );
1507 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1508 }
1509 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1510 }
1511
1512 public static function setUpBeforeClass() {
1513 // put stuff here that should happen before all tests in this unit
1514 }
1515
1516 public static function tearDownAfterClass() {
1517 $tablesToTruncate = array(
1518 'civicrm_contact',
1519 'civicrm_financial_type',
1520 'civicrm_contribution',
1521 'civicrm_contribution_page',
1522 );
1523 $unitTest = new CiviUnitTestCase();
1524 $unitTest->quickCleanup($tablesToTruncate);
1525 }
1526
1527 /**
1528 * Create a payment processor instance.
1529 */
1530 protected function setupPaymentProcessor() {
1531 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
1532 'payment_processor_type_id' => 'Dummy',
1533 'class_name' => 'Payment_Dummy',
1534 'billing_mode' => 1,
1535 ));
1536 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
1537 }
1538
1539 /**
1540 * Test submit recurring pledge.
1541 *
1542 * - 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.
1543 */
1544 public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() {
1545 $this->params['adjust_recur_start_date'] = TRUE;
1546 $this->params['is_pay_later'] = FALSE;
1547 $this->setUpContributionPage();
1548 $this->setUpPledgeBlock();
1549 $this->setupPaymentProcessor();
1550 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
1551 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
1552
1553 $submitParams = array(
1554 'id' => (int) $this->_ids['contribution_page'],
1555 'amount' => 100,
1556 'billing_first_name' => 'Billy',
1557 'billing_middle_name' => 'Goat',
1558 'billing_last_name' => 'Gruff',
1559 'email' => 'billy@goat.gruff',
1560 'payment_processor_id' => 1,
1561 'credit_card_number' => '4111111111111111',
1562 'credit_card_type' => 'Visa',
1563 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1564 'cvv2' => 123,
1565 'pledge_frequency_interval' => 1,
1566 'pledge_frequency_unit' => 'week',
1567 'pledge_installments' => 3,
1568 'is_pledge' => TRUE,
1569 'pledge_block_id' => (int) $this->_ids['pledge_block_id'],
1570 );
1571
1572 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1573
1574 // Check if contribution created.
1575 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1576 'contribution_page_id' => $this->_ids['contribution_page'],
1577 'contribution_status_id' => 'Completed', // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1578 ));
1579
1580 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1581
1582 // Check if pledge created.
1583 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1584 $this->assertEquals(date('Ymd', strtotime($pledge['pledge_start_date'])), date('Ymd', strtotime("+1 month")));
1585 $this->assertEquals($pledge['pledge_amount'], 300.00);
1586
1587 // Check if pledge payments created.
1588 $params = array(
1589 'pledge_id' => $pledge['id'],
1590 );
1591 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1592 $this->assertEquals($pledgePayment['count'], 3);
1593 $this->assertEquals(date('Ymd', strtotime($pledgePayment['values'][1]['scheduled_date'])), date('Ymd', strtotime("+1 month")));
1594 $this->assertEquals($pledgePayment['values'][1]['scheduled_amount'], 100.00);
1595 $this->assertEquals($pledgePayment['values'][1]['status_id'], 1); // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1596
1597 // Check contribution recur record.
1598 $recur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
1599 $this->assertEquals(date('Ymd', strtotime($recur['start_date'])), date('Ymd', strtotime("+1 month")));
1600 $this->assertEquals($recur['amount'], 100.00);
1601 $this->assertEquals($recur['contribution_status_id'], 5); // In progress status.
1602 }
1603
1604 /**
1605 * Test submit pledge payment.
1606 *
1607 * - test submitting a pledge payment using contribution form.
1608 */
1609 public function testSubmitPledgePayment() {
1610 $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment();
1611 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1612 $params = array(
1613 'pledge_id' => $pledge['id'],
1614 );
1615 $submitParams = array(
1616 'id' => (int) $pledge['pledge_contribution_page_id'],
1617 'pledge_amount' => array(2 => 1),
1618 'billing_first_name' => 'Billy',
1619 'billing_middle_name' => 'Goat',
1620 'billing_last_name' => 'Gruff',
1621 'email' => 'billy@goat.gruff',
1622 'payment_processor_id' => 1,
1623 'credit_card_number' => '4111111111111111',
1624 'credit_card_type' => 'Visa',
1625 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1626 'cvv2' => 123,
1627 'pledge_id' => $pledge['id'],
1628 'cid' => $pledge['contact_id'],
1629 'contact_id' => $pledge['contact_id'],
1630 'amount' => 100.00,
1631 'is_pledge' => TRUE,
1632 'pledge_block_id' => $this->_ids['pledge_block_id'],
1633 );
1634 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1635 $this->assertEquals($pledgePayment['values'][2]['status_id'], 2);
1636
1637 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1638
1639 // Check if contribution created.
1640 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1641 'contribution_page_id' => $pledge['pledge_contribution_page_id'],
1642 'contribution_status_id' => 'Completed',
1643 'contact_id' => $pledge['contact_id'],
1644 'contribution_recur_id' => array('IS NULL' => 1),
1645 ));
1646
1647 $this->assertEquals(100.00, $contribution['total_amount']);
1648 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1649 $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed");
1650 $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);
1651 }
1652
1653 /**
1654 * Test form submission with multiple option price set.
1655 *
1656 * @param string $thousandSeparator
1657 * punctuation used to refer to thousands.
1658 *
1659 * @dataProvider getThousandSeparators
1660 */
1661 public function testSubmitContributionPageWithPriceSet($thousandSeparator) {
1662 $this->setCurrencySeparators($thousandSeparator);
1663 $this->_priceSetParams['is_quick_config'] = 0;
1664 $this->setUpContributionPage();
1665 $submitParams = array(
1666 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1667 'id' => (int) $this->_ids['contribution_page'],
1668 'amount' => 80,
1669 'first_name' => 'Billy',
1670 'last_name' => 'Gruff',
1671 'email' => 'billy@goat.gruff',
1672 'is_pay_later' => TRUE,
1673 );
1674 $this->addPriceFields($submitParams);
1675
1676 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1677 $contribution = $this->callAPISuccessGetSingle('contribution', array(
1678 'contribution_page_id' => $this->_ids['contribution_page'],
1679 'contribution_status_id' => 2,
1680 ));
1681 $this->assertEquals(80, $contribution['total_amount']);
1682 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
1683 'contribution_id' => $contribution['id'],
1684 ));
1685 $this->assertEquals(3, $lineItems['count']);
1686 $totalLineAmount = 0;
1687 foreach ($lineItems['values'] as $lineItem) {
1688 $totalLineAmount = $totalLineAmount + $lineItem['line_total'];
1689 }
1690 $this->assertEquals(80, $totalLineAmount);
1691 }
1692
1693 /**
1694 * Function to add additional price fields to priceset.
1695 * @param array $params
1696 */
1697 public function addPriceFields(&$params) {
1698 $priceSetID = reset($this->_ids['price_set']);
1699 $priceField = $this->callAPISuccess('price_field', 'create', array(
1700 'price_set_id' => $priceSetID,
1701 'label' => 'Chicken Breed',
1702 'html_type' => 'CheckBox',
1703 ));
1704 $priceFieldValue1 = $this->callAPISuccess('price_field_value', 'create', array(
1705 'price_set_id' => $priceSetID,
1706 'price_field_id' => $priceField['id'],
1707 'label' => 'Shoe-eating chicken -1',
1708 'financial_type_id' => 'Donation',
1709 'amount' => 30,
1710 ));
1711 $priceFieldValue2 = $this->callAPISuccess('price_field_value', 'create', array(
1712 'price_set_id' => $priceSetID,
1713 'price_field_id' => $priceField['id'],
1714 'label' => 'Shoe-eating chicken -2',
1715 'financial_type_id' => 'Donation',
1716 'amount' => 40,
1717 ));
1718 $params['price_' . $priceField['id']] = array(
1719 $priceFieldValue1['id'] => 1,
1720 $priceFieldValue2['id'] => 1,
1721 );
1722 }
1723
1724 /**
1725 * Test Tax Amount is calculated properly when using PriceSet with Field Type = Text/Numeric Quantity
1726 *
1727 * @param string $thousandSeparator
1728 * punctuation used to refer to thousands.
1729 *
1730 * @dataProvider getThousandSeparators
1731 */
1732 public function testSubmitContributionPageWithPriceSetQuantity($thousandSeparator) {
1733 $this->setCurrencySeparators($thousandSeparator);
1734 $this->_priceSetParams['is_quick_config'] = 0;
1735 $this->enableTaxAndInvoicing();
1736 $financialType = $this->createFinancialType();
1737 $financialTypeId = $financialType['id'];
1738 // 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%
1739 $this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5);
1740
1741 $this->setUpContributionPage();
1742 $submitParams = array(
1743 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
1744 'id' => (int) $this->_ids['contribution_page'],
1745 'first_name' => 'J',
1746 'last_name' => 'T',
1747 'email' => 'JT@ohcanada.ca',
1748 'is_pay_later' => TRUE,
1749 'receive_date' => date('Y-m-d H:i:s'),
1750 );
1751
1752 // Create PriceSet/PriceField
1753 $priceSetID = reset($this->_ids['price_set']);
1754 $priceField = $this->callAPISuccess('price_field', 'create', array(
1755 'price_set_id' => $priceSetID,
1756 'label' => 'Printing Rights',
1757 'html_type' => 'Text',
1758 ));
1759 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1760 'price_set_id' => $priceSetID,
1761 'price_field_id' => $priceField['id'],
1762 'label' => 'Printing Rights',
1763 'financial_type_id' => $financialTypeId,
1764 'amount' => '16.95',
1765 ));
1766 $priceFieldId = $priceField['id'];
1767
1768 // Set quantity for our test
1769 $submitParams['price_' . $priceFieldId] = 180;
1770
1771 // contribution_page submit requires amount and tax_amount - and that's ok we're not testing that - we're testing at the LineItem level
1772 $submitParams['amount'] = $this->formatMoneyInput(180 * 16.95);
1773 // This is the correct Tax Amount - use it later to compare to what the CiviCRM Core came up with at the LineItem level
1774 $submitParams['tax_amount'] = $this->formatMoneyInput(180 * 16.95 * 0.10);
1775
1776 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1777 $contribution = $this->callAPISuccessGetSingle('contribution', array(
1778 'contribution_page_id' => $this->_ids['contribution_page'],
1779 ));
1780
1781 // Retrieve the lineItem that belongs to the Printing Rights and check the tax_amount CiviCRM Core calculated for it
1782 $lineItem = $this->callAPISuccessGetSingle('LineItem', array(
1783 'contribution_id' => $contribution['id'],
1784 'label' => 'Printing Rights',
1785 ));
1786
1787 $lineItem_TaxAmount = round($lineItem['tax_amount'], 2);
1788
1789 $this->assertEquals($lineItem['line_total'], $contribution['total_amount'], 'Contribution total should match line total');
1790 $this->assertEquals($lineItem_TaxAmount, round(180 * 16.95 * 0.10, 2), 'Wrong Sales Tax Amount is calculated and stored.');
1791 }
1792
1793 public function hook_civicrm_alterPaymentProcessorParams($paymentObj, &$rawParams, &$cookedParams) {
1794 // Ensure total_amount are the same if they're both given.
1795 $total_amount = CRM_Utils_Array::value('total_amount', $rawParams);
1796 $amount = CRM_Utils_Array::value('amount', $rawParams);
1797 if (!empty($total_amount) && !empty($amount) && $total_amount != $amount) {
1798 throw new Exception("total_amount '$total_amount' and amount '$amount' differ.");
1799 }
1800
1801 // Log parameters for later debugging and testing.
1802 $message = __FUNCTION__ . ": {$rawParams['TEST_UNIQ']}:";
1803 $log_params = array_intersect_key($rawParams, array(
1804 'amount' => 1,
1805 'total_amount' => 1,
1806 'contributionID' => 1,
1807 ));
1808 $message .= json_encode($log_params);
1809 $log = new CRM_Utils_SystemLogger();
1810 $log->debug($message, $_REQUEST);
1811 }
1812
1813 }