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