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