@@ -26,6 +26,11 @@ class Stripe implements BillingProvider
26
26
*/
27
27
protected $ config = [];
28
28
29
+ /**
30
+ * Errors caught during operations
31
+ */
32
+ protected array $ errors = [];
33
+
29
34
/**
30
35
* Stripe client
31
36
* @var StripeClient
@@ -322,6 +327,63 @@ public function subscribe(array $data): Session
322
327
return $ session ;
323
328
}
324
329
330
+ /**
331
+ * @inheritDoc
332
+ */
333
+ public function changeSubcription (array $ data ): bool
334
+ {
335
+ $ user = auth ()->user ();
336
+
337
+ $ tier = ($ data ['id ' ] ?? null ) ? $ this ->tiers [$ data ['id ' ]] : array_values (array_filter ($ this ->tiers , function ($ tier ) use ($ data ) {
338
+ return $ tier ['name ' ] === $ data ['name ' ];
339
+ }))[0 ];
340
+
341
+ $ oldSubscription = $ this ->provider ->subscriptions ->retrieve ($ user ->subscription ()['subscription_id ' ]);
342
+ $ stripeData = [
343
+ 'items ' => [
344
+ [
345
+ 'id ' => $ oldSubscription ->items ->data [0 ]->id ,
346
+ 'price ' => $ tier ['id ' ],
347
+ ]
348
+ ],
349
+ 'proration_behavior ' => 'create_prorations ' ,
350
+ ];
351
+
352
+ if ($ data ['metadata ' ] ?? null ) {
353
+ $ stripeData ['metadata ' ] = array_merge ($ stripeData ['metadata ' ], $ data ['metadata ' ]);
354
+ }
355
+
356
+ try {
357
+ $ this ->provider ->subscriptions ->update ($ oldSubscription ->id , $ stripeData );
358
+ } catch (\Throwable $ th ) {
359
+ $ this ->errors [] = $ th ->getMessage ();
360
+ return false ;
361
+ }
362
+
363
+ return true ;
364
+ }
365
+
366
+ /**
367
+ * @inheritDoc
368
+ */
369
+ public function cancelSubscription (string $ id ): bool
370
+ {
371
+ $ user = auth ()->user ();
372
+
373
+ if (!$ user ->subscription ()) {
374
+ return true ;
375
+ }
376
+
377
+ try {
378
+ $ this ->provider ->subscriptions ->cancel ($ id );
379
+ } catch (\Throwable $ th ) {
380
+ $ this ->errors [] = $ th ->getMessage ();
381
+ return false ;
382
+ }
383
+
384
+ return true ;
385
+ }
386
+
325
387
/**
326
388
* @inheritDoc
327
389
*/
@@ -341,7 +403,22 @@ public function session(string $id): ?Session
341
403
*/
342
404
public function webhook (): Event
343
405
{
344
- return new Event ([]);
406
+ try {
407
+ $ event = \Stripe \Webhook::constructEvent (
408
+ @file_get_contents ('php://input ' ),
409
+ $ _SERVER ['HTTP_STRIPE_SIGNATURE ' ] ?? '' ,
410
+ $ this ->config ['connection ' ]['secrets.webhook ' ] ?? null
411
+ );
412
+ } catch (\Throwable $ th ) {
413
+ response ()->exit ($ th , 400 );
414
+ }
415
+
416
+ return new Event ([
417
+ 'type ' => $ event ['type ' ],
418
+ 'data ' => $ event ['data ' ],
419
+ 'id ' => $ event ['id ' ],
420
+ 'created ' => $ event ['created ' ],
421
+ ]);
345
422
}
346
423
347
424
/**
@@ -369,9 +446,9 @@ public function tiers(?string $billingPeriod = null): array
369
446
/**
370
447
* @inheritDoc
371
448
*/
372
- public function tier (string $ id ): array
449
+ public function tier (string $ id ): ? array
373
450
{
374
- return $ this ->tiers [$ id ];
451
+ return $ this ->tiers [$ id ] ?? null ;
375
452
}
376
453
377
454
/**
@@ -409,6 +486,6 @@ public function provider(): StripeClient
409
486
*/
410
487
public function errors (): array
411
488
{
412
- return [] ;
489
+ return $ this -> errors ;
413
490
}
414
491
}
0 commit comments