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