3 use Civi\Payment\Exception\PaymentProcessorException
;
4 use Civi\Api4\Contribution
;
7 * Class CRM_Core_Payment_PayPalProIPNTest
10 class CRM_Core_Payment_AuthorizeNetIPNTest
extends CiviUnitTestCase
{
11 use CRMTraits_Financial_OrderTrait
;
13 protected $_contributionID;
14 protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424';
15 protected $_financialTypeID = 1;
16 protected $_contactID;
17 protected $_contributionRecurID;
18 protected $_contributionPageID;
19 protected $_paymentProcessorID;
23 * @throws \CRM_Core_Exception
25 public function 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
,
33 'payment_processor' => $this->_paymentProcessorID
,
35 'receipt_from_email' => 'gaia@the.cosmos',
36 'receipt_from_name' => 'Pachamama',
37 'is_email_receipt' => TRUE,
39 $this->_contributionPageID
= $contributionPage['id'];
42 public function tearDown() {
43 $this->quickCleanUpFinancialEntities();
47 * Ensure recurring contributions from Contribution Pages
48 * with receipt turned off don't send a receipt.
50 * @throws \CiviCRM_API3_Exception
51 * @throws \CRM_Core_Exception
53 public function testIPNPaymentRecurNoReceipt() {
54 $mut = new CiviMailUtils($this, TRUE);
55 // Turn off receipts in contribution page.
57 'id' => $this->_contributionPageID
,
58 'is_email_receipt' => FALSE,
60 $this->callAPISuccess('contributionPage', 'update', $api_params);
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';
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,
82 'credit_card_exp_date' => [
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',
98 'hidden_AdditionalDetail' => 1,
99 'hidden_Premium' => 1,
100 'payment_processor_id' => $this->_paymentProcessorID
,
102 'source' => 'bob sled race',
103 'contribution_page_id' => $this->_contributionPageID
,
105 ], CRM_Core_Action
::ADD
);
107 catch (PaymentProcessorException
$e) {
108 $this->markTestSkipped('Error from A.net - cannot proceed');
110 $this->_contributionID
= $contribution->id
;
111 $this->ids
['Contribution'][0] = $contribution->id
;
112 $this->_contributionRecurID
= $contribution->contribution_recur_id
;
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.
119 $IPN = new CRM_Core_Payment_AuthorizeNetIPN(
120 $this->getRecurTransaction(['x_subscription_id' => $processor_id])
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']));
127 // Now send a second one (authorize seems to treat first and second contributions
129 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction([
130 'x_subscription_id' => $processor_id,
131 'x_subscription_paynum' => 2,
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();
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']);
147 * Test IPN response updates contribution_recur & contribution for first & second contribution
149 * @throws \CRM_Core_Exception
151 public function testIPNPaymentRecurSuccess() {
152 $this->setupRecurringPaymentProcessorTransaction();
153 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
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());
164 $contribution = $this->callAPISuccess('contribution', 'get', [
165 'contribution_recur_id' => $this->_contributionRecurID
,
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'])));
174 * Test payment processor is correctly assigned for the IPN payment.
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
]);
181 //Call IPN with processor id.
182 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['processor_id' => $this->_paymentProcessorID2
]));
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']);
194 * Test IPN response updates contribution_recur & contribution for first & second contribution
196 * @throws \CRM_Core_Exception
198 public function testIPNPaymentRecurSuccessSuppliedReceiveDate() {
199 $this->setupRecurringPaymentProcessorTransaction();
200 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
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()));
211 $contribution = $this->callAPISuccess('contribution', 'get', [
212 'contribution_recur_id' => $this->_contributionRecurID
,
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'])));
221 * Test IPN response updates contribution_recur & contribution for first & second contribution
223 * @throws \CRM_Core_Exception
225 public function testIPNPaymentMembershipRecurSuccess() {
226 $this->createRepeatMembershipOrder();
227 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
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']);
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());
239 $contribution = $this->callAPISuccess('contribution', 'get', [
240 'contribution_recur_id' => $this->_contributionRecurID
,
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',
253 $this->validateAllContributions();
254 $this->validateAllPayments();
258 * Test IPN response mails don't leak.
260 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
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');
268 $this->callAPISuccess('ContributionSoft', 'create', [
269 'contact_id' => $this->individualCreate(),
270 'contribution_id' => $this->_contributionID
,
271 'soft_credit_type_id' => 'in_memory_of',
275 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
277 $mut->checkAllMailLog([
278 'Membership Type: General',
279 'Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
281 'Membership Start Date:',
283 'First Name: Anthony',
284 'Last Name: Anderson',
285 'Email Address: anthony_anderson@civicrm.org',
287 'This membership will be automatically renewed every',
289 'Thanks for your auto renew membership sign-up',
292 $mut->clearMessages();
293 $this->_contactID
= $this->individualCreate(['first_name' => 'Antonia', 'prefix_id' => 'Mrs.', 'email' => 'antonia_anderson@civicrm.org']);
294 $this->_invoiceID
= uniqid();
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']));
302 $mut->checkAllMailLog([
303 'Membership Type: General',
304 'Mrs. Antonia Anderson II',
305 'antonia_anderson@civicrm.org',
307 'Membership Start Date:',
308 'Transaction #: hers',
310 'First Name: Antonia',
311 'Last Name: Anderson',
312 'Email Address: antonia_anderson@civicrm.org',
313 'This membership will be automatically renewed every',
315 'Thanks for your auto renew membership sign-up',
318 $shouldNotBeInMailing = [
322 $mails = $mut->getAllMessages('raw');
323 foreach ($mails as $mail) {
324 $mut->checkMailForStrings([], $shouldNotBeInMailing, '', $mail);
327 $mut->clearMessages();
331 * Test IPN response mails don't leak.
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());
339 $mut->checkAllMailLog([
340 'Membership Type: General',
341 'Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
343 'Membership Start Date:',
345 'First Name: Anthony',
346 'Last Name: Anderson',
347 'Email Address: anthony_anderson@civicrm.org',
348 'This membership will be automatically renewed every',
350 'Thanks for your auto renew membership sign-up',
353 $this->_contactID
= $this->individualCreate(['first_name' => 'Antonia', 'prefix_id' => 'Mrs.', 'email' => 'antonia_anderson@civicrm.org']);
354 $this->_invoiceID
= uniqid();
355 $this->_contributionPageID
= NULL;
357 $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]);
358 $mut->clearMessages();
359 $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['x_trans_id' => 'hers']));
362 $mut->checkAllMailLog([
363 'Membership Type: General',
364 'Mrs. Antonia Anderson II',
365 'antonia_anderson@civicrm.org',
367 'Membership Start Date:',
368 'Transaction #: hers',
369 'This membership will be automatically renewed every',
371 'Thanks for your auto renew membership sign-up',
374 'First Name: Anthony',
375 'First Name: Antonia',
376 'Last Name: Anderson',
378 'Email Address: antonia_anderson@civicrm.org',
382 $mut->clearMessages();
386 * Get detail for recurring transaction.
388 * @param array $params
389 * Additional parameters.
392 * Parameters like AuthorizeNet silent post paramters.
394 public function getRecurTransaction($params = []) {
396 'x_amount' => '200.00',
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" => "",
413 "x_freight" => "0.00",
414 "x_tax_exempt" => "FALSE",
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',
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',
434 'x_trans_id' => '6511143069',
435 'x_auth_code' => '123456',
437 'x_response_reason_text' => 'This transaction has been approved.',
438 'x_response_reason_code' => '1',
439 'x_response_code' => '1',
446 public function getRecurSubsequentTransaction($params = []) {
447 return array_merge($this->getRecurTransaction(), [
448 'x_trans_id' => 'second_one',
449 'x_MD5_Hash' => 'EA7A3CD65A85757827F51212CA1486A8',