Skip to content
Snippets Groups Projects
Commit 09f8d997 authored by mattwire's avatar mattwire
Browse files

Move charge.failed to new webhook events processor

parent 16935172
Branches
Tags
1 merge request!217Implement Stripe Checkout (with support for SEPA and ACH)
......@@ -411,6 +411,10 @@ class CRM_Core_Payment_StripeIPN {
$return = $webhookEventProcessor->doChargeRefunded();
break;
case 'charge.failed':
$return = $webhookEventProcessor->doChargeFailed();
break;
case 'invoice.payment_failed':
$return = $webhookEventProcessor->doInvoicePaymentFailed();
break;
......@@ -466,34 +470,8 @@ class CRM_Core_Payment_StripeIPN {
* @throws \Stripe\Exception\ApiErrorException
*/
private function processEventType() {
$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];
// NOTE: If you add an event here make sure you add it to the webhook or it will never be received!
switch($this->eventType) {
// One-time donation and per invoice payment.
case 'charge.failed':
// If we don't have a customer_id we can't do anything with it!
// It's quite likely to be a fraudulent/spam so we ignore.
if (empty(CRM_Stripe_Api::getObjectParam('customer_id', $this->getData()->object))) {
return TRUE;
}
if (!$this->setInfo()) {
// We could not find this contribution.
return TRUE;
}
$params = [
'contribution_id' => $this->contribution['id'],
'order_reference' => $this->invoice_id ?? $this->charge_id,
'cancel_date' => $this->receive_date,
'cancel_reason' => $this->retrieve('failure_message', 'String'),
];
$this->updateContributionFailed($params);
return TRUE;
case 'customer.subscription.updated':
// Subscription is updated. This used to be "implemented" but didn't work
return TRUE;
......
......@@ -271,6 +271,7 @@ class Events {
}
/**
* Webhook event: charge.succeeded / charge.captured
* We process charge.succeeded per charge.captured
*
* @return \stdClass
......@@ -350,6 +351,7 @@ class Events {
}
/**
* Webhook event: charge.refunded
* Process the charge.refunded event from Stripe
*
* @return \stdClass
......@@ -435,18 +437,73 @@ class Events {
'total_amount' => $refundParams['total_amount'],
]);
if (!empty($refundPayment['count'])) {
$return->message = 'OK - refund already recorded. Contribution ID: ' . $contribution['id'];
$return->message = __FUNCTION__ . ' Refund already recorded. Contribution ID: ' . $contribution['id'];
$return->ok = TRUE;
}
else {
$this->updateContributionRefund($refundParams);
$return->message = 'OK - refund recorded. Contribution ID: ' . $contribution['id'];
$return->message = __FUNCTION__ . 'Refund recorded. Contribution ID: ' . $contribution['id'];
$return->ok = TRUE;
}
$lock->release();
return $return;
}
/**
* Webhook event: charge.failed
* One-time donation and per invoice payment
*
* @return \stdClass
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function doChargeFailed(): \stdClass {
$return = $this->getResultObject();
// Check we have the right data object for this event
if (($this->getData()->object['object'] ?? '') !== 'charge') {
$return->message = __FUNCTION__ . ' Invalid object type';
return $return;
}
// If we don't have a customer_id we can't do anything with it!
// It's quite likely to be a fraudulent/spam so we ignore.
if (empty($this->getValueFromStripeObject('customer_id', 'String'))) {
$return->message = __FUNCTION__ . ' ignoring - no customer_id';
$return->ok = TRUE;
return $return;
}
// Charge ID is required
$chargeID = $this->getValueFromStripeObject('charge_id', 'String');
if (!$chargeID) {
$return->message = __FUNCTION__ . ' Missing charge_id';
return $return;
}
// Invoice ID is optional
$invoiceID = $this->getValueFromStripeObject('invoice_id', 'String');
$contribution = $this->findContribution($chargeID, $invoiceID);
if (empty($contribution)) {
$return->message = __FUNCTION__ . ' Contribution not found';
return $return;
}
$failedContributionParams = [
'contribution_id' => $contribution['id'],
'order_reference' => $invoiceID ?? $chargeID,
'cancel_date' => $this->getValueFromStripeObject('receive_date', 'String'),
'cancel_reason' => $this->getValueFromStripeObject('failure_message', 'String'),
];
$this->updateContributionFailed($failedContributionParams);
$return->message = __FUNCTION__ . ' contributionID: ' . $contribution['id'];
$return->ok = TRUE;
return $return;
}
/**
* Webhook event: checkout.session.completed
*
......@@ -513,7 +570,7 @@ class Events {
}
/**
* Webhook event: invoice.paid
* Webhook event: invoice.paid / invoice.payment_succeeded
* 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
......@@ -632,7 +689,7 @@ class Events {
}
/**
* invoice.finalized
* Webhook event: invoice.finalized
*
* @return \stdClass
* @throws \CRM_Core_Exception
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment