Newer
Older

mattwire
committed
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\Stripe\Webhook;
use Civi\Api4\Contribution;
use CRM_Stripe_ExtensionUtil as E;

mattwire
committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Events {
use \CRM_Core_Payment_MJWIPNTrait;
/**
* @var \CRM_Core_Payment_Stripe Payment processor
*/
private $paymentProcessor;
/**
* @param string $eventID
*
* @return void
*/
public function setEventID(string $eventID): void {
$this->eventID = $eventID;
}
/**
* @param string $eventType
*
* @return void
*/
public function setEventType(string $eventType): void {
$this->eventType = $eventType;
}
/**
* @param \Stripe\StripeObject|\PropertySpy $data
*
* @return void
*/
public function setData($data): void {
$this->data = $data;
}
/**
* @return \stdClass
*/
private function getResultObject() {
$return = new \stdClass();
$return->message = '';
$return->ok = FALSE;
$return->exception = NULL;
return $return;
}
/**
* @param string $name The key of the required value
* @param string $dataType The datatype of the required value (eg. String)
*
* @return int|mixed|null
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
private function getValueFromStripeObject(string $name, string $dataType) {
$value = \CRM_Stripe_Api::getObjectParam($name, $this->getData()->object);
$value = \CRM_Utils_Type::validate($value, $dataType, FALSE);
return $value;
}
/**
* A) A one-off contribution will have trxn_id == stripe.charge_id
* B) A contribution linked to a recur (stripe subscription):
* 1. May have the trxn_id == stripe.subscription_id if the invoice was not generated at the time the contribution
* was created
* (Eg. the recur was setup with a future recurring start date).
* This will be updated to trxn_id == stripe.invoice_id when a suitable IPN is received
* @todo: Which IPN events will update this?
* 2. May have the trxn_id == stripe.invoice_id if the invoice was generated at the time the contribution was
* created OR the contribution has been updated by the IPN when the invoice was generated.
*
* @param string $chargeID Optional, one of chargeID, invoiceID, subscriptionID must be specified
* @param string $invoiceID
* @param string $subscriptionID
*
* @return array
* @throws \CRM_Core_Exception
*/
private function findContribution(string $chargeID = '', string $invoiceID = '', string $subscriptionID = ''): array {
$paymentParams = [
'contribution_test' => $this->getPaymentProcessor()->getIsTestMode(),
];
// A) One-off contribution
if (!empty($chargeID)) {
$paymentParams['trxn_id'] = $chargeID;
$contributionApi3 = civicrm_api3('Mjwpayment', 'get_contribution', $paymentParams);
}
// B2) Contribution linked to subscription and we have invoice_id
// @todo there is a case where $contribution is not defined (i.e. if charge_id is empty)
if (empty($contributionApi3['count'])) {

mattwire
committed
unset($paymentParams['trxn_id']);
if (!empty($invoiceID)) {
$paymentParams['order_reference'] = $invoiceID;
$contributionApi3 = civicrm_api3('Mjwpayment', 'get_contribution', $paymentParams);
}
}
// B1) Contribution linked to subscription and we have subscription_id
// @todo there is a case where $contribution is not defined (i.e. if charge_id, invoice_id are empty)
if (empty($contributionApi3['count'])) {

mattwire
committed
unset($paymentParams['trxn_id']);
if (!empty($subscriptionID)) {
$paymentParams['order_reference'] = $subscriptionID;
$contributionApi3 = civicrm_api3('Mjwpayment', 'get_contribution', $paymentParams);
}
}
// @todo there is a case where $contribution is not defined (i.e. if charge_id, invoice_id, subscription_id are empty)
if (empty($contributionApi3['count'])) {

mattwire
committed
if ((bool)\Civi::settings()->get('stripe_ipndebug')) {
$message = $this->getPaymentProcessor()->getPaymentProcessorLabel() . 'No matching contributions for event ' . $this->getEventID();
\Civi::log()->debug($message);
}
$result = [];
\CRM_Mjwshared_Hook::webhookEventNotMatched('stripe', $this, 'contribution_not_found', $result);
if (empty($result['contribution'])) {
return [];
}
$contribution = $result['contribution'];
}
else {
$contribution = $contributionApi3['values'][$contributionApi3['id']];
}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
return $contribution ?? [];
}
/**
* @param string $chargeID
*
* @return float
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
private function getFeeFromCharge(string $chargeID) {
if (($this->getData()->object['object'] !== 'charge') && (!empty($chargeID))) {
$charge = $this->getPaymentProcessor()->stripeClient->charges->retrieve($chargeID);
$balanceTransactionID = \CRM_Stripe_Api::getObjectParam('balance_transaction', $charge);
}
else {
$balanceTransactionID = $this->getValueFromStripeObject('balance_transaction', 'String');
}
return $this->getPaymentProcessor()->getFeeFromBalanceTransaction($balanceTransactionID, $this->getValueFromStripeObject('currency', 'String'));
}
/**
* This allows us to end a subscription once:
* a) We've reached the end date / number of installments
* b) The recurring contribution is marked as completed
*
* @throws \CiviCRM_API3_Exception
*/
private function handleInstallmentsForSubscription(string $subscriptionID = '', int $contributionRecurID = NULL) {
// Check that we have both contributionRecurID and subscriptionID
if ((empty($contributionRecurID)) || (empty($subscriptionID))) {
return;
}
$contributionRecur = \Civi\Api4\ContributionRecur::get(FALSE)
->addWhere('id', '=', $contributionRecurID)
->execute()
->first();
// Do we have an end date?
if (empty($contributionRecur['end_date'])) {
return;
}
// There is no easy way of retrieving a count of all invoices for a subscription so we ignore the "installments"
// parameter for now and rely on checking end_date (which was calculated based on number of installments...)
// if (empty($contributionRecur['installments'])) { return; }
$stripeSubscription = $this->getPaymentProcessor()->stripeClient->subscriptions->retrieve($subscriptionID);
// If we've passed the end date cancel the subscription
if (($stripeSubscription->current_period_end >= strtotime($contributionRecur['end_date']))
|| ($contributionRecur['contribution_status_id']
== \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', 'Completed'))) {
$this->getPaymentProcessor()->stripeClient->subscriptions->update($subscriptionID, ['cancel_at_period_end' => TRUE]);
$this->updateRecurCompleted(['id' => $contributionRecurID]);
}
}
/**
* Get the recurring contribution from the Stripe subscription ID
*
* @return array
* @throws \CRM_Core_Exception
*/
public function getRecurFromSubscriptionID($subscriptionID): array {
if (empty($subscriptionID)) {
return [];
}
// Get the recurring contribution record associated with the Stripe subscription.
$contributionRecur = \Civi\Api4\ContributionRecur::get(FALSE)
->addWhere('processor_id', '=', $subscriptionID)
->addWhere('is_test', 'IN', [TRUE, FALSE])
->execute()
->first();
if (empty($contributionRecur)) {
if ((bool)\Civi::settings()->get('stripe_ipndebug')) {
$message = $this->getPaymentProcessor()->getPaymentProcessorLabel() . ': ' . $this->getEventID() . ': Cannot find recurring contribution for subscription ID: ' . $subscriptionID;
\Civi::log()->debug($message);
}
return [];
}
return $contributionRecur;
}
/**
* Create the next contribution for a recurring contribution
* This happens when Stripe generates a new invoice and notifies us (normally by invoice.finalized but
* invoice.payment_succeeded sometimes arrives first).
*
* @param string $chargeID
* @param string $invoiceID
* @param array $contributionRecur
*
* @return int
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public function createNextContributionForRecur(string $chargeID, string $invoiceID, array $contributionRecur): int {
// We have a recurring contribution but no contribution so we'll repeattransaction
// Stripe has generated a new invoice (next payment in a subscription) so we
// create a new contribution in CiviCRM
$repeatContributionParams = [
'contribution_recur_id' => $contributionRecur['id'],
'contribution_status_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
'receive_date' => $this->getValueFromStripeObject('receive_date', 'String'),
'order_reference' => $invoiceID,
'trxn_id' => $chargeID,
'total_amount' => $this->getValueFromStripeObject('amount', 'String'),
'fee_amount' => $this->getFeeFromCharge($chargeID),
];
return $this->repeatContribution($repeatContributionParams);
// Don't touch the contributionRecur as it's updated automatically by Contribution.repeattransaction

mattwire
committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
}
/**
* Process the charge.refunded event from Stripe
*
* @return \stdClass
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public function doChargeRefunded() {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'charge') {
$return->message = 'doChargeRefunded Invalid object type';
return $return;
}
// Cancelling an uncaptured paymentIntent triggers charge.refunded but we don't want to process that
if (empty(\CRM_Stripe_Api::getObjectParam('captured', $this->getData()->object))) {
$return->ok = TRUE;
return $return;
}
// Charge ID is required
$chargeID = $this->getValueFromStripeObject('charge_id', 'String');
if (!$chargeID) {
$return->message = 'doChargeRefunded Missing charge_id';
return $return;
}
// Invoice ID is optional
$invoiceID = $this->getValueFromStripeObject('invoice_id', 'String');
// This gives us the refund date + reason code
$refunds = $this->getPaymentProcessor()->stripeClient->refunds->all(['charge' => $chargeID, 'limit' => 1]);
$refund = $refunds->data[0];
// Stripe does not refund fees - see https://support.stripe.com/questions/understanding-fees-for-refunded-payments
// This gives us the actual amount refunded
$amountRefunded = \CRM_Stripe_Api::getObjectParam('amount_refunded', $this->getData()->object);
// Get the CiviCRM contribution that matches the Stripe metadata we have from the event
$contribution = $this->findContribution($chargeID, $invoiceID);
if (empty($contribution)) {
$return->message = 'doChargeRefunded Contribution not found';
return $return;
}
if (isset($contribution['payments'])) {
foreach ($contribution['payments'] as $payment) {
if ($payment['trxn_id'] === $refund->id) {
$return->message = 'Refund ' . $refund->id . ' already recorded in CiviCRM';

mattwire
committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
return $return;
}
if ($payment['trxn_id'] === $chargeID) {
// This triggers the financial transactions/items to be updated correctly.
$cancelledPaymentID = $payment['id'];
}
}
}
$refundParams = [
'contribution_id' => $contribution['id'],
'total_amount' => 0 - abs($amountRefunded),
'trxn_date' => date('YmdHis', $refund->created),
'trxn_result_code' => $refund->reason,
'fee_amount' => 0,
'trxn_id' => $refund->id,
'order_reference' => $invoiceID,
];
if (!empty($cancelledPaymentID)) {
$refundParams['cancelled_payment_id'] = $cancelledPaymentID;
}
$lock = \Civi::lockManager()->acquire('data.contribute.contribution.' . $refundParams['contribution_id']);
if (!$lock->isAcquired()) {
\Civi::log()->error('Could not acquire lock to record refund for contribution: ' . $refundParams['contribution_id']);
}
$refundPayment = civicrm_api3('Payment', 'get', [
'trxn_id' => $refundParams['trxn_id'],
'total_amount' => $refundParams['total_amount'],
]);
if (!empty($refundPayment['count'])) {
$return->message = 'OK - refund already recorded';
$return->ok = TRUE;

mattwire
committed
}
else {
$this->updateContributionRefund($refundParams);
$return->message = 'OK - refund recorded';
$return->ok = TRUE;

mattwire
committed
}
$lock->release();
return $return;
}
/**
* Webhook event: checkout.session.completed
*
* @return \stdClass
public function doCheckoutSessionCompleted(): \stdClass {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'checkout.session') {
$return->message = __FUNCTION__ . ' Invalid object type';
return $return;
}
// Invoice ID is required
$clientReferenceID = $this->getValueFromStripeObject('client_reference_id', 'String');
if (!$clientReferenceID) {
$return->message = __FUNCTION__ . ' Missing client_reference_id';
return $return;
}
$paymentIntentID = $this->getValueFromStripeObject('payment_intent_id', 'String');
if (!$paymentIntentID) {
$return->message = __FUNCTION__ . ' Missing payment_intent ID';
return $return;
}
$subscriptionID = $this->getValueFromStripeObject('subscription_id', 'String');
$contribution = \Civi\Api4\Contribution::get(FALSE)
->addWhere('invoice_id', '=', $clientReferenceID)
->addWhere('is_test', 'IN', [TRUE, FALSE])
->execute()
->first();
if (empty($contribution)) {
$return->message = __FUNCTION__ . ' contribution not found for client_reference_id';
return $return;
}
\Civi\Api4\Contribution::update(FALSE)
->addWhere('id', '=', $contribution['id'])
->addValue('trxn_id', $paymentIntentID)
->execute();
$return->message = __FUNCTION__ . ' contributionID: ' . $contribution['id'];
$return->ok = TRUE;
return $return;
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
}
/**
* Webhook event: invoice.paid
* Invoice changed to paid. This is nearly identical to invoice.payment_succeeded
*
* The invoice.payment_successful type Event object is created and sent to any webhook endpoints configured
* to accept that type of Event when the PaymentIntent powering the payment of an Invoice is used successfully.
* The invoice.paid type Event object is created and sent to any webhook endpoints configured to accept that
* type of Event when the Invoice object has its paid property modified to a "true" value
* (see https://stripe.com/docs/api/invoices/object#invoice_object-paid).
*
* Successful recurring payment. Either we are completing an existing contribution or it's the next one in a subscription
*
* @return \stdClass
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function doInvoicePaid() {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'invoice') {
$return->message = __FUNCTION__ . ' Invalid object type';
return $return;
}
// Invoice ID is required
$invoiceID = $this->getValueFromStripeObject('invoice_id', 'String');
if (!$invoiceID) {
$return->message = __FUNCTION__ . ' Missing invoice_id';
return $return;
}
$chargeID = $this->getValueFromStripeObject('charge_id', 'String');
$subscriptionID = $this->getValueFromStripeObject('subscription_id', 'String');
$contributionRecur = $this->getRecurFromSubscriptionID($subscriptionID);
if (empty($contributionRecur)) {
$return->message = __FUNCTION__ . ': ' . E::ts('No contributionRecur record found in CiviCRM. Ignored.');
$return->ok = TRUE;
return $return;
}
// Acquire the lock to find/create contribution
$lock = \Civi::lockManager()->acquire('data.contribute.contribution.' . $invoiceID);
if (!$lock->isAcquired()) {
\Civi::log()->error('Could not acquire lock to record ' . $this->getEventType() . ' for Stripe InvoiceID: ' . $invoiceID);
}
// We *normally/ideally* expect to be able to find the contribution,
// since the logical order of events would be invoice.finalized first which
// creates a contribution; then invoice.payment_succeeded/paid following, which would
// find it.
$contribution = $this->findContribution($chargeID, $invoiceID, $subscriptionID);
if (empty($contribution)) {
// We were unable to locate the Contribution; it could be the next one in a subscription.
if (empty($contributionRecur['id'])) {
// Hmmm. We could not find the contribution recur record either. Silently ignore this event(!)
$return->ok = TRUE;
$return->message = __FUNCTION__ . ': ' . E::ts('No contribution or recur record found in CiviCRM. Ignored.');
return $return;
}
else {
// We have a recurring contribution but have not yet received invoice.finalized so we don't have the next contribution yet.
// invoice.payment_succeeded sometimes comes before invoice.finalized so trigger the same behaviour here to create a new contribution
$contributionID = $this->createNextContributionForRecur($chargeID, $invoiceID, $contributionRecur);
// Now get the contribution we just created.
$contribution = Contribution::get(FALSE)
->addWhere('id', '=', $contributionID)
->execute()
->first();
}
}
// Release the lock to find/create contribution
$lock->release();
// Now acquire lock to record payment on the contribution
$lock = \Civi::lockManager()->acquire('data.contribute.contribution.' . $contribution['id']);
if (!$lock->isAcquired()) {
\Civi::log()->error('Could not acquire lock to record ' . $this->getEventType() . ' for contribution: ' . $contribution['id']);
}
// By this point we should have a contribution
if (civicrm_api3('Mjwpayment', 'get_payment', [
'trxn_id' => $chargeID,
'status_id' => 'Completed',
])['count'] > 0) {
// Payment already recorded
$return->ok = TRUE;
$return->message = E::ts('Payment already recorded');
return $return;
}
$pendingContributionStatusID = (int) \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
$failedContributionStatusID = (int) \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Failed');
$statusesAllowedToComplete = [$pendingContributionStatusID, $failedContributionStatusID];
// If contribution is in Pending or Failed state record payment and transition to Completed
if (in_array($contribution['contribution_status_id'], $statusesAllowedToComplete)) {
$contributionParams = [
'contribution_id' => $contribution['id'],
'trxn_date' => $this->getValueFromStripeObject('receive_date', 'String'),
'order_reference' => $invoiceID,
'trxn_id' => $chargeID,
'total_amount' => $this->getValueFromStripeObject('amount', 'String'),
'fee_amount' => $this->getFeeFromCharge($chargeID),
'contribution_status_id' => $contribution['contribution_status_id'],
];
$this->updateContributionCompleted($contributionParams);
// Don't touch the contributionRecur as it's updated automatically by Contribution.completetransaction
}
$lock->release();
$this->handleInstallmentsForSubscription($subscriptionID, $contributionRecur['id']);
$return->ok = TRUE;
return $return;
}
/**
* invoice.finalized
*
* @return \stdClass
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public function doInvoiceFinalized() {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'invoice') {
$return->message = __FUNCTION__ . ' Invalid object type';
return $return;
}
// Invoice ID is required
$invoiceID = $this->getValueFromStripeObject('invoice_id', 'String');
if (!$invoiceID) {
$return->message = __FUNCTION__ . ' Missing invoice_id';
return $return;
}
$chargeID = $this->getValueFromStripeObject('charge_id', 'String');
$subscriptionID = $this->getValueFromStripeObject('subscription_id', 'String');
$contributionRecur = $this->getRecurFromSubscriptionID($subscriptionID);
if (empty($contributionRecur)) {
$return->message = __FUNCTION__ . ': ' . E::ts('No contributionRecur record found in CiviCRM. Ignored.');
$return->ok = TRUE;
return $return;
}
$contribution = $this->findContribution($chargeID, $invoiceID, $subscriptionID);
// An invoice has been created and finalized (ready for payment)
// This usually happens automatically through a Stripe subscription
if (empty($contribution)) {
// Unable to find a Contribution.
$this->createNextContributionForRecur($chargeID, $invoiceID, $contributionRecur);
$return->ok = TRUE;
return $return;
}
// For a future recur start date we setup the initial contribution with the
// Stripe subscriptionID because we didn't have an invoice.
// Now we do we can map subscription_id to invoice_id so payment can be recorded
// via subsequent IPN requests (eg. invoice.payment_succeeded)
if ($contribution['trxn_id'] === $subscriptionID) {
$this->updateContribution([
'contribution_id' => $contribution['id'],
'trxn_id' => $invoiceID,
]);
}
$return->ok = TRUE;
return $return;
}
/**
* Webhook event: invoice.payment_failed
* Failed recurring payment. Either we are failing an existing contribution or it's the next one in a subscription
*
* @return \stdClass
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public function doInvoicePaymentFailed() {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'invoice') {
$return->message = __FUNCTION__ . ' Invalid object type';
return $return;
}
// Invoice ID is required
$invoiceID = $this->getValueFromStripeObject('invoice_id', 'String');
if (!$invoiceID) {
$return->message = __FUNCTION__ . ' Missing invoice_id';
return $return;
}
$chargeID = $this->getValueFromStripeObject('charge_id', 'String');
// Get the CiviCRM contribution that matches the Stripe metadata we have from the event
$contribution = $this->findContribution($chargeID, $invoiceID);
if (empty($contribution)) {
$return->message = __FUNCTION__ . ' Contribution not found';
return $return;
}
$pendingContributionStatusID = (int) \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
if ($contribution['contribution_status_id'] == $pendingContributionStatusID) {
// If this contribution is Pending, set it to Failed.
// To obtain the failure_message we need to look up the charge object
$failureMessage = '';
if ($chargeID) {
$stripeCharge = $this->getPaymentProcessor()->stripeClient->charges->retrieve($chargeID);
$failureMessage = \CRM_Stripe_Api::getObjectParam('failure_message', $stripeCharge);
$failureMessage = is_string($failureMessage) ? $failureMessage : '';
}
$receiveDate = $this->getValueFromStripeObject('receive_date', 'String');
$params = [
'contribution_id' => $contribution['id'],
'order_reference' => $invoiceID,
'cancel_date' => $receiveDate,
'cancel_reason' => $failureMessage,
];
$this->updateContributionFailed($params);
}
$return->ok = TRUE;
return $return;
}