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