@@ -133,7 +133,7 @@ public function a_user_is_not_created_when_a_stripe_customer_subscription_is_cre
133
133
}
134
134
135
135
#[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 ()
137
137
{
138
138
Bus::fake ([CreateAnystackLicenseJob::class]);
139
139
@@ -172,18 +172,85 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
172
172
173
173
$ this ->postJson ('/stripe/webhook ' , $ payload );
174
174
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
+
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
+
175
247
Bus::assertDispatched (CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $ job ) {
176
248
return $ job->
user ->
email ===
'[email protected] ' &&
177
249
$ job ->subscription === Subscription::Max &&
178
250
$ job ->subscriptionItemId === $ job ->user ->subscriptions ->first ()->items ()->first ()->id &&
179
251
$ job ->firstName === 'John ' &&
180
252
$ job ->lastName === 'Doe ' ;
181
253
});
182
-
183
- $ user ->refresh ();
184
-
185
- $ this ->assertNotEmpty ($ user ->subscriptions );
186
- $ this ->assertNotEmpty ($ user ->subscriptions ->first ()->items );
187
254
}
188
255
189
256
protected function mockStripeClient (?User $ user = null ): void
0 commit comments