Skip to content

Incorrect subscription type due to race condition #1773

@AntonioDiPassio-AppSys

Description

@AntonioDiPassio-AppSys

Cashier Stripe Version

15.7.1

Laravel Version

12.3.1

PHP Version

8.2.12

Database Driver & Version

No response

Description

When creating a subscription with a non default type, there's a possibility it's still being saved as type 'default'.
This happens when the webhook customer.subscriptions.created is processed before the actual subscription creating function has finished.
More specifically: When executing auth()->user()->newSubscription('test')->create(), if the webhook is received and processed between lines 257 and 263 of SubscriptionBuilder.

Line 257-261:
$stripeSubscription = $this->owner->stripe()->subscriptions->create(array_merge( ['customer' => $stripeCustomer->id], $this->buildPayload(), $subscriptionOptions ));

Will create the subscription in Stripe

Line 263:
$subscription = $this->createSubscription($stripeSubscription);

Will create the subscription in the database (if it doesn't exist yet):
`protected function createSubscription(StripeSubscription $stripeSubscription)
{
if ($subscription = $this->owner->subscriptions()->where('stripe_id', $stripeSubscription->id)->first()) {
return $subscription;
}

...

`

It could be circumvented by adding metadata 'type' or 'name' in Stripe, but imho this should not be required.

Steps To Reproduce

  • Create a user
  • $user()->newSubscription('test')->create()

If you simulate a negative race condition by delaying line 263 in SubscriptionBuilder (by putting a breakpoint in local dev, or inserting a sleep(5), or ...), the webhook will be processed first and the subscription will be created with type 'default'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions