@vicacha-devs/medusa-payment-bold 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/.medusa/server/src/admin/index.cjs +1732 -0
  2. package/.medusa/server/src/admin/index.js +1732 -0
  3. package/.medusa/server/src/api/admin/bold/banks/route.js +15 -0
  4. package/.medusa/server/src/api/admin/bold/payment-button/route.js +44 -0
  5. package/.medusa/server/src/api/admin/bold/payment-link/route.js +61 -0
  6. package/.medusa/server/src/api/admin/bold/payment-link-methods/route.js +16 -0
  7. package/.medusa/server/src/api/admin/bold/payment-methods/route.js +17 -0
  8. package/.medusa/server/src/api/admin/bold/payment-qr/route.js +53 -0
  9. package/.medusa/server/src/api/admin/bold/payment-status/route.js +37 -0
  10. package/.medusa/server/src/api/admin/bold/payment-stream/route.js +103 -0
  11. package/.medusa/server/src/api/admin/bold/pos-push/route.js +46 -0
  12. package/.medusa/server/src/api/admin/bold/refund/route.js +37 -0
  13. package/.medusa/server/src/api/admin/bold/terminals/route.js +16 -0
  14. package/.medusa/server/src/api/admin/plugin/route.js +10 -0
  15. package/.medusa/server/src/api/hooks/payment/bold/route.js +104 -0
  16. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  17. package/.medusa/server/src/hooks/index.js +10 -0
  18. package/.medusa/server/src/hooks/use-bold-checkout-handler.js +135 -0
  19. package/.medusa/server/src/hooks/use-bold-payment-methods.js +20 -0
  20. package/.medusa/server/src/hooks/use-bold-payment-status-stream.js +19 -0
  21. package/.medusa/server/src/hooks/use-bold-refund.js +19 -0
  22. package/.medusa/server/src/hooks/use-bold-terminals.js +13 -0
  23. package/.medusa/server/src/hooks/use-create-bold-button.js +19 -0
  24. package/.medusa/server/src/hooks/use-create-bold-link.js +19 -0
  25. package/.medusa/server/src/hooks/use-create-bold-qr.js +19 -0
  26. package/.medusa/server/src/hooks/use-create-bold-terminal.js +19 -0
  27. package/.medusa/server/src/index.js +21 -0
  28. package/.medusa/server/src/lib/error-utils.js +25 -0
  29. package/.medusa/server/src/lib/get-active-providers.js +34 -0
  30. package/.medusa/server/src/lib/get-provider-options.js +20 -0
  31. package/.medusa/server/src/lib/index.js +4 -0
  32. package/.medusa/server/src/lib/session-helpers.js +11 -0
  33. package/.medusa/server/src/providers/bold-button/index.js +157 -0
  34. package/.medusa/server/src/providers/bold-link/index.js +249 -0
  35. package/.medusa/server/src/providers/bold-online/index.js +169 -0
  36. package/.medusa/server/src/providers/bold-terminal/index.js +170 -0
  37. package/.medusa/server/src/rbac.js +58 -0
  38. package/.medusa/server/src/sdk/client.js +112 -0
  39. package/.medusa/server/src/sdk/query-keys.js +13 -0
  40. package/.medusa/server/src/services/bold-client.js +304 -0
  41. package/.medusa/server/src/services/integrity.js +54 -0
  42. package/.medusa/server/src/types/constants.js +55 -0
  43. package/.medusa/server/src/types/enums.js +15 -0
  44. package/.medusa/server/src/types/errors.js +80 -0
  45. package/.medusa/server/src/types/index.js +3 -0
  46. package/.medusa/server/src/workflows/create-bold-payment-link.js +7 -0
  47. package/.medusa/server/src/workflows/create-bold-refund.js +7 -0
  48. package/.medusa/server/src/workflows/get-bold-payment-methods.js +7 -0
  49. package/.medusa/server/src/workflows/get-integration-api-payment-methods.js +7 -0
  50. package/.medusa/server/src/workflows/get-payment-link-methods.js +7 -0
  51. package/.medusa/server/src/workflows/get-terminals.js +7 -0
  52. package/.medusa/server/src/workflows/index.js +7 -0
  53. package/.medusa/server/src/workflows/steps/create-bold-link.js +43 -0
  54. package/.medusa/server/src/workflows/steps/get-bold-payment-methods.js +12 -0
  55. package/.medusa/server/src/workflows/steps/get-payment-link-methods.js +12 -0
  56. package/.medusa/server/src/workflows/steps/get-terminals.js +13 -0
  57. package/.medusa/server/src/workflows/steps/refund-bold-payment.js +15 -0
  58. package/README.md +106 -2
  59. package/package.json +115 -3
  60. package/src/__tests__/bold-contract.spec.ts +40 -0
  61. package/src/__tests__/bold-qa-emulator.ts +582 -0
  62. package/src/__tests__/bold-use-cases.spec.ts +773 -0
  63. package/src/__tests__/medusa-payment-shim.mjs +334 -0
  64. package/src/__tests__/register-ts-resolver.mjs +101 -0
  65. package/src/admin/README.md +71 -0
  66. package/src/admin/components/bold-terminal-status.tsx +166 -0
  67. package/src/admin/components/icons/bold-fav.tsx +45 -0
  68. package/src/admin/components/icons/copy.tsx +23 -0
  69. package/src/admin/components/icons/index.ts +3 -0
  70. package/src/admin/components/icons/qr-code.tsx +33 -0
  71. package/src/admin/components/icons/smart-phone.tsx +23 -0
  72. package/src/admin/components/index.ts +4 -0
  73. package/src/admin/components/logos/bold.tsx +45 -0
  74. package/src/admin/components/method-item.tsx +19 -0
  75. package/src/admin/components/provider-card.tsx +21 -0
  76. package/src/admin/components/section.tsx +23 -0
  77. package/src/admin/i18n/README.md +58 -0
  78. package/src/admin/i18n/index.ts +11 -0
  79. package/src/admin/i18n/json/ar.json +99 -0
  80. package/src/admin/i18n/json/en.json +99 -0
  81. package/src/admin/i18n/json/es.json +99 -0
  82. package/src/admin/i18n/json/tr.json +99 -0
  83. package/src/admin/routes/settings/bold/components/bold-methods-sidebar.tsx +96 -0
  84. package/src/admin/routes/settings/bold/components/bold-providers-section.tsx +67 -0
  85. package/src/admin/routes/settings/bold/components/index.ts +2 -0
  86. package/src/admin/routes/settings/bold/page.tsx +109 -0
  87. package/src/admin/tsconfig.json +24 -0
  88. package/src/admin/vite-env.d.ts +1 -0
  89. package/src/admin/widgets/bold-audit-timeline.tsx +155 -0
  90. package/src/admin/widgets/bold-payment-actions/bold-payment-actions.tsx +114 -0
  91. package/src/admin/widgets/bold-payment-actions/components/bold-payment-tabs.tsx +127 -0
  92. package/src/admin/widgets/bold-payment-actions/components/bold-remote-payment-panel.tsx +104 -0
  93. package/src/admin/widgets/bold-payment-actions/components/bold-terminal-section.tsx +71 -0
  94. package/src/admin/widgets/bold-payment-actions/hooks/bold-order-balance.ts +126 -0
  95. package/src/admin/widgets/bold-payment-actions/hooks/use-bold-widget-state.tsx +181 -0
  96. package/src/admin/widgets/bold-refund-actions.tsx +151 -0
  97. package/src/admin/widgets/index.ts +12 -0
  98. package/src/api/README.md +37 -0
  99. package/src/api/admin/bold/banks/route.ts +23 -0
  100. package/src/api/admin/bold/payment-button/route.ts +57 -0
  101. package/src/api/admin/bold/payment-link/route.ts +68 -0
  102. package/src/api/admin/bold/payment-link-methods/route.ts +24 -0
  103. package/src/api/admin/bold/payment-methods/route.ts +27 -0
  104. package/src/api/admin/bold/payment-qr/route.ts +68 -0
  105. package/src/api/admin/bold/payment-status/route.ts +48 -0
  106. package/src/api/admin/bold/payment-stream/route.ts +157 -0
  107. package/src/api/admin/bold/pos-push/route.ts +59 -0
  108. package/src/api/admin/bold/refund/route.ts +51 -0
  109. package/src/api/admin/bold/terminals/route.ts +26 -0
  110. package/src/api/admin/plugin/route.ts +14 -0
  111. package/src/api/hooks/payment/bold/route.ts +156 -0
  112. package/src/api/store/plugin/route.ts +5 -0
  113. package/src/hooks/index.ts +9 -0
  114. package/src/hooks/use-bold-checkout-handler.ts +172 -0
  115. package/src/hooks/use-bold-payment-methods.ts +36 -0
  116. package/src/hooks/use-bold-payment-status-stream.ts +55 -0
  117. package/src/hooks/use-bold-refund.ts +33 -0
  118. package/src/hooks/use-bold-terminals.ts +22 -0
  119. package/src/hooks/use-create-bold-button.ts +34 -0
  120. package/src/hooks/use-create-bold-link.ts +33 -0
  121. package/src/hooks/use-create-bold-qr.ts +33 -0
  122. package/src/hooks/use-create-bold-terminal.ts +37 -0
  123. package/src/index.ts +31 -0
  124. package/src/lib/error-utils.ts +29 -0
  125. package/src/lib/get-active-providers.ts +52 -0
  126. package/src/lib/get-provider-options.ts +54 -0
  127. package/src/lib/index.ts +3 -0
  128. package/src/lib/session-helpers.ts +16 -0
  129. package/src/providers/README.md +30 -0
  130. package/src/providers/bold-button/index.ts +244 -0
  131. package/src/providers/bold-link/index.ts +350 -0
  132. package/src/providers/bold-online/index.ts +254 -0
  133. package/src/providers/bold-terminal/index.ts +258 -0
  134. package/src/rbac.ts +72 -0
  135. package/src/sdk/client.ts +157 -0
  136. package/src/sdk/query-keys.ts +13 -0
  137. package/src/services/bold-client.ts +470 -0
  138. package/src/services/integrity.ts +74 -0
  139. package/src/types/constants.ts +63 -0
  140. package/src/types/enums.ts +13 -0
  141. package/src/types/errors.ts +88 -0
  142. package/src/types/index.ts +517 -0
  143. package/src/workflows/create-bold-payment-link.ts +16 -0
  144. package/src/workflows/create-bold-refund.ts +16 -0
  145. package/src/workflows/get-bold-payment-methods.ts +13 -0
  146. package/src/workflows/get-integration-api-payment-methods.ts +13 -0
  147. package/src/workflows/get-payment-link-methods.ts +13 -0
  148. package/src/workflows/get-terminals.ts +10 -0
  149. package/src/workflows/index.ts +6 -0
  150. package/src/workflows/steps/create-bold-link.ts +74 -0
  151. package/src/workflows/steps/get-bold-payment-methods.ts +21 -0
  152. package/src/workflows/steps/get-payment-link-methods.ts +19 -0
  153. package/src/workflows/steps/get-terminals.ts +20 -0
  154. package/src/workflows/steps/refund-bold-payment.ts +28 -0
  155. package/tsconfig.json +41 -0
@@ -0,0 +1,127 @@
1
+ import { ArrowUpRightOnBox } from "@medusajs/icons"
2
+ import { Tabs } from "@medusajs/ui"
3
+ import { useTranslation } from "react-i18next"
4
+
5
+ import { QrCodeIcon } from "../../../components/icons"
6
+ import { BoldRemotePaymentPanel } from "./bold-remote-payment-panel"
7
+
8
+ interface BoldPaymentTabsProps {
9
+ activeTab: "link" | "qr"
10
+ onTabChange: (tab: "link" | "qr") => void
11
+ state: {
12
+ paymentLink: string | null
13
+ isApproved: boolean
14
+ isLinkEnabled: boolean
15
+ isQrEnabled: boolean
16
+ isCreatingLink: boolean
17
+ isCreatingQr: boolean
18
+ }
19
+ actions: {
20
+ handleCreateRemotePayment: (mode: "link" | "qr") => void
21
+ }
22
+ onCopy: (text: string) => void
23
+ }
24
+
25
+ export const BoldPaymentTabs = ({
26
+ activeTab,
27
+ onTabChange,
28
+ state,
29
+ actions,
30
+ onCopy,
31
+ }: BoldPaymentTabsProps) => {
32
+ const { t } = useTranslation()
33
+
34
+ const hasMultipleTabs = state.isLinkEnabled && state.isQrEnabled
35
+ const isGeneratingRemote = state.isCreatingLink || state.isCreatingQr
36
+ const singleMode = state.isLinkEnabled ? "link" : "qr"
37
+
38
+ return (
39
+ <div className="border border-ui-border-base rounded-lg p-3 space-y-3 bg-ui-bg-subtle/40">
40
+ {hasMultipleTabs ? (
41
+ <Tabs
42
+ value={activeTab}
43
+ onValueChange={(v) => onTabChange(v as "link" | "qr")}
44
+ className="w-full space-y-3"
45
+ >
46
+ <Tabs.List className="w-full grid grid-cols-2 gap-1 p-1 bg-ui-bg-subtle rounded-md border border-ui-border-base">
47
+ {state.isLinkEnabled && (
48
+ <Tabs.Trigger
49
+ value="link"
50
+ className="flex items-center justify-center gap-x-1.5 text-xs font-medium py-1.5 rounded-md"
51
+ >
52
+ <ArrowUpRightOnBox className="w-3.5 h-3.5 text-ui-fg-subtle" />
53
+ <span>{t("bold.admin.widget.link_btn")}</span>
54
+ </Tabs.Trigger>
55
+ )}
56
+
57
+ {state.isQrEnabled && (
58
+ <Tabs.Trigger
59
+ value="qr"
60
+ className="flex items-center justify-center gap-x-1.5 text-xs font-medium py-1.5 rounded-md"
61
+ >
62
+ <QrCodeIcon className="w-3.5 h-3.5 text-ui-fg-subtle" />
63
+ <span>{t("bold.admin.widget.qr_btn")}</span>
64
+ </Tabs.Trigger>
65
+ )}
66
+ </Tabs.List>
67
+
68
+ {state.isLinkEnabled && (
69
+ <Tabs.Content value="link">
70
+ <BoldRemotePaymentPanel
71
+ mode="link"
72
+ isApproved={state.isApproved}
73
+ isLoading={state.isCreatingLink}
74
+ isDisabled={isGeneratingRemote}
75
+ paymentLink={state.paymentLink}
76
+ showPayload={activeTab === "link"}
77
+ onGenerate={() => actions.handleCreateRemotePayment("link")}
78
+ onCopy={onCopy}
79
+ />
80
+ </Tabs.Content>
81
+ )}
82
+
83
+ {state.isQrEnabled && (
84
+ <Tabs.Content value="qr">
85
+ <BoldRemotePaymentPanel
86
+ mode="qr"
87
+ isApproved={state.isApproved}
88
+ isLoading={state.isCreatingQr}
89
+ isDisabled={isGeneratingRemote}
90
+ paymentLink={state.paymentLink}
91
+ showPayload={activeTab === "qr"}
92
+ onGenerate={() => actions.handleCreateRemotePayment("qr")}
93
+ onCopy={onCopy}
94
+ />
95
+ </Tabs.Content>
96
+ )}
97
+ </Tabs>
98
+ ) : (
99
+ <div className="space-y-2">
100
+ <div className="flex items-center gap-x-2 font-medium text-xs text-ui-fg-base pb-1">
101
+ {state.isLinkEnabled ? (
102
+ <ArrowUpRightOnBox className="w-4 h-4" />
103
+ ) : (
104
+ <QrCodeIcon className="w-4 h-4" />
105
+ )}
106
+ <span>
107
+ {state.isLinkEnabled
108
+ ? t("bold.admin.widget.link_btn")
109
+ : t("bold.admin.widget.qr_btn")}
110
+ </span>
111
+ </div>
112
+
113
+ <BoldRemotePaymentPanel
114
+ mode={singleMode}
115
+ isApproved={state.isApproved}
116
+ isLoading={isGeneratingRemote}
117
+ isDisabled={isGeneratingRemote}
118
+ paymentLink={state.paymentLink}
119
+ showPayload={Boolean(state.paymentLink)}
120
+ onGenerate={() => actions.handleCreateRemotePayment(singleMode)}
121
+ onCopy={onCopy}
122
+ />
123
+ </div>
124
+ )}
125
+ </div>
126
+ )
127
+ }
@@ -0,0 +1,104 @@
1
+ import { ArrowUpRightOnBox } from "@medusajs/icons"
2
+ import { Button, Input, Text } from "@medusajs/ui"
3
+ import { useTranslation } from "react-i18next"
4
+
5
+ import { CopyIcon, QrCodeIcon } from "../../../components/icons"
6
+
7
+ interface BoldRemotePaymentPanelProps {
8
+ mode: "link" | "qr"
9
+ isApproved: boolean
10
+ isLoading: boolean
11
+ isDisabled: boolean
12
+ paymentLink: string | null
13
+ showPayload: boolean
14
+ onGenerate: () => void
15
+ onCopy: (text: string) => void
16
+ }
17
+
18
+ export const BoldRemotePaymentPanel = ({
19
+ mode,
20
+ isApproved,
21
+ isLoading,
22
+ isDisabled,
23
+ paymentLink,
24
+ showPayload,
25
+ onGenerate,
26
+ onCopy,
27
+ }: BoldRemotePaymentPanelProps) => {
28
+ const { t } = useTranslation()
29
+
30
+ const isLink = mode === "link"
31
+
32
+ return (
33
+ <div className="space-y-2 pt-1">
34
+ <Button
35
+ variant="secondary"
36
+ size="small"
37
+ className="w-full flex items-center justify-center gap-x-1.5"
38
+ isLoading={isLoading}
39
+ onClick={onGenerate}
40
+ disabled={isApproved || isDisabled}
41
+ >
42
+ {isLink ? (
43
+ <ArrowUpRightOnBox className="w-4 h-4" />
44
+ ) : (
45
+ <QrCodeIcon className="w-4 h-4" />
46
+ )}
47
+ <span>
48
+ {isApproved
49
+ ? t("bold.admin.widget.approved")
50
+ : isLink
51
+ ? t("bold.admin.widget.link_btn")
52
+ : t("bold.admin.widget.qr_btn")}
53
+ </span>
54
+ </Button>
55
+
56
+ {paymentLink &&
57
+ showPayload &&
58
+ (isLink ? (
59
+ <div className="pt-2 space-y-1">
60
+ <Text className="text-xs text-ui-fg-subtle">
61
+ {t("bold.admin.widget.generated_link")}
62
+ </Text>
63
+ <div className="flex items-center gap-x-1.5">
64
+ <Input
65
+ value={paymentLink}
66
+ readOnly
67
+ size="small"
68
+ className="text-xs font-mono"
69
+ />
70
+ <Button
71
+ variant="transparent"
72
+ size="small"
73
+ onClick={() => onCopy(paymentLink)}
74
+ >
75
+ <CopyIcon />
76
+ </Button>
77
+ </div>
78
+ </div>
79
+ ) : (
80
+ <div className="p-3 border border-ui-border-base bg-ui-bg-base rounded-md flex flex-col items-center justify-center space-y-2 text-center">
81
+ <QrCodeIcon className="w-8 h-8 text-ui-fg-subtle" />
82
+ <Text className="text-xs text-ui-fg-subtle font-medium">
83
+ {t("bold.admin.widget.scan_instruction")}
84
+ </Text>
85
+ <div className="flex items-center gap-x-1.5 w-full pt-1">
86
+ <Input
87
+ value={paymentLink}
88
+ readOnly
89
+ size="small"
90
+ className="text-xs font-mono"
91
+ />
92
+ <Button
93
+ variant="transparent"
94
+ size="small"
95
+ onClick={() => onCopy(paymentLink)}
96
+ >
97
+ <CopyIcon />
98
+ </Button>
99
+ </div>
100
+ </div>
101
+ ))}
102
+ </div>
103
+ )
104
+ }
@@ -0,0 +1,71 @@
1
+ import { Button, Input, Select } from "@medusajs/ui"
2
+ import { useTranslation } from "react-i18next"
3
+
4
+ import { SmartphoneIcon } from "../../../components/icons"
5
+
6
+ interface BoldTerminalSectionProps {
7
+ terminalSerial: string
8
+ terminalModel: string
9
+ isApproved: boolean
10
+ isPushingToPos: boolean
11
+ onSerialChange: (val: string) => void
12
+ onModelChange: (val: string) => void
13
+ onSubmit: () => void
14
+ }
15
+
16
+ export const BoldTerminalSection = ({
17
+ terminalSerial,
18
+ terminalModel,
19
+ isApproved,
20
+ isPushingToPos,
21
+ onSerialChange,
22
+ onModelChange,
23
+ onSubmit,
24
+ }: BoldTerminalSectionProps) => {
25
+ const { t } = useTranslation()
26
+
27
+ return (
28
+ <div className="border border-ui-border-base rounded-lg p-3 space-y-3 bg-ui-bg-subtle/40">
29
+ <div className="flex items-center gap-x-2 font-medium text-xs text-ui-fg-base">
30
+ <SmartphoneIcon />
31
+ <span>{t("bold.admin.widget.terminal_btn")}</span>
32
+ </div>
33
+ <div className="space-y-2">
34
+ <Select
35
+ value={terminalModel}
36
+ onValueChange={onModelChange}
37
+ disabled={isApproved || isPushingToPos}
38
+ >
39
+ <Select.Trigger>
40
+ <Select.Value placeholder="Select Model" />
41
+ </Select.Trigger>
42
+ <Select.Content>
43
+ <Select.Item value="SMART_POS">Smart POS</Select.Item>
44
+ <Select.Item value="NEO">Bold Neo</Select.Item>
45
+ </Select.Content>
46
+ </Select>
47
+
48
+ <Input
49
+ size="small"
50
+ placeholder={t("bold.admin.widget.terminal_placeholder")}
51
+ value={terminalSerial}
52
+ onChange={(e) => onSerialChange(e.target.value)}
53
+ disabled={isApproved || isPushingToPos}
54
+ />
55
+
56
+ <Button
57
+ variant="secondary"
58
+ size="small"
59
+ className="w-full"
60
+ isLoading={isPushingToPos}
61
+ onClick={onSubmit}
62
+ disabled={isApproved || isPushingToPos}
63
+ >
64
+ {isApproved
65
+ ? t("bold.admin.widget.approved")
66
+ : t("bold.admin.widget.send_to_terminal")}
67
+ </Button>
68
+ </div>
69
+ </div>
70
+ )
71
+ }
@@ -0,0 +1,126 @@
1
+ import { AdminOrder } from "@medusajs/framework/types"
2
+
3
+ const PARTIAL_PAYMENT_STATUSES = [
4
+ "partially_paid",
5
+ "partially_captured",
6
+ "partially_authorized",
7
+ ]
8
+
9
+ export interface BoldOrderBalanceInfo {
10
+ paymentCollection:
11
+ | NonNullable<AdminOrder["payment_collections"]>[number]
12
+ | undefined
13
+ paymentCollectionId: string | undefined
14
+ isPartiallyPaid: boolean
15
+ payableAmount: number
16
+ belongsToBold: boolean
17
+ isApproved: boolean
18
+ }
19
+
20
+ export const getBoldOrderBalanceInfo = (
21
+ order: AdminOrder
22
+ ): BoldOrderBalanceInfo => {
23
+ const collections = order.payment_collections || []
24
+ const orderPaymentStatus = (order.payment_status || "").toLowerCase()
25
+ const pendingDifference = Number(
26
+ (order as any).summary?.pending_difference ?? 0
27
+ )
28
+
29
+ const paymentCollection = collections.length
30
+ ? collections.find((pc) => {
31
+ const status = (pc.status || "").toLowerCase()
32
+ return !["captured", "completed"].includes(status)
33
+ }) || collections[collections.length - 1]
34
+ : undefined
35
+
36
+ const paymentCollectionId = paymentCollection?.id
37
+
38
+ const totalCaptured = collections.reduce((acc, pc) => {
39
+ const pcCaptured = Number(
40
+ (pc as any).captured_amount ??
41
+ (pc.payments || []).reduce(
42
+ (sum, p) => sum + (p.captured_at ? Number(p.amount || 0) : 0),
43
+ 0
44
+ )
45
+ )
46
+ return acc + pcCaptured
47
+ }, 0)
48
+
49
+ const collectionStatus = (paymentCollection?.status || "").toLowerCase()
50
+ const collectionAmount = Number(paymentCollection?.amount ?? order.total ?? 0)
51
+ const collectionCaptured = Number(
52
+ (paymentCollection as any)?.captured_amount ?? 0
53
+ )
54
+ const collectionRemaining = Math.max(0, collectionAmount - collectionCaptured)
55
+
56
+ const isPartiallyPaid =
57
+ PARTIAL_PAYMENT_STATUSES.includes(orderPaymentStatus) ||
58
+ PARTIAL_PAYMENT_STATUSES.includes(collectionStatus) ||
59
+ (totalCaptured > 0 && totalCaptured < Number(order.total || 0)) ||
60
+ (totalCaptured > 0 && pendingDifference > 0)
61
+
62
+ const remaining =
63
+ pendingDifference > 0
64
+ ? pendingDifference
65
+ : collectionRemaining > 0
66
+ ? collectionRemaining
67
+ : Math.max(0, Number(order.total || 0) - totalCaptured)
68
+
69
+ const payableAmount =
70
+ isPartiallyPaid && remaining > 0 ? remaining : Number(order.total || 0)
71
+
72
+ const hasBoldHistory = collections.some((collection) => {
73
+ const sessions = collection.payment_sessions || []
74
+ const payments = collection.payments || []
75
+ const hasBoldSession = sessions.some((s) => s.provider_id?.includes("bold"))
76
+ const hasBoldPayment = payments.some((p) => p.provider_id?.includes("bold"))
77
+ return hasBoldSession || hasBoldPayment
78
+ })
79
+
80
+ const belongsToBold =
81
+ collections.length > 0 && (hasBoldHistory || isPartiallyPaid)
82
+
83
+ const deriveIsApproved = (): boolean => {
84
+ if (!paymentCollection) return false
85
+ if (isPartiallyPaid) return false
86
+
87
+ if (
88
+ ["paid", "captured"].includes(orderPaymentStatus) &&
89
+ pendingDifference <= 0
90
+ ) {
91
+ return true
92
+ }
93
+
94
+ if (["captured", "completed"].includes(collectionStatus)) return true
95
+
96
+ const capturedPaymentsAmount = (paymentCollection.payments || []).reduce(
97
+ (sum, p) => sum + (p.captured_at ? Number(p.amount || 0) : 0),
98
+ 0
99
+ )
100
+ if (collectionAmount > 0 && capturedPaymentsAmount >= collectionAmount) {
101
+ return true
102
+ }
103
+
104
+ const sessions = paymentCollection.payment_sessions || []
105
+ if (
106
+ sessions.some(
107
+ (s) =>
108
+ ["captured", "authorized"].includes(s.status || "") &&
109
+ Number(s.amount || 0) >= collectionAmount
110
+ )
111
+ ) {
112
+ return true
113
+ }
114
+
115
+ return false
116
+ }
117
+
118
+ return {
119
+ paymentCollection,
120
+ paymentCollectionId,
121
+ isPartiallyPaid,
122
+ payableAmount,
123
+ belongsToBold,
124
+ isApproved: deriveIsApproved(),
125
+ }
126
+ }
@@ -0,0 +1,181 @@
1
+ import { AdminOrder } from "@medusajs/framework/types"
2
+ import { toast } from "@medusajs/ui"
3
+ import { usePaymentProviders } from "@vicacha-devs/medusa-shared-admin/admin"
4
+ import { useMemo, useState } from "react"
5
+ import { useTranslation } from "react-i18next"
6
+
7
+ import { EBoldPaymentProvider } from "../../../../types"
8
+ import { getActiveBoldProviders } from "../../../../lib"
9
+ import {
10
+ useCreateBoldLink,
11
+ useCreateBoldQr,
12
+ usePushToBoldTerminal,
13
+ useBoldPaymentStatusStream,
14
+ } from "../../../../hooks"
15
+ import { getBoldOrderBalanceInfo } from "./bold-order-balance"
16
+
17
+ export const useBoldWidgetState = (order: AdminOrder) => {
18
+ const { t } = useTranslation()
19
+
20
+ const [terminalSerial, setTerminalSerial] = useState("")
21
+ const [terminalModel, setTerminalModel] = useState("SMART_POS")
22
+ const [paymentLink, setPaymentLink] = useState<string | null>(null)
23
+ const [activeSessionId, setActiveSessionId] = useState<string | null>(null)
24
+
25
+ // 1. Providers
26
+ const { providers = [], isLoading: isLoadingProviders } =
27
+ usePaymentProviders()
28
+ const activeBoldProviders = getActiveBoldProviders(providers)
29
+
30
+ const isTerminalEnabled = activeBoldProviders.includes(
31
+ EBoldPaymentProvider.TERMINAL
32
+ )
33
+ const isLinkEnabled = activeBoldProviders.includes(EBoldPaymentProvider.LINK)
34
+ const isQrEnabled = activeBoldProviders.includes(EBoldPaymentProvider.ONLINE)
35
+
36
+ // 2. Balance & Status Check (Supports unpaid and partially paid orders)
37
+ const {
38
+ paymentCollectionId,
39
+ isPartiallyPaid,
40
+ payableAmount,
41
+ belongsToBold,
42
+ isApproved,
43
+ } = useMemo(() => getBoldOrderBalanceInfo(order), [order])
44
+
45
+ // 3. SSE Stream
46
+ useBoldPaymentStatusStream({
47
+ sessionId: activeSessionId,
48
+ enabled: !!activeSessionId && !isApproved,
49
+ onSuccess: () => {
50
+ setActiveSessionId(null)
51
+ toast(t("bold.admin.widget.toast.payment_approved"), {
52
+ description: t("bold.admin.widget.status.approved_msg"),
53
+ })
54
+ },
55
+ onError: () => {
56
+ setActiveSessionId(null)
57
+ toast(t("bold.admin.widget.toast.payment_rejected"), {
58
+ description: t("bold.admin.widget.status.rejected_msg"),
59
+ })
60
+ },
61
+ })
62
+
63
+ // 4. Mutations
64
+ const { mutateAsync: createLink, isPending: isCreatingLink } =
65
+ useCreateBoldLink()
66
+ const { mutateAsync: createQr, isPending: isCreatingQr } = useCreateBoldQr()
67
+ const { mutateAsync: pushToTerminal, isPending: isPushingToPos } =
68
+ usePushToBoldTerminal()
69
+
70
+ const reference = `ORD_${order.display_id || order.id}_${Date.now()}`.slice(
71
+ 0,
72
+ 60
73
+ )
74
+
75
+ // 5. Handlers
76
+ const handlePushToPos = async () => {
77
+ if (!paymentCollectionId || !terminalSerial) {
78
+ toast(t("bold.admin.widget.toast.error"), {
79
+ description: !terminalSerial
80
+ ? t("bold.admin.widget.terminal_placeholder")
81
+ : t("bold.admin.widget.status.rejected_msg"),
82
+ })
83
+ return
84
+ }
85
+
86
+ try {
87
+ const res = await pushToTerminal({
88
+ amount: payableAmount,
89
+ currency: order.currency_code,
90
+ terminalModel,
91
+ terminalSerial,
92
+ paymentCollectionId,
93
+ reference,
94
+ userEmail: order.email || "guest@noemail.local",
95
+ })
96
+ if (res?.paymentSession?.id) setActiveSessionId(res.paymentSession.id)
97
+ toast(t("bold.admin.widget.toast.terminal_sent"), {
98
+ description: t("bold.admin.widget.status.waiting_terminal"),
99
+ })
100
+ } catch (err: any) {
101
+ toast(t("bold.admin.widget.toast.payment_rejected"), {
102
+ description: err.message,
103
+ })
104
+ }
105
+ }
106
+
107
+ const handleCreateRemotePayment = async (mode: "link" | "qr") => {
108
+ if (!paymentCollectionId) {
109
+ toast(t("bold.admin.widget.toast.error"), {
110
+ description: t("bold.admin.widget.status.rejected_msg"),
111
+ })
112
+ return
113
+ }
114
+
115
+ try {
116
+ if (mode === "link") {
117
+ const result = await createLink({
118
+ amount: payableAmount,
119
+ currency: order.currency_code,
120
+ reference,
121
+ description: `Order ${order.display_id || order.id}`,
122
+ email: order.email || "guest@noemail.local",
123
+ paymentCollectionId,
124
+ })
125
+
126
+ const generatedUrl =
127
+ result.paymentSession?.data?.url ||
128
+ result.paymentSession?.data?.payment_url ||
129
+ null
130
+
131
+ setPaymentLink(generatedUrl)
132
+ if (result.paymentSession?.id)
133
+ setActiveSessionId(result.paymentSession.id)
134
+ toast(t("bold.admin.widget.toast.link_generated"), {
135
+ description: t("bold.admin.widget.status.waiting_link"),
136
+ })
137
+ } else {
138
+ const result = await createQr({
139
+ amount: payableAmount,
140
+ currency_code: order.currency_code,
141
+ paymentCollectionId,
142
+ })
143
+ setPaymentLink(result.qr_payload)
144
+ if (result?.paymentSession?.id)
145
+ setActiveSessionId(result.paymentSession.id)
146
+ toast(t("bold.admin.widget.toast.qr_generated"), {
147
+ description: t("bold.admin.widget.status.waiting_qr"),
148
+ })
149
+ }
150
+ } catch (err: any) {
151
+ toast(t("bold.admin.widget.toast.payment_rejected"), {
152
+ description: err.message,
153
+ })
154
+ }
155
+ }
156
+
157
+ return {
158
+ state: {
159
+ terminalSerial,
160
+ terminalModel,
161
+ paymentLink,
162
+ isApproved,
163
+ isPartiallyPaid,
164
+ payableAmount,
165
+ isLoadingProviders,
166
+ isTerminalEnabled,
167
+ isLinkEnabled,
168
+ isQrEnabled,
169
+ isCreatingLink,
170
+ isCreatingQr,
171
+ isPushingToPos,
172
+ hasBoldProviders: activeBoldProviders.length > 0 && belongsToBold,
173
+ },
174
+ actions: {
175
+ setTerminalSerial,
176
+ setTerminalModel,
177
+ handlePushToPos,
178
+ handleCreateRemotePayment,
179
+ },
180
+ }
181
+ }