AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

hitpay-setup.tsx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import { router } from '@inertiajs/react';
  2. import {
  3. AlertTriangle,
  4. Check,
  5. ExternalLink,
  6. KeyRound,
  7. Unlink,
  8. } from 'lucide-react';
  9. import type { KeyboardEvent } from 'react';
  10. import { useState } from 'react';
  11. import HitPayController from '@/actions/App/Http/Controllers/HitPayController';
  12. import InputError from '@/components/input-error';
  13. import { Badge } from '@/components/ui/badge';
  14. import { Button } from '@/components/ui/button';
  15. import {
  16. Card,
  17. CardContent,
  18. CardDescription,
  19. CardHeader,
  20. CardTitle,
  21. } from '@/components/ui/card';
  22. import { Checkbox } from '@/components/ui/checkbox';
  23. import { Input } from '@/components/ui/input';
  24. import { Label } from '@/components/ui/label';
  25. import { Spinner } from '@/components/ui/spinner';
  26. import type { StoreSummary } from '@/types';
  27. export type HitPayMethod = 'duitnow' | 'touch_n_go' | 'shopee_pay';
  28. export type HitPaySettings = {
  29. connected: boolean;
  30. /** The HitPay methods the merchant ticked. */
  31. methods: HitPayMethod[];
  32. /** Ticked methods HitPay refused when a shopper tried to pay: switched off in the merchant's HitPay account. */
  33. refused: HitPayMethod[];
  34. refused_at: string | null;
  35. /** True when the app talks to HitPay's sandbox (test accounts). */
  36. sandbox: boolean;
  37. };
  38. /** Keep in step with HitPayAccount::METHODS. HitPay's own fees, checked 2 Dec 2026. */
  39. const METHODS: { key: HitPayMethod; label: string; fee: string }[] = [
  40. { key: 'duitnow', label: 'DuitNow QR', fee: '1.2%' },
  41. { key: 'touch_n_go', label: 'Touch ’n Go eWallet', fee: '1.9%' },
  42. { key: 'shopee_pay', label: 'ShopeePay', fee: '2.2%' },
  43. ];
  44. /** "DuitNow QR and Touch ’n Go eWallet". */
  45. function listNames(keys: HitPayMethod[]): string {
  46. const names = METHODS.filter((method) => keys.includes(method.key)).map(
  47. (method) => method.label,
  48. );
  49. return names.length > 1
  50. ? `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
  51. : (names[0] ?? '');
  52. }
  53. /**
  54. * DuitNow QR and e-wallets through the merchant's own HitPay account. The merchant pastes their API key.
  55. * It sits inside the Settings form, so its buttons never submit that form.
  56. */
  57. export function HitPaySetup({
  58. store,
  59. hitpay,
  60. }: {
  61. store: StoreSummary;
  62. hitpay: HitPaySettings;
  63. }) {
  64. const [apiKey, setApiKey] = useState('');
  65. const [changingKey, setChangingKey] = useState(false);
  66. const [methods, setMethods] = useState<HitPayMethod[]>(hitpay.methods);
  67. const [busy, setBusy] = useState<'connect' | 'save' | 'disconnect' | null>(
  68. null,
  69. );
  70. const [errors, setErrors] = useState<Record<string, string>>({});
  71. const dashboardUrl = hitpay.sandbox
  72. ? 'https://dashboard.sandbox.hit-pay.com'
  73. : 'https://dashboard.hit-pay.com';
  74. const signUpUrl = hitpay.sandbox
  75. ? 'https://dashboard.sandbox.hit-pay.com/register'
  76. : 'https://hitpayapp.com/my';
  77. const send = (
  78. kind: 'connect' | 'save' | 'disconnect',
  79. visit: (options: {
  80. preserveScroll: true;
  81. onStart: () => void;
  82. onError: (errors: Record<string, string>) => void;
  83. onSuccess: () => void;
  84. onFinish: () => void;
  85. }) => void,
  86. ) =>
  87. visit({
  88. preserveScroll: true,
  89. onStart: () => {
  90. setBusy(kind);
  91. setErrors({});
  92. },
  93. onError: (next) => setErrors(next),
  94. onSuccess: () => {
  95. setApiKey('');
  96. setChangingKey(false);
  97. },
  98. onFinish: () => setBusy(null),
  99. });
  100. const connect = () =>
  101. send('connect', (options) =>
  102. router.post(
  103. HitPayController.connect.url(store.slug),
  104. { api_key: apiKey },
  105. options,
  106. ),
  107. );
  108. const save = () =>
  109. send('save', (options) =>
  110. router.put(
  111. HitPayController.update.url(store.slug),
  112. { methods },
  113. options,
  114. ),
  115. );
  116. const disconnect = () => {
  117. if (
  118. !window.confirm(
  119. 'Disconnect HitPay? Shoppers will no longer see DuitNow QR, Touch ’n Go or ShopeePay.',
  120. )
  121. ) {
  122. return;
  123. }
  124. send('disconnect', (options) =>
  125. router.delete(HitPayController.disconnect.url(store.slug), options),
  126. );
  127. };
  128. // Enter in the key box connects, instead of saving the whole Settings form.
  129. const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
  130. if (event.key === 'Enter') {
  131. event.preventDefault();
  132. connect();
  133. }
  134. };
  135. const keyField = (
  136. <div className="grid gap-2">
  137. <Label htmlFor="hitpay-api-key">HitPay API key</Label>
  138. <div className="flex flex-col gap-2 sm:flex-row">
  139. <Input
  140. id="hitpay-api-key"
  141. type="password"
  142. autoComplete="off"
  143. spellCheck={false}
  144. value={apiKey}
  145. onChange={(event) => setApiKey(event.target.value)}
  146. onKeyDown={onKeyDown}
  147. placeholder="Paste your key"
  148. aria-invalid={errors.api_key ? true : undefined}
  149. data-test="hitpay-api-key"
  150. />
  151. <Button
  152. type="button"
  153. onClick={connect}
  154. disabled={busy !== null || apiKey.trim() === ''}
  155. data-test="hitpay-connect"
  156. >
  157. {busy === 'connect' ? <Spinner /> : <KeyRound />}
  158. Connect HitPay
  159. </Button>
  160. </div>
  161. <InputError message={errors.api_key} />
  162. <p className="text-xs text-muted-foreground">
  163. We check the key with HitPay before saving it. It is stored
  164. encrypted and never shown again.
  165. </p>
  166. </div>
  167. );
  168. return (
  169. <Card id="hitpay" data-test="hitpay">
  170. <CardHeader>
  171. <CardTitle className="flex flex-wrap items-center gap-2">
  172. DuitNow QR and e-wallets
  173. {hitpay.connected && (
  174. <Badge className="bg-emerald-600 text-white hover:bg-emerald-600">
  175. <Check className="size-3" />
  176. Connected
  177. </Badge>
  178. )}
  179. </CardTitle>
  180. <CardDescription>
  181. Let shoppers pay by DuitNow QR, Touch ’n Go or ShopeePay,
  182. through your own HitPay account. HitPay pays it into your
  183. bank. We take no cut. You only pay HitPay’s fees.
  184. </CardDescription>
  185. </CardHeader>
  186. <CardContent className="space-y-5 text-sm">
  187. {hitpay.sandbox && (
  188. <p
  189. className="rounded-md bg-muted p-3 text-xs"
  190. data-test="hitpay-sandbox"
  191. >
  192. Test mode: this app uses HitPay’s sandbox, so connect a
  193. sandbox account. No real money moves.
  194. </p>
  195. )}
  196. {!hitpay.connected ? (
  197. <>
  198. <ol className="list-decimal space-y-1.5 pl-5 text-muted-foreground">
  199. <li>
  200. <a
  201. href={signUpUrl}
  202. target="_blank"
  203. rel="noreferrer"
  204. className="font-medium text-foreground underline underline-offset-4"
  205. >
  206. Sign up for HitPay
  207. </a>{' '}
  208. as a Malaysian business. It is free, with no
  209. monthly fee.
  210. </li>
  211. <li>
  212. In HitPay, open{' '}
  213. <span className="font-medium text-foreground">
  214. Payment Methods
  215. </span>{' '}
  216. and click Turn On for Touch ’n Go &amp; DuitNow
  217. and for ShopeePay. New HitPay accounts start
  218. with every method off.
  219. </li>
  220. <li>
  221. Open Developers, then API Keys, and copy your
  222. API key.
  223. </li>
  224. <li>Paste it below.</li>
  225. </ol>
  226. {keyField}
  227. </>
  228. ) : (
  229. <>
  230. {hitpay.refused.length > 0 && (
  231. <div
  232. role="alert"
  233. 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"
  234. data-test="hitpay-refused"
  235. >
  236. <AlertTriangle className="mt-0.5 size-4 shrink-0" />
  237. <div className="space-y-1">
  238. <p className="font-medium">
  239. HitPay turned down{' '}
  240. {listNames(hitpay.refused)}
  241. {hitpay.refused_at &&
  242. ` on ${new Date(hitpay.refused_at).toLocaleString('en-MY', { day: 'numeric', month: 'short', hour: 'numeric', minute: '2-digit' })}`}
  243. </p>
  244. <p>
  245. {hitpay.refused.length === 1
  246. ? 'It is'
  247. : 'They are'}{' '}
  248. switched off in your HitPay account, so
  249. shoppers do not see{' '}
  250. {hitpay.refused.length === 1
  251. ? 'it'
  252. : 'them'}{' '}
  253. for now. In HitPay, open Payment Methods
  254. and click Turn On, then click Save here.
  255. Or untick{' '}
  256. {hitpay.refused.length === 1
  257. ? 'it'
  258. : 'them'}{' '}
  259. and Save.
  260. </p>
  261. </div>
  262. </div>
  263. )}
  264. <fieldset className="space-y-3">
  265. <legend className="font-medium">
  266. Shoppers can pay by
  267. </legend>
  268. {METHODS.map((method) => (
  269. <div
  270. key={method.key}
  271. className="flex items-center gap-3"
  272. >
  273. <Checkbox
  274. id={`hitpay-${method.key}`}
  275. checked={methods.includes(method.key)}
  276. onCheckedChange={(checked) =>
  277. setMethods((current) =>
  278. checked === true
  279. ? [...current, method.key]
  280. : current.filter(
  281. (key) =>
  282. key !==
  283. method.key,
  284. ),
  285. )
  286. }
  287. />
  288. <Label
  289. htmlFor={`hitpay-${method.key}`}
  290. className="font-normal"
  291. >
  292. {method.label}
  293. <span className="ml-2 text-muted-foreground tabular-nums">
  294. {method.fee} fee
  295. </span>
  296. </Label>
  297. </div>
  298. ))}
  299. <InputError message={errors.methods} />
  300. <p className="text-xs text-muted-foreground">
  301. Each one must also be switched on in your HitPay
  302. account: in HitPay, open Payment Methods and
  303. click Turn On. New HitPay accounts start with
  304. every method off.
  305. </p>
  306. </fieldset>
  307. <div className="flex flex-wrap gap-2">
  308. <Button
  309. type="button"
  310. onClick={save}
  311. disabled={busy !== null}
  312. data-test="hitpay-save"
  313. >
  314. {busy === 'save' && <Spinner />}
  315. Save
  316. </Button>
  317. <Button type="button" variant="outline" asChild>
  318. <a
  319. href={dashboardUrl}
  320. target="_blank"
  321. rel="noreferrer"
  322. >
  323. <ExternalLink />
  324. Open HitPay
  325. </a>
  326. </Button>
  327. {!changingKey && (
  328. <Button
  329. type="button"
  330. variant="outline"
  331. onClick={() => setChangingKey(true)}
  332. >
  333. <KeyRound />
  334. Change key
  335. </Button>
  336. )}
  337. <Button
  338. type="button"
  339. variant="ghost"
  340. onClick={disconnect}
  341. disabled={busy !== null}
  342. className="text-red-700 hover:text-red-800 dark:text-red-400"
  343. data-test="hitpay-disconnect"
  344. >
  345. {busy === 'disconnect' ? (
  346. <Spinner />
  347. ) : (
  348. <Unlink />
  349. )}
  350. Disconnect
  351. </Button>
  352. </div>
  353. {changingKey && keyField}
  354. <p className="text-xs text-muted-foreground">
  355. Refunds and payouts are managed in your HitPay
  356. dashboard. DuitNow payments cannot be refunded
  357. through HitPay.
  358. </p>
  359. </>
  360. )}
  361. </CardContent>
  362. </Card>
  363. );
  364. }