| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- import { router } from '@inertiajs/react';
- import {
- AlertTriangle,
- Check,
- ExternalLink,
- KeyRound,
- Unlink,
- } from 'lucide-react';
- import type { KeyboardEvent } from 'react';
- import { useState } from 'react';
- import HitPayController from '@/actions/App/Http/Controllers/HitPayController';
- import InputError from '@/components/input-error';
- import { Badge } from '@/components/ui/badge';
- import { Button } from '@/components/ui/button';
- import {
- Card,
- CardContent,
- CardDescription,
- CardHeader,
- CardTitle,
- } from '@/components/ui/card';
- import { Checkbox } from '@/components/ui/checkbox';
- import { Input } from '@/components/ui/input';
- import { Label } from '@/components/ui/label';
- import { Spinner } from '@/components/ui/spinner';
- import type { StoreSummary } from '@/types';
-
- export type HitPayMethod = 'duitnow' | 'touch_n_go' | 'shopee_pay';
-
- export type HitPaySettings = {
- connected: boolean;
- /** The HitPay methods the merchant ticked. */
- methods: HitPayMethod[];
- /** Ticked methods HitPay refused when a shopper tried to pay: switched off in the merchant's HitPay account. */
- refused: HitPayMethod[];
- refused_at: string | null;
- /** True when the app talks to HitPay's sandbox (test accounts). */
- sandbox: boolean;
- };
-
- /** Keep in step with HitPayAccount::METHODS. HitPay's own fees, checked 2 Dec 2026. */
- const METHODS: { key: HitPayMethod; label: string; fee: string }[] = [
- { key: 'duitnow', label: 'DuitNow QR', fee: '1.2%' },
- { key: 'touch_n_go', label: 'Touch ’n Go eWallet', fee: '1.9%' },
- { key: 'shopee_pay', label: 'ShopeePay', fee: '2.2%' },
- ];
-
- /** "DuitNow QR and Touch ’n Go eWallet". */
- function listNames(keys: HitPayMethod[]): string {
- const names = METHODS.filter((method) => keys.includes(method.key)).map(
- (method) => method.label,
- );
-
- return names.length > 1
- ? `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
- : (names[0] ?? '');
- }
-
- /**
- * DuitNow QR and e-wallets through the merchant's own HitPay account. The merchant pastes their API key.
- * It sits inside the Settings form, so its buttons never submit that form.
- */
- export function HitPaySetup({
- store,
- hitpay,
- }: {
- store: StoreSummary;
- hitpay: HitPaySettings;
- }) {
- const [apiKey, setApiKey] = useState('');
- const [changingKey, setChangingKey] = useState(false);
- const [methods, setMethods] = useState<HitPayMethod[]>(hitpay.methods);
- const [busy, setBusy] = useState<'connect' | 'save' | 'disconnect' | null>(
- null,
- );
- const [errors, setErrors] = useState<Record<string, string>>({});
-
- const dashboardUrl = hitpay.sandbox
- ? 'https://dashboard.sandbox.hit-pay.com'
- : 'https://dashboard.hit-pay.com';
- const signUpUrl = hitpay.sandbox
- ? 'https://dashboard.sandbox.hit-pay.com/register'
- : 'https://hitpayapp.com/my';
-
- const send = (
- kind: 'connect' | 'save' | 'disconnect',
- visit: (options: {
- preserveScroll: true;
- onStart: () => void;
- onError: (errors: Record<string, string>) => void;
- onSuccess: () => void;
- onFinish: () => void;
- }) => void,
- ) =>
- visit({
- preserveScroll: true,
- onStart: () => {
- setBusy(kind);
- setErrors({});
- },
- onError: (next) => setErrors(next),
- onSuccess: () => {
- setApiKey('');
- setChangingKey(false);
- },
- onFinish: () => setBusy(null),
- });
-
- const connect = () =>
- send('connect', (options) =>
- router.post(
- HitPayController.connect.url(store.slug),
- { api_key: apiKey },
- options,
- ),
- );
-
- const save = () =>
- send('save', (options) =>
- router.put(
- HitPayController.update.url(store.slug),
- { methods },
- options,
- ),
- );
-
- const disconnect = () => {
- if (
- !window.confirm(
- 'Disconnect HitPay? Shoppers will no longer see DuitNow QR, Touch ’n Go or ShopeePay.',
- )
- ) {
- return;
- }
-
- send('disconnect', (options) =>
- router.delete(HitPayController.disconnect.url(store.slug), options),
- );
- };
-
- // Enter in the key box connects, instead of saving the whole Settings form.
- const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
- if (event.key === 'Enter') {
- event.preventDefault();
- connect();
- }
- };
-
- const keyField = (
- <div className="grid gap-2">
- <Label htmlFor="hitpay-api-key">HitPay API key</Label>
- <div className="flex flex-col gap-2 sm:flex-row">
- <Input
- id="hitpay-api-key"
- type="password"
- autoComplete="off"
- spellCheck={false}
- value={apiKey}
- onChange={(event) => setApiKey(event.target.value)}
- onKeyDown={onKeyDown}
- placeholder="Paste your key"
- aria-invalid={errors.api_key ? true : undefined}
- data-test="hitpay-api-key"
- />
- <Button
- type="button"
- onClick={connect}
- disabled={busy !== null || apiKey.trim() === ''}
- data-test="hitpay-connect"
- >
- {busy === 'connect' ? <Spinner /> : <KeyRound />}
- Connect HitPay
- </Button>
- </div>
- <InputError message={errors.api_key} />
- <p className="text-xs text-muted-foreground">
- We check the key with HitPay before saving it. It is stored
- encrypted and never shown again.
- </p>
- </div>
- );
-
- return (
- <Card id="hitpay" data-test="hitpay">
- <CardHeader>
- <CardTitle className="flex flex-wrap items-center gap-2">
- DuitNow QR and e-wallets
- {hitpay.connected && (
- <Badge className="bg-emerald-600 text-white hover:bg-emerald-600">
- <Check className="size-3" />
- Connected
- </Badge>
- )}
- </CardTitle>
- <CardDescription>
- Let shoppers pay by DuitNow QR, Touch ’n Go or ShopeePay,
- through your own HitPay account. HitPay pays it into your
- bank. We take no cut. You only pay HitPay’s fees.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-5 text-sm">
- {hitpay.sandbox && (
- <p
- className="rounded-md bg-muted p-3 text-xs"
- data-test="hitpay-sandbox"
- >
- Test mode: this app uses HitPay’s sandbox, so connect a
- sandbox account. No real money moves.
- </p>
- )}
-
- {!hitpay.connected ? (
- <>
- <ol className="list-decimal space-y-1.5 pl-5 text-muted-foreground">
- <li>
- <a
- href={signUpUrl}
- target="_blank"
- rel="noreferrer"
- className="font-medium text-foreground underline underline-offset-4"
- >
- Sign up for HitPay
- </a>{' '}
- as a Malaysian business. It is free, with no
- monthly fee.
- </li>
- <li>
- In HitPay, open{' '}
- <span className="font-medium text-foreground">
- Payment Methods
- </span>{' '}
- and click Turn On for Touch ’n Go & DuitNow
- and for ShopeePay. New HitPay accounts start
- with every method off.
- </li>
- <li>
- Open Developers, then API Keys, and copy your
- API key.
- </li>
- <li>Paste it below.</li>
- </ol>
- {keyField}
- </>
- ) : (
- <>
- {hitpay.refused.length > 0 && (
- <div
- role="alert"
- className="flex items-start gap-2 rounded-md border border-amber-300 bg-amber-50 p-3 text-amber-900 dark:border-amber-800 dark:bg-amber-950/40 dark:text-amber-200"
- data-test="hitpay-refused"
- >
- <AlertTriangle className="mt-0.5 size-4 shrink-0" />
- <div className="space-y-1">
- <p className="font-medium">
- HitPay turned down{' '}
- {listNames(hitpay.refused)}
- {hitpay.refused_at &&
- ` on ${new Date(hitpay.refused_at).toLocaleString('en-MY', { day: 'numeric', month: 'short', hour: 'numeric', minute: '2-digit' })}`}
- </p>
- <p>
- {hitpay.refused.length === 1
- ? 'It is'
- : 'They are'}{' '}
- switched off in your HitPay account, so
- shoppers do not see{' '}
- {hitpay.refused.length === 1
- ? 'it'
- : 'them'}{' '}
- for now. In HitPay, open Payment Methods
- and click Turn On, then click Save here.
- Or untick{' '}
- {hitpay.refused.length === 1
- ? 'it'
- : 'them'}{' '}
- and Save.
- </p>
- </div>
- </div>
- )}
-
- <fieldset className="space-y-3">
- <legend className="font-medium">
- Shoppers can pay by
- </legend>
- {METHODS.map((method) => (
- <div
- key={method.key}
- className="flex items-center gap-3"
- >
- <Checkbox
- id={`hitpay-${method.key}`}
- checked={methods.includes(method.key)}
- onCheckedChange={(checked) =>
- setMethods((current) =>
- checked === true
- ? [...current, method.key]
- : current.filter(
- (key) =>
- key !==
- method.key,
- ),
- )
- }
- />
- <Label
- htmlFor={`hitpay-${method.key}`}
- className="font-normal"
- >
- {method.label}
- <span className="ml-2 text-muted-foreground tabular-nums">
- {method.fee} fee
- </span>
- </Label>
- </div>
- ))}
- <InputError message={errors.methods} />
- <p className="text-xs text-muted-foreground">
- Each one must also be switched on in your HitPay
- account: in HitPay, open Payment Methods and
- click Turn On. New HitPay accounts start with
- every method off.
- </p>
- </fieldset>
-
- <div className="flex flex-wrap gap-2">
- <Button
- type="button"
- onClick={save}
- disabled={busy !== null}
- data-test="hitpay-save"
- >
- {busy === 'save' && <Spinner />}
- Save
- </Button>
- <Button type="button" variant="outline" asChild>
- <a
- href={dashboardUrl}
- target="_blank"
- rel="noreferrer"
- >
- <ExternalLink />
- Open HitPay
- </a>
- </Button>
- {!changingKey && (
- <Button
- type="button"
- variant="outline"
- onClick={() => setChangingKey(true)}
- >
- <KeyRound />
- Change key
- </Button>
- )}
- <Button
- type="button"
- variant="ghost"
- onClick={disconnect}
- disabled={busy !== null}
- className="text-red-700 hover:text-red-800 dark:text-red-400"
- data-test="hitpay-disconnect"
- >
- {busy === 'disconnect' ? (
- <Spinner />
- ) : (
- <Unlink />
- )}
- Disconnect
- </Button>
- </div>
-
- {changingKey && keyField}
-
- <p className="text-xs text-muted-foreground">
- Refunds and payouts are managed in your HitPay
- dashboard. DuitNow payments cannot be refunded
- through HitPay.
- </p>
- </>
- )}
- </CardContent>
- </Card>
- );
- }
|