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