Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Core_Payment_PayPalProIPN extends CRM_Core_Payment_BaseIPN { | |
34 | ||
518fa0ee | 35 | public static $_paymentProcessor = NULL; |
c8aa607b | 36 | |
37 | /** | |
38 | * Input parameters from payment processor. Store these so that | |
39 | * the code does not need to keep retrieving from the http request | |
40 | * @var array | |
41 | */ | |
be2fb01f | 42 | protected $_inputParameters = []; |
c8aa607b | 43 | |
44 | /** | |
fe482240 | 45 | * Store for the variables from the invoice string. |
c8aa607b | 46 | * @var array |
47 | */ | |
be2fb01f | 48 | protected $_invoiceData = []; |
c8aa607b | 49 | |
50 | /** | |
fe482240 | 51 | * Is this a payment express transaction. |
518fa0ee | 52 | * @var bool |
c8aa607b | 53 | */ |
54 | protected $_isPaymentExpress = FALSE; | |
55 | ||
56 | /** | |
57 | * Are we dealing with an event an 'anything else' (contribute) | |
58 | * @var string component | |
59 | */ | |
60 | protected $_component = 'contribute'; | |
0dbefed3 | 61 | |
c8aa607b | 62 | /** |
fe482240 | 63 | * Constructor function. |
0dbefed3 | 64 | * |
6a0b768e TO |
65 | * @param array $inputData |
66 | * Contents of HTTP REQUEST. | |
0dbefed3 EM |
67 | * |
68 | * @throws CRM_Core_Exception | |
c8aa607b | 69 | */ |
00be9182 | 70 | public function __construct($inputData) { |
c8aa607b | 71 | $this->setInputParameters($inputData); |
72 | $this->setInvoiceData(); | |
6a488035 TO |
73 | parent::__construct(); |
74 | } | |
75 | ||
c8aa607b | 76 | /** |
fe482240 | 77 | * get the values from the rp_invoice_id string. |
77b97be7 | 78 | * |
6a0b768e TO |
79 | * @param string $name |
80 | * E.g. i, values are stored in the string with letter codes. | |
81 | * @param bool $abort | |
16b10e64 | 82 | * Throw exception if not found |
77b97be7 EM |
83 | * |
84 | * @throws CRM_Core_Exception | |
16b10e64 | 85 | * @return mixed |
c8aa607b | 86 | */ |
00be9182 | 87 | public function getValue($name, $abort = TRUE) { |
c8aa607b | 88 | if ($abort && empty($this->_invoiceData[$name])) { |
89 | throw new CRM_Core_Exception("Failure: Missing Parameter $name"); | |
6a488035 TO |
90 | } |
91 | else { | |
c8aa607b | 92 | return CRM_Utils_Array::value($name, $this->_invoiceData); |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Set $this->_invoiceData from the input array | |
98 | */ | |
00be9182 | 99 | public function setInvoiceData() { |
22e263ad | 100 | if (empty($this->_inputParameters['rp_invoice_id'])) { |
c8aa607b | 101 | $this->_isPaymentExpress = TRUE; |
102 | return; | |
103 | } | |
104 | $rpInvoiceArray = explode('&', $this->_inputParameters['rp_invoice_id']); | |
105 | // for clarify let's also store without the single letter unreadable | |
106 | //@todo after more refactoring we might ditch storing the one letter stuff | |
be2fb01f | 107 | $mapping = [ |
c8aa607b | 108 | 'i' => 'invoice_id', |
109 | 'm' => 'component', | |
110 | 'c' => 'contact_id', | |
111 | 'b' => 'contribution_id', | |
112 | 'r' => 'contribution_recur_id', | |
113 | 'p' => 'participant_id', | |
114 | 'e' => 'event_id', | |
be2fb01f | 115 | ]; |
c8aa607b | 116 | foreach ($rpInvoiceArray as $rpInvoiceValue) { |
117 | $rpValueArray = explode('=', $rpInvoiceValue); | |
118 | $this->_invoiceData[$rpValueArray[0]] = $rpValueArray[1]; | |
119 | $this->_inputParameters[$mapping[$rpValueArray[0]]] = $rpValueArray[1]; | |
120 | // p has been overloaded & could mean contribution page or participant id. Clearly we need an | |
121 | // alphabet with more letters. | |
122 | // the mode will always be resolved before the mystery p is reached | |
22e263ad | 123 | if ($rpValueArray[1] == 'contribute') { |
c8aa607b | 124 | $mapping['p'] = 'contribution_page_id'; |
125 | } | |
6a488035 | 126 | } |
22e263ad | 127 | if (empty($this->_inputParameters['component'])) { |
949babe8 JM |
128 | $this->_isPaymentExpress = TRUE; |
129 | } | |
6a488035 TO |
130 | } |
131 | ||
c8aa607b | 132 | /** |
6a0b768e TO |
133 | * @param string $name |
134 | * Of variable to return. | |
135 | * @param string $type | |
136 | * Data type. | |
c8aa607b | 137 | * - String |
138 | * - Integer | |
6a0b768e TO |
139 | * @param string $location |
140 | * Deprecated. | |
141 | * @param bool $abort | |
142 | * Abort if empty. | |
77b97be7 EM |
143 | * |
144 | * @throws CRM_Core_Exception | |
72b3a70c | 145 | * @return mixed |
c8aa607b | 146 | */ |
00be9182 | 147 | public function retrieve($name, $type, $location = 'POST', $abort = TRUE) { |
c8aa607b | 148 | $value = CRM_Utils_Type::validate( |
149 | CRM_Utils_Array::value($name, $this->_inputParameters), | |
150 | $type, | |
151 | FALSE | |
6a488035 TO |
152 | ); |
153 | if ($abort && $value === NULL) { | |
c8aa607b | 154 | throw new CRM_Core_Exception("Could not find an entry for $name in $location"); |
6a488035 TO |
155 | } |
156 | return $value; | |
157 | } | |
158 | ||
c8aa607b | 159 | /** |
fe482240 | 160 | * Process recurring contributions. |
c8aa607b | 161 | * @param array $input |
162 | * @param array $ids | |
163 | * @param array $objects | |
6a0b768e | 164 | * @param bool $first |
d2803851 | 165 | * @return void |
c8aa607b | 166 | */ |
00be9182 | 167 | public function recur(&$input, &$ids, &$objects, $first) { |
6a488035 | 168 | if (!isset($input['txnType'])) { |
d2803851 | 169 | Civi::log()->debug('PayPalProIPN: Could not find txn_type in input request.'); |
6a488035 | 170 | echo "Failure: Invalid parameters<p>"; |
d2803851 | 171 | return; |
6a488035 TO |
172 | } |
173 | ||
6a488035 TO |
174 | $recur = &$objects['contributionRecur']; |
175 | ||
176 | // make sure the invoice ids match | |
177 | // make sure the invoice is valid and matches what we have in | |
178 | // the contribution record | |
179 | if ($recur->invoice_id != $input['invoice']) { | |
d2803851 | 180 | Civi::log()->debug('PayPalProIPN: Invoice values dont match between database and IPN request recur is ' . $recur->invoice_id . ' input is ' . $input['invoice']); |
6a488035 | 181 | echo "Failure: Invoice values dont match between database and IPN request recur is " . $recur->invoice_id . " input is " . $input['invoice']; |
d2803851 | 182 | return; |
6a488035 TO |
183 | } |
184 | ||
185 | $now = date('YmdHis'); | |
186 | ||
187 | // fix dates that already exist | |
be2fb01f | 188 | $dates = ['create', 'start', 'end', 'cancel', 'modified']; |
6a488035 TO |
189 | foreach ($dates as $date) { |
190 | $name = "{$date}_date"; | |
191 | if ($recur->$name) { | |
192 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
193 | } | |
194 | } | |
195 | ||
196 | $sendNotification = FALSE; | |
197 | $subscriptionPaymentStatus = NULL; | |
198 | //List of Transaction Type | |
199 | /* | |
e70a7fc0 | 200 | recurring_payment_profile_created RP Profile Created |
b44e3f84 | 201 | recurring_payment RP Successful Payment |
e70a7fc0 TO |
202 | recurring_payment_failed RP Failed Payment |
203 | recurring_payment_profile_cancel RP Profile Cancelled | |
204 | recurring_payment_expired RP Profile Expired | |
205 | recurring_payment_skipped RP Profile Skipped | |
b44e3f84 | 206 | recurring_payment_outstanding_payment RP Successful Outstanding Payment |
e70a7fc0 TO |
207 | recurring_payment_outstanding_payment_failed RP Failed Outstanding Payment |
208 | recurring_payment_suspended RP Profile Suspended | |
209 | recurring_payment_suspended_due_to_max_failed_payment RP Profile Suspended due to Max Failed Payment | |
210 | */ | |
6a488035 | 211 | |
6a488035 | 212 | //set transaction type |
c8aa607b | 213 | $txnType = $this->retrieve('txn_type', 'String'); |
6a488035 | 214 | //Changes for paypal pro recurring payment |
d2803851 | 215 | $contributionStatuses = array_flip(CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate')); |
6a488035 TO |
216 | switch ($txnType) { |
217 | case 'recurring_payment_profile_created': | |
be2fb01f | 218 | if (in_array($recur->contribution_status_id, [ |
518fa0ee SL |
219 | $contributionStatuses['Pending'], |
220 | $contributionStatuses['In Progress'], | |
221 | ]) | |
353ffa53 TO |
222 | && !empty($recur->processor_id) |
223 | ) { | |
e25b58d1 | 224 | echo "already handled"; |
d2803851 | 225 | return; |
e25b58d1 | 226 | } |
6a488035 | 227 | $recur->create_date = $now; |
d2803851 | 228 | $recur->contribution_status_id = $contributionStatuses['Pending']; |
d57577d4 | 229 | $recur->processor_id = $this->retrieve('recurring_payment_id', 'String'); |
6a488035 TO |
230 | $recur->trxn_id = $recur->processor_id; |
231 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
232 | $sendNotification = TRUE; | |
233 | break; | |
234 | ||
235 | case 'recurring_payment': | |
236 | if ($first) { | |
237 | $recur->start_date = $now; | |
238 | } | |
239 | else { | |
5b51381e | 240 | if ($input['paymentStatus'] != 'Completed') { |
241 | throw new CRM_Core_Exception("Ignore all IPN payments that are not completed"); | |
242 | } | |
b3924856 | 243 | |
5b51381e | 244 | // In future moving to create pending & then complete, but this OK for now. |
245 | // Also consider accepting 'Failed' like other processors. | |
b3924856 MWMC |
246 | $input['contribution_status_id'] = $contributionStatuses['Completed']; |
247 | $input['invoice_id'] = md5(uniqid(rand(), TRUE)); | |
248 | $input['original_contribution_id'] = $ids['contribution']; | |
249 | $input['contribution_recur_id'] = $ids['contributionRecur']; | |
5b51381e | 250 | |
251 | civicrm_api3('Contribution', 'repeattransaction', $input); | |
252 | return; | |
6a488035 TO |
253 | } |
254 | ||
255 | //contribution installment is completed | |
c8aa607b | 256 | if ($this->retrieve('profile_status', 'String') == 'Expired') { |
22e263ad | 257 | if (!empty($recur->end_date)) { |
e25b58d1 | 258 | echo "already handled"; |
d2803851 | 259 | return; |
e25b58d1 | 260 | } |
d2803851 | 261 | $recur->contribution_status_id = $contributionStatuses['Completed']; |
6a488035 TO |
262 | $recur->end_date = $now; |
263 | $sendNotification = TRUE; | |
264 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; | |
265 | } | |
266 | ||
267 | // make sure the contribution status is not done | |
268 | // since order of ipn's is unknown | |
d2803851 MW |
269 | if ($recur->contribution_status_id != $contributionStatuses['Completed']) { |
270 | $recur->contribution_status_id = $contributionStatuses['In Progress']; | |
6a488035 TO |
271 | } |
272 | break; | |
273 | } | |
274 | ||
275 | $recur->save(); | |
276 | ||
277 | if ($sendNotification) { | |
278 | $autoRenewMembership = FALSE; | |
279 | if ($recur->id && | |
280 | isset($ids['membership']) && $ids['membership'] | |
281 | ) { | |
282 | $autoRenewMembership = TRUE; | |
283 | } | |
284 | //send recurring Notification email for user | |
285 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
286 | $ids['contact'], | |
287 | $ids['contributionPage'], | |
288 | $recur, | |
289 | $autoRenewMembership | |
290 | ); | |
291 | } | |
292 | ||
293 | if ($txnType != 'recurring_payment') { | |
d2803851 | 294 | return; |
6a488035 TO |
295 | } |
296 | ||
297 | if (!$first) { | |
bc66bc9e PJ |
298 | //check if this contribution transaction is already processed |
299 | //if not create a contribution and then get it processed | |
6a488035 | 300 | $contribution = new CRM_Contribute_BAO_Contribution(); |
bc66bc9e PJ |
301 | $contribution->trxn_id = $input['trxn_id']; |
302 | if ($contribution->trxn_id && $contribution->find()) { | |
d2803851 | 303 | Civi::log()->debug('PayPalProIPN: Returning since contribution has already been handled.'); |
bc66bc9e | 304 | echo "Success: Contribution has already been handled<p>"; |
d2803851 | 305 | return; |
bc66bc9e PJ |
306 | } |
307 | ||
73004adf | 308 | $contribution->contact_id = $recur->contact_id; |
353ffa53 | 309 | $contribution->financial_type_id = $objects['contributionType']->id; |
6a488035 TO |
310 | $contribution->contribution_page_id = $ids['contributionPage']; |
311 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
6a488035 TO |
312 | $contribution->currency = $objects['contribution']->currency; |
313 | $contribution->payment_instrument_id = $objects['contribution']->payment_instrument_id; | |
314 | $contribution->amount_level = $objects['contribution']->amount_level; | |
94d1bc8d | 315 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
6a488035 | 316 | $objects['contribution'] = &$contribution; |
5b51381e | 317 | $contribution->invoice_id = md5(uniqid(rand(), TRUE)); |
6a488035 | 318 | } |
da88a16b | 319 | // CRM-13737 - am not aware of any reason why payment_date would not be set - this if is a belt & braces |
2aa397bc | 320 | $objects['contribution']->receive_date = !empty($input['payment_date']) ? date('YmdHis', strtotime($input['payment_date'])) : $now; |
6a488035 | 321 | |
d2803851 | 322 | $this->single($input, $ids, $objects, TRUE, $first); |
6a488035 TO |
323 | } |
324 | ||
6c786a9b | 325 | /** |
d2803851 MW |
326 | * @param array $input |
327 | * @param array $ids | |
328 | * @param array $objects | |
6c786a9b EM |
329 | * @param bool $recur |
330 | * @param bool $first | |
7a9ab499 | 331 | * |
d2803851 | 332 | * @return void |
6c786a9b | 333 | */ |
00be9182 | 334 | public function single(&$input, &$ids, &$objects, $recur = FALSE, $first = FALSE) { |
6a488035 TO |
335 | $contribution = &$objects['contribution']; |
336 | ||
337 | // make sure the invoice is valid and matches what we have in the contribution record | |
338 | if ((!$recur) || ($recur && $first)) { | |
339 | if ($contribution->invoice_id != $input['invoice']) { | |
d2803851 | 340 | Civi::log()->debug('PayPalProIPN: Invoice values dont match between database and IPN request.'); |
6a488035 | 341 | echo "Failure: Invoice values dont match between database and IPN request<p>contribution is" . $contribution->invoice_id . " and input is " . $input['invoice']; |
d2803851 | 342 | return; |
6a488035 TO |
343 | } |
344 | } | |
345 | else { | |
346 | $contribution->invoice_id = md5(uniqid(rand(), TRUE)); | |
347 | } | |
348 | ||
349 | if (!$recur) { | |
350 | if ($contribution->total_amount != $input['amount']) { | |
d2803851 | 351 | Civi::log()->debug('PayPalProIPN: Amount values dont match between database and IPN request.'); |
6a488035 | 352 | echo "Failure: Amount values dont match between database and IPN request<p>"; |
d2803851 | 353 | return; |
6a488035 TO |
354 | } |
355 | } | |
356 | else { | |
357 | $contribution->total_amount = $input['amount']; | |
358 | } | |
359 | ||
360 | $transaction = new CRM_Core_Transaction(); | |
361 | ||
6a488035 TO |
362 | $status = $input['paymentStatus']; |
363 | if ($status == 'Denied' || $status == 'Failed' || $status == 'Voided') { | |
d2803851 MW |
364 | $this->failed($objects, $transaction); |
365 | return; | |
6a488035 TO |
366 | } |
367 | elseif ($status == 'Pending') { | |
d2803851 MW |
368 | $this->pending($objects, $transaction); |
369 | return; | |
6a488035 TO |
370 | } |
371 | elseif ($status == 'Refunded' || $status == 'Reversed') { | |
d2803851 MW |
372 | $this->cancelled($objects, $transaction); |
373 | return; | |
6a488035 TO |
374 | } |
375 | elseif ($status != 'Completed') { | |
d2803851 MW |
376 | $this->unhandled($objects, $transaction); |
377 | return; | |
6a488035 TO |
378 | } |
379 | ||
380 | // check if contribution is already completed, if so we ignore this ipn | |
d2803851 MW |
381 | $completedStatusId = CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
382 | if ($contribution->contribution_status_id == $completedStatusId) { | |
6a488035 | 383 | $transaction->commit(); |
d2803851 | 384 | Civi::log()->debug('PayPalProIPN: Returning since contribution has already been handled.'); |
6a488035 | 385 | echo "Success: Contribution has already been handled<p>"; |
d2803851 | 386 | return; |
6a488035 TO |
387 | } |
388 | ||
389 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
390 | } | |
391 | ||
eaf9432c LB |
392 | /** |
393 | * Gets PaymentProcessorID for PayPal | |
394 | * | |
395 | * @return int | |
396 | */ | |
397 | public function getPayPalPaymentProcessorID() { | |
398 | // This is an unreliable method as there could be more than one instance. | |
399 | // Recommended approach is to use the civicrm/payment/ipn/xx url where xx is the payment | |
400 | // processor id & the handleNotification function (which should call the completetransaction api & by-pass this | |
401 | // entirely). The only thing the IPN class should really do is extract data from the request, validate it | |
402 | // & call completetransaction or call fail? (which may not exist yet). | |
d2803851 MW |
403 | |
404 | Civi::log()->warning('Unreliable method used to get payment_processor_id for PayPal Pro IPN - this will cause problems if you have more than one instance'); | |
405 | ||
eaf9432c LB |
406 | $paymentProcessorTypeID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', |
407 | 'PayPal', 'id', 'name' | |
408 | ); | |
be2fb01f | 409 | return (int) civicrm_api3('PaymentProcessor', 'getvalue', [ |
eaf9432c | 410 | 'is_test' => 0, |
be2fb01f | 411 | 'options' => ['limit' => 1], |
eaf9432c LB |
412 | 'payment_processor_type_id' => $paymentProcessorTypeID, |
413 | 'return' => 'id', | |
be2fb01f | 414 | ]); |
eaf9432c LB |
415 | |
416 | } | |
417 | ||
c8aa607b | 418 | /** |
419 | * This is the main function to call. It should be sufficient to instantiate the class | |
420 | * (with the input parameters) & call this & all will be done | |
421 | * | |
422 | * @todo the references to POST throughout this class need to be removed | |
d2803851 | 423 | * @return void |
c8aa607b | 424 | */ |
00be9182 | 425 | public function main() { |
6a488035 TO |
426 | CRM_Core_Error::debug_var('GET', $_GET, TRUE, TRUE); |
427 | CRM_Core_Error::debug_var('POST', $_POST, TRUE, TRUE); | |
22e263ad | 428 | if ($this->_isPaymentExpress) { |
c8aa607b | 429 | $this->handlePaymentExpress(); |
b9bb2511 | 430 | return; |
c8aa607b | 431 | } |
be2fb01f | 432 | $objects = $ids = $input = []; |
353ffa53 | 433 | $this->_component = $input['component'] = self::getValue('m'); |
949babe8 | 434 | $input['invoice'] = self::getValue('i', TRUE); |
6a488035 TO |
435 | // get the contribution and contact ids from the GET params |
436 | $ids['contact'] = self::getValue('c', TRUE); | |
437 | $ids['contribution'] = self::getValue('b', TRUE); | |
438 | ||
439 | $this->getInput($input, $ids); | |
440 | ||
c8aa607b | 441 | if ($this->_component == 'event') { |
6a488035 TO |
442 | $ids['event'] = self::getValue('e', TRUE); |
443 | $ids['participant'] = self::getValue('p', TRUE); | |
444 | $ids['contributionRecur'] = self::getValue('r', FALSE); | |
445 | } | |
446 | else { | |
447 | // get the optional ids | |
c8aa607b | 448 | //@ how can this not be broken retrieving from GET as we are dealing with a POST request? |
449 | // copy & paste? Note the retrieve function now uses data from _REQUEST so this will be included | |
6a488035 TO |
450 | $ids['membership'] = self::retrieve('membershipID', 'Integer', 'GET', FALSE); |
451 | $ids['contributionRecur'] = self::getValue('r', FALSE); | |
452 | $ids['contributionPage'] = self::getValue('p', FALSE); | |
453 | $ids['related_contact'] = self::retrieve('relatedContactID', 'Integer', 'GET', FALSE); | |
454 | $ids['onbehalf_dupe_alert'] = self::retrieve('onBehalfDupeAlert', 'Integer', 'GET', FALSE); | |
455 | } | |
456 | ||
457 | if (!$ids['membership'] && $ids['contributionRecur']) { | |
458 | $sql = " | |
459 | SELECT m.id | |
460 | FROM civicrm_membership m | |
461 | INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = %1 | |
462 | WHERE m.contribution_recur_id = %2 | |
463 | LIMIT 1"; | |
be2fb01f CW |
464 | $sqlParams = [ |
465 | 1 => [$ids['contribution'], 'Integer'], | |
466 | 2 => [$ids['contributionRecur'], 'Integer'], | |
467 | ]; | |
6a488035 TO |
468 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql, $sqlParams)) { |
469 | $ids['membership'] = $membershipId; | |
470 | } | |
471 | } | |
472 | ||
eaf9432c | 473 | $paymentProcessorID = self::getPayPalPaymentProcessorID(); |
6a488035 TO |
474 | |
475 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
d2803851 | 476 | return; |
6a488035 TO |
477 | } |
478 | ||
479 | self::$_paymentProcessor = &$objects['paymentProcessor']; | |
c8aa607b | 480 | //?? how on earth would we not have component be one of these? |
481 | // they are the only valid settings & this IPN file can't even be called without one of them | |
482 | // grepping for this class doesn't find other paths to call this class | |
483 | if ($this->_component == 'contribute' || $this->_component == 'event') { | |
6a488035 TO |
484 | if ($ids['contributionRecur']) { |
485 | // check if first contribution is completed, else complete first contribution | |
486 | $first = TRUE; | |
d2803851 MW |
487 | $completedStatusId = CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
488 | if ($objects['contribution']->contribution_status_id == $completedStatusId) { | |
6a488035 TO |
489 | $first = FALSE; |
490 | } | |
d2803851 MW |
491 | $this->recur($input, $ids, $objects, $first); |
492 | return; | |
6a488035 | 493 | } |
6a488035 | 494 | } |
d2803851 | 495 | $this->single($input, $ids, $objects, FALSE, FALSE); |
6a488035 TO |
496 | } |
497 | ||
6c786a9b | 498 | /** |
d2803851 MW |
499 | * @param array $input |
500 | * @param array $ids | |
6c786a9b | 501 | * |
d2803851 | 502 | * @return void |
6c786a9b EM |
503 | * @throws CRM_Core_Exception |
504 | */ | |
00be9182 | 505 | public function getInput(&$input, &$ids) { |
6a488035 | 506 | if (!$this->getBillingID($ids)) { |
d2803851 | 507 | return; |
6a488035 TO |
508 | } |
509 | ||
510 | $input['txnType'] = self::retrieve('txn_type', 'String', 'POST', FALSE); | |
511 | $input['paymentStatus'] = self::retrieve('payment_status', 'String', 'POST', FALSE); | |
6a488035 TO |
512 | |
513 | $input['amount'] = self::retrieve('mc_gross', 'Money', 'POST', FALSE); | |
514 | $input['reasonCode'] = self::retrieve('ReasonCode', 'String', 'POST', FALSE); | |
515 | ||
516 | $billingID = $ids['billing']; | |
be2fb01f | 517 | $lookup = [ |
6a488035 TO |
518 | "first_name" => 'first_name', |
519 | "last_name" => 'last_name', | |
520 | "street_address-{$billingID}" => 'address_street', | |
521 | "city-{$billingID}" => 'address_city', | |
522 | "state-{$billingID}" => 'address_state', | |
523 | "postal_code-{$billingID}" => 'address_zip', | |
524 | "country-{$billingID}" => 'address_country_code', | |
be2fb01f | 525 | ]; |
6a488035 TO |
526 | foreach ($lookup as $name => $paypalName) { |
527 | $value = self::retrieve($paypalName, 'String', 'POST', FALSE); | |
528 | $input[$name] = $value ? $value : NULL; | |
529 | } | |
530 | ||
353ffa53 | 531 | $input['is_test'] = self::retrieve('test_ipn', 'Integer', 'POST', FALSE); |
6a488035 TO |
532 | $input['fee_amount'] = self::retrieve('mc_fee', 'Money', 'POST', FALSE); |
533 | $input['net_amount'] = self::retrieve('settle_amount', 'Money', 'POST', FALSE); | |
353ffa53 | 534 | $input['trxn_id'] = self::retrieve('txn_id', 'String', 'POST', FALSE); |
12829b5d | 535 | $input['payment_date'] = $input['receive_date'] = self::retrieve('payment_date', 'String', 'POST', FALSE); |
94155403 | 536 | $input['total_amount'] = $input['amount']; |
6a488035 | 537 | } |
c8aa607b | 538 | |
539 | /** | |
fe482240 | 540 | * Handle payment express IPNs. |
6439290e | 541 | * |
c8aa607b | 542 | * For one off IPNS no actual response is required |
543 | * Recurring is more difficult as we have limited confirmation material | |
949babe8 JM |
544 | * lets look up invoice id in recur_contribution & rely on the unique transaction id to ensure no |
545 | * duplicated | |
546 | * this may not be acceptable to all sites - e.g. if they are shipping or delivering something in return | |
547 | * then the quasi security of the ids array might be required - although better to | |
548 | * http://stackoverflow.com/questions/4848227/validate-that-ipn-call-is-from-paypal | |
549 | * but let's assume knowledge on invoice id & schedule is enough for now esp for donations | |
550 | * only contribute is handled | |
c8aa607b | 551 | */ |
00be9182 | 552 | public function handlePaymentExpress() { |
2aa397bc TO |
553 | //@todo - loads of copy & paste / code duplication but as this not going into core need to try to |
554 | // keep discreet | |
555 | // also note that a lot of the complexity above could be removed if we used | |
556 | // http://stackoverflow.com/questions/4848227/validate-that-ipn-call-is-from-paypal | |
557 | // as membership id etc can be derived by the load objects fn | |
be2fb01f | 558 | $objects = $ids = $input = []; |
949babe8 | 559 | $isFirst = FALSE; |
6439290e | 560 | $input['invoice'] = self::getValue('i', FALSE); |
6d8785b9 JP |
561 | //Avoid return in case of unit test. |
562 | if (empty($input['invoice']) && empty($this->_inputParameters['is_unit_test'])) { | |
b9bb2511 JP |
563 | return; |
564 | } | |
353ffa53 | 565 | $input['txnType'] = $this->retrieve('txn_type', 'String'); |
be2fb01f | 566 | $contributionRecur = civicrm_api3('contribution_recur', 'getsingle', [ |
6439290e | 567 | 'return' => 'contact_id, id, payment_processor_id', |
568 | 'invoice_id' => $input['invoice'], | |
be2fb01f | 569 | ]); |
6439290e | 570 | |
571 | if ($input['txnType'] !== 'recurring_payment' && $input['txnType'] !== 'recurring_payment_profile_created') { | |
949babe8 JM |
572 | throw new CRM_Core_Exception('Paypal IPNS not handled other than recurring_payments'); |
573 | } | |
6439290e | 574 | |
949babe8 | 575 | $this->getInput($input, $ids); |
6439290e | 576 | if ($input['txnType'] === 'recurring_payment' && $this->transactionExists($input['trxn_id'])) { |
949babe8 JM |
577 | throw new CRM_Core_Exception('This transaction has already been processed'); |
578 | } | |
579 | ||
949babe8 JM |
580 | $ids['contact'] = $contributionRecur['contact_id']; |
581 | $ids['contributionRecur'] = $contributionRecur['id']; | |
6439290e | 582 | $result = civicrm_api3('contribution', 'getsingle', ['invoice_id' => $input['invoice'], 'contribution_test' => '']); |
949babe8 JM |
583 | |
584 | $ids['contribution'] = $result['id']; | |
d2803851 MW |
585 | //@todo hardcoding 'pending' for now |
586 | $pendingStatusId = CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); | |
587 | if ($result['contribution_status_id'] == $pendingStatusId) { | |
2aa397bc | 588 | $isFirst = TRUE; |
949babe8 JM |
589 | } |
590 | // arg api won't get this - fix it | |
be2fb01f | 591 | $ids['contributionPage'] = CRM_Core_DAO::singleValueQuery("SELECT contribution_page_id FROM civicrm_contribution WHERE invoice_id = %1", [ |
518fa0ee SL |
592 | 1 => [ |
593 | $ids['contribution'], | |
594 | 'Integer', | |
595 | ], | |
596 | ]); | |
949babe8 JM |
597 | // only handle component at this stage - not terribly sure how a recurring event payment would arise |
598 | // & suspec main function may be a victom of copy & paste | |
599 | // membership would be an easy add - but not relevant to my customer... | |
353ffa53 | 600 | $this->_component = $input['component'] = 'contribute'; |
949babe8 | 601 | $input['trxn_date'] = date('Y-m-d-H-i-s', strtotime(self::retrieve('time_created', 'String'))); |
6439290e | 602 | $paymentProcessorID = $contributionRecur['payment_processor_id']; |
949babe8 JM |
603 | |
604 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
605 | throw new CRM_Core_Exception('Data did not validate'); | |
606 | } | |
6439290e | 607 | $this->recur($input, $ids, $objects, $isFirst); |
949babe8 JM |
608 | } |
609 | ||
610 | /** | |
fe482240 | 611 | * Function check if transaction already exists. |
949babe8 | 612 | * @param string $trxn_id |
72b3a70c | 613 | * @return bool|void |
949babe8 | 614 | */ |
00be9182 | 615 | public function transactionExists($trxn_id) { |
22e263ad | 616 | if (CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_contribution WHERE trxn_id = %1", |
be2fb01f CW |
617 | [ |
618 | 1 => [$trxn_id, 'String'], | |
619 | ]) | |
353ffa53 | 620 | ) { |
949babe8 JM |
621 | return TRUE; |
622 | } | |
c8aa607b | 623 | } |
96025800 | 624 | |
6a488035 | 625 | } |