Merge pull request #18729 from eileenmcnaughton/aipn
[civicrm-core.git] / tests / phpunit / CRM / Core / Payment / AuthorizeNetIPNTest.php
1 <?php
2
3 use Civi\Payment\Exception\PaymentProcessorException;
4 use Civi\Api4\Contribution;
5
6 /**
7 * Class CRM_Core_Payment_PayPalProIPNTest
8 * @group headless
9 */
10 class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
11 use CRMTraits_Financial_OrderTrait;
12
13 protected $_contributionID;
14 protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424';
15 protected $_financialTypeID = 1;
16 protected $_contactID;
17 protected $_contributionRecurID;
18 protected $_contributionPageID;
19 protected $_paymentProcessorID;
20
21 /**
22 *
23 * @throws \CRM_Core_Exception
24 */
25 public function setUp() {
26 parent::setUp();
27 $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0]);
28 $this->_contactID = $this->individualCreate();
29 $contributionPage = $this->callAPISuccess('contribution_page', 'create', [
30 'title' => 'Test Contribution Page',
31 'financial_type_id' => $this->_financialTypeID,
32 'currency' => 'USD',
33 'payment_processor' => $this->_paymentProcessorID,
34 'max_amount' => 1000,
35 'receipt_from_email' => 'gaia@the.cosmos',
36 'receipt_from_name' => 'Pachamama',
37 'is_email_receipt' => TRUE,
38 ]);
39 $this->_contributionPageID = $contributionPage['id'];
40 }
41
42 public function tearDown() {
43 $this->quickCleanUpFinancialEntities();
44 }
45
46 /**
47 * Ensure recurring contributions from Contribution Pages
48 * with receipt turned off don't send a receipt.
49 *
50 * @throws \CiviCRM_API3_Exception
51 * @throws \CRM_Core_Exception
52 */
53 public function testIPNPaymentRecurNoReceipt() {
54 $mut = new CiviMailUtils($this, TRUE);
55 // Turn off receipts in contribution page.
56 $api_params = [
57 'id' => $this->_contributionPageID,
58 'is_email_receipt' => FALSE,
59 ];
60 $this->callAPISuccess('contributionPage', 'update', $api_params);
61
62 // Create initial recurring payment and initial contribution.
63 // Note - we can't use setupRecurringPaymentProcessorTransaction(), which
64 // would be convenient because it does not fully mimic the real user
65 // experience. Using setupRecurringPaymentProcessorTransaction() doesn't
66 // specify is_email_receipt so it is always set to 1. We need to more
67 // closely mimic what happens with a live transaction to test that
68 // is_email_receipt is not set to 1 if the originating contribution page
69 // has is_email_receipt set to 0.
70 $form = new CRM_Contribute_Form_Contribution();
71 $form->_mode = 'Live';
72 try {
73 $contribution = $form->testSubmit([
74 'total_amount' => 200,
75 'financial_type_id' => 1,
76 'receive_date' => date('m/d/Y'),
77 'receive_date_time' => date('H:i:s'),
78 'contact_id' => $this->_contactID,
79 'contribution_status_id' => 1,
80 'credit_card_number' => 4444333322221111,
81 'cvv2' => 123,
82 'credit_card_exp_date' => [
83 'M' => 9,
84 'Y' => 2025,
85 ],
86 'credit_card_type' => 'Visa',
87 'billing_first_name' => 'Junko',
88 'billing_middle_name' => '',
89 'billing_last_name' => 'Adams',
90 'billing_street_address-5' => time() . ' Lincoln St S',
91 'billing_city-5' => 'Maryknoll',
92 'billing_state_province_id-5' => 1031,
93 'billing_postal_code-5' => 10545,
94 'billing_country_id-5' => 1228,
95 'frequency_interval' => 1,
96 'frequency_unit' => 'month',
97 'installments' => 2,
98 'hidden_AdditionalDetail' => 1,
99 'hidden_Premium' => 1,
100 'payment_processor_id' => $this->_paymentProcessorID,
101 'currency' => 'USD',
102 'source' => 'bob sled race',
103 'contribution_page_id' => $this->_contributionPageID,
104 'is_recur' => TRUE,
105 ], CRM_Core_Action::ADD);
106 }
107 catch (PaymentProcessorException $e) {
108 $this->markTestSkipped('Error from A.net - cannot proceed');
109 }
110 $this->_contributionID = $contribution->id;
111 $this->ids['Contribution'][0] = $contribution->id;
112 $this->_contributionRecurID = $contribution->contribution_recur_id;
113
114 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['id' => $this->_contributionRecurID]);
115 $processor_id = $contributionRecur['processor_id'];
116 $this->assertEquals('Pending', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionRecur['contribution_status_id']));
117 // Process the initial one after a second's break to ensure modified date really is later.
118 sleep(1);
119 $IPN = new CRM_Core_Payment_AuthorizeNetIPN(
120 $this->getRecurTransaction(['x_subscription_id' => $processor_id])
121 );
122 $IPN->main();
123 $updatedContributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['id' => $this->_contributionRecurID]);
124 $this->assertEquals('In Progress', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $updatedContributionRecur['contribution_status_id']));
125 $this->assertTrue(strtotime($updatedContributionRecur['modified_date']) > strtotime($contributionRecur['modified_date']));
126
127 // Now send a second one (authorize seems to treat first and second contributions
128 // differently.
129 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction([
130 'x_subscription_id' => $processor_id,
131 'x_subscription_paynum' => 2,
132 ]));
133 $IPN->main();
134 $updatedContributionRecurAgain = $this->callAPISuccessGetSingle('ContributionRecur', ['id' => $this->_contributionRecurID]);
135 $this->assertEquals('Completed', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $updatedContributionRecurAgain['contribution_status_id']));
136 $this->assertEquals(date('Y-m-d'), substr($updatedContributionRecurAgain['end_date'], 0, 10));
137 // There should not be any email.
138 $mut->assertMailLogEmpty();
139
140 $contributions = Contribution::get()->addWhere('contribution_recur_id', '=', $this->_contributionRecurID)->addSelect('contribution_page_id')->execute();
141 foreach ($contributions as $contribution) {
142 $this->assertEquals($this->_contributionPageID, $contribution['contribution_page_id']);
143 }
144 }
145
146 /**
147 * Test IPN response updates contribution_recur & contribution for first & second contribution
148 *
149 * @throws \CRM_Core_Exception
150 */
151 public function testIPNPaymentRecurSuccess() {
152 $this->setupRecurringPaymentProcessorTransaction();
153 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
154 $IPN->main();
155 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
156 $this->assertEquals(1, $contribution['contribution_status_id']);
157 $this->assertEquals('6511143069', $contribution['trxn_id']);
158 // source gets set by processor
159 $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
160 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
161 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
162 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction());
163 $IPN->main();
164 $contribution = $this->callAPISuccess('contribution', 'get', [
165 'contribution_recur_id' => $this->_contributionRecurID,
166 'sequential' => 1,
167 ]);
168 $this->assertEquals(2, $contribution['count']);
169 $this->assertEquals('second_one', $contribution['values'][1]['trxn_id']);
170 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][1]['receive_date'])));
171 }
172
173 /**
174 * Test payment processor is correctly assigned for the IPN payment.
175 */
176 public function testIPNPaymentRecurSuccessMultiAuthNetProcessor() {
177 //Create and set up recur payment using second instance of AuthNet Processor.
178 $this->_paymentProcessorID2 = $this->paymentProcessorAuthorizeNetCreate(['name' => 'Authorize2', 'is_test' => 0]);
179 $this->setupRecurringPaymentProcessorTransaction(['payment_processor_id' => $this->_paymentProcessorID2]);
180
181 //Call IPN with processor id.
182 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['processor_id' => $this->_paymentProcessorID2]));
183 $IPN->main();
184 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
185 $this->assertEquals(1, $contribution['contribution_status_id']);
186 $this->assertEquals('6511143069', $contribution['trxn_id']);
187 // source gets set by processor
188 $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
189 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
190 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
191 }
192
193 /**
194 * Test IPN response updates contribution_recur & contribution for first & second contribution
195 *
196 * @throws \CRM_Core_Exception
197 */
198 public function testIPNPaymentRecurSuccessSuppliedReceiveDate() {
199 $this->setupRecurringPaymentProcessorTransaction();
200 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
201 $IPN->main();
202 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
203 $this->assertEquals(1, $contribution['contribution_status_id']);
204 $this->assertEquals('6511143069', $contribution['trxn_id']);
205 // source gets set by processor
206 $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
207 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
208 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
209 $IPN = new CRM_Core_Payment_AuthorizeNetIPN(array_merge(['receive_date' => '2010-07-01'], $this->getRecurSubsequentTransaction()));
210 $IPN->main();
211 $contribution = $this->callAPISuccess('contribution', 'get', [
212 'contribution_recur_id' => $this->_contributionRecurID,
213 'sequential' => 1,
214 ]);
215 $this->assertEquals(2, $contribution['count']);
216 $this->assertEquals('second_one', $contribution['values'][1]['trxn_id']);
217 $this->assertEquals('2010-07-01', date('Y-m-d', strtotime($contribution['values'][1]['receive_date'])));
218 }
219
220 /**
221 * Test IPN response updates contribution_recur & contribution for first & second contribution
222 *
223 * @throws \CRM_Core_Exception
224 */
225 public function testIPNPaymentMembershipRecurSuccess() {
226 $this->createRepeatMembershipOrder();
227 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
228 $IPN->main();
229 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->ids['Contribution'][0]]);
230 $this->assertEquals(1, $contribution['contribution_status_id']);
231 $this->assertEquals('6511143069', $contribution['trxn_id']);
232
233 // source gets set by processor
234 $this->assertEquals('Online Contribution:', substr($contribution['contribution_source'], 0, 20));
235 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
236 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
237 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction());
238 $IPN->main();
239 $contribution = $this->callAPISuccess('contribution', 'get', [
240 'contribution_recur_id' => $this->_contributionRecurID,
241 'sequential' => 1,
242 ]);
243 $this->assertEquals(2, $contribution['count']);
244 // Ensure both contributions are coded as credit card contributions.
245 $this->assertEquals(1, $contribution['values'][0]['payment_instrument_id']);
246 $this->assertEquals(1, $contribution['values'][1]['payment_instrument_id']);
247 $this->assertEquals('second_one', $contribution['values'][1]['trxn_id']);
248 $this->callAPISuccessGetSingle('membership_payment', ['contribution_id' => $contribution['values'][1]['id']]);
249 $this->callAPISuccessGetSingle('line_item', [
250 'contribution_id' => $contribution['values'][1]['id'],
251 'entity_table' => 'civicrm_membership',
252 ]);
253 $this->validateAllContributions();
254 $this->validateAllPayments();
255 }
256
257 /**
258 * Test IPN response mails don't leak.
259 *
260 * @throws \CRM_Core_Exception
261 */
262 public function testIPNPaymentMembershipRecurSuccessNoLeakage() {
263 $mut = new CiviMailUtils($this, TRUE);
264 $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]);
265 $this->addProfile('supporter_profile', $this->_contributionPageID);
266 $this->addProfile('honoree_individual', $this->_contributionPageID, 'soft_credit');
267
268 $this->callAPISuccess('ContributionSoft', 'create', [
269 'contact_id' => $this->individualCreate(),
270 'contribution_id' => $this->_contributionID,
271 'soft_credit_type_id' => 'in_memory_of',
272 'amount' => 200,
273 ]);
274
275 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
276 $IPN->main();
277 $mut->checkAllMailLog([
278 'Membership Type: General',
279 'Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
280 'Amount: $ 200.00',
281 'Membership Start Date:',
282 'Supporter Profile',
283 'First Name: Anthony',
284 'Last Name: Anderson',
285 'Email Address: anthony_anderson@civicrm.org',
286 'Honor',
287 'This membership will be automatically renewed every',
288 'Dear Anthony',
289 'Thanks for your auto renew membership sign-up',
290 'In Memory of',
291 ]);
292 $mut->clearMessages();
293 $this->_contactID = $this->individualCreate(['first_name' => 'Antonia', 'prefix_id' => 'Mrs.', 'email' => 'antonia_anderson@civicrm.org']);
294 $this->_invoiceID = uniqid();
295
296 // Note, the second contribution is not in honor of anyone and the
297 // receipt should not mention honor at all.
298 $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]);
299 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['x_trans_id' => 'hers']));
300 $IPN->main();
301
302 $mut->checkAllMailLog([
303 'Membership Type: General',
304 'Mrs. Antonia Anderson II',
305 'antonia_anderson@civicrm.org',
306 'Amount: $ 200.00',
307 'Membership Start Date:',
308 'Transaction #: hers',
309 'Supporter Profile',
310 'First Name: Antonia',
311 'Last Name: Anderson',
312 'Email Address: antonia_anderson@civicrm.org',
313 'This membership will be automatically renewed every',
314 'Dear Antonia',
315 'Thanks for your auto renew membership sign-up',
316 ]);
317
318 $shouldNotBeInMailing = [
319 'Honor',
320 'In Memory of',
321 ];
322 $mails = $mut->getAllMessages('raw');
323 foreach ($mails as $mail) {
324 $mut->checkMailForStrings([], $shouldNotBeInMailing, '', $mail);
325 }
326 $mut->stop();
327 $mut->clearMessages();
328 }
329
330 /**
331 * Test IPN response mails don't leak.
332 */
333 public function testIPNPaymentMembershipRecurSuccessNoLeakageOnlineThenOffline() {
334 $mut = new CiviMailUtils($this, TRUE);
335 $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]);
336 $this->addProfile('supporter_profile', $this->_contributionPageID);
337 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
338 $IPN->main();
339 $mut->checkAllMailLog([
340 'Membership Type: General',
341 'Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
342 'Amount: $ 200.00',
343 'Membership Start Date:',
344 'Supporter Profile',
345 'First Name: Anthony',
346 'Last Name: Anderson',
347 'Email Address: anthony_anderson@civicrm.org',
348 'This membership will be automatically renewed every',
349 'Dear Anthony',
350 'Thanks for your auto renew membership sign-up',
351 ]);
352
353 $this->_contactID = $this->individualCreate(['first_name' => 'Antonia', 'prefix_id' => 'Mrs.', 'email' => 'antonia_anderson@civicrm.org']);
354 $this->_invoiceID = uniqid();
355 $this->_contributionPageID = NULL;
356
357 $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]);
358 $mut->clearMessages();
359 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['x_trans_id' => 'hers']));
360 $IPN->main();
361
362 $mut->checkAllMailLog([
363 'Membership Type: General',
364 'Mrs. Antonia Anderson II',
365 'antonia_anderson@civicrm.org',
366 'Amount: $ 200.00',
367 'Membership Start Date:',
368 'Transaction #: hers',
369 'This membership will be automatically renewed every',
370 'Dear Antonia',
371 'Thanks for your auto renew membership sign-up',
372 ],
373 [
374 'First Name: Anthony',
375 'First Name: Antonia',
376 'Last Name: Anderson',
377 'Supporter Profile',
378 'Email Address: antonia_anderson@civicrm.org',
379 ]);
380
381 $mut->stop();
382 $mut->clearMessages();
383 }
384
385 /**
386 * Get detail for recurring transaction.
387 *
388 * @param array $params
389 * Additional parameters.
390 *
391 * @return array
392 * Parameters like AuthorizeNet silent post paramters.
393 */
394 public function getRecurTransaction($params = []) {
395 return array_merge([
396 'x_amount' => '200.00',
397 "x_country" => 'US',
398 'x_phone' => "",
399 "x_fax" => "",
400 "x_email" => "me@gmail.com",
401 "x_description" => "lots of money",
402 "x_type" => "auth_capture",
403 "x_ship_to_first_name" => "",
404 "x_ship_to_last_name" => "",
405 "x_ship_to_company" => "",
406 "x_ship_to_address" => "",
407 "x_ship_to_city" => "",
408 "x_ship_to_state" => "",
409 "x_ship_to_zip" => "",
410 "x_ship_to_country" => "",
411 "x_tax" => "0.00",
412 "x_duty" => "0.00",
413 "x_freight" => "0.00",
414 "x_tax_exempt" => "FALSE",
415 "x_po_num" => "",
416 "x_MD5_Hash" => "1B7C0C5B4DEDD9CAD0636E35E22FC594",
417 "x_cvv2_resp_code" => "",
418 "x_cavv_response" => "",
419 "x_test_request" => "false",
420 "x_subscription_id" => $this->_contactID,
421 "x_subscription_paynum" => "1",
422 'x_first_name' => 'Robert',
423 'x_zip' => '90210',
424 'x_state' => 'WA',
425 'x_city' => 'Dallas',
426 'x_address' => '41 My ST',
427 'x_invoice_num' => $this->ids['Contribution'][0],
428 'x_cust_id' => $this->_contactID,
429 'x_company' => 'nowhere@civicrm.org',
430 'x_last_name' => 'Roberts',
431 'x_account_number' => 'XXXX5077',
432 'x_card_type' => 'Visa',
433 'x_method' => 'CC',
434 'x_trans_id' => '6511143069',
435 'x_auth_code' => '123456',
436 'x_avs_code' => 'Z',
437 'x_response_reason_text' => 'This transaction has been approved.',
438 'x_response_reason_code' => '1',
439 'x_response_code' => '1',
440 ], $params);
441 }
442
443 /**
444 * @return array
445 */
446 public function getRecurSubsequentTransaction($params = []) {
447 return array_merge($this->getRecurTransaction(), [
448 'x_trans_id' => 'second_one',
449 'x_MD5_Hash' => 'EA7A3CD65A85757827F51212CA1486A8',
450 ], $params);
451 }
452
453 }