Skip to content

Commit

Permalink
refactor: use the exit statement in webhook events only when it is pr…
Browse files Browse the repository at this point in the history
…ocessed
  • Loading branch information
glaubersilva committed Oct 27, 2023
1 parent 8fd8f4c commit bc50c34
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ChargeRefunded
*
* @see https://stripe.com/docs/api/events/types#event_types-charge.refunded
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -30,28 +31,29 @@ class ChargeRefunded
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
*
* @throws Exception
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var Charge $stripeCharge */
$stripeCharge = $event->data->object;

$donation = give()->donations->getByGatewayTransactionId($stripeCharge->payment_intent);

if (!$donation || !$this->shouldProcessDonation($donation)) {
return;
return false;
}

if ($stripeCharge->refunded && !$donation->status->isRefunded()) {
Expand All @@ -63,5 +65,7 @@ public function processEvent(Event $event)
'content' => __('Payment refunded in Stripe.', 'give'),
]);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomerSubscriptionCreated
*
* @see https://stripe.com/docs/api/events/types#event_types-customer.subscription.created
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -28,18 +29,19 @@ class CustomerSubscriptionCreated
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var StripeSubscription $stripeSubscription */
$stripeSubscription = $event->data->object;
Expand All @@ -48,11 +50,11 @@ public function processEvent(Event $event)

// only use this for next gen for now
if (!$subscription || !$this->shouldProcessSubscription($subscription)) {
return;
return false;
}

// exit early to prevent legacy webhook
// we don't need to do anything here at the moment because the subscription is already created & active
exit;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CustomerSubscriptionDeleted
*
* @see https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -29,19 +30,20 @@ class CustomerSubscriptionDeleted
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
* @throws Exception
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var StripeSubscription $stripeSubscription */
$stripeSubscription = $event->data->object;
Expand All @@ -50,12 +52,14 @@ public function processEvent(Event $event)

// only use this for next gen for now
if (!$subscription || !$this->shouldProcessSubscription($subscription)) {
return;
return false;
}

if (!$subscription->status->isCancelled() && !$subscription->status->isCompleted()) {
$subscription->status = SubscriptionStatus::COMPLETED();
$subscription->save();
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class InvoicePaymentFailed
*
* @see https://stripe.com/docs/api/events/types#event_types-invoice.payment_failed
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -29,20 +30,21 @@ class InvoicePaymentFailed
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
*
* @throws Exception
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var Invoice $invoice */
$invoice = $event->data->object;
Expand All @@ -51,7 +53,7 @@ public function processEvent(Event $event)

// only use this for next gen for now
if (!$subscription || !$this->shouldProcessSubscription($subscription)) {
return;
return false;
}

if (
Expand All @@ -64,6 +66,8 @@ public function processEvent(Event $event)
$subscription->status = SubscriptionStatus::FAILING();
$subscription->save();
}

return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,28 @@ class InvoicePaymentSucceeded
*
* @see https://stripe.com/docs/api/events/types#event_types-invoice.payment_succeeded
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
* @throws \Exception
*/
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
* @throws \Exception
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var Invoice $invoice */
$invoice = $event->data->object;
Expand All @@ -54,7 +55,7 @@ public function processEvent(Event $event)

// only use this for next gen for now
if (!$subscription || !$this->shouldProcessSubscription($subscription)) {
return;
return false;
}

if ($initialDonation = give()->donations->getByGatewayTransactionId($invoice->payment_intent)) {
Expand All @@ -71,6 +72,8 @@ public function processEvent(Event $event)
$subscriptionModel->handleSubscriptionCompleted();
}
}

return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PaymentIntentPaymentFailed
*
* @see https://stripe.com/docs/api/events/types#event_types-payment_intent.payment_failed
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -30,26 +31,27 @@ class PaymentIntentPaymentFailed
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var PaymentIntent $paymentIntent */
$paymentIntent = $event->data->object;

$donation = give()->donations->getByGatewayTransactionId($paymentIntent->id);

if (!$donation || !$this->shouldProcessDonation($donation)) {
return;
return false;
}

if ($donation->type->isSingle() && !$donation->status->isFailed()) {
Expand All @@ -61,5 +63,7 @@ public function processEvent(Event $event)
'content' => __('Payment failed in Stripe.', 'give'),
]);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PaymentIntentSucceeded
*
* @see https://stripe.com/docs/api/events/types#event_types-invoice.payment_succeeded
*
* @unreleased Add exit statement only when the event is successfully processed.
* @since 3.0.0
*
* @return void
Expand All @@ -30,26 +31,27 @@ class PaymentIntentSucceeded
public function __invoke(Event $event)
{
try {
$this->processEvent($event);
if ($this->processEvent($event)) {
exit;
}
} catch (Exception $exception) {
$this->logWebhookError($event, $exception);
}

exit;
}

/**
* @unreleased Return a bool value.
* @since 3.0.0
*/
public function processEvent(Event $event)
public function processEvent(Event $event): bool
{
/* @var PaymentIntent $paymentIntent */
$paymentIntent = $event->data->object;

$donation = give()->donations->getByGatewayTransactionId($paymentIntent->id);

if (!$donation || !$this->shouldProcessDonation($donation)) {
return;
return false;
}

if ($donation->type->isSingle() && !$donation->status->isComplete()) {
Expand All @@ -61,5 +63,7 @@ public function processEvent(Event $event)
'content' => __('Payment succeeded in Stripe.', 'give'),
]);
}

return true;
}
}

0 comments on commit bc50c34

Please sign in to comment.