Skip to content

Commit bbdfbe5

Browse files
committed
fix: use cashier subscription_item_id fk, not stripe subscription item id
1 parent 4899966 commit bbdfbe5

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

app/Jobs/HandleCustomerSubscriptionCreatedJob.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Queue\SerializesModels;
1111
use Laravel\Cashier\Cashier;
1212
use Laravel\Cashier\Events\WebhookHandled;
13+
use Laravel\Cashier\SubscriptionItem;
1314
use Stripe\Subscription;
1415

1516
class HandleCustomerSubscriptionCreatedJob implements ShouldQueue
@@ -38,7 +39,10 @@ public function handle(): void
3839
}
3940

4041
$subscriptionPlan = \App\Enums\Subscription::fromStripeSubscription($stripeSubscription);
41-
$subscriptionItemId = $stripeSubscription->items->first()->id;
42+
$cashierSubscriptionItemId = SubscriptionItem::query()
43+
->where('stripe_id', $stripeSubscription->items->first()->id)
44+
->first()
45+
->id;
4246

4347
$nameParts = explode(' ', $user->name ?? '', 2);
4448
$firstName = $nameParts[0] ?: null;
@@ -47,7 +51,7 @@ public function handle(): void
4751
dispatch(new CreateAnystackLicenseJob(
4852
$user,
4953
$subscriptionPlan,
50-
$subscriptionItemId,
54+
$cashierSubscriptionItemId,
5155
$firstName,
5256
$lastName,
5357
));

tests/Feature/Jobs/HandleCustomerSubscriptionCreatedJobTest.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,7 @@ class HandleCustomerSubscriptionCreatedJobTest extends TestCase
2121
/** @test */
2222
public function it_dispatches_the_create_anystack_license_job_with_correct_data()
2323
{
24-
$mockCustomer = Customer::constructFrom([
25-
'id' => 'cus_S9dhoV2rJK2Auy',
26-
'email' => '[email protected]',
27-
'name' => 'John Doe',
28-
]);
29-
30-
$this->mockStripeClient($mockCustomer);
31-
32-
dispatch_sync(new CreateUserFromStripeCustomer($mockCustomer));
24+
$this->createTestData('John Doe');
3325

3426
Bus::fake();
3527

@@ -54,15 +46,7 @@ public function it_dispatches_the_create_anystack_license_job_with_correct_data(
5446
*/
5547
public function it_extracts_customer_name_parts_correctly($fullName, $expectedFirstName, $expectedLastName)
5648
{
57-
$mockCustomer = Customer::constructFrom([
58-
'id' => 'cus_S9dhoV2rJK2Auy',
59-
'email' => '[email protected]',
60-
'name' => $fullName,
61-
]);
62-
63-
$this->mockStripeClient($mockCustomer);
64-
65-
dispatch_sync(new CreateUserFromStripeCustomer($mockCustomer));
49+
$this->createTestData($fullName);
6650

6751
$webhookCall = new WebhookHandled($this->getTestWebhookPayload());
6852

@@ -117,6 +101,37 @@ public function it_fails_when_customer_has_no_email()
117101
Bus::assertNotDispatched(CreateAnystackLicenseJob::class);
118102
}
119103

104+
protected function createTestData(?string $customerName)
105+
{
106+
$mockCustomer = Customer::constructFrom([
107+
'id' => $this->getTestWebhookPayload()['data']['object']['customer'],
108+
'email' => $email = '[email protected]',
109+
'name' => $customerName,
110+
]);
111+
112+
$this->mockStripeClient($mockCustomer);
113+
114+
dispatch_sync(new CreateUserFromStripeCustomer($mockCustomer));
115+
116+
$user = User::query()->where('email', $email)->firstOrFail();
117+
118+
$subscription = \Laravel\Cashier\Subscription::factory()
119+
->for($user, 'user')
120+
->create([
121+
'stripe_id' => $this->getTestWebhookPayload()['data']['object']['id'],
122+
'stripe_status' => 'active',
123+
'stripe_price' => $this->getTestWebhookPayload()['data']['object']['items']['data'][0]['price']['id'],
124+
'quantity' => 1,
125+
]);
126+
$subscriptionItem = \Laravel\Cashier\SubscriptionItem::factory()
127+
->for($subscription, 'subscription')
128+
->create([
129+
'stripe_id' => $this->getTestWebhookPayload()['data']['object']['items']['data'][0]['id'],
130+
'stripe_price' => $this->getTestWebhookPayload()['data']['object']['items']['data'][0]['price']['id'],
131+
'quantity' => 1,
132+
]);
133+
}
134+
120135
protected function getTestWebhookPayload(): array
121136
{
122137
return [

0 commit comments

Comments
 (0)