additional fixes
[civicrm-core.git] / tests / phpunit / CRM / Core / Payment / AuthorizeNetTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
4cbe18b8
EM
28/**
29 * Class CRM_Core_Payment_AuthorizeNetTest
acb109b7 30 * @group headless
4cbe18b8 31 */
6a488035 32class CRM_Core_Payment_AuthorizeNetTest extends CiviUnitTestCase {
6a488035 33
00be9182 34 public function setUp() {
6a488035 35 parent::setUp();
d6944518 36 $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate();
6a488035 37
d6944518 38 $this->processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
e6ff1593 39 $this->_financialTypeId = 1;
6a488035
TO
40
41 // for some strange unknown reason, in batch mode this value gets set to null
42 // so crude hack here to avoid an exception and hence an error
6c6e6187 43 $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
6a488035
TO
44 }
45
00be9182 46 public function tearDown() {
b38530f2 47 $this->quickCleanUpFinancialEntities();
6a488035
TO
48 }
49
50 /**
100fef9d 51 * Create a single post dated payment as a recurring transaction.
6a488035
TO
52 *
53 * Test works but not both due to some form of caching going on in the SmartySingleton
54 */
00be9182 55 public function testCreateSingleNowDated() {
92915c55
TO
56 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
57 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
6a488035 58 $nameParams = array('first_name' => $firstName, 'last_name' => $lastName);
92915c55 59 $contactId = $this->individualCreate($nameParams);
6a488035 60
6a488035 61 $invoiceID = sha1(rand());
92915c55 62 $amount = rand(100, 1000) . '.00';
6a488035 63
d2da7b20 64 $recur = $this->callAPISuccess('ContributionRecur', 'create', array(
6a488035 65 'contact_id' => $contactId,
92915c55
TO
66 'amount' => $amount,
67 'currency' => 'USD',
6a488035
TO
68 'frequency_unit' => 'week',
69 'frequency_interval' => 1,
70 'installments' => 2,
92915c55
TO
71 'start_date' => date('Ymd'),
72 'create_date' => date('Ymd'),
73 'invoice_id' => $invoiceID,
6a488035
TO
74 'contribution_status_id' => 2,
75 'is_test' => 1,
d6944518 76 'payment_processor_id' => $this->_paymentProcessorID,
d2da7b20 77 ));
6a488035 78
d2da7b20 79 $contribution = $this->callAPISuccess('Contribution', 'create', array(
92915c55
TO
80 'contact_id' => $contactId,
81 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
82 'receive_date' => date('Ymd'),
83 'total_amount' => $amount,
92915c55
TO
84 'invoice_id' => $invoiceID,
85 'currency' => 'USD',
d2da7b20 86 'contribution_recur_id' => $recur['id'],
92915c55 87 'is_test' => 1,
6a488035 88 'contribution_status_id' => 2,
d2da7b20 89 ));
6a488035
TO
90
91 $params = array(
92 'qfKey' => '08ed21c7ca00a1f7d32fff2488596ef7_4454',
93 'hidden_CreditCard' => 1,
94 'billing_first_name' => $firstName,
95 'billing_middle_name' => "",
96 'billing_last_name' => $lastName,
97 'billing_street_address-5' => '8 Hobbitton Road',
98 'billing_city-5' => 'The Shire',
99 'billing_state_province_id-5' => 1012,
100 'billing_postal_code-5' => 5010,
101 'billing_country_id-5' => 1228,
102 'credit_card_number' => '4007000000027',
103 'cvv2' => 123,
104 'credit_card_exp_date' => array(
105 'M' => 10,
106 'Y' => 2019,
107 ),
108 'credit_card_type' => 'Visa',
109 'is_recur' => 1,
110 'frequency_interval' => 1,
111 'frequency_unit' => 'month',
112 'installments' => 12,
e6ff1593 113 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
114 'is_email_receipt' => 1,
115 'from_email_address' => "{$firstName}.{$lastName}@example.com",
116 'receive_date' => date('Ymd'),
117 'receipt_date_time' => '',
d6944518 118 'payment_processor_id' => $this->_paymentProcessorID,
6a488035
TO
119 'price_set_id' => '',
120 'total_amount' => $amount,
121 'currency' => 'USD',
122 'source' => "Mordor",
123 'soft_credit_to' => '',
124 'soft_contact_id' => '',
125 'billing_state_province-5' => 'IL',
126 'state_province-5' => 'IL',
127 'billing_country-5' => 'US',
128 'country-5' => 'US',
129 'year' => 2019,
130 'month' => 10,
131 'ip_address' => '127.0.0.1',
132 'amount' => 7,
133 'amount_level' => 0,
134 'currencyID' => 'USD',
135 'pcp_display_in_roll' => "",
136 'pcp_roll_nickname' => "",
137 'pcp_personal_note' => "",
138 'non_deductible_amount' => "",
139 'fee_amount' => "",
140 'net_amount' => "",
141 'invoiceID' => $invoiceID,
142 'contribution_page_id' => "",
143 'thankyou_date' => NULL,
144 'honor_contact_id' => NULL,
145 'first_name' => $firstName,
146 'middle_name' => '',
147 'last_name' => $lastName,
148 'street_address' => '8 Hobbiton Road',
149 'city' => 'The Shire',
150 'state_province' => 'IL',
151 'postal_code' => 5010,
152 'country' => 'US',
153 'contributionType_name' => 'My precious',
154 'contributionType_accounting_code' => '',
155 'contributionPageID' => '',
156 'email' => "{$firstName}.{$lastName}@example.com",
157 'contactID' => $contactId,
d2da7b20 158 'contributionID' => $contribution['id'],
e6ff1593 159 'contributionTypeID' => $this->_financialTypeId,
d2da7b20 160 'contributionRecurID' => $recur['id'],
6a488035
TO
161 );
162
163 // turn verifySSL off
08ef4ddd 164 Civi::settings()->set('verifySSL', '0');
7f5c21bb 165 $this->doPayment($params);
6a488035 166 // turn verifySSL on
08ef4ddd 167 Civi::settings()->set('verifySSL', '0');
6a488035
TO
168
169 // if subscription was successful, processor_id / subscription-id must not be null
d2da7b20 170 $this->assertDBNotNull('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id',
6a488035
TO
171 'id', 'Failed to create subscription with Authorize.'
172 );
173
174 // cancel it or the transaction will be rejected by A.net if the test is re-run
d2da7b20 175 $subscriptionID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id');
a7d56832 176 $message = '';
6a488035
TO
177 $result = $this->processor->cancelSubscription($message, array('subscriptionId' => $subscriptionID));
178 $this->assertTrue($result, 'Failed to cancel subscription with Authorize.');
6a488035
TO
179 }
180
181 /**
eceb18cc 182 * Create a single post dated payment as a recurring transaction.
6a488035 183 */
00be9182 184 public function testCreateSinglePostDated() {
6a488035
TO
185 $start_date = date('Ymd', strtotime("+ 1 week"));
186
92915c55
TO
187 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
188 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
6a488035 189 $nameParams = array('first_name' => $firstName, 'last_name' => $lastName);
92915c55 190 $contactId = $this->individualCreate($nameParams);
6a488035
TO
191
192 $ids = array('contribution' => NULL);
193 $invoiceID = sha1(rand());
92915c55 194 $amount = rand(100, 1000) . '.00';
6a488035
TO
195
196 $contributionRecurParams = array(
197 'contact_id' => $contactId,
92915c55
TO
198 'amount' => $amount,
199 'currency' => 'USD',
6a488035
TO
200 'frequency_unit' => 'month',
201 'frequency_interval' => 1,
202 'installments' => 3,
92915c55
TO
203 'start_date' => $start_date,
204 'create_date' => date('Ymd'),
205 'invoice_id' => $invoiceID,
6a488035
TO
206 'contribution_status_id' => 2,
207 'is_test' => 1,
23a89bba 208 'payment_processor_id' => $this->_paymentProcessorID,
6a488035
TO
209 );
210 $recur = CRM_Contribute_BAO_ContributionRecur::add($contributionRecurParams, $ids);
211
212 $contributionParams = array(
92915c55
TO
213 'contact_id' => $contactId,
214 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
215 'receive_date' => $start_date,
216 'total_amount' => $amount,
92915c55
TO
217 'invoice_id' => $invoiceID,
218 'currency' => 'USD',
6a488035
TO
219 'contribution_recur_id' => $recur->id,
220 'is_test' => 1,
221 'contribution_status_id' => 2,
222 );
e6ff1593 223
a7d56832 224 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
225
226 $params = array(
227 'qfKey' => '00ed21c7ca00a1f7d555555596ef7_4454',
228 'hidden_CreditCard' => 1,
229 'billing_first_name' => $firstName,
230 'billing_middle_name' => "",
231 'billing_last_name' => $lastName,
232 'billing_street_address-5' => '8 Hobbitton Road',
233 'billing_city-5' => 'The Shire',
234 'billing_state_province_id-5' => 1012,
235 'billing_postal_code-5' => 5010,
236 'billing_country_id-5' => 1228,
237 'credit_card_number' => '4007000000027',
238 'cvv2' => 123,
239 'credit_card_exp_date' => array(
240 'M' => 11,
241 'Y' => 2019,
242 ),
243 'credit_card_type' => 'Visa',
244 'is_recur' => 1,
245 'frequency_interval' => 1,
246 'frequency_unit' => 'month',
247 'installments' => 3,
e6ff1593 248 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
249 'is_email_receipt' => 1,
250 'from_email_address' => "{$firstName}.{$lastName}@example.com",
251 'receive_date' => $start_date,
252 'receipt_date_time' => '',
23a89bba 253 'payment_processor_id' => $this->_paymentProcessorID,
6a488035
TO
254 'price_set_id' => '',
255 'total_amount' => $amount,
256 'currency' => 'USD',
257 'source' => "Mordor",
258 'soft_credit_to' => '',
259 'soft_contact_id' => '',
260 'billing_state_province-5' => 'IL',
261 'state_province-5' => 'IL',
262 'billing_country-5' => 'US',
263 'country-5' => 'US',
264 'year' => 2019,
265 'month' => 10,
266 'ip_address' => '127.0.0.1',
267 'amount' => 70,
268 'amount_level' => 0,
269 'currencyID' => 'USD',
270 'pcp_display_in_roll' => "",
271 'pcp_roll_nickname' => "",
272 'pcp_personal_note' => "",
273 'non_deductible_amount' => "",
274 'fee_amount' => "",
275 'net_amount' => "",
276 'invoice_id' => "",
277 'contribution_page_id' => "",
278 'thankyou_date' => NULL,
279 'honor_contact_id' => NULL,
280 'invoiceID' => $invoiceID,
281 'first_name' => $firstName,
282 'middle_name' => 'bob',
283 'last_name' => $lastName,
284 'street_address' => '8 Hobbiton Road',
285 'city' => 'The Shire',
286 'state_province' => 'IL',
287 'postal_code' => 5010,
288 'country' => 'US',
289 'contributionType_name' => 'My precious',
290 'contributionType_accounting_code' => '',
291 'contributionPageID' => '',
292 'email' => "{$firstName}.{$lastName}@example.com",
293 'contactID' => $contactId,
a7d56832 294 'contributionID' => $contribution['id'],
e6ff1593 295 'contributionTypeID' => $this->_financialTypeId,
6a488035
TO
296 'contributionRecurID' => $recur->id,
297 );
298
299 // if cancel-subscription has been called earlier 'subscriptionType' would be set to cancel.
300 // to make a successful call for another trxn, we need to set it to something else.
301 $smarty = CRM_Core_Smarty::singleton();
302 $smarty->assign('subscriptionType', 'create');
303
304 // turn verifySSL off
08ef4ddd 305 Civi::settings()->set('verifySSL', '0');
7f5c21bb 306 $this->doPayment($params);
6a488035 307 // turn verifySSL on
08ef4ddd 308 Civi::settings()->set('verifySSL', '0');
6a488035
TO
309
310 // if subscription was successful, processor_id / subscription-id must not be null
311 $this->assertDBNotNull('CRM_Contribute_DAO_ContributionRecur', $recur->id, 'processor_id',
312 'id', 'Failed to create subscription with Authorize.'
313 );
314
315 // cancel it or the transaction will be rejected by A.net if the test is re-run
316 $subscriptionID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $recur->id, 'processor_id');
a7d56832 317 $message = '';
6a488035
TO
318 $result = $this->processor->cancelSubscription($message, array('subscriptionId' => $subscriptionID));
319 $this->assertTrue($result, 'Failed to cancel subscription with Authorize.');
6a488035 320 }
96025800 321
7f5c21bb 322 /**
323 * Process payment against the Authorize.net test server.
324 *
325 * Skip the test if the server is unresponsive.
326 *
327 * @param array $params
328 *
329 * @throws PHPUnit_Framework_SkippedTestError
330 */
331 public function doPayment($params) {
332 try {
333 $this->processor->doPayment($params);
334 }
335 catch (Exception $e) {
3dc2013c 336 $this->assertTrue((strpos($e->getMessage(), 'E00001: Internal Error Occurred.') !== FALSE),
64087f90 337 'AuthorizeNet failed for unknown reason.' . $e->getMessage());
7f5c21bb 338 $this->markTestSkipped('AuthorizeNet test server is not in a good mood so we can\'t test this right now');
339 }
340 }
341
6a488035 342}