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(hitpay.methods); const [busy, setBusy] = useState<'connect' | 'save' | 'disconnect' | null>( null, ); const [errors, setErrors] = useState>({}); 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) => 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) => { if (event.key === 'Enter') { event.preventDefault(); connect(); } }; const keyField = (
setApiKey(event.target.value)} onKeyDown={onKeyDown} placeholder="Paste your key" aria-invalid={errors.api_key ? true : undefined} data-test="hitpay-api-key" />

We check the key with HitPay before saving it. It is stored encrypted and never shown again.

); return ( DuitNow QR and e-wallets {hitpay.connected && ( Connected )} 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. {hitpay.sandbox && (

Test mode: this app uses HitPay’s sandbox, so connect a sandbox account. No real money moves.

)} {!hitpay.connected ? ( <>
  1. Sign up for HitPay {' '} as a Malaysian business. It is free, with no monthly fee.
  2. In HitPay, open{' '} Payment Methods {' '} and click Turn On for Touch ’n Go & DuitNow and for ShopeePay. New HitPay accounts start with every method off.
  3. Open Developers, then API Keys, and copy your API key.
  4. Paste it below.
{keyField} ) : ( <> {hitpay.refused.length > 0 && (

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' })}`}

{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.

)}
Shoppers can pay by {METHODS.map((method) => (
setMethods((current) => checked === true ? [...current, method.key] : current.filter( (key) => key !== method.key, ), ) } />
))}

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.

{!changingKey && ( )}
{changingKey && keyField}

Refunds and payouts are managed in your HitPay dashboard. DuitNow payments cannot be refunded through HitPay.

)}
); }