3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_recur* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_ContributionPageTest
extends CiviUnitTestCase
{
36 protected $_apiversion = 3;
37 protected $testAmount = 34567;
40 protected $contactIds = array();
41 protected $_entity = 'contribution_page';
42 protected $contribution_result = NULL;
43 protected $_priceSetParams = array();
45 * Payment processor details.
48 protected $_paymentProcessor = array();
57 protected $_ids = array();
60 public $DBResetRequired = TRUE;
62 public function setUp() {
64 $this->contactIds
[] = $this->individualCreate();
65 $this->params
= array(
66 'title' => "Test Contribution Page",
67 'financial_type_id' => 1,
69 'goal_amount' => $this->testAmount
,
71 'is_monetary' => TRUE,
72 'is_email_receipt' => TRUE,
73 'receipt_from_email' => 'yourconscience@donate.com',
74 'receipt_from_name' => 'Ego Freud',
77 $this->_priceSetParams
= array(
78 'is_quick_config' => 1,
79 'extends' => 'CiviContribute',
80 'financial_type_id' => 'Donation',
85 public function tearDown() {
86 foreach ($this->contactIds
as $id) {
87 $this->callAPISuccess('contact', 'delete', array('id' => $id));
89 $this->quickCleanUpFinancialEntities();
92 public function testCreateContributionPage() {
93 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
94 $this->assertEquals(1, $result['count']);
95 $this->assertNotNull($result['values'][$result['id']]['id']);
96 $this->getAndCheck($this->params
, $result['id'], $this->_entity
);
99 public function testGetBasicContributionPage() {
100 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
101 $this->id
= $createResult['id'];
104 'financial_type_id' => 1,
106 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
107 $this->assertEquals(1, $getResult['count']);
110 public function testGetContributionPageByAmount() {
111 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
112 $this->id
= $createResult['id'];
114 'amount' => '' . $this->testAmount
, // 3456
116 'financial_type_id' => 1,
118 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
119 $this->assertEquals(1, $getResult['count']);
122 public function testDeleteContributionPage() {
123 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
124 $deleteParams = array('id' => $createResult['id']);
125 $this->callAPIAndDocument($this->_entity
, 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
126 $checkDeleted = $this->callAPISuccess($this->_entity
, 'get', array());
127 $this->assertEquals(0, $checkDeleted['count']);
130 public function testGetFieldsContributionPage() {
131 $result = $this->callAPISuccess($this->_entity
, 'getfields', array('action' => 'create'));
132 $this->assertEquals(12, $result['values']['start_date']['type']);
137 * Test form submission with basic price set.
139 public function testSubmit() {
140 $this->setUpContributionPage();
141 $priceFieldID = reset($this->_ids
['price_field']);
142 $priceFieldValueID = reset($this->_ids
['price_field_value']);
143 $submitParams = array(
144 'price_' . $priceFieldID => $priceFieldValueID,
145 'id' => (int) $this->_ids
['contribution_page'],
149 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
150 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
151 //assert non-deductible amount
152 $this->assertEquals(5.00, $contribution['non_deductible_amount']);
156 * Test form submission with billing first & last name where the contact does NOT
157 * otherwise have one.
159 public function testSubmitNewBillingNameData() {
160 $this->setUpContributionPage();
161 $contact = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com'));
162 $priceFieldID = reset($this->_ids
['price_field']);
163 $priceFieldValueID = reset($this->_ids
['price_field_value']);
164 $submitParams = array(
165 'price_' . $priceFieldID => $priceFieldValueID,
166 'id' => (int) $this->_ids
['contribution_page'],
168 'billing_first_name' => 'Wonder',
169 'billing_last_name' => 'Woman',
170 'contactID' => $contact['id'],
171 'email' => 'wonderwoman@amazon.com',
174 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
175 $contact = $this->callAPISuccess('Contact', 'get', array(
176 'id' => $contact['id'],
184 $this->assertEquals(array(
185 'first_name' => 'Wonder',
186 'last_name' => 'Woman',
187 'display_name' => 'Wonder Woman',
188 'sort_name' => 'Woman, Wonder',
189 'id' => $contact['id'],
190 'contact_id' => $contact['id'],
191 ), $contact['values'][$contact['id']]);
196 * Test form submission with billing first & last name where the contact does
197 * otherwise have one and should not be overwritten.
199 public function testSubmitNewBillingNameDoNotOverwrite() {
200 $this->setUpContributionPage();
201 $contact = $this->callAPISuccess('Contact', 'create', array(
202 'contact_type' => 'Individual',
203 'email' => 'wonderwoman@amazon.com',
204 'first_name' => 'Super',
205 'last_name' => 'Boy',
207 $priceFieldID = reset($this->_ids
['price_field']);
208 $priceFieldValueID = reset($this->_ids
['price_field_value']);
209 $submitParams = array(
210 'price_' . $priceFieldID => $priceFieldValueID,
211 'id' => (int) $this->_ids
['contribution_page'],
213 'billing_first_name' => 'Wonder',
214 'billing_last_name' => 'Woman',
215 'contactID' => $contact['id'],
216 'email' => 'wonderwoman@amazon.com',
219 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
220 $contact = $this->callAPISuccess('Contact', 'get', array(
221 'id' => $contact['id'],
229 $this->assertEquals(array(
230 'first_name' => 'Super',
231 'last_name' => 'Boy',
232 'display_name' => 'Super Boy',
233 'sort_name' => 'Boy, Super',
234 'id' => $contact['id'],
235 'contact_id' => $contact['id'],
236 ), $contact['values'][$contact['id']]);
241 * Test process with instant payment when more than one configured for the page.
245 public function testSubmitRecurMultiProcessorInstantPayment() {
246 $this->setUpContributionPage();
247 $this->setupPaymentProcessor();
248 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
249 'payment_processor_type_id' => 'Dummy',
250 'name' => 'processor 2',
251 'class_name' => 'Payment_Dummy',
254 $dummyPP = Civi\Payment\System
::singleton()->getById($paymentProcessor2ID);
255 $dummyPP->setDoDirectPaymentResult(array(
256 'payment_status_id' => 1,
257 'trxn_id' => 'create_first_success',
260 $processor = $dummyPP->getPaymentProcessor();
261 $this->callAPISuccess('ContributionPage', 'create', array(
262 'id' => $this->_ids
['contribution_page'],
263 'payment_processor' => array($paymentProcessor2ID, $this->_ids
['payment_processor']),
266 $priceFieldID = reset($this->_ids
['price_field']);
267 $priceFieldValueID = reset($this->_ids
['price_field_value']);
268 $submitParams = array(
269 'price_' . $priceFieldID => $priceFieldValueID,
270 'id' => (int) $this->_ids
['contribution_page'],
273 'frequency_interval' => 1,
274 'frequency_unit' => 'month',
275 'payment_processor_id' => $paymentProcessor2ID,
278 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
279 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
280 'contribution_page_id' => $this->_ids
['contribution_page'],
281 'contribution_status_id' => 1,
283 $this->assertEquals('create_first_success', $contribution['trxn_id']);
284 $this->assertEquals(10, $contribution['total_amount']);
285 $this->assertEquals(.85, $contribution['fee_amount']);
286 $this->assertEquals(9.15, $contribution['net_amount']);
287 $this->_checkFinancialRecords(array(
288 'id' => $contribution['id'],
289 'total_amount' => $contribution['total_amount'],
290 'payment_instrument_id' => $processor['payment_instrument_id'],
295 * Test submit with a membership block in place.
297 public function testSubmitMembershipBlockNotSeparatePayment() {
298 $this->setUpMembershipContributionPage();
299 $submitParams = array(
300 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
301 'id' => (int) $this->_ids
['contribution_page'],
303 'billing_first_name' => 'Billy',
304 'billing_middle_name' => 'Goat',
305 'billing_last_name' => 'Gruff',
306 'selectMembership' => $this->_ids
['membership_type'],
310 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
311 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
312 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
313 $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']));
317 * Test submit with a membership block in place.
319 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
320 $mut = new CiviMailUtils($this, TRUE);
321 $this->setUpMembershipContributionPage();
322 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
324 $submitParams = array(
325 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
326 'id' => (int) $this->_ids
['contribution_page'],
328 'billing_first_name' => 'Billy',
329 'billing_middle_name' => 'Goat',
330 'billing_last_name' => 'Gruff',
331 'selectMembership' => $this->_ids
['membership_type'],
332 'email-Primary' => 'billy-goat@the-bridge.net',
333 'payment_processor_id' => $this->_paymentProcessor
['id'],
334 'credit_card_number' => '4111111111111111',
335 'credit_card_type' => 'Visa',
336 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
340 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
341 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
342 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
343 $mut->checkMailLog(array(
344 'Membership Type: General',
347 $mut->clearMessages();
351 * Test submit with a membership block in place.
353 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
354 $mut = new CiviMailUtils($this, TRUE);
355 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
356 $this->setUpMembershipContributionPage();
357 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
359 $submitParams = array(
360 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
361 'id' => (int) $this->_ids
['contribution_page'],
363 'billing_first_name' => 'Billy',
364 'billing_middle_name' => 'Goat',
365 'billing_last_name' => 'Gruffier',
366 'selectMembership' => $this->_ids
['membership_type'],
367 'email-Primary' => 'billy-goat@the-new-bridge.net',
370 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
371 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
372 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
373 $mut->checkMailLog(array(
374 'Membership Type: General',
382 $mut->clearMessages(999);
386 * Test submit with a membership block in place.
388 public function testSubmitMembershipBlockIsSeparatePayment() {
389 $this->setUpMembershipContributionPage(TRUE);
390 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
391 $submitParams = array(
392 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
393 'id' => (int) $this->_ids
['contribution_page'],
395 'billing_first_name' => 'Billy',
396 'billing_middle_name' => 'Goat',
397 'billing_last_name' => 'Gruff',
398 'selectMembership' => $this->_ids
['membership_type'],
401 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
402 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
403 $this->assertCount(2, $contributions['values']);
404 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
405 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
406 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
407 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
411 * Test submit with a membership block in place.
413 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
414 $mut = new CiviMailUtils($this, TRUE);
415 $this->setUpMembershipContributionPage(TRUE);
416 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
418 $submitParams = array(
419 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
420 'id' => (int) $this->_ids
['contribution_page'],
422 'billing_first_name' => 'Billy',
423 'billing_middle_name' => 'Goat',
424 'billing_last_name' => 'Gruff',
425 'selectMembership' => $this->_ids
['membership_type'],
426 'email-Primary' => 'billy-goat@the-bridge.net',
427 'payment_processor_id' => $this->_paymentProcessor
['id'],
428 'credit_card_number' => '4111111111111111',
429 'credit_card_type' => 'Visa',
430 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
434 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
435 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
436 $this->assertCount(2, $contributions['values']);
437 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
438 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
439 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
440 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
441 $mut->checkAllMailLog(array(
446 $mut->clearMessages(999);
450 * Test submit with a membership block in place.
452 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
453 $mut = new CiviMailUtils($this, TRUE);
454 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
455 $this->setUpMembershipContributionPage(TRUE);
456 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
458 $submitParams = array(
459 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
460 'id' => (int) $this->_ids
['contribution_page'],
462 'billing_first_name' => 'Billy',
463 'billing_middle_name' => 'Goat',
464 'billing_last_name' => 'Gruffalo',
465 'selectMembership' => $this->_ids
['membership_type'],
466 'payment_processor_id' => 0,
467 'email-Primary' => 'gruffalo@the-bridge.net',
470 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
471 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
472 $this->assertCount(2, $contributions['values']);
473 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
474 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
475 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
476 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
477 $mut->checkMailLog(array(
479 'General Membership: $ 0.00',
483 $mut->clearMessages();
487 * Test submit with a membership block in place.
489 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
490 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
491 $this->_ids
['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
492 $this->setUpMembershipContributionPage(TRUE);
493 $submitParams = array(
494 'price_' . $this->_ids
['price_field'][0] => $this->_ids
['price_field_value'][1],
495 'id' => (int) $this->_ids
['contribution_page'],
497 'billing_first_name' => 'Billy',
498 'billing_middle_name' => 'Goat',
499 'billing_last_name' => 'Gruff',
500 'selectMembership' => $this->_ids
['membership_type'][1],
503 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
504 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
505 $this->assertCount(2, $contributions['values']);
506 $ids = array_keys($contributions['values']);
507 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
508 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
509 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
510 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
511 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
512 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
516 * Test submit with a membership block in place.
518 * We are expecting a separate payment for the membership vs the contribution.
520 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
521 $this->setUpMembershipContributionPage(TRUE);
522 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessor
['id']);
523 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
524 $submitParams = array(
525 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
526 'id' => (int) $this->_ids
['contribution_page'],
528 'billing_first_name' => 'Billy',
529 'billing_middle_name' => 'Goat',
530 'billing_last_name' => 'Gruff',
531 'selectMembership' => $this->_ids
['membership_type'],
532 'payment_processor_id' => $this->_paymentProcessor
['id'],
533 'credit_card_number' => '4111111111111111',
534 'credit_card_type' => 'Visa',
535 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
539 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
540 $contributions = $this->callAPISuccess('contribution', 'get', array(
541 'contribution_page_id' => $this->_ids
['contribution_page'],
542 'contribution_status_id' => 1,
544 $this->assertCount(2, $contributions['values']);
545 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
546 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
547 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
548 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
549 foreach ($contributions['values'] as $contribution) {
550 $this->assertEquals(.72, $contribution['fee_amount']);
551 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
556 * Test that when a transaction fails the pending contribution remains.
558 * An activity should also be created. CRM-16417.
560 public function testSubmitPaymentProcessorFailure() {
561 $this->setUpContributionPage();
562 $this->setupPaymentProcessor();
563 $this->createLoggedInUser();
564 $priceFieldID = reset($this->_ids
['price_field']);
565 $priceFieldValueID = reset($this->_ids
['price_field_value']);
566 $submitParams = array(
567 'price_' . $priceFieldID => $priceFieldValueID,
568 'id' => (int) $this->_ids
['contribution_page'],
570 'payment_processor_id' => 1,
571 'credit_card_number' => '4111111111111111',
572 'credit_card_type' => 'Visa',
573 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
577 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
578 $contribution = $this->callAPISuccessGetSingle('contribution', array(
579 'contribution_page_id' => $this->_ids
['contribution_page'],
580 'contribution_status_id' => 2,
583 $this->callAPISuccessGetSingle('activity', array(
584 'source_record_id' => $contribution['id'],
585 'activity_type_id' => 'Failed Payment',
591 * Test submit recurring membership with immediate confirmation (IATS style).
593 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
594 * processor (IATS style - denoted by returning trxn_id)
595 * - the first creates a new membership, completed contribution, in progress recurring. Check these
596 * - create another - end date should be extended
598 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
599 $this->params
['is_recur'] = 1;
600 $this->params
['recur_frequency_unit'] = 'month';
601 $this->setUpMembershipContributionPage();
602 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
603 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
604 $processor = $dummyPP->getPaymentProcessor();
606 $submitParams = array(
607 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
608 'id' => (int) $this->_ids
['contribution_page'],
610 'billing_first_name' => 'Billy',
611 'billing_middle_name' => 'Goat',
612 'billing_last_name' => 'Gruff',
613 'email' => 'billy@goat.gruff',
614 'selectMembership' => $this->_ids
['membership_type'],
615 'payment_processor_id' => 1,
616 'credit_card_number' => '4111111111111111',
617 'credit_card_type' => 'Visa',
618 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
621 'frequency_interval' => 1,
622 'frequency_unit' => 'month',
625 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
626 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
627 'contribution_page_id' => $this->_ids
['contribution_page'],
628 'contribution_status_id' => 1,
630 $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']);
632 $this->assertEquals('create_first_success', $contribution['trxn_id']);
633 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
634 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
635 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
636 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
637 $this->assertEquals(1, $membership['status_id']);
638 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
640 $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
641 //renew it with processor setting completed - should extend membership
642 $submitParams['contact_id'] = $contribution['contact_id'];
643 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
644 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
645 $this->callAPISuccess('contribution', 'getsingle', array(
646 'id' => array('NOT IN' => array($contribution['id'])),
647 'contribution_page_id' => $this->_ids
['contribution_page'],
648 'contribution_status_id' => 1,
650 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
651 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
652 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
653 $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
654 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
658 * Test submit recurring membership with immediate confirmation (IATS style).
660 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
661 * processor (IATS style - denoted by returning trxn_id)
662 * - the first creates a new membership, completed contribution, in progress recurring. Check these
663 * - create another - end date should be extended
665 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
667 $this->setUpMembershipContributionPage(TRUE);
668 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
669 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
671 $submitParams = array(
672 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
673 'id' => (int) $this->_ids
['contribution_page'],
675 'billing_first_name' => 'Billy',
676 'billing_middle_name' => 'Goat',
677 'billing_last_name' => 'Gruff',
678 'email' => 'billy@goat.gruff',
679 'selectMembership' => $this->_ids
['membership_type'],
680 'payment_processor_id' => 1,
681 'credit_card_number' => '4111111111111111',
682 'credit_card_type' => 'Visa',
683 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
686 'auto_renew' => TRUE,
687 'frequency_interval' => 1,
688 'frequency_unit' => 'month',
691 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
692 $contribution = $this->callAPISuccess('contribution', 'get', array(
693 'contribution_page_id' => $this->_ids
['contribution_page'],
694 'contribution_status_id' => 1,
697 $this->assertEquals(2, $contribution['count']);
698 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
699 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
700 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
701 $this->callAPISuccess('contribution_recur', 'getsingle', array());
705 * Test submit recurring membership with delayed confirmation (Authorize.net style)
706 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
707 * processor (Authorize.net style - denoted by NOT returning trxn_id)
708 * - the first creates a pending membership, pending contribution, penging recurring. Check these
709 * - complete the transaction
710 * - create another - end date should NOT be extended
712 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
713 $this->params
['is_recur'] = 1;
714 $this->params
['recur_frequency_unit'] = 'month';
715 $this->setUpMembershipContributionPage();
716 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
717 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
718 $this->membershipTypeCreate(array('name' => 'Student'));
720 // Add a contribution & a couple of memberships so the id will not be 1 & will differ from membership id.
721 // This saves us from 'accidental success'.
722 $this->contributionCreate(array('contact_id' => $this->contactIds
[0]));
723 $this->contactMembershipCreate(array('contact_id' => $this->contactIds
[0]));
724 $this->contactMembershipCreate(array('contact_id' => $this->contactIds
[0], 'membership_type_id' => 'Student'));
726 $submitParams = array(
727 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
728 'id' => (int) $this->_ids
['contribution_page'],
730 'billing_first_name' => 'Billy',
731 'billing_middle_name' => 'Goat',
732 'billing_last_name' => 'Gruff',
733 'email' => 'billy@goat.gruff',
734 'selectMembership' => $this->_ids
['membership_type'],
735 'payment_processor_id' => 1,
736 'credit_card_number' => '4111111111111111',
737 'credit_card_type' => 'Visa',
738 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
741 'frequency_interval' => 1,
742 'frequency_unit' => 'month',
745 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
746 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
747 'contribution_page_id' => $this->_ids
['contribution_page'],
748 'contribution_status_id' => 2,
751 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
752 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
753 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
754 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
755 $this->assertEquals(5, $membership['status_id']);
757 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
758 $this->assertEquals('civicrm_membership', $line['entity_table']);
759 $this->assertEquals($membership['id'], $line['entity_id']);
761 $this->callAPISuccess('contribution', 'completetransaction', array(
762 'id' => $contribution['id'],
763 'trxn_id' => 'ipn_called',
764 'payment_processor_id' => $this->_paymentProcessor
['id'],
766 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id']));
767 $this->assertEquals('civicrm_membership', $line['entity_table']);
768 $this->assertEquals($membership['id'], $line['entity_id']);
770 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
771 //renew it with processor setting completed - should extend membership
772 $submitParams = array_merge($submitParams, array(
773 'contact_id' => $contribution['contact_id'],
775 'frequency_interval' => 1,
776 'frequency_unit' => 'month',
780 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
781 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
782 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
784 'NOT IN' => array($contribution['id']),
786 'contribution_page_id' => $this->_ids
['contribution_page'],
787 'contribution_status_id' => 2,
790 $line = $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $newContribution['id']));
791 $this->assertEquals('civicrm_membership', $line['entity_table']);
792 $this->assertEquals($membership['id'], $line['entity_id']);
794 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
795 //no renewal as the date hasn't changed
796 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
797 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
798 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
802 * Set up membership contribution page.
803 * @param bool $isSeparatePayment
805 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
806 $this->setUpMembershipBlockPriceSet();
807 $this->setupPaymentProcessor();
808 $this->setUpContributionPage();
810 $this->callAPISuccess('membership_block', 'create', array(
811 'entity_id' => $this->_ids
['contribution_page'],
812 'entity_table' => 'civicrm_contribution_page',
813 'is_required' => TRUE,
815 'is_separate_payment' => $isSeparatePayment,
816 'membership_type_default' => $this->_ids
['membership_type'],
821 * Set up pledge block.
823 public function setUpPledgeBlock() {
825 'entity_table' => 'civicrm_contribution_page',
826 'entity_id' => $this->_ids
['contribution_page'],
827 'pledge_frequency_unit' => 'week',
828 'is_pledge_interval' => 0,
829 'pledge_start_date' => json_encode(array('calendar_date' => date('Ymd', strtotime("+1 month")))),
831 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::create($params);
832 $this->_ids
['pledge_block_id'] = $pledgeBlock->id
;
836 * The default data set does not include a complete default membership price set - not quite sure why.
838 * This function ensures it exists & populates $this->_ids with it's data
840 public function setUpMembershipBlockPriceSet() {
841 $this->_ids
['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
842 'name' => 'default_membership_type_amount',
845 if (empty($this->_ids
['membership_type'])) {
846 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
848 $priceField = $this->callAPISuccess('price_field', 'create', array(
849 'price_set_id' => reset($this->_ids
['price_set']),
850 'name' => 'membership_amount',
851 'label' => 'Membership Amount',
852 'html_type' => 'Radio',
855 $this->_ids
['price_field'][] = $priceField['id'];
857 foreach ($this->_ids
['membership_type'] as $membershipTypeID) {
858 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
859 'name' => 'membership_amount',
860 'label' => 'Membership Amount',
862 'financial_type_id' => 'Donation',
863 'format.only_id' => TRUE,
864 'membership_type_id' => $membershipTypeID,
865 'price_field_id' => $priceField['id'],
867 $this->_ids
['price_field_value'][] = $priceFieldValue;
872 * Add text field other amount to the price set.
874 public function addOtherAmountFieldToMembershipPriceSet() {
875 $this->_ids
['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
876 'price_set_id' => reset($this->_ids
['price_set']),
877 'name' => 'other_amount',
878 'label' => 'Other Amount',
879 'html_type' => 'Text',
880 'format.only_id' => TRUE,
883 $this->_ids
['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
884 'financial_type_id' => 'Donation',
885 'format.only_id' => TRUE,
886 'label' => 'Other Amount',
888 'price_field_id' => $this->_ids
['price_field']['other_amount'],
893 * Help function to set up contribution page with some defaults.
895 public function setUpContributionPage() {
896 $contributionPageResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
897 if (empty($this->_ids
['price_set'])) {
898 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams
);
899 $this->_ids
['price_set'][] = $priceSet['id'];
901 $priceSetID = reset($this->_ids
['price_set']);
902 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
904 if (empty($this->_ids
['price_field'])) {
905 $priceField = $this->callAPISuccess('price_field', 'create', array(
906 'price_set_id' => $priceSetID,
907 'label' => 'Goat Breed',
908 'html_type' => 'Radio',
910 $this->_ids
['price_field'] = array($priceField['id']);
912 if (empty($this->_ids
['price_field_value'])) {
913 $this->callAPISuccess('price_field_value', 'create', array(
914 'price_set_id' => $priceSetID,
915 'price_field_id' => $priceField['id'],
916 'label' => 'Long Haired Goat',
917 'financial_type_id' => 'Donation',
919 'financial_type_id' => 'Donation',
920 'non_deductible_amount' => 15,
923 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
924 'price_set_id' => $priceSetID,
925 'price_field_id' => $priceField['id'],
926 'label' => 'Shoe-eating Goat',
927 'financial_type_id' => 'Donation',
929 'financial_type_id' => 'Donation',
930 'non_deductible_amount' => 5,
933 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
935 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
938 public static function setUpBeforeClass() {
939 // put stuff here that should happen before all tests in this unit
942 public static function tearDownAfterClass() {
943 $tablesToTruncate = array(
945 'civicrm_financial_type',
946 'civicrm_contribution',
947 'civicrm_contribution_page',
949 $unitTest = new CiviUnitTestCase();
950 $unitTest->quickCleanup($tablesToTruncate);
954 * Create a payment processor instance.
956 protected function setupPaymentProcessor() {
957 $this->params
['payment_processor_id'] = $this->_ids
['payment_processor'] = $this->paymentProcessorCreate(array(
958 'payment_processor_type_id' => 'Dummy',
959 'class_name' => 'Payment_Dummy',
962 $this->_paymentProcessor
= $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params
['payment_processor_id']));
966 * Test submit recurring pledge.
968 * - 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.
970 public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() {
971 $this->params
['adjust_recur_start_date'] = TRUE;
972 $this->params
['is_pay_later'] = FALSE;
973 $this->setUpContributionPage();
974 $this->setUpPledgeBlock();
975 $this->setupPaymentProcessor();
976 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
977 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
979 $submitParams = array(
980 'id' => (int) $this->_ids
['contribution_page'],
982 'billing_first_name' => 'Billy',
983 'billing_middle_name' => 'Goat',
984 'billing_last_name' => 'Gruff',
985 'email' => 'billy@goat.gruff',
986 'payment_processor_id' => 1,
987 'credit_card_number' => '4111111111111111',
988 'credit_card_type' => 'Visa',
989 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
991 'pledge_frequency_interval' => 1,
992 'pledge_frequency_unit' => 'week',
993 'pledge_installments' => 3,
995 'pledge_block_id' => (int) $this->_ids
['pledge_block_id'],
998 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1000 // Check if contribution created.
1001 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1002 'contribution_page_id' => $this->_ids
['contribution_page'],
1003 'contribution_status_id' => 'Completed', // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1006 $this->assertEquals('create_first_success', $contribution['trxn_id']);
1008 // Check if pledge created.
1009 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1010 $this->assertEquals(date('Ymd', strtotime($pledge['pledge_start_date'])), date('Ymd', strtotime("+1 month")));
1011 $this->assertEquals($pledge['pledge_amount'], 300.00);
1013 // Check if pledge payments created.
1015 'pledge_id' => $pledge['id'],
1017 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1018 $this->assertEquals($pledgePayment['count'], 3);
1019 $this->assertEquals(date('Ymd', strtotime($pledgePayment['values'][1]['scheduled_date'])), date('Ymd', strtotime("+1 month")));
1020 $this->assertEquals($pledgePayment['values'][1]['scheduled_amount'], 100.00);
1021 $this->assertEquals($pledgePayment['values'][1]['status_id'], 1); // Will be pending when actual payment processor is used (dummy processor does not support future payments).
1023 // Check contribution recur record.
1024 $recur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
1025 $this->assertEquals(date('Ymd', strtotime($recur['start_date'])), date('Ymd', strtotime("+1 month")));
1026 $this->assertEquals($recur['amount'], 100.00);
1027 $this->assertEquals($recur['contribution_status_id'], 5); // In progress status.
1031 * Test submit pledge payment.
1033 * - test submitting a pledge payment using contribution form.
1035 public function testSubmitPledgePayment() {
1036 $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment();
1037 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1039 'pledge_id' => $pledge['id'],
1041 $submitParams = array(
1042 'id' => (int) $pledge['pledge_contribution_page_id'],
1043 'pledge_amount' => array(2 => 1),
1044 'billing_first_name' => 'Billy',
1045 'billing_middle_name' => 'Goat',
1046 'billing_last_name' => 'Gruff',
1047 'email' => 'billy@goat.gruff',
1048 'payment_processor_id' => 1,
1049 'credit_card_number' => '4111111111111111',
1050 'credit_card_type' => 'Visa',
1051 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1053 'pledge_id' => $pledge['id'],
1054 'cid' => $pledge['contact_id'],
1055 'contact_id' => $pledge['contact_id'],
1057 'is_pledge' => TRUE,
1058 'pledge_block_id' => $this->_ids
['pledge_block_id'],
1060 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1061 $this->assertEquals($pledgePayment['values'][2]['status_id'], 2);
1063 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
1065 // Check if contribution created.
1066 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1067 'contribution_page_id' => $pledge['pledge_contribution_page_id'],
1068 'contribution_status_id' => 'Completed',
1069 'contact_id' => $pledge['contact_id'],
1070 'contribution_recur_id' => array('IS NULL' => 1),
1073 $this->assertEquals(100.00, $contribution['total_amount']);
1074 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1075 $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed");
1076 $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);