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