Merge pull request #16800 from pradpnayak/notifyEmail
[civicrm-core.git] / tests / phpunit / CRM / Core / Payment / PayPalProIPNTest.php
CommitLineData
a86d27fc 1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
a86d27fc 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 |
a86d27fc 9 +--------------------------------------------------------------------+
e70a7fc0 10 */
a86d27fc 11
e9479dcf
EM
12/**
13 * Class CRM_Core_Payment_PayPalProIPNTest
acb109b7 14 * @group headless
e9479dcf 15 */
7c119c92 16class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase {
a86d27fc 17 protected $_contributionID;
18 protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424';
19 protected $_financialTypeID = 1;
20 protected $_contactID;
21 protected $_contributionRecurID;
22 protected $_contributionPageID;
23 protected $_paymentProcessorID;
3aaa68fb 24 /**
25 * IDs of entities created to support the tests.
26 *
27 * @var array
28 */
9099cab3 29 protected $ids = [];
a86d27fc 30
3aaa68fb 31 /**
32 * Set up function.
33 */
00be9182 34 public function setUp() {
a86d27fc 35 parent::setUp();
9099cab3 36 $this->_paymentProcessorID = $this->paymentProcessorCreate(['is_test' => 0]);
a86d27fc 37 $this->_contactID = $this->individualCreate();
9099cab3 38 $contributionPage = $this->callAPISuccess('contribution_page', 'create', [
39b959db
SL
39 'title' => "Test Contribution Page",
40 'financial_type_id' => $this->_financialTypeID,
41 'currency' => 'USD',
42 'payment_processor' => $this->_paymentProcessorID,
9099cab3 43 ]);
a86d27fc 44 $this->_contributionPageID = $contributionPage['id'];
a86d27fc 45 }
46
3aaa68fb 47 /**
48 * Tear down function.
49 */
00be9182 50 public function tearDown() {
b38530f2 51 $this->quickCleanUpFinancialEntities();
a86d27fc 52 }
53
54 /**
6b1b1558 55 * Test IPN response updates contribution_recur & contribution for first & second contribution.
56 *
57 * The scenario is that a pending contribution exists and the first call will update it to completed.
58 * The second will create a new contribution.
a86d27fc 59 */
00be9182 60 public function testIPNPaymentRecurSuccess() {
0dbefed3 61 $this->setupRecurringPaymentProcessorTransaction();
d0488f03 62 global $_GET;
63 $_GET = $this->getPaypalProRecurTransaction();
a86d27fc 64 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction());
65 $paypalIPN->main();
9099cab3 66 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
a86d27fc 67 $this->assertEquals(1, $contribution['contribution_status_id']);
68 $this->assertEquals('8XA571746W2698126', $contribution['trxn_id']);
69 // source gets set by processor
70 $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
9099cab3 71 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
a86d27fc 72 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
4774090c 73 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction());
74 $paypalIPN->main();
9099cab3 75 $contribution = $this->callAPISuccess('contribution', 'get', [
39b959db
SL
76 'contribution_recur_id' => $this->_contributionRecurID,
77 'sequential' => 1,
9099cab3 78 ]);
a86d27fc 79 $this->assertEquals(2, $contribution['count']);
80 $this->assertEquals('secondone', $contribution['values'][1]['trxn_id']);
b3cecff5 81 $this->assertEquals('Debit Card', $contribution['values'][1]['payment_instrument']);
a86d27fc 82 }
83
8c15aab2 84 /**
3aaa68fb 85 * Test IPN response updates contribution_recur & contribution for first & second contribution.
0dea0c7c 86 *
87 * @throws \CRM_Core_Exception
8c15aab2 88 */
00be9182 89 public function testIPNPaymentMembershipRecurSuccess() {
17e615c7 90 $durationUnit = 'year';
9099cab3
CW
91 $this->setupMembershipRecurringPaymentProcessorTransaction(['duration_unit' => $durationUnit, 'frequency_unit' => $durationUnit]);
92 $this->callAPISuccessGetSingle('membership_payment', []);
8c15aab2
EM
93 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction());
94 $paypalIPN->main();
9099cab3
CW
95 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
96 $membershipEndDate = $this->callAPISuccessGetValue('membership', ['return' => 'end_date']);
8c15aab2
EM
97 $this->assertEquals(1, $contribution['contribution_status_id']);
98 $this->assertEquals('8XA571746W2698126', $contribution['trxn_id']);
99 // source gets set by processor
100 $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
9099cab3 101 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
8c15aab2
EM
102 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
103 $paypalIPN = new CRM_Core_Payment_PaypalProIPN($this->getPaypalProRecurSubsequentTransaction());
104 $paypalIPN->main();
17e615c7
MW
105
106 $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membershipEndDate);
9099cab3
CW
107 $this->assertEquals($renewedMembershipEndDate, $this->callAPISuccessGetValue('membership', ['return' => 'end_date']));
108 $contribution = $this->callAPISuccess('contribution', 'get', [
39b959db
SL
109 'contribution_recur_id' => $this->_contributionRecurID,
110 'sequential' => 1,
9099cab3 111 ]);
8c15aab2
EM
112 $this->assertEquals(2, $contribution['count']);
113 $this->assertEquals('secondone', $contribution['values'][1]['trxn_id']);
9099cab3 114 $this->callAPISuccessGetCount('line_item', [
39b959db
SL
115 'entity_id' => $this->ids['membership'],
116 'entity_table' => 'civicrm_membership',
9099cab3
CW
117 ], 2);
118 $this->callAPISuccessGetSingle('line_item', [
39b959db
SL
119 'contribution_id' => $contribution['values'][1]['id'],
120 'entity_table' => 'civicrm_membership',
9099cab3
CW
121 ]);
122 $this->callAPISuccessGetSingle('membership_payment', ['contribution_id' => $contribution['values'][1]['id']]);
69140e67 123
8c15aab2 124 }
92915c55 125
da88a16b 126 /**
3aaa68fb 127 * CRM-13743 test IPN edge case where the first transaction fails and the second succeeds.
128 *
da88a16b
E
129 * We are checking that the created contribution has the same date as IPN says it should
130 * Note that only one contribution will be created (no evidence of the failed contribution is left)
131 * It seems likely that may change in future & this test will start failing (I point this out in the hope it
132 * will help future debuggers)
133 */
00be9182 134 public function testIPNPaymentCRM13743() {
0dbefed3 135 $this->setupRecurringPaymentProcessorTransaction();
da88a16b
E
136 $firstPaymentParams = $this->getPaypalProRecurTransaction();
137 $firstPaymentParams['txn_type'] = 'recurring_payment_failed';
138 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($firstPaymentParams);
139 $paypalIPN->main();
140
9099cab3 141 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
da88a16b
E
142 $this->assertEquals(2, $contribution['contribution_status_id']);
143 $this->assertEquals('', $contribution['trxn_id']);
9099cab3 144 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
da88a16b
E
145 $this->assertEquals(2, $contributionRecur['contribution_status_id']);
146 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction());
147 $paypalIPN->main();
9099cab3 148 $contribution = $this->callAPISuccess('contribution', 'get', [
39b959db
SL
149 'contribution_recur_id' => $this->_contributionRecurID,
150 'sequential' => 1,
9099cab3 151 ]);
da88a16b
E
152 $this->assertEquals(1, $contribution['count']);
153 $this->assertEquals('secondone', $contribution['values'][0]['trxn_id']);
154 $this->assertEquals(strtotime('03:59:05 Jul 14, 2013 PDT'), strtotime($contribution['values'][0]['receive_date']));
155 }
156
a86d27fc 157 /**
3aaa68fb 158 * Check a payment express IPN call does not throw any errors.
159 *
a86d27fc 160 * At this stage nothing it supposed to happen so it's a pretty blunt test
161 * but at least it should be e-notice free
a86d27fc 162 * The browser interaction will update Paypal express payments
163 * The ipn code redirects POSTs to paypal pro & GETs to paypal std but the
164 * documentation (https://www.paypalobjects.com/webstatic/en_US/developer/docs/pdf/ipnguide.pdf)
165 * implies only POSTS are sent server to server.
166 * So, it's likely Paypal Std IPNs aren't working.
167 * However, for Paypal Pro users payment express transactions can't work as they don't hold the component
168 * which is required for them to be handled by either the Pro or Express class
169 *
170 * So, the point of this test is simply to ensure it fails in a known way & with a better message than
171 * previously & that refactorings don't mess with that
172 *
173 * Obviously if the behaviour is fixed then the test should be updated!
174 */
00be9182 175 public function testIPNPaymentExpressNoError() {
0dbefed3 176 $this->setupRecurringPaymentProcessorTransaction();
a86d27fc 177 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressTransactionIPN());
92e4c2a5 178 try {
a86d27fc 179 $paypalIPN->main();
180 }
92915c55 181 catch (CRM_Core_Exception $e) {
9099cab3 182 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
a86d27fc 183 // no change
184 $this->assertEquals(2, $contribution['contribution_status_id']);
16c12d4e 185 $this->assertEquals('Paypal IPNS not handled other than recurring_payments', $e->getMessage());
a86d27fc 186 return;
187 }
188 $this->fail('The Paypal Express IPN should have caused an exception');
189 }
190
a86d27fc 191 /**
eceb18cc 192 * Get PaymentExpress IPN for a single transaction.
a6c01b45
CW
193 * @return array
194 * array representing a Paypal IPN POST
a86d27fc 195 */
00be9182 196 public function getPaypalExpressTransactionIPN() {
9099cab3 197 return [
a86d27fc 198 'mc_gross' => '200.00',
199 'invoice' => $this->_invoiceID,
200 'protection_eligibility' => 'Eligible',
201 'address_status' => 'confirmer',
202 'payer_id' => 'ZYXHBZSULPQE3',
203 'tax' => '0.00',
204 'address_street' => '13 Streety Street',
205 'payment_rate' => '03:32:12 Jul 29, 2013 PDT',
206 'payment_status' => 'Completed',
207 'charset' => 'windows-1252',
208 'address_zip' => '90210',
209 'first_name' => 'Mary-Jane',
210 'mc_fee' => '4.70',
211 'address_country_core' => 'US',
212 'address_name' => 'Mary-Jane',
213 'notify_version' => '3.7',
214 'custom' => '',
215 'payer_status' => 'unverified',
86797006 216 'address_country' => 'UNITED STATES',
a86d27fc 217 'address_city' => 'Portland',
218 'quantity' => '1',
219 'verify_sign' => 'AUyUU3IMAvssa3j4KorlbLnfr.9.AW7GX-sL7Ts1brCHvn13npvO-pqf',
220 'payer_email' => 'mary@nowhere.com',
221 'txn_id' => '3X9131350B932393N',
222 'payment_type' => 'instant',
223 'last_name' => 'Bob',
224 'address_state' => 'ME',
225 'receiver_email' => 'email@civicrm.org',
226 'payment_fee' => '4.70',
227 'received_id' => 'GUH3W7BJLGTY3',
228 'txn_type' => 'express_checkout',
229 'item_name' => '',
230 'mc_currency' => 'USD',
231 'item_number' => '',
232 'residence_country' => 'US',
233 'handling_amount' => '0.00',
234 'transaction_subject' => '',
235 'payment_gross' => '200.00',
236 'shipping' => '0.00',
237 'ipn_track_id' => '5r27c2e31rl7c',
6d8785b9 238 'is_unit_test' => TRUE,
9099cab3 239 ];
a86d27fc 240 }
241
242 /**
eceb18cc 243 * Get IPN results from follow on IPN transactions.
a6c01b45
CW
244 * @return array
245 * array representing a Paypal IPN POST
a86d27fc 246 */
00be9182 247 public function getSubsequentPaypalExpressTransaction() {
9099cab3 248 return [
a86d27fc 249 'mc_gross' => '5.00',
250 'period_type' => ' Regular',
251 'outstanding_balance' => '0.00',
252 'next_payment_date' => '03:00:00 Aug 14, 2013 PDT',
253 'protection_eligibility' => 'Eligible',
254 'payment_cycle' => 'Monthly',
255 'address_status' => 'confirmed',
256 'tax' => '0.00',
257 'payer_id' => 'ACRAM59AAS2E4',
258 'address_street' => '54 Soul Street',
259 'payment_date' => '03:58:39 Jul 14, 2013 PDT',
260 'payment_status' => 'Completed',
261 'product_name' => '5 Per 1 month',
262 'charset' => 'windows-1252',
263 'rp_invoice_id' => 'i=' . $this->_invoiceID . '&m=&c=&r=&b=&p=' . $this->_contributionPageID,
264 'recurring_payment_id' => 'I-3EEUC094KYQW',
265 'address_zip' => '90210',
266 'first_name' => 'Alanna',
267 'mc_fee' => '0.41',
268 'address_country_code' => 'US',
269 'address_name' => 'Alanna Morrissette',
270 'notify_version' => '3.7',
271 'amount_per_cycle' => '5.00',
272 'payer_status' => 'unverified',
273 'currency_code' => 'USD',
6439290e 274 'business' => 'mpa@example.com',
86797006 275 'address_country' => 'UNITED STATES',
a86d27fc 276 'address_city' => 'Limestone',
277 'verify_sign' => 'AXi4DULbes8quzIiq2YNsdTJH5ciPPPzG9PcQvkQg4BjfvWi8aY9GgDb',
278 'payer_email' => 'passport45051@yahoo.com',
279 'initial_payment_amount' => '0.00',
280 'profile_status' => 'Active',
281 'amount' => '5.00',
282 'txn_id' => '03W6561902100533N',
283 'payment_type' => 'instant',
284 'last_name' => 'Morrissette',
285 'address_state' => 'ME',
6439290e 286 'receiver_email' => 'info@example.com',
a86d27fc 287 'payment_fee' => '0.41',
288 'receiver_id' => 'GTH8P7UQWWTY6',
289 'txn_type' => 'recurring_payment',
290 'mc_currency' => 'USD',
291 'residence_country' => 'US',
292 'transaction_subject' => '5 Per 1 month',
293 'payment_gross' => '5.00',
294 'shipping' => '0.00',
295 'product_type' => '1',
296 'time_created' => '12:02:25 May 14, 2013 PDT',
21dfd5f5 297 'ipn_track_id' => '912e5010eb5a6',
9099cab3 298 ];
a86d27fc 299 }
92915c55 300
a86d27fc 301 /**
3aaa68fb 302 * Get IPN style details for an incoming recurring transaction.
a86d27fc 303 */
00be9182 304 public function getPaypalProRecurTransaction() {
9099cab3 305 return [
a86d27fc 306 'amount' => '15.00',
307 'initial_payment_amount' => '0.00',
308 'profile_status' => 'Active',
309 'payer_id' => '4NHUTA7ZUE92C',
310 'product_type' => '1',
311 'ipn_track_id' => '30171ad0afe3g',
312 'outstanding_balance' => '0.00',
313 'shipping' => '0.00',
314 'charset' => 'windows-1252',
315 'period_type' => ' Regular',
316 'payment_gross' => '15.00',
317 'currency_code' => 'USD',
318 'receipt_id' => '1428-3355-5949-8495',
319 'verify_sign' => 'AoPC4BjkCyDFEXbSkoZcgqH3hpacA3RXyCD10axGfqyaRhHqwz1UZzX7',
320 'payment_cycle' => 'Monthly',
321 'txn_type' => 'recurring_payment',
322 'receiver_id' => 'GWE8P7BJVLMY6',
323 'payment_fee' => '0.63',
324 'mc_currency' => 'USD',
325 'transaction_subject' => '',
326 'protection_eligibility' => 'Ineligible',
327 'payer_status' => 'unverified',
328 'first_name' => 'Robert',
329 'product_name' => ' => 15 Per 1 month',
330 'amount_per_cycle' => '15.00',
331 'mc_gross' => '15.00',
332 'payment_date' => '03:59:05 Jul 14, 2013 PDT',
d0488f03 333 'rp_invoice_id' => 'i=' . $this->_invoiceID . '&m=contribute&c=' . $this->_contactID . '&r=' . $this->_contributionRecurID . '&b=' . $this->_contributionID . '&p=null',
a86d27fc 334 'payment_status' => 'Completed',
335 'business' => 'nowhere@civicrm.org',
336 'last_name' => 'Roberty',
337 'txn_id' => '8XA571746W2698126',
338 'mc_fee' => '0.63',
339 'time_created' => '14 => 51 => 55 Feb 14, 2013 PST',
340 'resend' => 'true',
341 'payment_type' => 'instant',
342 'notify_version' => '3.7',
343 'recurring_payment_id' => 'I-8XHAKBG12SFP',
344 'receiver_email' => 'nil@civicrm.org',
345 'next_payment_date' => '03:00:00 Aug 14, 2013 PDT',
346 'tax' => '0.00',
21dfd5f5 347 'residence_country' => 'US',
9099cab3 348 ];
a86d27fc 349 }
4cbe18b8
EM
350
351 /**
3aaa68fb 352 * Get IPN-style details for a second incoming transaction.
353 *
4cbe18b8
EM
354 * @return array
355 */
00be9182 356 public function getPaypalProRecurSubsequentTransaction() {
9099cab3 357 return array_merge($this->getPaypalProRecurTransaction(), ['txn_id' => 'secondone']);
a86d27fc 358 }
96025800 359
6439290e 360 /**
361 * Test IPN response update for a paypal express profile creation confirmation.
362 */
363 public function testIPNPaymentExpressRecurSuccess() {
364 $this->setupRecurringPaymentProcessorTransaction(['processor_id' => '']);
365 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressRecurSubscriptionConfirmation());
366 $paypalIPN->main();
9099cab3 367 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
6439290e 368 $this->assertEquals('I-JW77S1PY2032', $contributionRecur['processor_id']);
369 }
370
371 /**
372 * Get response consistent with creating a new profile.
373 *
374 * @return array
375 */
376 public function getPaypalExpressRecurSubscriptionConfirmation() {
377 return [
378 'payment_cycle' => 'Monthly',
379 'txn_type' => 'recurring_payment_profile_created',
380 'last_name' => 'buyer',
381 'next_payment_date' => '03:00:00 May 09, 2018 PDT',
382 'residence_country' => 'GB',
383 'initial_payment_amount' => '0.00',
384 'rp_invoice_id' => 'i=' . $this->_invoiceID
39b959db
SL
385 . '&m=&c=' . $this->_contributionID
386 . '&r=' . $this->_contributionRecurID
387 . '&b=' . $this->_contactID
388 . '&p=' . $this->_contributionPageID,
6439290e 389 'currency_code' => 'GBP',
390 'time_created' => '12:39:01 May 09, 2018 PDT',
391 'verify_sign' => 'AUg223oCjn4HgJXKkrICawXQ3fyUA2gAd1.f1IPJ4r.9sln-nWcB-EJG',
392 'period_type' => 'Regular',
393 'payer_status' => 'verified',
394 'test_ipn' => '1',
395 'tax' => '0.00',
396 'payer_email' => 'payer@example.com',
397 'first_name' => 'test',
398 'receiver_email' => 'shop@example.com',
399 'payer_id' => 'BWXXXM8111HDS',
400 'product_type' => 1,
401 'shipping' => '0.00',
402 'amount_per_cycle' => '6.00',
403 'profile_status' => 'Active',
404 'charset' => 'windows-1252',
405 'notify_version' => '3.9',
406 'amount' => '6.00',
407 'outstanding_balance' => '0.00',
408 'recurring_payment_id' => 'I-JW77S1PY2032',
409 'product_name' => '6 Per 1 month',
410 'ipn_track_id' => '6255554274055',
411 ];
412 }
413
a86d27fc 414}