Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicated subscription created event #194

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ protected function handleSubscriptionCreated(array $payload)
throw new InvalidPassthroughPayload;
}

if ($this->subscriptionExists($payload['subscription_id'])) {
return;
}

$customer = $this->findOrCreateCustomer($payload['passthrough']);

$trialEndsAt = $payload['status'] === Subscription::STATUS_TRIALING
Expand Down Expand Up @@ -278,6 +282,17 @@ protected function findSubscription(string $subscriptionId)
return Cashier::$subscriptionModel::firstWhere('paddle_id', $subscriptionId);
}

/**
* Determine if a subscription with a given Paddle ID already exists.
*
* @param string $subscriptionId
* @return bool
*/
protected function subscriptionExists(string $subscriptionId)
{
return Cashier::$subscriptionModel::where('paddle_id', $subscriptionId)->exists();
}

/**
* Determine if a receipt with a given Order ID already exists.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,50 @@ public function test_it_can_handle_a_subscription_created_event_if_billable_alre
});
}

public function test_it_can_handle_a_duplicated_subscription_created_event()
{
Cashier::fake();

$user = $this->createUser();

for ($i = 0; $i < 2; $i++) {
$this->postJson('paddle/webhook', [
'alert_name' => 'subscription_created',
'user_id' => 'foo',
'email' => $user->paddleEmail(),
'passthrough' => json_encode([
'billable_id' => $user->id,
'billable_type' => $user->getMorphClass(),
'subscription_name' => 'main',
]),
'quantity' => 1,
'status' => Subscription::STATUS_ACTIVE,
'subscription_id' => 'bar',
'subscription_plan_id' => 1234,
])->assertOk();
}

$this->assertDatabaseHas('customers', [
'billable_id' => $user->id,
'billable_type' => $user->getMorphClass(),
]);

$this->assertDatabaseHas('subscriptions', [
'billable_id' => $user->id,
'billable_type' => $user->getMorphClass(),
'name' => 'main',
'paddle_id' => 'bar',
'paddle_plan' => 1234,
'paddle_status' => Subscription::STATUS_ACTIVE,
'quantity' => 1,
'trial_ends_at' => null,
]);

Cashier::assertSubscriptionCreated(function (SubscriptionCreated $event) use ($user) {
return $event->billable->id === $user->id && $event->subscription->paddle_plan === 1234;
});
}

public function test_it_can_handle_a_subscription_updated_event()
{
Cashier::fake();
Expand Down