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