Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Subscription Update Webhook bugfix #1085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ protected function handleCustomerSubscriptionUpdated(array $payload)
$subscription->ends_at = $subscription->onTrial()
? $subscription->trial_ends_at
: Carbon::createFromTimestamp($data['current_period_end']);
} elseif ($data['cancel_at']) {
$subscription->ends_at = Carbon::createFromTimestamp($data['cancel_at']);
} else {
$subscription->ends_at = null;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Cashier\Tests\Feature;

use Carbon\Carbon;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Laravel\Cashier\Exceptions\PaymentActionRequired;
Expand Down Expand Up @@ -149,6 +150,64 @@ public function test_subscriptions_are_updated()
]);
}

public function test_subscriptions_on_update_cancel_at_date_is_correct()
{
$user = $this->createCustomer('subscriptions_are_updated', ['stripe_id' => 'cus_foo']);
$cancelDate = Carbon::now()->addMonths(6);

$subscription = $user->subscriptions()->create([
'name' => 'main',
'stripe_id' => 'sub_foo',
'stripe_plan' => 'plan_foo',
'stripe_status' => Subscription::STATUS_ACTIVE,
]);

$item = $subscription->items()->create([
'stripe_id' => 'it_foo',
'stripe_plan' => 'plan_bar',
'quantity' => 1,
]);

$this->postJson('stripe/webhook', [
'id' => 'foo',
'type' => 'customer.subscription.updated',
'data' => [
'object' => [
'id' => $subscription->stripe_id,
'customer' => 'cus_foo',
'cancel_at' => $cancelDate->timestamp,
'cancel_at_period_end' => false,
'items' => [
'data' => [[
'id' => 'bar',
'plan' => ['id' => 'plan_foo'],
'quantity' => 5,
]],
],
],
],
])->assertOk();

$this->assertDatabaseHas('subscriptions', [
'id' => $subscription->id,
'user_id' => $user->id,
'stripe_id' => 'sub_foo',
'quantity' => 5,
'ends_at' => $cancelDate->format('Y-m-d H:i:s'),
]);

$this->assertDatabaseHas('subscription_items', [
'subscription_id' => $subscription->id,
'stripe_id' => 'bar',
'stripe_plan' => 'plan_foo',
'quantity' => 5,
]);

$this->assertDatabaseMissing('subscription_items', [
'id' => $item->id,
]);
}

public function test_cancelled_subscription_is_properly_reactivated()
{
$user = $this->createCustomer('cancelled_subscription_is_properly_reactivated');
Expand Down