Skip to content

Commit 1d6cc2d

Browse files
[REF] Use function to getContributionRecurID
We are simply fetching it from the POST so it's cheap to fetch & we can stop passing it around once we are using a function, not a variable. This also changes the debug logging slightly to pass the values more correctly to log->debug().
1 parent 3a86f5a commit 1d6cc2d

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

CRM/Core/Payment/PayPalIPN.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function single($input, $ids, $contribution, $recur = FALSE) {
224224
return;
225225
}
226226

227-
CRM_Contribute_BAO_Contribution::completeOrder($input, $ids['contributionRecur'] ?? NULL, $contribution->id ?? NULL);
227+
CRM_Contribute_BAO_Contribution::completeOrder($input, $this->getContributionRecurID(), $contribution->id ?? NULL);
228228
}
229229

230230
/**
@@ -243,7 +243,6 @@ public function main() {
243243
$ids['contact'] = $this->retrieve('contactID', 'Integer', TRUE);
244244
$contributionID = $ids['contribution'] = $this->retrieve('contributionID', 'Integer', TRUE);
245245
$membershipID = $this->retrieve('membershipID', 'Integer', FALSE);
246-
$contributionRecurID = $this->retrieve('contributionRecurID', 'Integer', FALSE);
247246

248247
$this->getInput($input);
249248

@@ -259,13 +258,13 @@ public function main() {
259258
$ids['onbehalf_dupe_alert'] = $this->retrieve('onBehalfDupeAlert', 'Integer', FALSE);
260259
}
261260

262-
$paymentProcessorID = $this->getPayPalPaymentProcessorID($input, $contributionRecurID);
261+
$paymentProcessorID = $this->getPayPalPaymentProcessorID($input, $this->getContributionRecurID());
263262

264263
Civi::log()->debug('PayPalIPN: Received (ContactID: ' . $ids['contact'] . '; trxn_id: ' . $input['trxn_id'] . ').');
265264

266265
// Debugging related to possible missing membership linkage
267-
if ($contributionRecurID && $this->retrieve('membershipID', 'Integer', FALSE)) {
268-
$templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecurID);
266+
if ($this->getContributionRecurID() && $this->retrieve('membershipID', 'Integer', FALSE)) {
267+
$templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($this->getContributionRecurID());
269268
$membershipPayment = civicrm_api3('MembershipPayment', 'get', [
270269
'contribution_id' => $templateContribution['id'],
271270
'membership_id' => $membershipID,
@@ -285,12 +284,12 @@ public function main() {
285284
Civi::log()->debug('PayPalIPN: Will attempt to compensate');
286285
$input['membership_id'] = $this->retrieve('membershipID', 'Integer', FALSE);
287286
}
288-
if ($contributionRecurID) {
287+
if ($this->getContributionRecurID()) {
289288
$recurLinks = civicrm_api3('ContributionRecur', 'get', [
290289
'membership_id' => $membershipID,
291-
'contribution_recur_id' => $contributionRecurID,
290+
'contribution_recur_id' => $this->getContributionRecurID(),
292291
]);
293-
Civi::log()->debug('PayPalIPN: Membership should be linked to contribution recur record ' . $contributionRecurID
292+
Civi::log()->debug('PayPalIPN: Membership should be linked to contribution recur record ' . $this->getContributionRecurID()
294293
. ' ' . $recurLinks['count'] . 'links found'
295294
);
296295
}
@@ -326,13 +325,11 @@ public function main() {
326325

327326
$input['payment_processor_id'] = $paymentProcessorID;
328327

329-
if ($contributionRecurID) {
328+
if ($this->getContributionRecurID()) {
330329
$contributionRecur = new CRM_Contribute_BAO_ContributionRecur();
331-
$contributionRecur->id = $contributionRecurID;
330+
$contributionRecur->id = $this->getContributionRecurID();
332331
if (!$contributionRecur->find(TRUE)) {
333-
CRM_Core_Error::debug_log_message("Could not find contribution recur record: {$ids['ContributionRecur']} in IPN request: " . print_r($input, TRUE));
334-
echo "Failure: Could not find contribution recur record: {$ids['ContributionRecur']}<p>";
335-
return FALSE;
332+
throw new CRM_Core_Exception('Could not find contribution recur record');
336333
}
337334
// check if first contribution is completed, else complete first contribution
338335
$first = TRUE;
@@ -344,7 +341,7 @@ public function main() {
344341
if ($this->getFirstOrLastInSeriesStatus()) {
345342
//send recurring Notification email for user
346343
CRM_Contribute_BAO_ContributionPage::recurringNotify(
347-
$ids['contribution'],
344+
$contributionID,
348345
$this->getFirstOrLastInSeriesStatus(),
349346
$contributionRecur
350347
);
@@ -379,11 +376,11 @@ public function main() {
379376
}
380377
$this->single($input, [
381378
'participant' => $ids['participant'] ?? NULL,
382-
'contributionRecur' => $contributionRecurID,
379+
'contributionRecur' => $this->getContributionRecurID(),
383380
], $contribution);
384381
}
385382
catch (CRM_Core_Exception $e) {
386-
Civi::log()->debug($e->getMessage());
383+
Civi::log()->debug($e->getMessage() . ' input {input}', ['input' => $input]);
387384
echo 'Invalid or missing data';
388385
}
389386
}
@@ -511,4 +508,16 @@ protected function getFirstOrLastInSeriesStatus(): ?string {
511508
return NULL;
512509
}
513510

511+
/**
512+
* Get the recurring contribution ID.
513+
*
514+
* @return int|null
515+
*
516+
* @throws \CRM_Core_Exception
517+
*/
518+
protected function getContributionRecurID(): ?int {
519+
$id = $this->retrieve('contributionRecurID', 'Integer', FALSE);
520+
return $id ? (int) $id : NULL;
521+
}
522+
514523
}

0 commit comments

Comments
 (0)