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