This commit is contained in:
66
src/components/billing/payment-status-banner.tsx
Normal file
66
src/components/billing/payment-status-banner.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
/**
|
||||
* Banner shown after a return from Stripe Checkout.
|
||||
*
|
||||
* ?paid=1 → green success banner. The webhook may or may
|
||||
* not have processed yet, so we phrase the message
|
||||
* as "Payment received, status will update shortly"
|
||||
* and don't claim the status is already paid. A
|
||||
* light auto-refresh after a few seconds nudges
|
||||
* the page to pick up the new status badge.
|
||||
*
|
||||
* ?cancelled=1 → neutral grey banner: "Payment cancelled". The
|
||||
* invoice stays in 'open' state.
|
||||
*
|
||||
* The banner cleans up the query params from the URL so a page
|
||||
* reload doesn't repeat the message. We use router.replace() to
|
||||
* keep history clean.
|
||||
*/
|
||||
export function PaymentStatusBanner() {
|
||||
const router = useRouter();
|
||||
const t = useTranslations("customerBilling");
|
||||
const [state, setState] = useState<"paid" | "cancelled" | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (params.has("paid")) {
|
||||
setState("paid");
|
||||
// Reload after 4s so the status badge picks up the webhook's
|
||||
// effect on the invoice row. By then most webhook deliveries
|
||||
// have landed; if not the user just sees "open" and can
|
||||
// manually refresh.
|
||||
const timer = setTimeout(() => {
|
||||
router.refresh();
|
||||
}, 4000);
|
||||
// Strip the query string out of the URL.
|
||||
const cleanUrl = window.location.pathname;
|
||||
window.history.replaceState({}, "", cleanUrl);
|
||||
return () => clearTimeout(timer);
|
||||
} else if (params.has("cancelled")) {
|
||||
setState("cancelled");
|
||||
const cleanUrl = window.location.pathname;
|
||||
window.history.replaceState({}, "", cleanUrl);
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
if (state === "paid") {
|
||||
return (
|
||||
<div className="mb-4 p-3 rounded-md border border-success bg-success/10 text-sm text-success">
|
||||
{t("paymentReceived")}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (state === "cancelled") {
|
||||
return (
|
||||
<div className="mb-4 p-3 rounded-md border border-border bg-surface-2 text-sm text-text-secondary">
|
||||
{t("paymentCancelled")}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user