diff --git a/composer.json b/composer.json index 7c8d2ea..12ee5be 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "saucebase/core": "^3.0", + "saucebase/core": "^3.1", "stripe/stripe-php": "^22.0" }, "license": "MIT", @@ -29,7 +29,7 @@ ] }, "branch-alias": { - "dev-dev": "3.5.x-dev" + "dev-dev": "3.x-dev" } }, "autoload": { diff --git a/database/seeders/ShowcaseBillingDatabaseSeeder.php b/database/seeders/ShowcaseBillingDatabaseSeeder.php index f96a56f..5d4afbc 100644 --- a/database/seeders/ShowcaseBillingDatabaseSeeder.php +++ b/database/seeders/ShowcaseBillingDatabaseSeeder.php @@ -7,6 +7,7 @@ use Modules\Billing\Models\Product; use Modules\Billing\Services\CatalogPush; use Modules\Billing\Services\PaymentGatewayManager; +use Saucebase\Core\Attributes\DemoAccount; use Spatie\Permission\Models\Role; /** @@ -16,6 +17,7 @@ * created locally and pushed to the provider when its keys are present, which * is what makes the demo site's checkout work. */ +#[DemoAccount('billing@saucebase.dev', 'staff: the billing admin')] class ShowcaseBillingDatabaseSeeder extends Seeder { public function run(): void diff --git a/resources/boost/skills/saucebase-billing-development/SKILL.md b/resources/boost/skills/saucebase-billing-development/SKILL.md index a4945ae..a4c4e81 100644 --- a/resources/boost/skills/saucebase-billing-development/SKILL.md +++ b/resources/boost/skills/saucebase-billing-development/SKILL.md @@ -90,7 +90,7 @@ On return, the provider sends the buyer to `settings.billing?checkout_session={o - `SubscriptionTrialWillEnd` → mirrors trial dates and fires `TrialEnding` - `PaymentMethodAttached` / `PaymentMethodDetached` → keeps cards added or removed in the provider's portal in step, and clears a subscription pointing at a removed one. Attaching never makes a card the default - `CustomerUpdated` → the default card follows `invoice_settings.default_payment_method`, cleared when the provider clears it - - `InvoicePaid` → creates/updates Invoice, syncs subscription period dates + - `InvoicePaid` → creates/updates Invoice, syncs subscription period dates through `applyIfCurrent()` (a late invoice for an earlier period is skipped, and a Cancelled row is left alone; it does not stamp `last_event_at`, so `periodForwardOnly` also skips one whose period ends before the stored one: a delayed January invoice after February's) ### Subscription Lifecycle @@ -164,7 +164,7 @@ The **data contract is provider-neutral**: incoming webhooks, `retrieveSubscript It is **not** a drop-in for any provider yet: optional operations (portal, plan change) have no capability flags, payment identity across checkout/payment/refund is Stripe-shaped (an invoice ID stands in when there is no payment intent), and running two providers at once is sc-788. `NeutralGatewayTest` is the proof of the contract — a fake provider that speaks only the module's data. ### Payment Correlation -Payments are matched by provider identity, never by time. `createPaymentFromWebhook()` looks for an existing row by `provider_payment_id` (so a failed invoice that later succeeds updates the same record), then for the subscription's checkout-created payment that has no intent yet. An invoice whose subscription does not exist locally throws, and the retry lands after `CheckoutCompleted` has created it. +Payments are matched by provider identity, never by time. Checkout records a subscription's first payment under its invoice (`CheckoutSessionData::providerInvoiceId`, Stripe's session `invoice`), as the payment intent is not known yet. `createPaymentFromWebhook()` looks for an existing row by `provider_payment_id` (so a failed invoice that later succeeds updates the same record), then by the event's `providerInvoiceId`, and replaces checkout's stand-in with the intent. A gateway that names no invoice gets a second row for the first payment. An invoice whose subscription does not exist locally throws, and the retry lands after `CheckoutCompleted` has created it. ## ENV Variables @@ -264,7 +264,7 @@ purpose allows it by pattern, e.g. - `Product::description` is rich-editor HTML, also imported from the provider, rendered with `v-html` on the pricing and checkout pages. It is cast with core's `SanitizedHtml` (`Str::sanitizeHtml()` on read, stored as written), so every read is safe markup (sc-1131) - `CheckoutSession` uses `uuid` as the route key (not `id`) — always resolve via UUID in URLs - Every `provider_*_id` is namespaced by a `provider` column (the gateway driver slug). Look rows up with both, never by the provider ID alone; uniqueness is `(provider, provider_x_id)` -- `subscriptions.last_event_at` is when the last applied provider event happened. `SubscriptionUpdated`/`Deleted` skip events older than it, so a late delivery cannot roll state back +- `subscriptions.last_event_at` is when the last applied provider event happened. `SubscriptionUpdated`/`Deleted`, `TrialWillEnd` and `InvoicePaid`'s period sync skip events older than it, so a late delivery cannot roll state back - Webhook signature verification happens inside `StripeGateway::verifyAndParseWebhook()` before deduplication — a bad signature throws `InvalidWebhookSignatureException`, which `WebhookController` answers with an empty 400. Any other exception is an empty 500 so Stripe retries; do not catch and acknowledge (see *Errors*) - Stripe API versions from 2025-03-31 keep `current_period_start/end` on the subscription **item**, not the subscription. `StripeEventMapper::subscription()` checks both; nothing else reads periods from Stripe - Webhook tests build deliveries with `Tests\Support\StripeWebhook::make()`, which runs Stripe-shaped fixtures through the real mapper — mapper + service integration, not Stripe end to end. It defaults a completed checkout to `payment_status: paid`, as Stripe always sends one diff --git a/resources/js/lib/intervals.test.ts b/resources/js/lib/intervals.test.ts index 99d5456..b1fa5e6 100644 --- a/resources/js/lib/intervals.test.ts +++ b/resources/js/lib/intervals.test.ts @@ -4,6 +4,7 @@ import { getIntervalLabel, matchesInterval, normalizeInterval, + pickInterval, } from './intervals'; describe('intervals', () => { @@ -37,3 +38,18 @@ describe('intervals', () => { expect(getIntervalLabel('quarter')).toBe('quarter'); }); }); + +describe('pickInterval', () => { + it('keeps the current interval while it is offered', () => { + expect(pickInterval(['month', 'year'], 'year')).toBe('year'); + }); + + /** Decided while rendering, so the server's first paint selects a toggle that has cards. */ + it('falls back to the first offered interval', () => { + expect(pickInterval(['year', 'one_time'], 'month')).toBe('year'); + }); + + it('keeps the current one when nothing is offered', () => { + expect(pickInterval([], 'month')).toBe('month'); + }); +}); diff --git a/resources/js/lib/intervals.ts b/resources/js/lib/intervals.ts index a7978fa..e43a0a8 100644 --- a/resources/js/lib/intervals.ts +++ b/resources/js/lib/intervals.ts @@ -32,3 +32,13 @@ export function matchesInterval( export function getIntervalLabel(interval: string): string { return INTERVAL_CONFIG[interval]?.label || interval; } + +/** + * The toggle's interval: the current one while it is offered, else the first offered. + * Decided while rendering (not in an effect), so the server's first paint agrees. + */ +export function pickInterval(available: string[], current: string): string { + return available.length === 0 || available.includes(current) + ? current + : available[0]; +} diff --git a/resources/js/react/components/ProductSection.tsx b/resources/js/react/components/ProductSection.tsx index 82142cf..f0a4a9e 100644 --- a/resources/js/react/components/ProductSection.tsx +++ b/resources/js/react/components/ProductSection.tsx @@ -1,5 +1,5 @@ import { useT } from '@saucebase/react/i18n'; -import { useEffect, useMemo, useState, type ReactNode } from 'react'; +import { useMemo, useState, type ReactNode } from 'react'; import type { PlanAction, @@ -10,6 +10,7 @@ import { getIntervalLabel, matchesInterval, normalizeInterval, + pickInterval, } from '../../lib/intervals'; import ProductCard from './ProductCard'; @@ -60,17 +61,10 @@ export default function ProductSection({ ); }, [products]); - const [billingInterval, setBillingInterval] = useState('month'); - - // Set default to first available interval when products change - useEffect(() => { - if ( - availableIntervals.length > 0 && - !availableIntervals.includes(billingInterval) - ) { - setBillingInterval(availableIntervals[0]); - } - }, [availableIntervals, billingInterval]); + const [chosenInterval, setChosenInterval] = useState('month'); + // Derived while rendering, so the server's first paint selects an offered interval; + // an effect only ran in the browser, after a paint of the wrong (empty) toggle. + const billingInterval = pickInterval(availableIntervals, chosenInterval); const filteredProducts = useMemo( () => @@ -113,7 +107,7 @@ export default function ProductSection({ {availableIntervals.map((interval) => (