Add try catch to main loops on core ipn classes
[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 *
cd0f2a34 170 * So, the point of this test is simply to ensure it fails in a known way
a86d27fc 171 */
00be9182 172 public function testIPNPaymentExpressNoError() {
0dbefed3 173 $this->setupRecurringPaymentProcessorTransaction();
a86d27fc 174 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressTransactionIPN());
cd0f2a34 175 $paypalIPN->main();
176 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
177 // no change
178 $this->assertEquals(2, $contribution['contribution_status_id']);
a86d27fc 179 }
180
a86d27fc 181 /**
eceb18cc 182 * Get PaymentExpress IPN for a single transaction.
a6c01b45
CW
183 * @return array
184 * array representing a Paypal IPN POST
a86d27fc 185 */
00be9182 186 public function getPaypalExpressTransactionIPN() {
9099cab3 187 return [
a86d27fc 188 'mc_gross' => '200.00',
189 'invoice' => $this->_invoiceID,
190 'protection_eligibility' => 'Eligible',
191 'address_status' => 'confirmer',
192 'payer_id' => 'ZYXHBZSULPQE3',
193 'tax' => '0.00',
194 'address_street' => '13 Streety Street',
195 'payment_rate' => '03:32:12 Jul 29, 2013 PDT',
196 'payment_status' => 'Completed',
197 'charset' => 'windows-1252',
198 'address_zip' => '90210',
199 'first_name' => 'Mary-Jane',
200 'mc_fee' => '4.70',
201 'address_country_core' => 'US',
202 'address_name' => 'Mary-Jane',
203 'notify_version' => '3.7',
204 'custom' => '',
205 'payer_status' => 'unverified',
86797006 206 'address_country' => 'UNITED STATES',
a86d27fc 207 'address_city' => 'Portland',
208 'quantity' => '1',
209 'verify_sign' => 'AUyUU3IMAvssa3j4KorlbLnfr.9.AW7GX-sL7Ts1brCHvn13npvO-pqf',
210 'payer_email' => 'mary@nowhere.com',
211 'txn_id' => '3X9131350B932393N',
212 'payment_type' => 'instant',
213 'last_name' => 'Bob',
214 'address_state' => 'ME',
215 'receiver_email' => 'email@civicrm.org',
216 'payment_fee' => '4.70',
217 'received_id' => 'GUH3W7BJLGTY3',
218 'txn_type' => 'express_checkout',
219 'item_name' => '',
220 'mc_currency' => 'USD',
221 'item_number' => '',
222 'residence_country' => 'US',
223 'handling_amount' => '0.00',
224 'transaction_subject' => '',
225 'payment_gross' => '200.00',
226 'shipping' => '0.00',
227 'ipn_track_id' => '5r27c2e31rl7c',
6d8785b9 228 'is_unit_test' => TRUE,
9099cab3 229 ];
a86d27fc 230 }
231
232 /**
eceb18cc 233 * Get IPN results from follow on IPN transactions.
a6c01b45
CW
234 * @return array
235 * array representing a Paypal IPN POST
a86d27fc 236 */
00be9182 237 public function getSubsequentPaypalExpressTransaction() {
9099cab3 238 return [
a86d27fc 239 'mc_gross' => '5.00',
240 'period_type' => ' Regular',
241 'outstanding_balance' => '0.00',
242 'next_payment_date' => '03:00:00 Aug 14, 2013 PDT',
243 'protection_eligibility' => 'Eligible',
244 'payment_cycle' => 'Monthly',
245 'address_status' => 'confirmed',
246 'tax' => '0.00',
247 'payer_id' => 'ACRAM59AAS2E4',
248 'address_street' => '54 Soul Street',
249 'payment_date' => '03:58:39 Jul 14, 2013 PDT',
250 'payment_status' => 'Completed',
251 'product_name' => '5 Per 1 month',
252 'charset' => 'windows-1252',
253 'rp_invoice_id' => 'i=' . $this->_invoiceID . '&m=&c=&r=&b=&p=' . $this->_contributionPageID,
254 'recurring_payment_id' => 'I-3EEUC094KYQW',
255 'address_zip' => '90210',
256 'first_name' => 'Alanna',
257 'mc_fee' => '0.41',
258 'address_country_code' => 'US',
259 'address_name' => 'Alanna Morrissette',
260 'notify_version' => '3.7',
261 'amount_per_cycle' => '5.00',
262 'payer_status' => 'unverified',
263 'currency_code' => 'USD',
6439290e 264 'business' => 'mpa@example.com',
86797006 265 'address_country' => 'UNITED STATES',
a86d27fc 266 'address_city' => 'Limestone',
267 'verify_sign' => 'AXi4DULbes8quzIiq2YNsdTJH5ciPPPzG9PcQvkQg4BjfvWi8aY9GgDb',
268 'payer_email' => 'passport45051@yahoo.com',
269 'initial_payment_amount' => '0.00',
270 'profile_status' => 'Active',
271 'amount' => '5.00',
272 'txn_id' => '03W6561902100533N',
273 'payment_type' => 'instant',
274 'last_name' => 'Morrissette',
275 'address_state' => 'ME',
6439290e 276 'receiver_email' => 'info@example.com',
a86d27fc 277 'payment_fee' => '0.41',
278 'receiver_id' => 'GTH8P7UQWWTY6',
279 'txn_type' => 'recurring_payment',
280 'mc_currency' => 'USD',
281 'residence_country' => 'US',
282 'transaction_subject' => '5 Per 1 month',
283 'payment_gross' => '5.00',
284 'shipping' => '0.00',
285 'product_type' => '1',
286 'time_created' => '12:02:25 May 14, 2013 PDT',
21dfd5f5 287 'ipn_track_id' => '912e5010eb5a6',
9099cab3 288 ];
a86d27fc 289 }
92915c55 290
a86d27fc 291 /**
3aaa68fb 292 * Get IPN style details for an incoming recurring transaction.
a86d27fc 293 */
00be9182 294 public function getPaypalProRecurTransaction() {
9099cab3 295 return [
a86d27fc 296 'amount' => '15.00',
297 'initial_payment_amount' => '0.00',
298 'profile_status' => 'Active',
299 'payer_id' => '4NHUTA7ZUE92C',
300 'product_type' => '1',
301 'ipn_track_id' => '30171ad0afe3g',
302 'outstanding_balance' => '0.00',
303 'shipping' => '0.00',
304 'charset' => 'windows-1252',
305 'period_type' => ' Regular',
306 'payment_gross' => '15.00',
307 'currency_code' => 'USD',
308 'receipt_id' => '1428-3355-5949-8495',
309 'verify_sign' => 'AoPC4BjkCyDFEXbSkoZcgqH3hpacA3RXyCD10axGfqyaRhHqwz1UZzX7',
310 'payment_cycle' => 'Monthly',
311 'txn_type' => 'recurring_payment',
312 'receiver_id' => 'GWE8P7BJVLMY6',
313 'payment_fee' => '0.63',
314 'mc_currency' => 'USD',
315 'transaction_subject' => '',
316 'protection_eligibility' => 'Ineligible',
317 'payer_status' => 'unverified',
318 'first_name' => 'Robert',
319 'product_name' => ' => 15 Per 1 month',
320 'amount_per_cycle' => '15.00',
321 'mc_gross' => '15.00',
322 'payment_date' => '03:59:05 Jul 14, 2013 PDT',
d0488f03 323 'rp_invoice_id' => 'i=' . $this->_invoiceID . '&m=contribute&c=' . $this->_contactID . '&r=' . $this->_contributionRecurID . '&b=' . $this->_contributionID . '&p=null',
a86d27fc 324 'payment_status' => 'Completed',
325 'business' => 'nowhere@civicrm.org',
326 'last_name' => 'Roberty',
327 'txn_id' => '8XA571746W2698126',
328 'mc_fee' => '0.63',
329 'time_created' => '14 => 51 => 55 Feb 14, 2013 PST',
330 'resend' => 'true',
331 'payment_type' => 'instant',
332 'notify_version' => '3.7',
333 'recurring_payment_id' => 'I-8XHAKBG12SFP',
334 'receiver_email' => 'nil@civicrm.org',
335 'next_payment_date' => '03:00:00 Aug 14, 2013 PDT',
336 'tax' => '0.00',
21dfd5f5 337 'residence_country' => 'US',
9099cab3 338 ];
a86d27fc 339 }
4cbe18b8
EM
340
341 /**
3aaa68fb 342 * Get IPN-style details for a second incoming transaction.
343 *
4cbe18b8
EM
344 * @return array
345 */
00be9182 346 public function getPaypalProRecurSubsequentTransaction() {
9099cab3 347 return array_merge($this->getPaypalProRecurTransaction(), ['txn_id' => 'secondone']);
a86d27fc 348 }
96025800 349
6439290e 350 /**
351 * Test IPN response update for a paypal express profile creation confirmation.
352 */
353 public function testIPNPaymentExpressRecurSuccess() {
354 $this->setupRecurringPaymentProcessorTransaction(['processor_id' => '']);
355 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressRecurSubscriptionConfirmation());
356 $paypalIPN->main();
9099cab3 357 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
6439290e 358 $this->assertEquals('I-JW77S1PY2032', $contributionRecur['processor_id']);
359 }
360
361 /**
362 * Get response consistent with creating a new profile.
363 *
364 * @return array
365 */
366 public function getPaypalExpressRecurSubscriptionConfirmation() {
367 return [
368 'payment_cycle' => 'Monthly',
369 'txn_type' => 'recurring_payment_profile_created',
370 'last_name' => 'buyer',
371 'next_payment_date' => '03:00:00 May 09, 2018 PDT',
372 'residence_country' => 'GB',
373 'initial_payment_amount' => '0.00',
374 'rp_invoice_id' => 'i=' . $this->_invoiceID
39b959db
SL
375 . '&m=&c=' . $this->_contributionID
376 . '&r=' . $this->_contributionRecurID
377 . '&b=' . $this->_contactID
378 . '&p=' . $this->_contributionPageID,
6439290e 379 'currency_code' => 'GBP',
380 'time_created' => '12:39:01 May 09, 2018 PDT',
381 'verify_sign' => 'AUg223oCjn4HgJXKkrICawXQ3fyUA2gAd1.f1IPJ4r.9sln-nWcB-EJG',
382 'period_type' => 'Regular',
383 'payer_status' => 'verified',
384 'test_ipn' => '1',
385 'tax' => '0.00',
386 'payer_email' => 'payer@example.com',
387 'first_name' => 'test',
388 'receiver_email' => 'shop@example.com',
389 'payer_id' => 'BWXXXM8111HDS',
390 'product_type' => 1,
391 'shipping' => '0.00',
392 'amount_per_cycle' => '6.00',
393 'profile_status' => 'Active',
394 'charset' => 'windows-1252',
395 'notify_version' => '3.9',
396 'amount' => '6.00',
397 'outstanding_balance' => '0.00',
398 'recurring_payment_id' => 'I-JW77S1PY2032',
399 'product_name' => '6 Per 1 month',
400 'ipn_track_id' => '6255554274055',
401 ];
402 }
403
a86d27fc 404}