Skip to content

Commit

Permalink
Silently handle "no such customer" Stripe error.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Feb 5, 2025
1 parent 6cb7a84 commit 73d6ce0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ public function getStripeCustomerId()
return $this->stripeCustomerId;
}

/**
* @var string|null $stripeCustomerId
*/
public function setStripeCustomerId($stripeCustomerId)

Check failure on line 459 in src/Entity/User.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.2)

PHPDoc tag @var above a method has no effect.

Check failure on line 459 in src/Entity/User.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.3)

PHPDoc tag @var above a method has no effect.

Check failure on line 459 in src/Entity/User.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.2)

PHPDoc tag @var above a method has no effect.

Check failure on line 459 in src/Entity/User.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.3)

PHPDoc tag @var above a method has no effect.
{
$this->stripeCustomerId = $stripeCustomerId;
Expand Down
19 changes: 17 additions & 2 deletions src/Service/StripeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,22 @@ public function createIntent(PaymentInterface $payment, $savePaymentMethod = fal
sprintf('Order #%d | StripeManager::createIntent | %s', $order->getId(), json_encode($payload))
);

return Stripe\PaymentIntent::create($payload, $stripeOptions);
try {
return Stripe\PaymentIntent::create($payload, $stripeOptions);
} catch (Stripe\Exception\InvalidRequestException $e) {
// Do not die on error "No such customer"
// Clear the stored customer, and retry
if (Stripe\ErrorObject::CODE_RESOURCE_MISSING === $e->getStripeCode() && 'customer' === $e->getStripeParam()) {
if ($order->getCustomer() && $order->getCustomer()->hasUser()) {
$order->getCustomer()->getUser()->setStripeCustomerId(null);

return $this->createIntent($payment, $savePaymentMethod);
}
}

throw $e;
}

}

/**
Expand Down Expand Up @@ -367,7 +382,7 @@ public function attachPaymentMethodToCustomer(PaymentInterface $payment)
* @see https://stripe.com/docs/connect/cloning-customers-across-accounts
* @see https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods
*
* We clone the PaymentMethod in the connected account and then we use the clonned payment method id
* We clone the PaymentMethod in the connected account and then we use the cloned payment method id
* when we create the PaymentIntent to create the direct charge in the connected account.
*
* @return Stripe\PaymentMethod
Expand Down

0 comments on commit 73d6ce0

Please sign in to comment.