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