Skip to content

Commit

Permalink
[12.x] Allow Stripe dashboard subscriptions (#1058)
Browse files Browse the repository at this point in the history
* Allow Stripe dashboard subscriptions

* Add payload param

* Re-add test
  • Loading branch information
driesvints authored Feb 9, 2021
1 parent f423ece commit 370a058
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function handleCustomerSubscriptionCreated(array $payload)
$isSinglePlan = count($data['items']['data']) === 1;

$subscription = $user->subscriptions()->create([
'name' => $data['metadata']['name'],
'name' => $data['metadata']['name'] ?? $this->newSubscriptionName($payload),
'stripe_id' => $data['id'],
'stripe_status' => $data['status'],
'stripe_plan' => $isSinglePlan ? $firstItem['plan']['id'] : null,
Expand All @@ -101,6 +101,17 @@ protected function handleCustomerSubscriptionCreated(array $payload)
return $this->successMethod();
}

/**
* Determines the name that should be used when new subscriptions are created from the Stripe dashboard.
*
* @param array $payload
* @return string
*/
protected function newSubscriptionName(array $payload)
{
return 'default';
}

/**
* Handle customer subscription updated.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ public static function tearDownAfterClass(): void
static::deleteStripeResource(new Product(static::$productId));
}

public function test_subscriptions_are_created()
{
$user = $this->createCustomer('subscriptions_are_updated', ['stripe_id' => 'cus_foo']);

$this->postJson('stripe/webhook', [
'id' => 'foo',
'type' => 'customer.subscription.created',
'data' => [
'object' => [
'id' => 'sub_foo',
'customer' => 'cus_foo',
'cancel_at_period_end' => false,
'quantity' => 5,
'items' => [
'data' => [[
'id' => 'bar',
'plan' => ['id' => 'plan_foo'],
'quantity' => 10,
]],
],
'status' => 'active',
],
],
])->assertOk();

$this->assertDatabaseHas('subscriptions', [
'name' => 'default',
'user_id' => $user->id,
'stripe_id' => 'sub_foo',
'stripe_status' => 'active',
'quantity' => 5,
]);

$this->assertDatabaseHas('subscription_items', [
'stripe_id' => 'bar',
'stripe_plan' => 'plan_foo',
'quantity' => 10,
]);
}

public function test_subscriptions_are_updated()
{
$user = $this->createCustomer('subscriptions_are_updated', ['stripe_id' => 'cus_foo']);
Expand Down

0 comments on commit 370a058

Please sign in to comment.