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