3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\PriceFieldValue
;
15 * Test APIv3 civicrm_contribute_recur* functions
17 * @package CiviCRM_APIv3
18 * @subpackage API_Contribution
21 class api_v3_ContributionPageTest
extends CiviUnitTestCase
{
22 protected $testAmount = 34567;
25 protected $contactIds = [];
26 protected $_entity = 'ContributionPage';
27 protected $contribution_result = NULL;
28 protected $_priceSetParams = [];
29 protected $_membershipBlockAmount = 2;
31 * Payment processor details.
34 protected $_paymentProcessor = [];
46 * Should financials be checked after the test but before tear down.
50 protected $isValidateFinancialsOnPostAssert = TRUE;
55 * @throws \CRM_Core_Exception
57 public function setUp(): void
{
59 $this->contactIds
[] = $this->individualCreate();
61 'title' => 'Test Contribution Page',
62 'financial_type_id' => 1,
64 'goal_amount' => $this->testAmount
,
66 'pay_later_text' => 'Send check',
67 'is_monetary' => TRUE,
68 'is_email_receipt' => TRUE,
69 'receipt_from_email' => 'yourconscience@donate.com',
70 'receipt_from_name' => 'Ego Freud',
73 $this->_priceSetParams
= [
74 'is_quick_config' => 1,
75 'extends' => 'CiviContribute',
76 'financial_type_id' => 'Donation',
82 * Tear down after test.
84 * @throws \CRM_Core_Exception
86 public function tearDown(): void
{
87 foreach ($this->contactIds
as $id) {
88 $this->callAPISuccess('contact', 'delete', ['id' => $id]);
90 $this->quickCleanUpFinancialEntities();
95 * Test creating a contribution page.
99 * @dataProvider versionThreeAndFour
100 * @throws \CRM_Core_Exception
102 public function testCreateContributionPage($version) {
103 $this->basicCreateTest($version);
107 * Test getting a contribution page.
109 * @param int $version
111 * @dataProvider versionThreeAndFour
112 * @throws \CRM_Core_Exception
114 public function testGetBasicContributionPage($version) {
115 $this->_apiversion
= $version;
116 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
117 $this->id
= $createResult['id'];
120 'financial_type_id' => 1,
122 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
123 $this->assertEquals(1, $getResult['count']);
127 * Test get with amount as a parameter.
129 * @throws \CRM_Core_Exception
131 public function testGetContributionPageByAmount() {
132 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
133 $this->id
= $createResult['id'];
136 'amount' => '' . $this->testAmount
,
138 'financial_type_id' => 1,
140 $getResult = $this->callAPISuccess($this->_entity
, 'get', $getParams);
141 $this->assertEquals(1, $getResult['count']);
145 * Test page deletion.
147 * @param int $version
149 * @dataProvider versionThreeAndFour
150 * @throws \CRM_Core_Exception
152 public function testDeleteContributionPage($version) {
153 $this->basicDeleteTest($version);
157 * Test getfields function.
159 * @throws \CRM_Core_Exception
161 public function testGetFieldsContributionPage() {
162 $result = $this->callAPISuccess($this->_entity
, 'getfields', ['action' => 'create']);
163 $this->assertEquals(12, $result['values']['start_date']['type']);
167 * Test form submission with basic price set.
169 * @throws \CRM_Core_Exception
171 public function testSubmit() {
172 $this->setUpContributionPage();
173 $submitParams = $this->getBasicSubmitParams();
175 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
176 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
177 //assert non-deductible amount
178 $this->assertEquals(5.00, $contribution['non_deductible_amount']);
182 * Test form submission with basic price set.
184 * @throws \CRM_Core_Exception
186 public function testSubmitZeroDollar() {
187 $this->setUpContributionPage();
188 $priceFieldID = reset($this->_ids
['price_field']);
190 'price_' . $priceFieldID => $this->_ids
['price_field_value']['cheapskate'],
191 'id' => (int) $this->_ids
['contribution_page'],
193 'priceSetId' => $this->_ids
['price_set'][0],
194 'payment_processor_id' => '',
197 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
198 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
200 $this->assertEquals($this->formatMoneyInput(0), $contribution['non_deductible_amount']);
201 $this->assertEquals($this->formatMoneyInput(0), $contribution['total_amount']);
205 * Test form submission with billing first & last name where the contact does NOT
206 * otherwise have one.
208 * @throws \CRM_Core_Exception
210 public function testSubmitNewBillingNameData() {
211 $this->setUpContributionPage();
212 $contact = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com']);
213 $priceFieldID = reset($this->_ids
['price_field']);
214 $priceFieldValueID = reset($this->_ids
['price_field_value']);
216 'price_' . $priceFieldID => $priceFieldValueID,
217 'id' => (int) $this->_ids
['contribution_page'],
219 'billing_first_name' => 'Wonder',
220 'billing_last_name' => 'Woman',
221 'contactID' => $contact['id'],
222 'email' => 'wonderwoman@amazon.com',
225 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
226 $contact = $this->callAPISuccess('Contact', 'get', [
227 'id' => $contact['id'],
235 $this->assertEquals([
236 'first_name' => 'Wonder',
237 'last_name' => 'Woman',
238 'display_name' => 'Wonder Woman',
239 'sort_name' => 'Woman, Wonder',
240 'id' => $contact['id'],
241 'contact_id' => $contact['id'],
242 ], $contact['values'][$contact['id']]);
247 * Test form submission with billing first & last name where the contact does
248 * otherwise have one and should not be overwritten.
250 * @throws \CRM_Core_Exception
252 public function testSubmitNewBillingNameDoNotOverwrite() {
253 $this->setUpContributionPage();
254 $contact = $this->callAPISuccess('Contact', 'create', [
255 'contact_type' => 'Individual',
256 'email' => 'wonderwoman@amazon.com',
257 'first_name' => 'Super',
258 'last_name' => 'Boy',
260 $priceFieldID = reset($this->_ids
['price_field']);
261 $priceFieldValueID = reset($this->_ids
['price_field_value']);
263 'price_' . $priceFieldID => $priceFieldValueID,
264 'id' => (int) $this->_ids
['contribution_page'],
266 'billing_first_name' => 'Wonder',
267 'billing_last_name' => 'Woman',
268 'contactID' => $contact['id'],
269 'email' => 'wonderwoman@amazon.com',
272 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
273 $contact = $this->callAPISuccess('Contact', 'get', [
274 'id' => $contact['id'],
282 $this->assertEquals([
283 'first_name' => 'Super',
284 'last_name' => 'Boy',
285 'display_name' => 'Super Boy',
286 'sort_name' => 'Boy, Super',
287 'id' => $contact['id'],
288 'contact_id' => $contact['id'],
289 ], $contact['values'][$contact['id']]);
294 * Test process with instant payment when more than one configured for the page.
296 * @see https://issues.civicrm.org/jira/browse/CRM-16923
298 * @throws \CiviCRM_API3_Exception
299 * @throws \CRM_Core_Exception
301 public function testSubmitRecurMultiProcessorInstantPayment() {
302 $this->setUpContributionPage();
303 $this->setupPaymentProcessor();
304 $paymentProcessor2ID = $this->paymentProcessorCreate([
305 'payment_processor_type_id' => 'Dummy',
306 'name' => 'processor 2',
307 'class_name' => 'Payment_Dummy',
310 $dummyPP = Civi\Payment\System
::singleton()->getById($paymentProcessor2ID);
311 $dummyPP->setDoDirectPaymentResult([
312 'payment_status_id' => 1,
313 'trxn_id' => 'create_first_success',
316 $processor = $dummyPP->getPaymentProcessor();
317 $this->callAPISuccess('ContributionPage', 'create', [
318 'id' => $this->_ids
['contribution_page'],
319 'payment_processor' => [$paymentProcessor2ID, $this->_ids
['payment_processor']],
322 $priceFieldID = reset($this->_ids
['price_field']);
323 $priceFieldValueID = reset($this->_ids
['price_field_value']);
325 'price_' . $priceFieldID => $priceFieldValueID,
326 'id' => (int) $this->_ids
['contribution_page'],
329 'frequency_interval' => 1,
330 'frequency_unit' => 'month',
331 'payment_processor_id' => $paymentProcessor2ID,
334 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
335 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
336 'contribution_page_id' => $this->_ids
['contribution_page'],
337 'contribution_status_id' => 1,
339 $this->assertEquals('create_first_success', $contribution['trxn_id']);
340 $this->assertEquals(10, $contribution['total_amount']);
341 $this->assertEquals(.85, $contribution['fee_amount']);
342 $this->assertEquals(9.15, $contribution['net_amount']);
343 $this->_checkFinancialRecords([
344 'id' => $contribution['id'],
345 'total_amount' => $contribution['total_amount'],
346 'payment_instrument_id' => $processor['payment_instrument_id'],
351 * Test submit with a membership block in place.
353 * @throws \CRM_Core_Exception
355 public function testSubmitMembershipBlockNotSeparatePayment() {
356 $this->setUpMembershipContributionPage();
358 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
359 'id' => (int) $this->_ids
['contribution_page'],
360 'billing_first_name' => 'Billy',
361 'billing_middle_name' => 'Goat',
362 'billing_last_name' => 'Gruff',
363 'selectMembership' => $this->_ids
['membership_type'][0],
366 $this->callAPIAndDocument('ContributionPage', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
367 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
368 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]);
369 $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']]);
373 * Test submit with a membership block in place works with renewal.
375 * @throws \CRM_Core_Exception
377 public function testSubmitMembershipBlockNotSeparatePaymentProcessorInstantRenew() {
378 $this->setUpMembershipContributionPage();
379 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
380 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1]);
382 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
383 'id' => (int) $this->_ids
['contribution_page'],
384 'billing_first_name' => 'Billy',
385 'billing_middle_name' => 'Goat',
386 'billing_last_name' => 'Gruff',
387 'selectMembership' => $this->_ids
['membership_type'][0],
388 'payment_processor_id' => 1,
389 'credit_card_number' => '4111111111111111',
390 'credit_card_type' => 'Visa',
391 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
395 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
396 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
397 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]);
398 $this->callAPISuccessGetCount('LineItem', [
399 'entity_table' => 'civicrm_membership',
400 'entity_id' => $membershipPayment['id'],
403 $submitParams['contact_id'] = $contribution['contact_id'];
405 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
406 $this->callAPISuccessGetCount('LineItem', [
407 'entity_table' => 'civicrm_membership',
408 'entity_id' => $membershipPayment['id'],
410 $membership = $this->callAPISuccessGetSingle('Membership', [
411 'id' => $membershipPayment['membership_id'],
412 'return' => ['end_date', 'join_date', 'start_date'],
414 $this->assertEquals(date('Y-m-d'), $membership['start_date']);
415 $this->assertEquals(date('Y-m-d'), $membership['join_date']);
416 $this->assertEquals(date('Y-m-d', strtotime('+ 2 year - 1 day')), $membership['end_date']);
420 * Test submit with a membership block in place.
422 * @throws \CRM_Core_Exception
424 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail(): void
{
425 $mut = new CiviMailUtils($this, TRUE);
426 $this->setUpMembershipContributionPage();
427 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
430 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
431 'id' => (int) $this->_ids
['contribution_page'],
432 'billing_first_name' => 'Billy',
433 'billing_middle_name' => 'Goat',
434 'billing_last_name' => 'Gruff',
435 'selectMembership' => $this->_ids
['membership_type'][0],
436 'email-Primary' => 'billy-goat@the-bridge.net',
437 'payment_processor_id' => $this->_paymentProcessor
['id'],
438 'credit_card_number' => '4111111111111111',
439 'credit_card_type' => 'Visa',
440 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
444 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
445 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
446 $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]);
448 'Membership Type: General',
449 'Test Frontend title',
452 $mut->clearMessages();
456 * Test submit with a membership block in place.
460 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
461 $mut = new CiviMailUtils($this, TRUE);
462 $this->_ids
['membership_type'] = [$this->membershipTypeCreate(['minimum_fee' => 0])];
463 $this->setUpMembershipContributionPage();
464 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
465 $priceFieldValueID = reset($this->_ids
['price_field_value']);
466 PriceFieldValue
::update()->addWhere('id', '=', $priceFieldValueID)->setValues(['amount' => 0])->execute();
469 'price_' . $this->_ids
['price_field'][0] => $priceFieldValueID,
470 'id' => (int) $this->_ids
['contribution_page'],
471 'billing_first_name' => 'Billy',
472 'billing_middle_name' => 'Goat',
473 'billing_last_name' => 'Gruffier',
474 'selectMembership' => $this->_ids
['membership_type'][0],
475 'email-Primary' => 'billy-goat@the-new-bridge.net',
476 'payment_processor_id' => $this->params
['payment_processor_id'],
479 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
480 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->_ids
['contribution_page']]);
481 $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]);
482 //Assert only one mail is being sent.
483 $msgs = $mut->getAllMessages();
484 $this->assertCount(1, $msgs);
487 'Membership Type: General',
493 $mut->clearMessages();
497 * Test submit with a pay later and check line item in mails.
499 * @throws \CRM_Core_Exception
501 public function testSubmitMembershipBlockIsSeparatePaymentPayLaterWithEmail() {
502 $mut = new CiviMailUtils($this, TRUE);
503 $this->setUpMembershipContributionPage(TRUE);
505 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
506 'id' => (int) $this->_ids
['contribution_page'],
507 'billing_first_name' => 'Billy',
508 'billing_middle_name' => 'Goat',
509 'billing_last_name' => 'Gruff',
511 'selectMembership' => $this->_ids
['membership_type'][0],
512 'email-Primary' => 'billy-goat@the-bridge.net',
515 $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
516 $contributions = $this->callAPISuccess('Contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']])['values'];
517 $this->assertCount(2, $contributions);
518 $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => ['IN' => array_keys($contributions)]]);
520 'Membership Amount -... $ 2.00',
523 $mut->clearMessages();
527 * Test submit with a membership block in place.
529 * @throws \CRM_Core_Exception
531 public function testSubmitMembershipBlockIsSeparatePayment() {
532 $this->setUpMembershipContributionPage(TRUE);
533 $this->_ids
['membership_type'] = [$this->membershipTypeCreate(['minimum_fee' => 2])];
535 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
536 'id' => (int) $this->_ids
['contribution_page'],
537 'billing_first_name' => 'Billy',
538 'billing_middle_name' => 'Goat',
539 'billing_last_name' => 'Gruff',
540 'selectMembership' => $this->_ids
['membership_type'][0],
543 $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
544 $contributions = $this->callAPISuccess('contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']]);
545 $this->assertCount(2, $contributions['values']);
546 $lines = $this->callAPISuccess('LineItem', 'get', ['sequential' => 1]);
547 $this->assertEquals(2, $lines['values'][0]['line_total']);
548 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle');
549 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
550 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
551 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
555 * Test submit with a membership block in place.
557 * @throws \CRM_Core_Exception
559 public function testSubmitMembershipBlockIsSeparatePaymentWithPayLater() {
560 $this->setUpMembershipContributionPage(TRUE);
561 $this->_ids
['membership_type'] = [$this->membershipTypeCreate(['minimum_fee' => 2])];
564 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
565 'id' => (int) $this->_ids
['contribution_page'],
566 'billing_first_name' => 'Billy',
567 'billing_middle_name' => 'Goat',
568 'billing_last_name' => 'Gruff',
570 'selectMembership' => $this->_ids
['membership_type'],
573 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
574 $contributions = $this->callAPISuccess('contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']]);
575 $this->assertCount(2, $contributions['values']);
576 foreach ($contributions['values'] as $val) {
577 $this->assertEquals(CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), $val['contribution_status_id']);
580 //Membership should be in Pending state.
581 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
582 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
583 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
584 $pendingStatus = $this->callAPISuccessGetSingle('MembershipStatus', ['return' => ['id'], 'name' => 'Pending']);
585 $this->assertEquals($membership['status_id'], $pendingStatus['id']);
586 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
590 * Test submit with a membership block in place.
592 * @throws \CRM_Core_Exception
594 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
595 // Need to work on valid financials on this test.
596 $this->isValidateFinancialsOnPostAssert
= FALSE;
597 $mut = new CiviMailUtils($this, TRUE);
598 $this->setUpMembershipContributionPage(TRUE);
599 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
602 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
603 'id' => (int) $this->_ids
['contribution_page'],
605 'billing_first_name' => 'Billy',
606 'billing_middle_name' => 'Goat',
607 'billing_last_name' => 'Gruff',
608 'selectMembership' => $this->_ids
['membership_type'],
609 'email-Primary' => 'billy-goat@the-bridge.net',
610 'payment_processor_id' => $this->_paymentProcessor
['id'],
611 'credit_card_number' => '4111111111111111',
612 'credit_card_type' => 'Visa',
613 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
617 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
618 $contributions = $this->callAPISuccess('contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']]);
619 $this->assertCount(2, $contributions['values']);
620 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
621 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
622 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
623 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
624 // We should have two separate email messages, each with their own amount
625 // line and no total line.
626 $mut->checkAllMailLog(
637 $mut->clearMessages();
641 * Test submit with a membership block in place.
643 * @throws \CRM_Core_Exception
645 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
646 // Need to work on valid financials on this test.
647 $this->isValidateFinancialsOnPostAssert
= FALSE;
648 $mut = new CiviMailUtils($this, TRUE);
649 $this->_ids
['membership_type'] = [$this->membershipTypeCreate(['minimum_fee' => 0])];
650 $this->setUpMembershipContributionPage(TRUE);
651 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
654 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
655 'id' => (int) $this->_ids
['contribution_page'],
657 'billing_first_name' => 'Billy',
658 'billing_middle_name' => 'Goat',
659 'billing_last_name' => 'Gruffalo',
660 'selectMembership' => $this->_ids
['membership_type'],
661 'payment_processor_id' => 0,
662 'email-Primary' => 'gruffalo@the-bridge.net',
665 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
666 $contributions = $this->callAPISuccess('contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']]);
667 $this->assertCount(2, $contributions['values']);
668 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
669 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
670 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
671 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
674 'General Membership: $ 0.00',
678 $mut->clearMessages();
682 * Test submit with a membership block in place.
684 * @throws \CRM_Core_Exception
686 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
687 // Need to work on valid financials on this test.
688 $this->isValidateFinancialsOnPostAssert
= FALSE;
689 $this->_ids
['membership_type'] = [$this->membershipTypeCreate(['minimum_fee' => 6])];
690 $this->_ids
['membership_type'][] = $this->membershipTypeCreate(['name' => 'Student', 'minimum_fee' => 50]);
691 $this->setUpMembershipContributionPage(TRUE);
693 'price_' . $this->_ids
['price_field'][0] => $this->_ids
['price_field_value'][1],
694 'id' => (int) $this->_ids
['contribution_page'],
696 'billing_first_name' => 'Billy',
697 'billing_middle_name' => 'Goat',
698 'billing_last_name' => 'Gruff',
699 'selectMembership' => $this->_ids
['membership_type'][1],
702 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
703 $contributions = $this->callAPISuccess('contribution', 'get', ['contribution_page_id' => $this->_ids
['contribution_page']]);
704 $this->assertCount(2, $contributions['values']);
705 $ids = array_keys($contributions['values']);
706 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
707 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
708 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
709 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
710 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
711 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
715 * Test submit with a membership block in place.
717 * We are expecting a separate payment for the membership vs the contribution.
719 * @throws \CRM_Core_Exception
720 * @throws \CiviCRM_API3_Exception
722 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
723 // Need to work on valid financials on this test.
724 $this->isValidateFinancialsOnPostAssert
= FALSE;
725 $mut = new CiviMailUtils($this, TRUE);
726 $this->setUpMembershipContributionPage(TRUE);
727 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessor
['id']);
728 $processor->setDoDirectPaymentResult(['payment_status_id' => 1, 'fee_amount' => .72]);
730 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
731 'id' => (int) $this->_ids
['contribution_page'],
733 'billing_first_name' => 'Billy',
734 'billing_middle_name' => 'Goat',
735 'billing_last_name' => 'Gruff',
736 'email-Primary' => 'henry@8th.king',
737 'selectMembership' => $this->_ids
['membership_type'],
738 'payment_processor_id' => $this->_paymentProcessor
['id'],
739 'credit_card_number' => '4111111111111111',
740 'credit_card_type' => 'Visa',
741 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
745 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
746 $contributions = $this->callAPISuccess('contribution', 'get', [
747 'contribution_page_id' => $this->_ids
['contribution_page'],
748 'contribution_status_id' => 1,
750 $this->assertCount(2, $contributions['values']);
751 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
752 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
753 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
754 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
755 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_table' => 'civicrm_membership']);
756 $this->assertEquals($lineItem['entity_id'], $membership['id']);
757 $this->assertEquals($lineItem['contribution_id'], $membershipPayment['contribution_id']);
758 $this->assertEquals($lineItem['qty'], 1);
759 $this->assertEquals($lineItem['unit_price'], 2);
760 $this->assertEquals($lineItem['line_total'], 2);
761 foreach ($contributions['values'] as $contribution) {
762 $this->assertEquals(.72, $contribution['fee_amount']);
763 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
765 // The total string is currently absent & it seems worse with - although at some point
766 // it may have been intended
767 $mut->checkAllMailLog(['$ 2.00', 'Contribution Amount', '$ 10.00'], ['Total:']);
769 $mut->clearMessages();
773 * Test submit with a membership block in place.
775 * Ensure a separate payment for the membership vs the contribution, with
778 * @param string $thousandSeparator
779 * punctuation used to refer to thousands.
781 * @throws \CRM_Core_Exception
782 * @throws \CiviCRM_API3_Exception
784 * @dataProvider getThousandSeparators
786 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNowChargesCorrectAmounts($thousandSeparator) {
787 $this->setCurrencySeparators($thousandSeparator);
788 $this->setUpMembershipContributionPage(TRUE);
789 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessor
['id']);
790 $processor->setDoDirectPaymentResult(['fee_amount' => .72]);
791 $test_uniq = uniqid();
792 $contributionPageAmount = 10;
794 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
795 'id' => (int) $this->_ids
['contribution_page'],
796 'amount' => $contributionPageAmount,
797 'billing_first_name' => 'Billy',
798 'billing_middle_name' => 'Goat',
799 'billing_last_name' => 'Gruff',
800 'email-Primary' => 'henry@8th.king',
801 'selectMembership' => $this->_ids
['membership_type'],
802 'payment_processor_id' => $this->_paymentProcessor
['id'],
803 'credit_card_number' => '4111111111111111',
804 'credit_card_type' => 'Visa',
805 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
807 'TEST_UNIQ' => $test_uniq,
811 $this->hookClass
->setHook('civicrm_alterPaymentProcessorParams', [$this, 'hook_civicrm_alterPaymentProcessorParams']);
813 $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
814 $this->callAPISuccess('contribution', 'get', [
815 'contribution_page_id' => $this->_ids
['contribution_page'],
816 'contribution_status_id' => 1,
819 $result = civicrm_api3('SystemLog', 'get', [
821 'message' => ['LIKE' => "%{$test_uniq}%"],
823 $this->assertCount(2, $result['values'], "Expected exactly 2 log entries matching {$test_uniq}.");
825 // Examine logged entries to ensure correct values.
826 $contribution_ids = [];
827 $found_membership_amount = $found_contribution_amount = FALSE;
828 foreach ($result['values'] as $value) {
829 list($junk, $json) = explode("$test_uniq:", $value['message']);
830 $logged_contribution = json_decode($json, TRUE);
831 $contribution_ids[] = $logged_contribution['contributionID'];
832 if (!empty($logged_contribution['total_amount'])) {
833 $amount = $logged_contribution['total_amount'];
836 $amount = $logged_contribution['amount'];
839 if ($amount == $this->_membershipBlockAmount
) {
840 $found_membership_amount = TRUE;
842 if ($amount == $contributionPageAmount) {
843 $found_contribution_amount = TRUE;
847 $distinct_contribution_ids = array_unique($contribution_ids);
848 $this->assertCount(2, $distinct_contribution_ids, "Expected exactly 2 log contributions with distinct contributionIDs.");
849 $this->assertTrue($found_contribution_amount, "Expected one log contribution with amount '$contributionPageAmount' (the contribution page amount)");
850 $this->assertTrue($found_membership_amount, "Expected one log contribution with amount '$this->_membershipBlockAmount' (the membership amount)");
854 * Test that when a transaction fails the pending contribution remains.
856 * An activity should also be created. CRM-16417.
858 * @throws \CRM_Core_Exception
860 public function testSubmitPaymentProcessorFailure() {
861 $this->setUpContributionPage();
862 $this->setupPaymentProcessor();
863 $this->createLoggedInUser();
864 $priceFieldID = reset($this->_ids
['price_field']);
865 $priceFieldValueID = reset($this->_ids
['price_field_value']);
867 'price_' . $priceFieldID => $priceFieldValueID,
868 'id' => (int) $this->_ids
['contribution_page'],
870 'payment_processor_id' => 1,
871 'credit_card_number' => '4111111111111111',
872 'credit_card_type' => 'Visa',
873 'credit_card_exp_date' => ['M' => 9, 'Y' => 2008],
877 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
878 $contribution = $this->callAPISuccessGetSingle('contribution', [
879 'contribution_page_id' => $this->_ids
['contribution_page'],
880 'contribution_status_id' => 'Failed',
883 $this->callAPISuccessGetSingle('activity', [
884 'source_record_id' => $contribution['id'],
885 'activity_type_id' => 'Failed Payment',
891 * Test submit recurring (yearly) membership with immediate confirmation (IATS style).
893 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
894 * processor (IATS style - denoted by returning trxn_id)
895 * - the first creates a new membership, completed contribution, in progress recurring. Check these
896 * - create another - end date should be extended
898 * @throws \CRM_Core_Exception
900 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentYear() {
901 // Need to work on valid financials on this test.
902 $this->isValidateFinancialsOnPostAssert
= FALSE;
903 $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'year', 'recur_frequency_unit' => 'year']);
907 * Test submit recurring (monthly) membership with immediate confirmation (IATS style).
909 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
910 * processor (IATS style - denoted by returning trxn_id)
911 * - the first creates a new membership, completed contribution, in progress recurring. Check these
912 * - create another - end date should be extended
914 * @throws \CRM_Core_Exception
916 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth() {
917 // Need to work on valid financials on this test.
918 $this->isValidateFinancialsOnPostAssert
= FALSE;
919 $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'month', 'recur_frequency_unit' => 'month']);
923 * Test submit recurring (mismatched frequency unit) membership with immediate confirmation (IATS style).
925 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
926 * processor (IATS style - denoted by returning trxn_id)
927 * - the first creates a new membership, completed contribution, in progress recurring. Check these
928 * - create another - end date should be extended
930 //public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentDifferentFrequency() {
931 // $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'month'));
935 * Helper function for testSubmitMembershipPriceSetPaymentProcessorRecurInstantPayment*
936 * @param array $params
938 * @throws \CRM_Core_Exception
940 public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment($params = []) {
941 // Need to work on valid financials on this test.
942 $this->isValidateFinancialsOnPostAssert
= FALSE;
943 $this->params
['is_recur'] = 1;
944 $this->params
['recur_frequency_unit'] = $params['recur_frequency_unit'];
945 $membershipTypeParams['duration_unit'] = $params['duration_unit'];
946 if ($params['recur_frequency_unit'] === $params['duration_unit']) {
947 $durationUnit = $params['duration_unit'];
950 $durationUnit = NULL;
952 $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
953 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
954 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
955 $processor = $dummyPP->getPaymentProcessor();
957 if ($params['recur_frequency_unit'] === $params['duration_unit']) {
958 // Membership will be in "New" state because it will get confirmed as payment matches
959 $expectedMembershipStatus = 1;
962 // Membership will still be in "Pending" state as it won't get confirmed as payment doesn't match
963 $expectedMembershipStatus = 5;
967 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
968 'id' => (int) $this->_ids
['contribution_page'],
970 'billing_first_name' => 'Billy',
971 'billing_middle_name' => 'Goat',
972 'billing_last_name' => 'Gruff',
973 'email' => 'billy@goat.gruff',
974 'selectMembership' => $this->_ids
['membership_type'],
975 'payment_processor_id' => 1,
976 'credit_card_number' => '4111111111111111',
977 'credit_card_type' => 'Visa',
978 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
981 'frequency_interval' => 1,
982 'frequency_unit' => $this->params
['recur_frequency_unit'],
985 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
986 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
987 'contribution_page_id' => $this->_ids
['contribution_page'],
988 'contribution_status_id' => 1,
990 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
992 $this->assertEquals('create_first_success', $contribution['trxn_id']);
993 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
994 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
995 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
996 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
997 $this->assertEquals($expectedMembershipStatus, $membership['status_id']);
998 $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
999 $this->assertEquals($contribution['contribution_recur_id'], $membership['contribution_recur_id']);
1001 $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $contribution['id'], 'entity_id' => $membership['id']]);
1002 //renew it with processor setting completed - should extend membership
1003 $submitParams['contact_id'] = $contribution['contact_id'];
1004 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_second_success']);
1005 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1006 $this->callAPISuccess('contribution', 'getsingle', [
1007 'id' => ['NOT IN' => [$contribution['id']]],
1008 'contribution_page_id' => $this->_ids
['contribution_page'],
1009 'contribution_status_id' => 1,
1011 $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1012 if ($durationUnit) {
1013 // We only have an end_date if frequency units match, otherwise membership won't be autorenewed and dates won't be calculated.
1014 $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membership['end_date']);
1015 $this->assertEquals($renewedMembershipEndDate, $renewedMembership['end_date']);
1017 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
1018 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
1019 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
1023 * Test submit recurring membership with immediate confirmation (IATS style).
1025 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1026 * processor (IATS style - denoted by returning trxn_id)
1027 * - the first creates a new membership, completed contribution, in progress recurring. Check these
1028 * - create another - end date should be extended
1030 * @throws \CRM_Core_Exception
1032 public function testSubmitMembershipComplexNonPriceSetPaymentPaymentProcessorRecurInstantPayment() {
1033 // Need to work on valid financials on this test.
1034 $this->isValidateFinancialsOnPostAssert
= FALSE;
1035 $this->params
['is_recur'] = 1;
1036 $this->params
['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
1037 // Add a membership so membership & contribution are not both 1.
1038 $preExistingMembershipID = $this->contactMembershipCreate(['contact_id' => $this->contactIds
[0]]);
1039 $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
1040 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1041 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
1042 $processor = $dummyPP->getPaymentProcessor();
1045 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
1046 'price_' . $this->_ids
['price_field']['cont'] => 88,
1047 'id' => (int) $this->_ids
['contribution_page'],
1049 'billing_first_name' => 'Billy',
1050 'billing_middle_name' => 'Goat',
1051 'billing_last_name' => 'Gruff',
1052 'email' => 'billy@goat.gruff',
1053 'selectMembership' => $this->_ids
['membership_type'],
1054 'payment_processor_id' => 1,
1055 'credit_card_number' => '4111111111111111',
1056 'credit_card_type' => 'Visa',
1057 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1060 'frequency_interval' => 1,
1061 'frequency_unit' => $this->params
['recur_frequency_unit'],
1064 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1065 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1066 'contribution_page_id' => $this->_ids
['contribution_page'],
1067 'contribution_status_id' => 1,
1069 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
1071 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1072 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
1073 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
1074 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1075 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
1076 $this->assertEquals(1, $membership['status_id']);
1077 $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
1079 $lines = $this->callAPISuccess('line_item', 'get', ['sequential' => 1, 'contribution_id' => $contribution['id']]);
1080 $this->assertEquals(2, $lines['count']);
1081 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1082 $this->assertEquals($preExistingMembershipID +
1, $lines['values'][0]['entity_id']);
1083 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1084 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
1085 $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID +
1]);
1087 //renew it with processor setting completed - should extend membership
1088 $submitParams['contact_id'] = $contribution['contact_id'];
1089 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_second_success']);
1090 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1091 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', [
1092 'id' => ['NOT IN' => [$contribution['id']]],
1093 'contribution_page_id' => $this->_ids
['contribution_page'],
1094 'contribution_status_id' => 1,
1096 $lines = $this->callAPISuccess('line_item', 'get', ['sequential' => 1, 'contribution_id' => $renewContribution['id']]);
1097 $this->assertEquals(2, $lines['count']);
1098 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1099 $this->assertEquals($preExistingMembershipID +
1, $lines['values'][0]['entity_id']);
1100 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1101 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
1103 $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1104 $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params
['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
1105 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
1106 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
1107 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
1111 * Test submit recurring membership with immediate confirmation (IATS style).
1113 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1114 * processor (IATS style - denoted by returning trxn_id)
1115 * - the first creates a new membership, completed contribution, in progress recurring. Check these
1116 * - create another - end date should be extended
1118 * @throws \CRM_Core_Exception
1120 public function testSubmitMembershipComplexPriceSetPaymentPaymentProcessorRecurInstantPayment() {
1121 $this->params
['is_recur'] = 1;
1122 $this->params
['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
1123 // Add a membership so membership & contribution are not both 1.
1124 $preExistingMembershipID = $this->contactMembershipCreate(['contact_id' => $this->contactIds
[0]]);
1125 $this->createPriceSetWithPage();
1126 $this->addSecondOrganizationMembershipToPriceSet();
1127 $this->setupPaymentProcessor();
1129 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1130 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
1131 $processor = $dummyPP->getPaymentProcessor();
1134 'price_' . $this->_ids
['price_field'][0] => $this->_ids
['price_field_value']['cont'],
1135 'price_' . $this->_ids
['price_field']['org1'] => $this->_ids
['price_field_value']['org1'],
1136 'price_' . $this->_ids
['price_field']['org2'] => $this->_ids
['price_field_value']['org2'],
1137 'id' => (int) $this->_ids
['contribution_page'],
1138 'billing_first_name' => 'Billy',
1139 'billing_middle_name' => 'Goat',
1140 'billing_last_name' => 'Gruff',
1141 'email' => 'billy@goat.gruff',
1142 'selectMembership' => NULL,
1143 'payment_processor_id' => 1,
1144 'credit_card_number' => '4111111111111111',
1145 'credit_card_type' => 'Visa',
1146 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1148 'frequency_interval' => 1,
1149 'frequency_unit' => $this->params
['recur_frequency_unit'],
1152 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1153 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1154 'contribution_page_id' => $this->_ids
['contribution_page'],
1155 'contribution_status_id' => 1,
1157 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
1159 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1160 $membershipPayments = $this->callAPISuccess('membership_payment', 'get', [
1162 'contribution_id' => $contribution['id'],
1164 $this->assertEquals(2, $membershipPayments['count']);
1165 $lines = $this->callAPISuccess('line_item', 'get', ['sequential' => 1, 'contribution_id' => $contribution['id']]);
1166 $this->assertEquals(3, $lines['count']);
1167 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1168 $this->assertEquals($preExistingMembershipID +
1, $lines['values'][0]['entity_id']);
1169 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1170 $this->assertEquals($contribution['id'], $lines['values'][1]['entity_id']);
1171 $this->assertEquals('civicrm_membership', $lines['values'][2]['entity_table']);
1172 $this->assertEquals($preExistingMembershipID +
2, $lines['values'][2]['entity_id']);
1174 $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id'], 'membership_id' => $preExistingMembershipID +
1]);
1175 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $preExistingMembershipID +
1]);
1177 //renew it with processor setting completed - should extend membership
1178 $submitParams['contact_id'] = $contribution['contact_id'];
1179 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_second_success']);
1180 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1181 $renewContribution = $this->callAPISuccess('contribution', 'getsingle', [
1182 'id' => ['NOT IN' => [$contribution['id']]],
1183 'contribution_page_id' => $this->_ids
['contribution_page'],
1184 'contribution_status_id' => 1,
1186 $lines = $this->callAPISuccess('line_item', 'get', ['sequential' => 1, 'contribution_id' => $renewContribution['id']]);
1187 $this->assertEquals(3, $lines['count']);
1188 $this->assertEquals('civicrm_membership', $lines['values'][0]['entity_table']);
1189 $this->assertEquals($preExistingMembershipID +
1, $lines['values'][0]['entity_id']);
1190 $this->assertEquals('civicrm_contribution', $lines['values'][1]['entity_table']);
1191 $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
1193 $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $preExistingMembershipID +
1]);
1194 $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params
['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
1198 * Extend the price set with a second organisation's membership.
1200 * @throws \CRM_Core_Exception
1202 public function addSecondOrganizationMembershipToPriceSet() {
1203 $organization2ID = $this->organizationCreate();
1204 $membershipTypes = $this->callAPISuccess('MembershipType', 'get', []);
1205 $this->_ids
['membership_type'] = array_keys($membershipTypes['values']);
1206 $this->_ids
['membership_type']['org2'] = $this->membershipTypeCreate(['contact_id' => $organization2ID, 'name' => 'Org 2']);
1207 $priceField = $this->callAPISuccess('PriceField', 'create', [
1208 'price_set_id' => $this->_ids
['price_set'],
1209 'html_type' => 'Radio',
1210 'name' => 'Org1 Price',
1211 'label' => 'Org1Price',
1213 $this->_ids
['price_field']['org1'] = $priceField['id'];
1215 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1216 'name' => 'org1 amount',
1217 'label' => 'org 1 Amount',
1219 'financial_type_id' => 'Member Dues',
1220 'format.only_id' => TRUE,
1221 'membership_type_id' => reset($this->_ids
['membership_type']),
1222 'price_field_id' => $priceField['id'],
1224 $this->_ids
['price_field_value']['org1'] = $priceFieldValue;
1226 $priceField = $this->callAPISuccess('PriceField', 'create', [
1227 'price_set_id' => $this->_ids
['price_set'],
1228 'html_type' => 'Radio',
1229 'name' => 'Org2 Price',
1230 'label' => 'Org2Price',
1232 $this->_ids
['price_field']['org2'] = $priceField['id'];
1234 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1235 'name' => 'org2 amount',
1236 'label' => 'org 2 Amount',
1238 'financial_type_id' => 'Member Dues',
1239 'format.only_id' => TRUE,
1240 'membership_type_id' => $this->_ids
['membership_type']['org2'],
1241 'price_field_id' => $priceField['id'],
1243 $this->_ids
['price_field_value']['org2'] = $priceFieldValue;
1248 * Test submit recurring membership with immediate confirmation (IATS style).
1250 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1251 * processor (IATS style - denoted by returning trxn_id)
1252 * - the first creates a new membership, completed contribution, in progress recurring. Check these
1253 * - create another - end date should be extended
1255 * @throws \CRM_Core_Exception
1257 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
1259 $this->setUpMembershipContributionPage(TRUE);
1260 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1261 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
1264 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
1265 'id' => (int) $this->_ids
['contribution_page'],
1267 'billing_first_name' => 'Billy',
1268 'billing_middle_name' => 'Goat',
1269 'billing_last_name' => 'Gruff',
1270 'email' => 'billy@goat.gruff',
1271 'selectMembership' => $this->_ids
['membership_type'][0],
1272 'payment_processor_id' => 1,
1273 'credit_card_number' => '4111111111111111',
1274 'credit_card_type' => 'Visa',
1275 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1278 'auto_renew' => TRUE,
1279 'frequency_interval' => 1,
1280 'frequency_unit' => 'month',
1283 $this->callAPIAndDocument('ContributionPage', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1284 $contribution = $this->callAPISuccess('contribution', 'get', [
1285 'contribution_page_id' => $this->_ids
['contribution_page'],
1286 'contribution_status_id' => 1,
1289 $this->assertEquals(2, $contribution['count']);
1290 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
1291 $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1292 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
1293 $this->callAPISuccess('contribution_recur', 'getsingle');
1297 * Test submit recurring membership with delayed confirmation (Authorize.net style)
1298 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
1299 * processor (Authorize.net style - denoted by NOT returning trxn_id)
1300 * - the first creates a pending membership, pending contribution, penging recurring. Check these
1301 * - complete the transaction
1302 * - create another - end date should NOT be extended
1304 * @throws \CRM_Core_Exception
1306 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
1307 $this->params
['is_recur'] = 1;
1308 $this->params
['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
1309 $this->setUpMembershipContributionPage();
1310 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1311 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 2]);
1312 $this->membershipTypeCreate(['name' => 'Student']);
1314 // Add a contribution & a couple of memberships so the id will not be 1 & will differ from membership id.
1315 // This saves us from 'accidental success'.
1316 $this->contributionCreate(['contact_id' => $this->contactIds
[0]]);
1317 $this->contactMembershipCreate(['contact_id' => $this->contactIds
[0]]);
1318 $this->contactMembershipCreate(['contact_id' => $this->contactIds
[0], 'membership_type_id' => 'Student']);
1321 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
1322 'id' => (int) $this->_ids
['contribution_page'],
1323 'billing_first_name' => 'Billy',
1324 'billing_middle_name' => 'Goat',
1325 'billing_last_name' => 'Gruff',
1326 'email' => 'billy@goat.gruff',
1327 'selectMembership' => $this->_ids
['membership_type'][0],
1328 'payment_processor_id' => 1,
1329 'credit_card_number' => '4111111111111111',
1330 'credit_card_type' => 'Visa',
1331 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1334 'frequency_interval' => 1,
1335 'frequency_unit' => $this->params
['recur_frequency_unit'],
1338 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1339 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1340 'contribution_page_id' => $this->_ids
['contribution_page'],
1341 'contribution_status_id' => 2,
1344 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
1345 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
1346 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1347 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
1348 $this->assertEquals(5, $membership['status_id']);
1350 $line = $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $contribution['id']]);
1351 $this->assertEquals('civicrm_membership', $line['entity_table']);
1352 $this->assertEquals($membership['id'], $line['entity_id']);
1354 $this->callAPISuccess('contribution', 'completetransaction', [
1355 'id' => $contribution['id'],
1356 'trxn_id' => 'ipn_called',
1357 'payment_processor_id' => $this->_paymentProcessor
['id'],
1359 $line = $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $contribution['id']]);
1360 $this->assertEquals('civicrm_membership', $line['entity_table']);
1361 $this->assertEquals($membership['id'], $line['entity_id']);
1363 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1364 //renew it with processor setting completed - should extend membership
1365 $submitParams = array_merge($submitParams, [
1366 'contact_id' => $contribution['contact_id'],
1368 'frequency_interval' => 1,
1369 'frequency_unit' => $this->params
['recur_frequency_unit'],
1372 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 2]);
1373 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1374 $newContribution = $this->callAPISuccess('contribution', 'getsingle', [
1376 'NOT IN' => [$contribution['id']],
1378 'contribution_page_id' => $this->_ids
['contribution_page'],
1379 'contribution_status_id' => 2,
1381 $line = $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $newContribution['id']]);
1382 $this->assertEquals('civicrm_membership', $line['entity_table']);
1383 $this->assertEquals($membership['id'], $line['entity_id']);
1385 $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
1386 //no renewal as the date hasn't changed
1387 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
1388 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $newContribution['contribution_recur_id']]);
1389 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
1393 * Test non-recur contribution with membership payment
1395 * @throws \CRM_Core_Exception
1397 public function testSubmitMembershipIsSeparatePaymentNotRecur() {
1398 //Create recur contribution page.
1399 $this->setUpMembershipContributionPage(TRUE, TRUE);
1400 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1401 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
1403 //Submit payment with recur disabled.
1405 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
1406 'id' => (int) $this->_ids
['contribution_page'],
1408 'frequency_interval' => 1,
1409 'frequency_unit' => 'month',
1410 'billing_first_name' => 'Billy',
1411 'billing_middle_name' => 'Goat',
1412 'billing_last_name' => 'Gruff',
1413 'email' => 'billy@goat.gruff',
1414 'selectMembership' => $this->_ids
['membership_type'][0],
1415 'payment_processor_id' => 1,
1416 'credit_card_number' => '4111111111111111',
1417 'credit_card_type' => 'Visa',
1418 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1422 //Assert if recur contribution is created.
1423 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1424 $recur = $this->callAPISuccess('contribution_recur', 'get', []);
1425 $this->assertEmpty($recur['count']);
1429 * Set up membership contribution page.
1431 * @param bool $isSeparatePayment
1432 * @param bool $isRecur
1433 * @param array $membershipTypeParams Parameters to pass to membershiptype.create API
1435 * @throws \CRM_Core_Exception
1437 public function setUpMembershipContributionPage($isSeparatePayment = FALSE, $isRecur = FALSE, $membershipTypeParams = []) {
1438 $this->setUpMembershipBlockPriceSet($membershipTypeParams);
1439 $this->setupPaymentProcessor();
1440 $this->setUpContributionPage($isRecur);
1442 $this->callAPISuccess('membership_block', 'create', [
1443 'entity_id' => $this->_ids
['contribution_page'],
1444 'entity_table' => 'civicrm_contribution_page',
1445 'is_required' => TRUE,
1446 'is_active' => TRUE,
1447 'is_separate_payment' => $isSeparatePayment,
1448 'membership_type_default' => $this->_ids
['membership_type'],
1453 * Set up pledge block.
1455 public function setUpPledgeBlock() {
1457 'entity_table' => 'civicrm_contribution_page',
1458 'entity_id' => $this->_ids
['contribution_page'],
1459 'pledge_frequency_unit' => 'week',
1460 'is_pledge_interval' => 0,
1461 'pledge_start_date' => json_encode(['calendar_date' => date('Ymd', strtotime("+1 month"))]),
1463 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::create($params);
1464 $this->_ids
['pledge_block_id'] = $pledgeBlock->id
;
1468 * The default data set does not include a complete default membership price set - not quite sure why.
1470 * This function ensures it exists & populates $this->_ids with it's data
1472 * @throws \CRM_Core_Exception
1474 public function setUpMembershipBlockPriceSet($membershipTypeParams = []) {
1475 $this->_ids
['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', [
1476 'name' => 'default_membership_type_amount',
1479 if (empty($this->_ids
['membership_type'])) {
1480 $membershipTypeParams = array_merge([
1482 ], $membershipTypeParams);
1483 $this->_ids
['membership_type'] = [$this->membershipTypeCreate($membershipTypeParams)];
1485 $priceField = $this->callAPISuccess('price_field', 'create', [
1486 'price_set_id' => reset($this->_ids
['price_set']),
1487 'name' => 'membership_amount',
1488 'label' => 'Membership Amount',
1489 'html_type' => 'Radio',
1492 $this->_ids
['price_field'][] = $priceField['id'];
1494 foreach ($this->_ids
['membership_type'] as $membershipTypeID) {
1495 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1496 'name' => 'membership_amount',
1497 'label' => 'Membership Amount',
1498 'amount' => $this->_membershipBlockAmount
,
1499 'financial_type_id' => 'Donation',
1500 'format.only_id' => TRUE,
1501 'membership_type_id' => $membershipTypeID,
1502 'price_field_id' => $priceField['id'],
1504 $this->_ids
['price_field_value'][] = $priceFieldValue;
1506 if (!empty($this->_ids
['membership_type']['org2'])) {
1507 $priceField = $this->callAPISuccess('price_field', 'create', [
1508 'price_set_id' => reset($this->_ids
['price_set']),
1509 'name' => 'membership_org2',
1510 'label' => 'Membership Org2',
1511 'html_type' => 'Checkbox',
1514 $this->_ids
['price_field']['org2'] = $priceField['id'];
1516 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1517 'name' => 'membership_org2',
1518 'label' => 'Membership org 2',
1520 'financial_type_id' => 'Member Dues',
1521 'format.only_id' => TRUE,
1522 'membership_type_id' => $this->_ids
['membership_type']['org2'],
1523 'price_field_id' => $priceField['id'],
1525 $this->_ids
['price_field_value']['org2'] = $priceFieldValue;
1527 $priceField = $this->callAPISuccess('price_field', 'create', [
1528 'price_set_id' => reset($this->_ids
['price_set']),
1529 'name' => 'Contribution',
1530 'label' => 'Contribution',
1531 'html_type' => 'Text',
1533 'is_enter_qty' => 1,
1535 $this->_ids
['price_field']['cont'] = $priceField['id'];
1536 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1537 'name' => 'contribution',
1538 'label' => 'Give me money',
1540 'financial_type_id' => 'Donation',
1541 'format.only_id' => TRUE,
1542 'price_field_id' => $priceField['id'],
1544 $this->_ids
['price_field_value'][] = $priceFieldValue;
1548 * Add text field other amount to the price set.
1550 * @throws \CRM_Core_Exception
1552 public function addOtherAmountFieldToMembershipPriceSet() {
1553 $this->_ids
['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', [
1554 'price_set_id' => reset($this->_ids
['price_set']),
1555 'name' => 'other_amount',
1556 'label' => 'Other Amount',
1557 'html_type' => 'Text',
1558 'format.only_id' => TRUE,
1561 $this->_ids
['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', [
1562 'financial_type_id' => 'Donation',
1563 'format.only_id' => TRUE,
1564 'label' => 'Other Amount',
1566 'price_field_id' => $this->_ids
['price_field']['other_amount'],
1571 * Help function to set up contribution page with some defaults.
1573 * @param bool $isRecur
1575 * @throws \CRM_Core_Exception
1577 public function setUpContributionPage($isRecur = FALSE) {
1579 $this->params
['is_recur'] = 1;
1580 $this->params
['recur_frequency_unit'] = 'month';
1582 $this->params
['frontend_title'] = 'Test Frontend title';
1583 $contributionPageResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
1584 if (empty($this->_ids
['price_set'])) {
1585 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams
);
1586 $this->_ids
['price_set'][] = $priceSet['id'];
1588 $priceSetID = reset($this->_ids
['price_set']);
1589 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1591 if (empty($this->_ids
['price_field'])) {
1592 $priceField = $this->callAPISuccess('price_field', 'create', [
1593 'price_set_id' => $priceSetID,
1594 'label' => 'Goat Breed',
1595 'html_type' => 'Radio',
1597 $this->_ids
['price_field'] = [$priceField['id']];
1599 if (empty($this->_ids
['price_field_value'])) {
1600 $this->callAPISuccess('price_field_value', 'create', [
1601 'price_set_id' => $priceSetID,
1602 'price_field_id' => $priceField['id'],
1603 'label' => 'Long Haired Goat',
1604 'financial_type_id' => 'Donation',
1606 'non_deductible_amount' => 15,
1608 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1609 'price_set_id' => $priceSetID,
1610 'price_field_id' => $priceField['id'],
1611 'label' => 'Shoe-eating Goat',
1612 'financial_type_id' => 'Donation',
1614 'non_deductible_amount' => 5,
1616 $this->_ids
['price_field_value'] = [$priceFieldValue['id']];
1618 $this->_ids
['price_field_value']['cheapskate'] = $this->callAPISuccess('price_field_value', 'create', [
1619 'price_set_id' => $priceSetID,
1620 'price_field_id' => $priceField['id'],
1621 'label' => 'Stingy Goat',
1622 'financial_type_id' => 'Donation',
1624 'non_deductible_amount' => 0,
1627 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1631 * Helper function to set up contribution page which can be used to purchase a
1632 * membership type for different intervals.
1634 * @throws \CRM_Core_Exception
1636 public function setUpMultiIntervalMembershipContributionPage() {
1637 // Need to work on valid financials on this test.
1638 $this->isValidateFinancialsOnPostAssert
= FALSE;
1639 $this->setupPaymentProcessor();
1640 $contributionPage = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
1641 $this->_ids
['contribution_page'] = $contributionPage['id'];
1643 $this->_ids
['membership_type'] = $this->membershipTypeCreate([
1646 'duration_unit' => 'month',
1649 $priceSet = $this->callAPISuccess('PriceSet', 'create', [
1650 'is_quick_config' => 0,
1651 'extends' => 'CiviMember',
1652 'financial_type_id' => 'Member Dues',
1653 'title' => 'CRM-21177',
1655 $this->_ids
['price_set'] = $priceSet['id'];
1657 $priceField = $this->callAPISuccess('price_field', 'create', [
1658 'price_set_id' => $this->_ids
['price_set'],
1659 'name' => 'membership_type',
1660 'label' => 'Membership Type',
1661 'html_type' => 'Radio',
1663 $this->_ids
['price_field'] = $priceField['id'];
1665 $priceFieldValueMonthly = $this->callAPISuccess('price_field_value', 'create', [
1666 'name' => 'CRM-21177_Monthly',
1667 'label' => 'CRM-21177 - Monthly',
1669 'membership_num_terms' => 1,
1670 'membership_type_id' => $this->_ids
['membership_type'],
1671 'price_field_id' => $this->_ids
['price_field'],
1672 'financial_type_id' => 'Member Dues',
1674 $this->_ids
['price_field_value_monthly'] = $priceFieldValueMonthly['id'];
1676 $priceFieldValueYearly = $this->callAPISuccess('price_field_value', 'create', [
1677 'name' => 'CRM-21177_Yearly',
1678 'label' => 'CRM-21177 - Yearly',
1680 'membership_num_terms' => 12,
1681 'membership_type_id' => $this->_ids
['membership_type'],
1682 'price_field_id' => $this->_ids
['price_field'],
1683 'financial_type_id' => 'Member Dues',
1685 $this->_ids
['price_field_value_yearly'] = $priceFieldValueYearly['id'];
1687 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $this->_ids
['contribution_page'], $this->_ids
['price_set']);
1689 $this->callAPISuccess('membership_block', 'create', [
1690 'entity_id' => $this->_ids
['contribution_page'],
1691 'entity_table' => 'civicrm_contribution_page',
1692 'is_required' => TRUE,
1693 'is_separate_payment' => FALSE,
1694 'is_active' => TRUE,
1695 'membership_type_default' => $this->_ids
['membership_type'],
1700 * Test submit with a membership block in place.
1702 * @throws \CRM_Core_Exception
1704 public function testSubmitMultiIntervalMembershipContributionPage() {
1705 $this->setUpMultiIntervalMembershipContributionPage();
1707 'price_' . $this->_ids
['price_field'] => $this->_ids
['price_field_value_monthly'],
1708 'id' => (int) $this->_ids
['contribution_page'],
1710 'first_name' => 'Billy',
1711 'last_name' => 'Gruff',
1712 'email' => 'billy@goat.gruff',
1713 'payment_processor_id' => $this->_ids
['payment_processor'],
1714 'credit_card_number' => '4111111111111111',
1715 'credit_card_type' => 'Visa',
1716 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1720 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1722 $submitParams['price_' . $this->_ids
['price_field']] = $this->_ids
['price_field_value_yearly'];
1723 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
1725 $contribution = $this->callAPISuccess('Contribution', 'get', [
1726 'contribution_page_id' => $this->_ids
['contribution_page'],
1728 'api.ContributionRecur.getsingle' => [],
1730 $this->assertEquals(1, $contribution['values'][0]['api.ContributionRecur.getsingle']['frequency_interval']);
1731 //$this->assertEquals(12, $contribution['values'][1]['api.ContributionRecur.getsingle']['frequency_interval']);
1734 public static function setUpBeforeClass() {
1735 // put stuff here that should happen before all tests in this unit
1739 * Create a payment processor instance.
1741 * @throws \CRM_Core_Exception
1743 protected function setupPaymentProcessor() {
1744 $this->params
['payment_processor_id'] = $this->_ids
['payment_processor'] = $this->paymentProcessorCreate([
1745 'payment_processor_type_id' => 'Dummy',
1746 'class_name' => 'Payment_Dummy',
1747 'billing_mode' => 1,
1749 $this->_paymentProcessor
= $this->callAPISuccess('payment_processor', 'getsingle', ['id' => $this->params
['payment_processor_id']]);
1753 * Test submit recurring pledge.
1755 * - 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.
1757 * @throws \CRM_Core_Exception
1759 public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() {
1760 // Need to work on valid financials on this test.
1761 $this->isValidateFinancialsOnPostAssert
= FALSE;
1762 $this->params
['adjust_recur_start_date'] = TRUE;
1763 $this->params
['is_pay_later'] = FALSE;
1764 $this->setUpContributionPage();
1765 $this->setUpPledgeBlock();
1766 $this->setupPaymentProcessor();
1767 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
1768 $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
1771 'id' => (int) $this->_ids
['contribution_page'],
1773 'billing_first_name' => 'Billy',
1774 'billing_middle_name' => 'Goat',
1775 'billing_last_name' => 'Gruff',
1776 'email' => 'billy@goat.gruff',
1777 'payment_processor_id' => 1,
1778 'credit_card_number' => '4111111111111111',
1779 'credit_card_type' => 'Visa',
1780 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1782 'pledge_frequency_interval' => 1,
1783 'pledge_frequency_unit' => 'week',
1784 'pledge_installments' => 3,
1785 'is_pledge' => TRUE,
1786 'pledge_block_id' => (int) $this->_ids
['pledge_block_id'],
1789 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1791 // Check if contribution created.
1792 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1793 'contribution_page_id' => $this->_ids
['contribution_page'],
1794 // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1795 'contribution_status_id' => 'Completed',
1798 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1800 // Check if pledge created.
1801 $pledge = $this->callAPISuccess('pledge', 'getsingle', []);
1802 $this->assertEquals(date('Ymd', strtotime($pledge['pledge_start_date'])), date('Ymd', strtotime("+1 month")));
1803 $this->assertEquals($pledge['pledge_amount'], 300.00);
1805 // Check if pledge payments created.
1807 'pledge_id' => $pledge['id'],
1809 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1810 $this->assertEquals($pledgePayment['count'], 3);
1811 $this->assertEquals(date('Ymd', strtotime($pledgePayment['values'][1]['scheduled_date'])), date('Ymd', strtotime("+1 month")));
1812 $this->assertEquals($pledgePayment['values'][1]['scheduled_amount'], 100.00);
1813 // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1814 $this->assertEquals($pledgePayment['values'][1]['status_id'], 1);
1816 // Check contribution recur record.
1817 $recur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
1818 $this->assertEquals(date('Ymd', strtotime($recur['start_date'])), date('Ymd', strtotime("+1 month")));
1819 $this->assertEquals($recur['amount'], 100.00);
1820 // In progress status.
1821 $this->assertEquals($recur['contribution_status_id'], 5);
1825 * Test submit pledge payment.
1827 * - test submitting a pledge payment using contribution form.
1829 * @throws \CRM_Core_Exception
1831 public function testSubmitPledgePayment() {
1832 // Need to work on valid financials on this test.
1833 $this->isValidateFinancialsOnPostAssert
= FALSE;
1834 $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment();
1835 $pledge = $this->callAPISuccess('pledge', 'getsingle', []);
1837 'pledge_id' => $pledge['id'],
1840 'id' => (int) $pledge['pledge_contribution_page_id'],
1841 'pledge_amount' => [2 => 1],
1842 'billing_first_name' => 'Billy',
1843 'billing_middle_name' => 'Goat',
1844 'billing_last_name' => 'Gruff',
1845 'email' => 'billy@goat.gruff',
1846 'payment_processor_id' => 1,
1847 'credit_card_number' => '4111111111111111',
1848 'credit_card_type' => 'Visa',
1849 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
1851 'pledge_id' => $pledge['id'],
1852 'cid' => $pledge['contact_id'],
1853 'contact_id' => $pledge['contact_id'],
1855 'is_pledge' => TRUE,
1856 'pledge_block_id' => $this->_ids
['pledge_block_id'],
1858 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1859 $this->assertEquals($pledgePayment['values'][2]['status_id'], 2);
1861 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1863 // Check if contribution created.
1864 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1865 'contribution_page_id' => $pledge['pledge_contribution_page_id'],
1866 'contribution_status_id' => 'Completed',
1867 'contact_id' => $pledge['contact_id'],
1868 'contribution_recur_id' => ['IS NULL' => 1],
1871 $this->assertEquals(100.00, $contribution['total_amount']);
1872 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1873 $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, 'This pledge payment should have been completed');
1874 $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);
1878 * Test form submission with multiple option price set.
1880 * @param string $thousandSeparator
1881 * punctuation used to refer to thousands.
1883 * @dataProvider getThousandSeparators
1884 * @throws \CRM_Core_Exception
1886 public function testSubmitContributionPageWithPriceSet($thousandSeparator) {
1887 $this->setCurrencySeparators($thousandSeparator);
1888 $this->_priceSetParams
['is_quick_config'] = 0;
1889 $this->setUpContributionPage();
1891 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
1892 'id' => (int) $this->_ids
['contribution_page'],
1893 'first_name' => 'Billy',
1894 'last_name' => 'Gruff',
1895 'email' => 'billy@goat.gruff',
1896 'is_pay_later' => TRUE,
1898 $this->addPriceFields($submitParams);
1900 $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
1901 $contribution = $this->callAPISuccessGetSingle('contribution', [
1902 'contribution_page_id' => $this->_ids
['contribution_page'],
1903 'contribution_status_id' => 'Pending',
1905 $this->assertEquals(80, $contribution['total_amount']);
1906 $lineItems = $this->callAPISuccess('LineItem', 'get', [
1907 'contribution_id' => $contribution['id'],
1909 $this->assertEquals(3, $lineItems['count']);
1910 $totalLineAmount = 0;
1911 foreach ($lineItems['values'] as $lineItem) {
1912 $totalLineAmount +
= $lineItem['line_total'];
1914 $this->assertEquals(80, $totalLineAmount);
1918 * Function to add additional price fields to priceset.
1920 * @param array $params
1922 * @throws \CRM_Core_Exception
1924 public function addPriceFields(&$params) {
1925 $priceSetID = reset($this->_ids
['price_set']);
1926 $priceField = $this->callAPISuccess('price_field', 'create', [
1927 'price_set_id' => $priceSetID,
1928 'label' => 'Chicken Breed',
1929 'html_type' => 'CheckBox',
1931 $priceFieldValue1 = $this->callAPISuccess('price_field_value', 'create', [
1932 'price_set_id' => $priceSetID,
1933 'price_field_id' => $priceField['id'],
1934 'label' => 'Shoe-eating chicken -1',
1935 'financial_type_id' => 'Donation',
1938 $priceFieldValue2 = $this->callAPISuccess('price_field_value', 'create', [
1939 'price_set_id' => $priceSetID,
1940 'price_field_id' => $priceField['id'],
1941 'label' => 'Shoe-eating chicken -2',
1942 'financial_type_id' => 'Donation',
1945 $params['price_' . $priceField['id']] = [
1946 $priceFieldValue1['id'] => 1,
1947 $priceFieldValue2['id'] => 1,
1952 * Test Tax Amount is calculated properly when using PriceSet with Field Type = Text/Numeric Quantity
1954 * @param string $thousandSeparator
1955 * punctuation used to refer to thousands.
1957 * @dataProvider getThousandSeparators
1958 * @throws \CRM_Core_Exception
1960 public function testSubmitContributionPageWithPriceSetQuantity($thousandSeparator) {
1961 $this->setCurrencySeparators($thousandSeparator);
1962 $this->_priceSetParams
['is_quick_config'] = 0;
1963 $this->enableTaxAndInvoicing();
1964 $financialType = $this->createFinancialType();
1965 $financialTypeId = $financialType['id'];
1966 // 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%
1967 $this->addTaxAccountToFinancialType($financialType['id']);
1969 $this->setUpContributionPage();
1971 'id' => (int) $this->_ids
['contribution_page'],
1972 'first_name' => 'J',
1974 'email' => 'JT@ohcanada.ca',
1975 'is_pay_later' => TRUE,
1976 'receive_date' => date('Y-m-d H:i:s'),
1979 // Add Existing PriceField
1980 // This is a Shoe-eating Goat; qty = 1; unit_price = $10.00; There is no sales tax on Goats
1981 $submitParams['price_' . $this->_ids
['price_field'][0]] = reset($this->_ids
['price_field_value']);
1983 // Create additional PriceSet/PriceField
1984 $priceSetID = reset($this->_ids
['price_set']);
1985 $priceField = $this->callAPISuccess('price_field', 'create', [
1986 'price_set_id' => $priceSetID,
1987 'label' => 'Printing Rights',
1988 'html_type' => 'Text',
1991 $this->callAPISuccess('price_field_value', 'create', [
1992 'price_set_id' => $priceSetID,
1993 'price_field_id' => $priceField['id'],
1994 'label' => 'Printing Rights',
1995 'financial_type_id' => $financialTypeId,
1996 'amount' => '16.95',
1998 $priceFieldId = $priceField['id'];
2000 // Set quantity for our test
2001 $submitParams['price_' . $priceFieldId] = 180;
2003 $priceField = $this->callAPISuccess('price_field', 'create', [
2004 'price_set_id' => $priceSetID,
2005 'label' => 'Another Line Item',
2006 'html_type' => 'Text',
2009 $this->callAPISuccess('price_field_value', 'create', [
2010 'price_set_id' => $priceSetID,
2011 'price_field_id' => $priceField['id'],
2012 'label' => 'Another Line Item',
2013 'financial_type_id' => $financialTypeId,
2016 $priceFieldId = $priceField['id'];
2018 // Set quantity for our test
2019 $submitParams['price_' . $priceFieldId] = 110;
2021 // This is the correct Tax Amount - use it later to compare to what the CiviCRM Core came up with at the LineItem level
2022 $submitParams['tax_amount'] = (180 * 16.95 * 0.10 +
110 * 2.95 * 0.10);
2024 $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
2025 $this->validateAllContributions();
2027 $contribution = $this->callAPISuccessGetSingle('contribution', [
2028 'contribution_page_id' => $this->_ids
['contribution_page'],
2031 // Retrieve the lineItem that belongs to the Goat
2032 $lineItem1 = $this->callAPISuccessGetSingle('LineItem', [
2033 'contribution_id' => $contribution['id'],
2034 'label' => 'Shoe-eating Goat',
2037 // Retrieve the lineItem that belongs to the Printing Rights and check the tax_amount CiviCRM Core calculated for it
2038 $lineItem2 = $this->callAPISuccessGetSingle('LineItem', [
2039 'contribution_id' => $contribution['id'],
2040 'label' => 'Printing Rights',
2043 // Retrieve the lineItem that belongs to the Another Line Item and check the tax_amount CiviCRM Core calculated for it
2044 $lineItem3 = $this->callAPISuccessGetSingle('LineItem', [
2045 'contribution_id' => $contribution['id'],
2046 'label' => 'Another Line Item',
2049 $this->assertEquals($lineItem1['line_total'] +
$lineItem2['line_total'] +
$lineItem3['line_total'], round(10 +
180 * 16.95 +
110 * 2.95, 2), 'Line Item Total is incorrect.');
2050 $this->assertEquals(round($lineItem1['tax_amount'] +
$lineItem2['tax_amount'] +
$lineItem3['tax_amount'], 2), round(180 * 16.95 * 0.10 +
110 * 2.95 * 0.10, 2), 'Wrong Sales Tax Amount is calculated and stored.');
2054 * Test validating a contribution page submit.
2056 * @throws \CRM_Core_Exception
2058 public function testValidate() {
2059 $this->setUpContributionPage();
2060 $errors = $this->callAPISuccess('ContributionPage', 'validate', array_merge($this->getBasicSubmitParams(), ['action' => 'submit']))['values'];
2061 $this->assertEmpty($errors);
2065 * Test validating a contribution page submit in POST context.
2067 * A likely use case for the validation is when the is being submitted and some handling is
2068 * to be done before processing but the validity of input needs to be checked first.
2070 * For example Paypal Checkout will replace the confirm button with it's own but we are able to validate
2071 * before paypal launches it's modal. In this case the $_REQUEST is post but we need validation to succeed.
2073 * @throws \CRM_Core_Exception
2075 public function testValidatePost() {
2076 $_SERVER['REQUEST_METHOD'] = 'POST';
2077 $this->setUpContributionPage();
2078 $errors = $this->callAPISuccess('ContributionPage', 'validate', array_merge($this->getBasicSubmitParams(), ['action' => 'submit']))['values'];
2079 $this->assertEmpty($errors);
2080 unset($_SERVER['REQUEST_METHOD']);
2084 * Test that an error is generated if required fields are not submitted.
2086 * @throws \CRM_Core_Exception
2088 public function testValidateOutputOnMissingRecurFields() {
2089 $this->params
['is_recur_interval'] = 1;
2090 $this->setUpContributionPage(TRUE);
2091 $submitParams = array_merge($this->getBasicSubmitParams(), ['action' => 'submit']);
2092 $submitParams['is_recur'] = 1;
2093 $submitParams['frequency_interval'] = '';
2094 $submitParams['frequency_unit'] = '';
2095 $errors = $this->callAPISuccess('ContributionPage', 'validate', $submitParams)['values'];
2096 $this->assertEquals('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).', $errors['frequency_interval']);
2100 * Implements hook_civicrm_alterPaymentProcessorParams().
2102 * @throws \Exception
2104 public function hook_civicrm_alterPaymentProcessorParams($paymentObj, &$rawParams, &$cookedParams) {
2105 // Ensure total_amount are the same if they're both given.
2106 $total_amount = $rawParams['total_amount'] ??
NULL;
2107 $amount = $rawParams['amount'] ??
NULL;
2108 if (!empty($total_amount) && !empty($amount) && $total_amount != $amount) {
2109 throw new Exception("total_amount '$total_amount' and amount '$amount' differ.");
2112 // Log parameters for later debugging and testing.
2113 $message = __FUNCTION__
. ": {$rawParams['TEST_UNIQ']}:";
2114 $log_params = array_intersect_key($rawParams, [
2116 'total_amount' => 1,
2117 'contributionID' => 1,
2119 $message .= json_encode($log_params);
2120 $log = new CRM_Utils_SystemLogger();
2121 $log->debug($message, $_REQUEST);
2125 * Get the params for a basic simple submit.
2129 protected function getBasicSubmitParams() {
2130 $priceFieldID = reset($this->_ids
['price_field']);
2131 $priceFieldValueID = reset($this->_ids
['price_field_value']);
2133 'price_' . $priceFieldID => $priceFieldValueID,
2134 'id' => (int) $this->_ids
['contribution_page'],
2136 'priceSetId' => $this->_ids
['price_set'][0],
2137 'payment_processor_id' => 0,