3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class CRM_Core_Payment_PayPalProIPNTest
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;
25 * IDs of entities created to support the tests.
34 public function 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
,
42 'payment_processor' => $this->_paymentProcessorID
,
44 $this->_contributionPageID
= $contributionPage['id'];
50 public function tearDown() {
51 $this->quickCleanUpFinancialEntities();
55 * Test IPN response updates contribution_recur & contribution for first & second contribution.
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.
60 public function testIPNPaymentRecurSuccess() {
61 $this->setupRecurringPaymentProcessorTransaction();
63 $_GET = $this->getPaypalProRecurTransaction();
64 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction());
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());
75 $contribution = $this->callAPISuccess('contribution', 'get', [
76 'contribution_recur_id' => $this->_contributionRecurID
,
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']);
85 * Test IPN response updates contribution_recur & contribution for first & second contribution.
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());
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());
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
,
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',
116 $this->callAPISuccessGetSingle('line_item', [
117 'contribution_id' => $contribution['values'][1]['id'],
118 'entity_table' => 'civicrm_membership',
120 $this->callAPISuccessGetSingle('membership_payment', ['contribution_id' => $contribution['values'][1]['id']]);
125 * CRM-13743 test IPN edge case where the first transaction fails and the second succeeds.
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)
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);
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());
146 $contribution = $this->callAPISuccess('contribution', 'get', [
147 'contribution_recur_id' => $this->_contributionRecurID
,
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']));
156 * Check a payment express IPN call does not throw any errors.
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
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
171 * Obviously if the behaviour is fixed then the test should be updated!
173 public function testIPNPaymentExpressNoError() {
174 $this->setupRecurringPaymentProcessorTransaction();
175 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressTransactionIPN());
179 catch (CRM_Core_Exception
$e) {
180 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID
]);
182 $this->assertEquals(2, $contribution['contribution_status_id']);
183 $this->assertEquals('Paypal IPNS not handled other than recurring_payments', $e->getMessage());
186 $this->fail('The Paypal Express IPN should have caused an exception');
190 * Get PaymentExpress IPN for a single transaction.
192 * array representing a Paypal IPN POST
194 public function getPaypalExpressTransactionIPN() {
196 'mc_gross' => '200.00',
197 'invoice' => $this->_invoiceID
,
198 'protection_eligibility' => 'Eligible',
199 'address_status' => 'confirmer',
200 'payer_id' => 'ZYXHBZSULPQE3',
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',
209 'address_country_core' => 'US',
210 'address_name' => 'Mary-Jane',
211 'notify_version' => '3.7',
213 'payer_status' => 'unverified',
214 'address_country' => 'UNITED STATES',
215 'address_city' => 'Portland',
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',
228 'mc_currency' => 'USD',
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,
241 * Get IPN results from follow on IPN transactions.
243 * array representing a Paypal IPN POST
245 public function getSubsequentPaypalExpressTransaction() {
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',
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',
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',
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',
300 * Get IPN style details for an incoming recurring transaction.
302 public function getPaypalProRecurTransaction() {
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',
337 'time_created' => '14 => 51 => 55 Feb 14, 2013 PST',
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',
345 'residence_country' => 'US',
350 * Get IPN-style details for a second incoming transaction.
354 public function getPaypalProRecurSubsequentTransaction() {
355 return array_merge($this->getPaypalProRecurTransaction(), ['txn_id' => 'secondone']);
359 * Test IPN response update for a paypal express profile creation confirmation.
361 public function testIPNPaymentExpressRecurSuccess() {
362 $this->setupRecurringPaymentProcessorTransaction(['processor_id' => '']);
363 $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalExpressRecurSubscriptionConfirmation());
365 $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID
]);
366 $this->assertEquals('I-JW77S1PY2032', $contributionRecur['processor_id']);
370 * Get response consistent with creating a new profile.
374 public function getPaypalExpressRecurSubscriptionConfirmation() {
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',
394 'payer_email' => 'payer@example.com',
395 'first_name' => 'test',
396 'receiver_email' => 'shop@example.com',
397 'payer_id' => 'BWXXXM8111HDS',
399 'shipping' => '0.00',
400 'amount_per_cycle' => '6.00',
401 'profile_status' => 'Active',
402 'charset' => 'windows-1252',
403 'notify_version' => '3.9',
405 'outstanding_balance' => '0.00',
406 'recurring_payment_id' => 'I-JW77S1PY2032',
407 'product_name' => '6 Per 1 month',
408 'ipn_track_id' => '6255554274055',