@@ -133,7 +133,7 @@ public function a_user_is_not_created_when_a_stripe_customer_subscription_is_cre
133133 }
134134
135135 #[Test]
136- public function a_license_is_created_when_a_stripe_subscription_is_created ()
136+ public function a_license_is_not_created_when_a_stripe_subscription_is_created ()
137137 {
138138 Bus::fake ([CreateAnystackLicenseJob::class]);
139139
@@ -172,18 +172,85 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
172172
173173 $ this ->postJson ('/stripe/webhook ' , $ payload );
174174
175+ Bus::assertNotDispatched (CreateAnystackLicenseJob::class);
176+
177+ $ user ->refresh ();
178+
179+ $ this ->assertNotEmpty ($ user ->subscriptions );
180+ $ this ->assertNotEmpty ($ user ->subscriptions ->first ()->items );
181+ }
182+
183+ #[Test]
184+ public function a_license_is_created_when_a_stripe_invoice_is_paid ()
185+ {
186+ Bus::fake ([CreateAnystackLicenseJob::class]);
187+
188+ $ user = User::factory ()->create ([
189+ 'stripe_id ' => 'cus_test123 ' ,
190+ 'name ' => 'John Doe ' ,
191+ 'email ' => 'john@example.com ' ,
192+ ]);
193+
194+ \Laravel \Cashier \Subscription::factory ()
195+ ->for ($ user , 'user ' )
196+ ->create ([
197+ 'stripe_id ' => 'sub_test123 ' ,
198+ 'stripe_status ' => 'incomplete ' , // the subscription is incomplete at the time this webhook is sent
199+ 'stripe_price ' => Subscription::Max->stripePriceId (),
200+ 'quantity ' => 1 ,
201+ ]);
202+ \Laravel \Cashier \SubscriptionItem::factory ()
203+ ->for ($ user ->subscriptions ->first (), 'subscription ' )
204+ ->create ([
205+ 'stripe_id ' => 'si_test ' ,
206+ 'stripe_price ' => Subscription::Max->stripePriceId (),
207+ 'quantity ' => 1 ,
208+ ]);
209+
210+ $ this ->mockStripeClient ($ user );
211+
212+ $ payload = [
213+ 'id ' => 'evt_test_webhook ' ,
214+ 'type ' => 'invoice.paid ' ,
215+ 'data ' => [
216+ 'object ' => [
217+ 'id ' => 'in_test ' ,
218+ 'object ' => 'invoice ' ,
219+ 'billing_reason ' => 'subscription_create ' ,
220+ 'customer ' => 'cus_test123 ' ,
221+ 'paid ' => true ,
222+ 'status ' => 'paid ' ,
223+ 'lines ' => [
224+ 'object ' => 'list ' ,
225+ 'data ' => [
226+ [
227+ 'id ' => 'il_test ' ,
228+ 'price ' => [
229+ 'id ' => Subscription::Max->stripePriceId (),
230+ 'object ' => 'price ' ,
231+ 'product ' => 'prod_test ' ,
232+ ],
233+ 'quantity ' => 1 ,
234+ 'subscription ' => 'sub_test123 ' ,
235+ 'subscription_item ' => 'si_test ' ,
236+ 'type ' => 'subscription ' ,
237+ ],
238+ ],
239+ ],
240+ 'subscription ' => 'sub_test123 ' ,
241+ ],
242+ ],
243+ ];
244+
245+ $ this ->postJson ('/stripe/webhook ' , $ payload );
246+
175247 Bus::assertDispatched (CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $ job ) {
176248 return $ job ->user ->email === 'john@example.com ' &&
177249 $ job ->subscription === Subscription::Max &&
178250 $ job ->subscriptionItemId === $ job ->user ->subscriptions ->first ()->items ()->first ()->id &&
179251 $ job ->firstName === 'John ' &&
180252 $ job ->lastName === 'Doe ' ;
181253 });
182-
183- $ user ->refresh ();
184-
185- $ this ->assertNotEmpty ($ user ->subscriptions );
186- $ this ->assertNotEmpty ($ user ->subscriptions ->first ()->items );
187254 }
188255
189256 protected function mockStripeClient (?User $ user = null ): void
0 commit comments