Skip to content

Commit 7133955

Browse files
committed
feat: add subscription management
1 parent 48314ed commit 7133955

File tree

1 file changed

+81
-4
lines changed

1 file changed

+81
-4
lines changed

src/Stripe.php

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Stripe implements BillingProvider
2626
*/
2727
protected $config = [];
2828

29+
/**
30+
* Errors caught during operations
31+
*/
32+
protected array $errors = [];
33+
2934
/**
3035
* Stripe client
3136
* @var StripeClient
@@ -322,6 +327,63 @@ public function subscribe(array $data): Session
322327
return $session;
323328
}
324329

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+
325387
/**
326388
* @inheritDoc
327389
*/
@@ -341,7 +403,22 @@ public function session(string $id): ?Session
341403
*/
342404
public function webhook(): Event
343405
{
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+
]);
345422
}
346423

347424
/**
@@ -369,9 +446,9 @@ public function tiers(?string $billingPeriod = null): array
369446
/**
370447
* @inheritDoc
371448
*/
372-
public function tier(string $id): array
449+
public function tier(string $id): ?array
373450
{
374-
return $this->tiers[$id];
451+
return $this->tiers[$id] ?? null;
375452
}
376453

377454
/**
@@ -409,6 +486,6 @@ public function provider(): StripeClient
409486
*/
410487
public function errors(): array
411488
{
412-
return [];
489+
return $this->errors;
413490
}
414491
}

0 commit comments

Comments
 (0)