Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
205 lines
7.9 KiB
TypeScript
205 lines
7.9 KiB
TypeScript
"use client";
|
||
|
||
import type { ReactNode } from "react";
|
||
import { Banknote, ChevronLeft, Layers, Wallet } from "lucide-react";
|
||
import { Dialog, DialogContent, DialogDescription, DialogTitle } from "@/app/components/ui/dialog";
|
||
import { Button } from "@/app/components/ui/button";
|
||
import { cn } from "@/app/lib/utils";
|
||
import { useMoney } from "@/app/pos/currency";
|
||
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
||
import type { MemberCheckoutDiscountLine, PosTender } from "@/app/lib/types";
|
||
import type { PosLine } from "@/app/pos/shared";
|
||
|
||
// The POS payment pop-up (redesign-pos-sale-flow, BABYBOO-38): the sale summary on the left; on the right
|
||
// the amount due, one card per configured tender plus Split, and the chosen method's content (passed as
|
||
// children by the page: the reference field, the wallet preview + PIN, or the split panel). While a split
|
||
// payment is pending (`locked`) the dialog cannot be closed and the method cannot change.
|
||
|
||
export type PaymentSelection = { kind: "TENDER"; id: string } | { kind: "SPLIT" };
|
||
|
||
interface PaymentDialogProps {
|
||
open: boolean;
|
||
onOpenChange: (open: boolean) => void;
|
||
locked: boolean;
|
||
customerName: string;
|
||
customerDetail?: string;
|
||
lines: PosLine[];
|
||
discountLines?: MemberCheckoutDiscountLine[];
|
||
total: number;
|
||
tenders: PosTender[];
|
||
selection: PaymentSelection | null;
|
||
onSelect: (selection: PaymentSelection) => void;
|
||
/** False for a walk-in: the wallet tender is members-only. */
|
||
isMember: boolean;
|
||
error: string | null;
|
||
/** False when the method places the sale itself (Split). */
|
||
showComplete: boolean;
|
||
completeDisabled: boolean;
|
||
placing: boolean;
|
||
onComplete: () => void;
|
||
children: ReactNode;
|
||
/** When true, disable wallet tenders and Split (add-gift-card-counter). */
|
||
giftCardsApplied?: boolean;
|
||
}
|
||
|
||
const CARD =
|
||
"flex items-center gap-3 rounded-lg border bg-background p-3 text-left transition-colors hover:border-primary/60 disabled:cursor-not-allowed disabled:opacity-50";
|
||
const ACTIVE = "border-primary ring-2 ring-primary/30";
|
||
|
||
export function PaymentDialog({
|
||
open,
|
||
onOpenChange,
|
||
locked,
|
||
customerName,
|
||
customerDetail,
|
||
lines,
|
||
discountLines,
|
||
total,
|
||
tenders,
|
||
selection,
|
||
onSelect,
|
||
isMember,
|
||
error,
|
||
showComplete,
|
||
completeDisabled,
|
||
placing,
|
||
onComplete,
|
||
children,
|
||
giftCardsApplied,
|
||
}: PaymentDialogProps) {
|
||
const money = useMoney();
|
||
const t = useT();
|
||
const discount = (discountLines ?? []).reduce((sum, d) => sum + d.amount, 0);
|
||
|
||
return (
|
||
<Dialog
|
||
open={open}
|
||
onOpenChange={(next) => {
|
||
if (!next && locked) return;
|
||
onOpenChange(next);
|
||
}}
|
||
>
|
||
<DialogContent
|
||
showCloseButton={!locked}
|
||
onEscapeKeyDown={(e) => {
|
||
if (locked) e.preventDefault();
|
||
}}
|
||
onInteractOutside={(e) => {
|
||
if (locked) e.preventDefault();
|
||
}}
|
||
className="max-h-[calc(100svh-2rem)] gap-0 overflow-y-auto p-0 sm:max-w-4xl"
|
||
>
|
||
<div className="grid md:grid-cols-[18rem_minmax(0,1fr)]">
|
||
<aside data-testid="payment-summary" className="flex flex-col gap-3 border-b bg-muted/40 p-5 md:border-b-0 md:border-r">
|
||
<Button
|
||
type="button"
|
||
variant="link"
|
||
size="sm"
|
||
className="self-start px-0"
|
||
disabled={locked}
|
||
onClick={() => onOpenChange(false)}
|
||
>
|
||
<ChevronLeft aria-hidden="true" className="size-4" />
|
||
{t("PosBackToSale")}
|
||
</Button>
|
||
<DialogTitle className="text-xl">{t("PosStepPayment")}</DialogTitle>
|
||
<DialogDescription className="sr-only">{t("PosPaymentDesc")}</DialogDescription>
|
||
<div className="rounded-lg border bg-background p-3">
|
||
<div className="font-semibold">{customerName}</div>
|
||
{customerDetail && <div className="text-xs text-muted-foreground">{customerDetail}</div>}
|
||
</div>
|
||
<ul className="space-y-1 text-sm">
|
||
{lines.map((l) => (
|
||
<li key={l.key} className="flex justify-between gap-2">
|
||
<span>
|
||
{l.qty}× {l.label}
|
||
</span>
|
||
<span className="tabular-nums">{money(l.unitPrice * l.qty)}</span>
|
||
</li>
|
||
))}
|
||
{discount > 0 && (
|
||
<li className="flex justify-between gap-2 text-positive">
|
||
<span>{t("PosDiscount")}</span>
|
||
<span className="tabular-nums">-{money(discount)}</span>
|
||
</li>
|
||
)}
|
||
</ul>
|
||
<div className="mt-auto flex justify-between border-t pt-3 text-lg font-semibold">
|
||
<span>{t("PosTotal")}</span>
|
||
<span data-testid="payment-total" className="tabular-nums">
|
||
{money(total)}
|
||
</span>
|
||
</div>
|
||
</aside>
|
||
|
||
{/* md:pt-12 keeps the dialog's close button clear of the amount-due panel. */}
|
||
<section className="flex flex-col gap-4 p-5 md:pt-12">
|
||
<div className="rounded-xl bg-primary p-5 text-primary-foreground">
|
||
<div className="text-sm opacity-80">{t("PosAmountDue")}</div>
|
||
<div data-testid="amount-due" className="text-3xl font-bold tabular-nums">
|
||
{money(total)}
|
||
</div>
|
||
</div>
|
||
|
||
<div role="radiogroup" aria-label={t("PosPaymentMethod")} className="grid gap-2 sm:grid-cols-2 lg:grid-cols-3">
|
||
{tenders.map((tender) => {
|
||
const membersOnly = tender.wallet && !isMember;
|
||
const checked = selection?.kind === "TENDER" && selection.id === tender.id;
|
||
return (
|
||
<button
|
||
key={tender.id}
|
||
type="button"
|
||
role="radio"
|
||
aria-checked={checked}
|
||
disabled={locked || membersOnly || (tender.wallet && giftCardsApplied)}
|
||
onClick={() => onSelect({ kind: "TENDER", id: tender.id })}
|
||
className={cn(CARD, checked && ACTIVE)}
|
||
>
|
||
{tender.wallet ? (
|
||
<Wallet aria-hidden="true" className="size-5 shrink-0" />
|
||
) : (
|
||
<Banknote aria-hidden="true" className="size-5 shrink-0" />
|
||
)}
|
||
<span className="flex flex-col">
|
||
<span className="font-semibold">{tender.label}</span>
|
||
{membersOnly && <span className="text-xs text-muted-foreground">{t("PosMembersOnly")}</span>}
|
||
</span>
|
||
</button>
|
||
);
|
||
})}
|
||
<button
|
||
type="button"
|
||
role="radio"
|
||
aria-checked={selection?.kind === "SPLIT"}
|
||
disabled={locked || giftCardsApplied}
|
||
onClick={() => onSelect({ kind: "SPLIT" })}
|
||
className={cn(CARD, selection?.kind === "SPLIT" && ACTIVE)}
|
||
>
|
||
<Layers aria-hidden="true" className="size-5 shrink-0" />
|
||
<span className="flex flex-col">
|
||
<span className="font-semibold">{t("PosSplit")}</span>
|
||
<span className="text-xs text-muted-foreground">{t("PosSplitHint")}</span>
|
||
</span>
|
||
</button>
|
||
</div>
|
||
|
||
<div>{children}</div>
|
||
|
||
{error && (
|
||
<p role="alert" className="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-sm text-destructive">
|
||
{error}
|
||
</p>
|
||
)}
|
||
|
||
{showComplete && (
|
||
<Button type="button" className="mt-auto h-12 w-full text-base" disabled={completeDisabled || placing} onClick={onComplete}>
|
||
{placing ? t("PosCompletingSale") : t("PosCompleteSale")}
|
||
</Button>
|
||
)}
|
||
</section>
|
||
</div>
|
||
</DialogContent>
|
||
</Dialog>
|
||
);
|
||
}
|