Skip to content
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
13 changes: 6 additions & 7 deletions app/Jobs/HandleInvoicePaidJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function handleSubscriptionUpdate(): void
'invoice_id' => $this->invoice->id,
]);

$this->updateSubscriptionCompedStatus();
$this->updateSubscriptionPricePaid();
}

private function handleSubscriptionCreated(): void
Expand All @@ -76,12 +76,12 @@ private function handleSubscriptionCreated(): void

if ($isRenewal && $licenseKey && $licenseId) {
$this->handleLegacyLicenseRenewal($subscription, $licenseKey, $licenseId);
$this->updateSubscriptionCompedStatus();
$this->updateSubscriptionPricePaid();

return;
}

$this->updateSubscriptionCompedStatus();
$this->updateSubscriptionPricePaid();
}

private function handleLegacyLicenseRenewal($subscription, string $licenseKey, string $licenseId): void
Expand Down Expand Up @@ -169,7 +169,7 @@ private function handleSubscriptionRenewal(): void
'invoice_id' => $this->invoice->id,
]);

$this->updateSubscriptionCompedStatus();
$this->updateSubscriptionPricePaid();
}

private function handleManualInvoice(): void
Expand Down Expand Up @@ -591,9 +591,9 @@ private function createBundlePluginLicense(User $user, Plugin $plugin, PluginBun
}

/**
* Mark the local Cashier subscription as comped if the invoice total is zero.
* Update the price paid on the local Cashier subscription from the invoice total.
*/
private function updateSubscriptionCompedStatus(): void
private function updateSubscriptionPricePaid(): void
{
if (! $this->invoice->subscription) {
return;
Expand All @@ -605,7 +605,6 @@ private function updateSubscriptionCompedStatus(): void
$invoiceTotal = $this->invoice->total ?? 0;

$subscription->update([
'is_comped' => $invoiceTotal <= 0,
'price_paid' => max(0, $invoiceTotal),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/customer/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
/>

{{-- Subscription Card --}}
@if($this->activeSubscription)
@if($this->activeSubscription?->active())
<x-dashboard-card
title="Subscription"
:value="$this->subscriptionName"
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/DashboardLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public function test_user_name_with_apostrophe_is_not_double_escaped_in_dashboar
$response->assertSee("Timmy D'Hooghe");
}

// ========================================
// Subscription Card Tests
// ========================================

public function test_dashboard_shows_no_active_subscription_when_subscription_is_canceled(): void
{
$user = User::factory()->create();
Subscription::factory()->for($user)->create([
'stripe_price' => self::MAX_PRICE_ID,
'stripe_status' => 'canceled',
'ends_at' => now()->subDay(),
]);

Livewire::actingAs($user)
->test(Dashboard::class)
->assertOk()
->assertSee('No active subscription')
->assertDontSee('badge="Active"');
}

// ========================================
// Sidebar Team Item Tests
// ========================================
Expand Down
53 changes: 52 additions & 1 deletion tests/Feature/Jobs/HandleInvoicePaidJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,56 @@ public function it_does_not_create_license_for_any_subscription(string $planKey)
Bus::assertNotDispatched(CreateAnystackLicenseJob::class);
}

#[Test]
public function it_does_not_auto_set_is_comped_when_invoice_total_is_zero(): void
{
Bus::fake();

$user = User::factory()->create([
'stripe_id' => 'cus_test123',
]);

$priceId = 'price_test_mini';
config(['subscriptions.plans.mini.stripe_price_id' => $priceId]);

$subscription = \Laravel\Cashier\Subscription::factory()
->for($user, 'user')
->create([
'stripe_id' => 'sub_test123',
'stripe_status' => 'active',
'stripe_price' => $priceId,
'quantity' => 1,
'is_comped' => false,
]);

SubscriptionItem::factory()
->for($subscription, 'subscription')
->create([
'stripe_id' => 'si_test123',
'stripe_price' => $priceId,
'quantity' => 1,
]);

$this->mockStripeSubscriptionRetrieve('sub_test123');

$invoice = $this->createStripeInvoice(
customerId: 'cus_test123',
subscriptionId: 'sub_test123',
billingReason: Invoice::BILLING_REASON_SUBSCRIPTION_CREATE,
priceId: $priceId,
subscriptionItemId: 'si_test123',
total: 0,
);

$job = new HandleInvoicePaidJob($invoice);
$job->handle();

$subscription->refresh();

$this->assertFalse((bool) $subscription->is_comped);
$this->assertEquals(0, $subscription->price_paid);
}

public static function subscriptionPlanProvider(): array
{
return [
Expand All @@ -81,14 +131,15 @@ private function createStripeInvoice(
string $billingReason,
string $priceId,
string $subscriptionItemId,
int $total = 25000,
): Invoice {
return Invoice::constructFrom([
'id' => 'in_test_'.uniqid(),
'object' => 'invoice',
'customer' => $customerId,
'subscription' => $subscriptionId,
'billing_reason' => $billingReason,
'total' => 25000,
'total' => $total,
'currency' => 'usd',
'payment_intent' => 'pi_test_'.uniqid(),
'metadata' => [],
Expand Down
Loading