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 | 11 | |
a201e208 | 12 | use Civi\Api4\Contribution; |
13 | ||
6a488035 TO |
14 | /** |
15 | * | |
16 | * @package CRM | |
ca5cec67 | 17 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
18 | */ |
19 | class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { | |
20 | ||
b5c2afd0 | 21 | /** |
f78c9258 | 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 | |
b5c2afd0 | 25 | */ |
be2fb01f | 26 | protected $_inputParameters = []; |
f78c9258 | 27 | |
28 | /** | |
29 | * Constructor function. | |
30 | * | |
31 | * @param array $inputData | |
32 | * Contents of HTTP REQUEST. | |
33 | * | |
34 | * @throws CRM_Core_Exception | |
35 | */ | |
36 | public function __construct($inputData) { | |
d2803851 | 37 | // CRM-19676 |
0aeed2bc BS |
38 | $params = (!empty($inputData['custom'])) ? |
39 | array_merge($inputData, json_decode($inputData['custom'], TRUE)) : | |
40 | $inputData; | |
41 | $this->setInputParameters($params); | |
6a488035 TO |
42 | parent::__construct(); |
43 | } | |
44 | ||
6c786a9b | 45 | /** |
100fef9d | 46 | * @param string $name |
d2803851 | 47 | * @param string $type |
6c786a9b EM |
48 | * @param bool $abort |
49 | * | |
50 | * @return mixed | |
d2803851 | 51 | * @throws \CRM_Core_Exception |
6c786a9b | 52 | */ |
f78c9258 | 53 | public function retrieve($name, $type, $abort = TRUE) { |
d2803851 | 54 | $value = CRM_Utils_Type::validate(CRM_Utils_Array::value($name, $this->_inputParameters), $type, FALSE); |
6a488035 | 55 | if ($abort && $value === NULL) { |
d2803851 | 56 | throw new CRM_Core_Exception("PayPalIPN: Could not find an entry for $name"); |
6a488035 TO |
57 | } |
58 | return $value; | |
59 | } | |
60 | ||
6c786a9b | 61 | /** |
d2803851 MW |
62 | * @param array $input |
63 | * @param array $ids | |
f6973d25 | 64 | * @param CRM_Contribute_BAO_ContributionRecur $recur |
65 | * @param CRM_Contribute_BAO_Contribution $contribution | |
d2803851 MW |
66 | * @param bool $first |
67 | * | |
68 | * @return void | |
7a9ab499 | 69 | * |
d2803851 MW |
70 | * @throws \CRM_Core_Exception |
71 | * @throws \CiviCRM_API3_Exception | |
6c786a9b | 72 | */ |
f6973d25 | 73 | public function recur($input, $ids, $recur, $contribution, $first) { |
6a488035 | 74 | if (!isset($input['txnType'])) { |
d2803851 | 75 | Civi::log()->debug('PayPalIPN: Could not find txn_type in input request'); |
6a488035 | 76 | echo "Failure: Invalid parameters<p>"; |
d2803851 | 77 | return; |
6a488035 TO |
78 | } |
79 | ||
729245ca | 80 | if ($input['txnType'] === 'subscr_payment' && |
81 | $input['paymentStatus'] !== 'Completed' | |
6a488035 | 82 | ) { |
d2803851 | 83 | Civi::log()->debug('PayPalIPN: Ignore all IPN payments that are not completed'); |
729245ca | 84 | echo 'Failure: Invalid parameters<p>'; |
d2803851 | 85 | return; |
6a488035 TO |
86 | } |
87 | ||
6a488035 TO |
88 | // make sure the invoice ids match |
89 | // make sure the invoice is valid and matches what we have in the contribution record | |
90 | if ($recur->invoice_id != $input['invoice']) { | |
d2803851 | 91 | Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request (RecurID: ' . $recur->id . ').'); |
3a317d6d | 92 | throw new CRM_Core_Exception("Failure: Invoice values dont match between database and IPN request"); |
6a488035 TO |
93 | } |
94 | ||
95 | $now = date('YmdHis'); | |
96 | ||
d2803851 | 97 | // set transaction type |
7b7ccbe7 | 98 | $txnType = $this->getTrxnType(); |
d2803851 | 99 | $contributionStatuses = array_flip(CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate')); |
6a488035 TO |
100 | switch ($txnType) { |
101 | case 'subscr_signup': | |
102 | $recur->create_date = $now; | |
d2803851 MW |
103 | // sometimes subscr_signup response come after the subscr_payment and set to pending mode. |
104 | ||
6a488035 TO |
105 | $statusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', |
106 | $recur->id, 'contribution_status_id' | |
107 | ); | |
d2803851 MW |
108 | if ($statusID != $contributionStatuses['In Progress']) { |
109 | $recur->contribution_status_id = $contributionStatuses['Pending']; | |
6a488035 | 110 | } |
f78c9258 | 111 | $recur->processor_id = $this->retrieve('subscr_id', 'String'); |
6a488035 | 112 | $recur->trxn_id = $recur->processor_id; |
6a488035 TO |
113 | break; |
114 | ||
115 | case 'subscr_eot': | |
d2803851 MW |
116 | if ($recur->contribution_status_id != $contributionStatuses['Cancelled']) { |
117 | $recur->contribution_status_id = $contributionStatuses['Completed']; | |
6a488035 TO |
118 | } |
119 | $recur->end_date = $now; | |
6a488035 TO |
120 | break; |
121 | ||
122 | case 'subscr_cancel': | |
d2803851 | 123 | $recur->contribution_status_id = $contributionStatuses['Cancelled']; |
6a488035 TO |
124 | $recur->cancel_date = $now; |
125 | break; | |
126 | ||
127 | case 'subscr_failed': | |
d2803851 | 128 | $recur->contribution_status_id = $contributionStatuses['Failed']; |
6a488035 TO |
129 | $recur->modified_date = $now; |
130 | break; | |
131 | ||
132 | case 'subscr_modify': | |
d2803851 | 133 | Civi::log()->debug('PayPalIPN: We do not handle modifications to subscriptions right now (RecurID: ' . $recur->id . ').'); |
6a488035 | 134 | echo "Failure: We do not handle modifications to subscriptions right now<p>"; |
d2803851 | 135 | return; |
6a488035 TO |
136 | |
137 | case 'subscr_payment': | |
138 | if ($first) { | |
139 | $recur->start_date = $now; | |
140 | } | |
141 | else { | |
142 | $recur->modified_date = $now; | |
143 | } | |
144 | ||
145 | // make sure the contribution status is not done | |
146 | // since order of ipn's is unknown | |
d2803851 MW |
147 | if ($recur->contribution_status_id != $contributionStatuses['Completed']) { |
148 | $recur->contribution_status_id = $contributionStatuses['In Progress']; | |
6a488035 TO |
149 | } |
150 | break; | |
151 | } | |
152 | ||
153 | $recur->save(); | |
154 | ||
3cf0a9be | 155 | if ($txnType !== 'subscr_payment') { |
6a488035 TO |
156 | return; |
157 | } | |
158 | ||
159 | if (!$first) { | |
d2803851 MW |
160 | // check if this contribution transaction is already processed |
161 | // if not create a contribution and then get it processed | |
6a488035 | 162 | $contribution = new CRM_Contribute_BAO_Contribution(); |
bc66bc9e PJ |
163 | $contribution->trxn_id = $input['trxn_id']; |
164 | if ($contribution->trxn_id && $contribution->find()) { | |
d2803851 | 165 | Civi::log()->debug('PayPalIPN: Returning since contribution has already been handled (trxn_id: ' . $contribution->trxn_id . ')'); |
bc66bc9e | 166 | echo "Success: Contribution has already been handled<p>"; |
d2803851 MW |
167 | return; |
168 | } | |
169 | ||
3cf0a9be | 170 | if ($input['paymentStatus'] !== 'Completed') { |
d2803851 | 171 | throw new CRM_Core_Exception("Ignore all IPN payments that are not completed"); |
bc66bc9e PJ |
172 | } |
173 | ||
f5bc4a2d MW |
174 | // In future moving to create pending & then complete, but this OK for now. |
175 | // Also consider accepting 'Failed' like other processors. | |
176 | $input['contribution_status_id'] = $contributionStatuses['Completed']; | |
f5bc4a2d MW |
177 | $input['original_contribution_id'] = $ids['contribution']; |
178 | $input['contribution_recur_id'] = $ids['contributionRecur']; | |
179 | ||
180 | civicrm_api3('Contribution', 'repeattransaction', $input); | |
181 | return; | |
6a488035 TO |
182 | } |
183 | ||
9b344b95 | 184 | $this->single($input, [ |
85d32733 | 185 | 'participant' => $ids['participant'] ?? NULL, |
186 | 'contributionRecur' => $recur->id, | |
f6973d25 | 187 | ], $contribution, TRUE); |
6a488035 TO |
188 | } |
189 | ||
6c786a9b | 190 | /** |
d2803851 MW |
191 | * @param array $input |
192 | * @param array $ids | |
667af247 | 193 | * @param \CRM_Contribute_BAO_Contribution $contribution |
6c786a9b | 194 | * @param bool $recur |
7a9ab499 | 195 | * |
d2803851 | 196 | * @return void |
84801709 | 197 | * @throws \CRM_Core_Exception |
198 | * @throws \CiviCRM_API3_Exception | |
6c786a9b | 199 | */ |
667af247 | 200 | public function single($input, $ids, $contribution, $recur = FALSE) { |
6a488035 TO |
201 | |
202 | // make sure the invoice is valid and matches what we have in the contribution record | |
175f6d95 | 203 | if ($contribution->invoice_id != $input['invoice']) { |
204 | Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request. (ID: ' . $contribution->id . ').'); | |
205 | echo "Failure: Invoice values dont match between database and IPN request<p>"; | |
206 | return; | |
6a488035 TO |
207 | } |
208 | ||
209 | if (!$recur) { | |
210 | if ($contribution->total_amount != $input['amount']) { | |
d2803851 | 211 | Civi::log()->debug('PayPalIPN: Amount values dont match between database and IPN request. (ID: ' . $contribution->id . ').'); |
6a488035 | 212 | echo "Failure: Amount values dont match between database and IPN request<p>"; |
d2803851 | 213 | return; |
6a488035 TO |
214 | } |
215 | } | |
216 | else { | |
217 | $contribution->total_amount = $input['amount']; | |
218 | } | |
219 | ||
6a488035 | 220 | // check if contribution is already completed, if so we ignore this ipn |
4a413eb6 | 221 | $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
d2803851 | 222 | if ($contribution->contribution_status_id == $completedStatusId) { |
d2803851 | 223 | Civi::log()->debug('PayPalIPN: Returning since contribution has already been handled. (ID: ' . $contribution->id . ').'); |
85ce114d | 224 | echo 'Success: Contribution has already been handled<p>'; |
d2803851 | 225 | return; |
6a488035 TO |
226 | } |
227 | ||
667af247 | 228 | CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $contribution); |
6a488035 TO |
229 | } |
230 | ||
2e2605fe EM |
231 | /** |
232 | * Main function. | |
233 | * | |
61470a1a | 234 | * @throws \API_Exception |
d2803851 | 235 | * @throws \CiviCRM_API3_Exception |
61470a1a | 236 | * @throws \Civi\API\Exception\UnauthorizedException |
2e2605fe | 237 | */ |
00be9182 | 238 | public function main() { |
cd0f2a34 | 239 | try { |
fcc57b56 | 240 | $ids = $input = []; |
cd0f2a34 | 241 | $component = $this->retrieve('module', 'String'); |
242 | $input['component'] = $component; | |
6a488035 | 243 | |
cd0f2a34 | 244 | $ids['contact'] = $this->retrieve('contactID', 'Integer', TRUE); |
245 | $contributionID = $ids['contribution'] = $this->retrieve('contributionID', 'Integer', TRUE); | |
246 | $membershipID = $this->retrieve('membershipID', 'Integer', FALSE); | |
247 | $contributionRecurID = $this->retrieve('contributionRecurID', 'Integer', FALSE); | |
6a488035 | 248 | |
cd0f2a34 | 249 | $this->getInput($input); |
6a488035 | 250 | |
cd0f2a34 | 251 | if ($component == 'event') { |
252 | $ids['event'] = $this->retrieve('eventID', 'Integer', TRUE); | |
253 | $ids['participant'] = $this->retrieve('participantID', 'Integer', TRUE); | |
fefee636 | 254 | } |
cd0f2a34 | 255 | else { |
256 | // get the optional ids | |
257 | $ids['membership'] = $membershipID; | |
258 | $ids['contributionRecur'] = $contributionRecurID; | |
259 | $ids['contributionPage'] = $this->retrieve('contributionPageID', 'Integer', FALSE); | |
260 | $ids['related_contact'] = $this->retrieve('relatedContactID', 'Integer', FALSE); | |
261 | $ids['onbehalf_dupe_alert'] = $this->retrieve('onBehalfDupeAlert', 'Integer', FALSE); | |
262 | } | |
263 | ||
264 | $paymentProcessorID = $this->getPayPalPaymentProcessorID($input, $ids); | |
265 | ||
266 | Civi::log()->debug('PayPalIPN: Received (ContactID: ' . $ids['contact'] . '; trxn_id: ' . $input['trxn_id'] . ').'); | |
267 | ||
268 | // Debugging related to possible missing membership linkage | |
269 | if ($contributionRecurID && $this->retrieve('membershipID', 'Integer', FALSE)) { | |
270 | $templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecurID); | |
271 | $membershipPayment = civicrm_api3('MembershipPayment', 'get', [ | |
272 | 'contribution_id' => $templateContribution['id'], | |
fefee636 | 273 | 'membership_id' => $membershipID, |
fefee636 | 274 | ]); |
cd0f2a34 | 275 | $lineItems = civicrm_api3('LineItem', 'get', [ |
276 | 'contribution_id' => $templateContribution['id'], | |
277 | 'entity_id' => $membershipID, | |
278 | 'entity_table' => 'civicrm_membership', | |
279 | ]); | |
280 | Civi::log()->debug('PayPalIPN: Received payment for membership ' . (int) $membershipID | |
281 | . '. Original contribution was ' . (int) $contributionID . '. The template for this contribution is ' | |
282 | . $templateContribution['id'] . ' it is linked to ' . $membershipPayment['count'] | |
283 | . 'payments for this membership. It has ' . $lineItems['count'] . ' line items linked to this membership.' | |
284 | . ' it is expected the original contribution will be linked by both entities to the membership.' | |
fefee636 | 285 | ); |
cd0f2a34 | 286 | if (empty($membershipPayment['count']) && empty($lineItems['count'])) { |
287 | Civi::log()->debug('PayPalIPN: Will attempt to compensate'); | |
288 | $input['membership_id'] = $this->retrieve('membershipID', 'Integer', FALSE); | |
289 | } | |
290 | if ($contributionRecurID) { | |
291 | $recurLinks = civicrm_api3('ContributionRecur', 'get', [ | |
292 | 'membership_id' => $membershipID, | |
293 | 'contribution_recur_id' => $contributionRecurID, | |
294 | ]); | |
295 | Civi::log()->debug('PayPalIPN: Membership should be linked to contribution recur record ' . $contributionRecurID | |
296 | . ' ' . $recurLinks['count'] . 'links found' | |
297 | ); | |
298 | } | |
299 | } | |
a2921b1f | 300 | $contribution = new CRM_Contribute_BAO_Contribution(); |
301 | $contribution->id = $ids['contribution']; | |
302 | if (!$contribution->find(TRUE)) { | |
303 | throw new CRM_Core_Exception('Failure: Could not find contribution record for ' . (int) $contribution->id, NULL, ['context' => "Could not find contribution record: {$contribution->id} in IPN request: " . print_r($input, TRUE)]); | |
304 | } | |
305 | ||
306 | // make sure contact exists and is valid | |
307 | // use the contact id from the contribution record as the id in the IPN may not be valid anymore. | |
308 | $contact = new CRM_Contact_BAO_Contact(); | |
309 | $contact->id = $contribution->contact_id; | |
310 | $contact->find(TRUE); | |
311 | if ($contact->id != $ids['contact']) { | |
312 | // If the ids do not match then it is possible the contact id in the IPN has been merged into another contact which is why we use the contact_id from the contribution | |
313 | CRM_Core_Error::debug_log_message("Contact ID in IPN {$ids['contact']} not found but contact_id found in contribution {$contribution->contact_id} used instead"); | |
314 | echo "WARNING: Could not find contact record: {$ids['contact']}<p>"; | |
315 | $ids['contact'] = $contribution->contact_id; | |
316 | } | |
317 | ||
318 | if (!empty($ids['contributionRecur'])) { | |
319 | $contributionRecur = new CRM_Contribute_BAO_ContributionRecur(); | |
320 | $contributionRecur->id = $ids['contributionRecur']; | |
321 | if (!$contributionRecur->find(TRUE)) { | |
322 | CRM_Core_Error::debug_log_message("Could not find contribution recur record: {$ids['ContributionRecur']} in IPN request: " . print_r($input, TRUE)); | |
323 | echo "Failure: Could not find contribution recur record: {$ids['ContributionRecur']}<p>"; | |
324 | return FALSE; | |
325 | } | |
326 | } | |
327 | ||
a2921b1f | 328 | // CRM-19478: handle oddity when p=null is set in place of contribution page ID, |
329 | if (!empty($ids['contributionPage']) && !is_numeric($ids['contributionPage'])) { | |
330 | // We don't need to worry if about removing contribution page id as it will be set later in | |
331 | // CRM_Contribute_BAO_Contribution::loadRelatedObjects(..) using $objects['contribution']->contribution_page_id | |
332 | unset($ids['contributionPage']); | |
333 | } | |
b5f812f7 | 334 | $ids['paymentProcessor'] = $paymentProcessorID; |
335 | if (!$contribution->loadRelatedObjects($input, $ids)) { | |
336 | return; | |
337 | } | |
6a488035 | 338 | |
dc65872a MW |
339 | $input['payment_processor_id'] = $paymentProcessorID; |
340 | ||
e813005b | 341 | if (!empty($ids['contributionRecur'])) { |
342 | // check if first contribution is completed, else complete first contribution | |
343 | $first = TRUE; | |
344 | $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); | |
fcc57b56 | 345 | if ($contribution->contribution_status_id == $completedStatusId) { |
e813005b | 346 | $first = FALSE; |
6a488035 | 347 | } |
fcc57b56 | 348 | $this->recur($input, $ids, $contributionRecur, $contribution, $first); |
3a317d6d | 349 | if ($this->getFirstOrLastInSeriesStatus()) { |
350 | //send recurring Notification email for user | |
351 | CRM_Contribute_BAO_ContributionPage::recurringNotify($this->getFirstOrLastInSeriesStatus(), | |
352 | $ids['contact'], | |
353 | $ids['contributionPage'], | |
354 | $contributionRecur, | |
355 | !empty($ids['membership']) | |
356 | ); | |
357 | } | |
e813005b | 358 | return; |
6a488035 | 359 | } |
e813005b | 360 | |
a83a1f47 | 361 | $status = $input['paymentStatus']; |
362 | if ($status === 'Denied' || $status === 'Failed' || $status === 'Voided') { | |
61470a1a | 363 | Contribution::update(FALSE)->setValues([ |
364 | 'cancel_date' => 'now', | |
365 | 'contribution_status_id:name' => 'Failed', | |
366 | ])->addWhere('id', '=', $contributionID)->execute(); | |
367 | Civi::log()->debug("Setting contribution status to Failed"); | |
a83a1f47 | 368 | return; |
369 | } | |
370 | if ($status === 'Pending') { | |
371 | Civi::log()->debug('Returning since contribution status is Pending'); | |
372 | return; | |
373 | } | |
374 | if ($status === 'Refunded' || $status === 'Reversed') { | |
a201e208 | 375 | Contribution::update(FALSE)->setValues([ |
376 | 'cancel_date' => 'now', | |
377 | 'contribution_status_id:name' => 'Cancelled', | |
378 | ])->addWhere('id', '=', $contributionID)->execute(); | |
379 | Civi::log()->debug("Setting contribution status to Cancelled"); | |
a83a1f47 | 380 | return; |
381 | } | |
382 | if ($status !== 'Completed') { | |
383 | Civi::log()->debug('Returning since contribution status is not handled'); | |
384 | return; | |
385 | } | |
9b344b95 | 386 | $this->single($input, [ |
0d74d91c | 387 | 'participant' => $ids['participant'] ?? NULL, |
85d32733 | 388 | 'contributionRecur' => $contributionRecurID, |
fcc57b56 | 389 | ], $contribution); |
cd0f2a34 | 390 | } |
391 | catch (CRM_Core_Exception $e) { | |
392 | Civi::log()->debug($e->getMessage()); | |
393 | echo 'Invalid or missing data'; | |
6a488035 | 394 | } |
6a488035 TO |
395 | } |
396 | ||
6c786a9b | 397 | /** |
d2803851 | 398 | * @param array $input |
7a9ab499 | 399 | * |
d2803851 | 400 | * @throws \CRM_Core_Exception |
6c786a9b | 401 | */ |
41ce57e7 | 402 | public function getInput(&$input) { |
403 | $billingID = CRM_Core_BAO_LocationType::getBilling(); | |
f78c9258 | 404 | $input['txnType'] = $this->retrieve('txn_type', 'String', FALSE); |
405 | $input['paymentStatus'] = $this->retrieve('payment_status', 'String', FALSE); | |
406 | $input['invoice'] = $this->retrieve('invoice', 'String', TRUE); | |
407 | $input['amount'] = $this->retrieve('mc_gross', 'Money', FALSE); | |
408 | $input['reasonCode'] = $this->retrieve('ReasonCode', 'String', FALSE); | |
6a488035 | 409 | |
be2fb01f | 410 | $lookup = [ |
6a488035 TO |
411 | "first_name" => 'first_name', |
412 | "last_name" => 'last_name', | |
413 | "street_address-{$billingID}" => 'address_street', | |
414 | "city-{$billingID}" => 'address_city', | |
415 | "state-{$billingID}" => 'address_state', | |
416 | "postal_code-{$billingID}" => 'address_zip', | |
417 | "country-{$billingID}" => 'address_country_code', | |
be2fb01f | 418 | ]; |
6a488035 | 419 | foreach ($lookup as $name => $paypalName) { |
f78c9258 | 420 | $value = $this->retrieve($paypalName, 'String', FALSE); |
6a488035 TO |
421 | $input[$name] = $value ? $value : NULL; |
422 | } | |
423 | ||
f78c9258 | 424 | $input['is_test'] = $this->retrieve('test_ipn', 'Integer', FALSE); |
425 | $input['fee_amount'] = $this->retrieve('mc_fee', 'Money', FALSE); | |
426 | $input['net_amount'] = $this->retrieve('settle_amount', 'Money', FALSE); | |
427 | $input['trxn_id'] = $this->retrieve('txn_id', 'String', FALSE); | |
f5bc4a2d MW |
428 | |
429 | $paymentDate = $this->retrieve('payment_date', 'String', FALSE); | |
430 | if (!empty($paymentDate)) { | |
431 | $receiveDateTime = new DateTime($paymentDate); | |
6800eef1 JGJ |
432 | /** |
433 | * The `payment_date` that Paypal sends back is in their timezone. Example return: 08:23:05 Jan 11, 2019 PST | |
434 | * Subsequently, we need to account for that, otherwise the recieve time will be incorrect for the local system | |
435 | */ | |
602836ee | 436 | $input['receive_date'] = CRM_Utils_Date::convertDateToLocalTime($receiveDateTime); |
f5bc4a2d MW |
437 | } |
438 | } | |
439 | ||
f5bc4a2d MW |
440 | /** |
441 | * Gets PaymentProcessorID for PayPal | |
442 | * | |
443 | * @param array $input | |
444 | * @param array $ids | |
445 | * | |
446 | * @return int | |
447 | * @throws \CRM_Core_Exception | |
448 | * @throws \CiviCRM_API3_Exception | |
449 | */ | |
450 | public function getPayPalPaymentProcessorID($input, $ids) { | |
451 | // First we try and retrieve from POST params | |
452 | $paymentProcessorID = $this->retrieve('processor_id', 'Integer', FALSE); | |
453 | if (!empty($paymentProcessorID)) { | |
454 | return $paymentProcessorID; | |
455 | } | |
456 | ||
457 | // Then we try and get it from recurring contribution ID | |
458 | if (!empty($ids['contributionRecur'])) { | |
be2fb01f | 459 | $contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', [ |
f5bc4a2d MW |
460 | 'id' => $ids['contributionRecur'], |
461 | 'return' => ['payment_processor_id'], | |
be2fb01f | 462 | ]); |
f5bc4a2d MW |
463 | if (!empty($contributionRecur['payment_processor_id'])) { |
464 | return $contributionRecur['payment_processor_id']; | |
465 | } | |
466 | } | |
467 | ||
468 | // This is an unreliable method as there could be more than one instance. | |
469 | // Recommended approach is to use the civicrm/payment/ipn/xx url where xx is the payment | |
470 | // processor id & the handleNotification function (which should call the completetransaction api & by-pass this | |
471 | // entirely). The only thing the IPN class should really do is extract data from the request, validate it | |
472 | // & call completetransaction or call fail? (which may not exist yet). | |
473 | ||
474 | Civi::log()->warning('Unreliable method used to get payment_processor_id for PayPal IPN - this will cause problems if you have more than one instance'); | |
475 | // Then we try and retrieve based on business email ID | |
476 | $paymentProcessorTypeID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', 'PayPal_Standard', 'id', 'name'); | |
477 | $processorParams = [ | |
478 | 'user_name' => $this->retrieve('business', 'String', FALSE), | |
479 | 'payment_processor_type_id' => $paymentProcessorTypeID, | |
480 | 'is_test' => empty($input['is_test']) ? 0 : 1, | |
481 | 'options' => ['limit' => 1], | |
482 | 'return' => ['id'], | |
483 | ]; | |
484 | $paymentProcessorID = civicrm_api3('PaymentProcessor', 'getvalue', $processorParams); | |
485 | if (empty($paymentProcessorID)) { | |
518fa0ee | 486 | throw new CRM_Core_Exception('PayPalIPN: Could not get Payment Processor ID'); |
f5bc4a2d MW |
487 | } |
488 | return $paymentProcessorID; | |
6a488035 | 489 | } |
96025800 | 490 | |
7b7ccbe7 | 491 | /** |
492 | * @return mixed | |
493 | * @throws \CRM_Core_Exception | |
494 | */ | |
495 | protected function getTrxnType() { | |
496 | $txnType = $this->retrieve('txn_type', 'String'); | |
497 | return $txnType; | |
498 | } | |
499 | ||
500 | /** | |
501 | * Get status code for first or last recurring in the series. | |
502 | * | |
503 | * If this is the first or last then return the status code, else | |
504 | * null. | |
505 | * | |
506 | * @return string|null | |
507 | * @throws \CRM_Core_Exception | |
508 | */ | |
509 | protected function getFirstOrLastInSeriesStatus(): ?string { | |
510 | $subscriptionPaymentStatus = NULL; | |
511 | if ($this->getTrxnType() === 'subscr_signup') { | |
512 | return CRM_Core_Payment::RECURRING_PAYMENT_START; | |
513 | } | |
514 | if ($this->getTrxnType() === 'subscr_eot') { | |
515 | return CRM_Core_Payment::RECURRING_PAYMENT_END; | |
516 | } | |
517 | return NULL; | |
518 | } | |
519 | ||
6a488035 | 520 | } |