3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_Payment_AuthorizeNetTest
32 class CRM_Core_Payment_AuthorizeNetTest
extends CiviUnitTestCase
{
34 public function setUp() {
36 $this->_paymentProcessorID
= $this->paymentProcessorAuthorizeNetCreate();
38 $this->processor
= Civi\Payment\System
::singleton()->getById($this->_paymentProcessorID
);
39 $this->_financialTypeId
= 1;
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
43 $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
46 public function tearDown() {
47 $this->quickCleanUpFinancialEntities();
51 * Create a single post dated payment as a recurring transaction.
53 * Test works but not both due to some form of caching going on in the SmartySingleton
55 public function testCreateSingleNowDated() {
56 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
57 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
58 $nameParams = array('first_name' => $firstName, 'last_name' => $lastName);
59 $contactId = $this->individualCreate($nameParams);
61 $invoiceID = sha1(rand());
62 $amount = rand(100, 1000) . '.00';
64 $recur = $this->callAPISuccess('ContributionRecur', 'create', array(
65 'contact_id' => $contactId,
68 'frequency_unit' => 'week',
69 'frequency_interval' => 1,
71 'start_date' => date('Ymd'),
72 'create_date' => date('Ymd'),
73 'invoice_id' => $invoiceID,
74 'contribution_status_id' => 2,
76 'payment_processor_id' => $this->_paymentProcessorID
,
79 $contribution = $this->callAPISuccess('Contribution', 'create', array(
80 'contact_id' => $contactId,
81 'financial_type_id' => $this->_financialTypeId
,
82 'receive_date' => date('Ymd'),
83 'total_amount' => $amount,
84 'invoice_id' => $invoiceID,
86 'contribution_recur_id' => $recur['id'],
88 'contribution_status_id' => 2,
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',
104 'credit_card_exp_date' => array(
108 'credit_card_type' => 'Visa',
110 'frequency_interval' => 1,
111 'frequency_unit' => 'month',
112 'installments' => 12,
113 'financial_type_id' => $this->_financialTypeId
,
114 'is_email_receipt' => 1,
115 'from_email_address' => "{$firstName}.{$lastName}@example.com",
116 'receive_date' => date('Ymd'),
117 'receipt_date_time' => '',
118 'payment_processor_id' => $this->_paymentProcessorID
,
119 'price_set_id' => '',
120 'total_amount' => $amount,
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',
131 'ip_address' => '127.0.0.1',
134 'currencyID' => 'USD',
135 'pcp_display_in_roll' => "",
136 'pcp_roll_nickname' => "",
137 'pcp_personal_note' => "",
138 'non_deductible_amount' => "",
141 'invoiceID' => $invoiceID,
142 'contribution_page_id' => "",
143 'thankyou_date' => NULL,
144 'honor_contact_id' => NULL,
145 'first_name' => $firstName,
147 'last_name' => $lastName,
148 'street_address' => '8 Hobbiton Road',
149 'city' => 'The Shire',
150 'state_province' => 'IL',
151 'postal_code' => 5010,
153 'contributionType_name' => 'My precious',
154 'contributionType_accounting_code' => '',
155 'contributionPageID' => '',
156 'email' => "{$firstName}.{$lastName}@example.com",
157 'contactID' => $contactId,
158 'contributionID' => $contribution['id'],
159 'contributionTypeID' => $this->_financialTypeId
,
160 'contributionRecurID' => $recur['id'],
163 // turn verifySSL off
164 Civi
::settings()->set('verifySSL', '0');
165 $this->doPayment($params);
167 Civi
::settings()->set('verifySSL', '0');
169 // if subscription was successful, processor_id / subscription-id must not be null
170 $this->assertDBNotNull('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id',
171 'id', 'Failed to create subscription with Authorize.'
174 // cancel it or the transaction will be rejected by A.net if the test is re-run
175 $subscriptionID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id');
177 $result = $this->processor
->cancelSubscription($message, array('subscriptionId' => $subscriptionID));
178 $this->assertTrue($result, 'Failed to cancel subscription with Authorize.');
182 * Create a single post dated payment as a recurring transaction.
184 public function testCreateSinglePostDated() {
185 $start_date = date('Ymd', strtotime("+ 1 week"));
187 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
188 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
189 $nameParams = array('first_name' => $firstName, 'last_name' => $lastName);
190 $contactId = $this->individualCreate($nameParams);
192 $ids = array('contribution' => NULL);
193 $invoiceID = sha1(rand());
194 $amount = rand(100, 1000) . '.00';
196 $contributionRecurParams = array(
197 'contact_id' => $contactId,
200 'frequency_unit' => 'month',
201 'frequency_interval' => 1,
203 'start_date' => $start_date,
204 'create_date' => date('Ymd'),
205 'invoice_id' => $invoiceID,
206 'contribution_status_id' => 2,
208 'payment_processor_id' => $this->_paymentProcessorID
,
210 $recur = CRM_Contribute_BAO_ContributionRecur
::add($contributionRecurParams, $ids);
212 $contributionParams = array(
213 'contact_id' => $contactId,
214 'financial_type_id' => $this->_financialTypeId
,
215 'receive_date' => $start_date,
216 'total_amount' => $amount,
217 'invoice_id' => $invoiceID,
219 'contribution_recur_id' => $recur->id
,
221 'contribution_status_id' => 2,
224 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
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',
239 'credit_card_exp_date' => array(
243 'credit_card_type' => 'Visa',
245 'frequency_interval' => 1,
246 'frequency_unit' => 'month',
248 'financial_type_id' => $this->_financialTypeId
,
249 'is_email_receipt' => 1,
250 'from_email_address' => "{$firstName}.{$lastName}@example.com",
251 'receive_date' => $start_date,
252 'receipt_date_time' => '',
253 'payment_processor_id' => $this->_paymentProcessorID
,
254 'price_set_id' => '',
255 'total_amount' => $amount,
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',
266 'ip_address' => '127.0.0.1',
269 'currencyID' => 'USD',
270 'pcp_display_in_roll' => "",
271 'pcp_roll_nickname' => "",
272 'pcp_personal_note' => "",
273 'non_deductible_amount' => "",
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,
289 'contributionType_name' => 'My precious',
290 'contributionType_accounting_code' => '',
291 'contributionPageID' => '',
292 'email' => "{$firstName}.{$lastName}@example.com",
293 'contactID' => $contactId,
294 'contributionID' => $contribution['id'],
295 'contributionTypeID' => $this->_financialTypeId
,
296 'contributionRecurID' => $recur->id
,
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');
304 // turn verifySSL off
305 Civi
::settings()->set('verifySSL', '0');
306 $this->doPayment($params);
308 Civi
::settings()->set('verifySSL', '0');
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.'
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');
318 $result = $this->processor
->cancelSubscription($message, array('subscriptionId' => $subscriptionID));
319 $this->assertTrue($result, 'Failed to cancel subscription with Authorize.');
323 * Process payment against the Authorize.net test server.
325 * Skip the test if the server is unresponsive.
327 * @param array $params
329 * @throws PHPUnit_Framework_SkippedTestError
331 public function doPayment($params) {
333 $this->processor
->doPayment($params);
335 catch (Exception
$e) {
336 $this->assertTrue((strpos($e->getMessage(), 'E00001: Internal Error Occurred.') !== FALSE),
337 'AuthorizeNet failed for unknown reason.' . $e->getMessage());
338 $this->markTestSkipped('AuthorizeNet test server is not in a good mood so we can\'t test this right now');