@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.
- package/.medusa/server/src/admin/index.cjs +1732 -0
- package/.medusa/server/src/admin/index.js +1732 -0
- package/.medusa/server/src/api/admin/bold/banks/route.js +15 -0
- package/.medusa/server/src/api/admin/bold/payment-button/route.js +44 -0
- package/.medusa/server/src/api/admin/bold/payment-link/route.js +61 -0
- package/.medusa/server/src/api/admin/bold/payment-link-methods/route.js +16 -0
- package/.medusa/server/src/api/admin/bold/payment-methods/route.js +17 -0
- package/.medusa/server/src/api/admin/bold/payment-qr/route.js +53 -0
- package/.medusa/server/src/api/admin/bold/payment-status/route.js +37 -0
- package/.medusa/server/src/api/admin/bold/payment-stream/route.js +103 -0
- package/.medusa/server/src/api/admin/bold/pos-push/route.js +46 -0
- package/.medusa/server/src/api/admin/bold/refund/route.js +37 -0
- package/.medusa/server/src/api/admin/bold/terminals/route.js +16 -0
- package/.medusa/server/src/api/admin/plugin/route.js +10 -0
- package/.medusa/server/src/api/hooks/payment/bold/route.js +104 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/hooks/index.js +10 -0
- package/.medusa/server/src/hooks/use-bold-checkout-handler.js +135 -0
- package/.medusa/server/src/hooks/use-bold-payment-methods.js +20 -0
- package/.medusa/server/src/hooks/use-bold-payment-status-stream.js +19 -0
- package/.medusa/server/src/hooks/use-bold-refund.js +19 -0
- package/.medusa/server/src/hooks/use-bold-terminals.js +13 -0
- package/.medusa/server/src/hooks/use-create-bold-button.js +19 -0
- package/.medusa/server/src/hooks/use-create-bold-link.js +19 -0
- package/.medusa/server/src/hooks/use-create-bold-qr.js +19 -0
- package/.medusa/server/src/hooks/use-create-bold-terminal.js +19 -0
- package/.medusa/server/src/index.js +21 -0
- package/.medusa/server/src/lib/error-utils.js +25 -0
- package/.medusa/server/src/lib/get-active-providers.js +34 -0
- package/.medusa/server/src/lib/get-provider-options.js +20 -0
- package/.medusa/server/src/lib/index.js +4 -0
- package/.medusa/server/src/lib/session-helpers.js +11 -0
- package/.medusa/server/src/providers/bold-button/index.js +157 -0
- package/.medusa/server/src/providers/bold-link/index.js +249 -0
- package/.medusa/server/src/providers/bold-online/index.js +169 -0
- package/.medusa/server/src/providers/bold-terminal/index.js +170 -0
- package/.medusa/server/src/rbac.js +58 -0
- package/.medusa/server/src/sdk/client.js +112 -0
- package/.medusa/server/src/sdk/query-keys.js +13 -0
- package/.medusa/server/src/services/bold-client.js +304 -0
- package/.medusa/server/src/services/integrity.js +54 -0
- package/.medusa/server/src/types/constants.js +55 -0
- package/.medusa/server/src/types/enums.js +15 -0
- package/.medusa/server/src/types/errors.js +80 -0
- package/.medusa/server/src/types/index.js +3 -0
- package/.medusa/server/src/workflows/create-bold-payment-link.js +7 -0
- package/.medusa/server/src/workflows/create-bold-refund.js +7 -0
- package/.medusa/server/src/workflows/get-bold-payment-methods.js +7 -0
- package/.medusa/server/src/workflows/get-integration-api-payment-methods.js +7 -0
- package/.medusa/server/src/workflows/get-payment-link-methods.js +7 -0
- package/.medusa/server/src/workflows/get-terminals.js +7 -0
- package/.medusa/server/src/workflows/index.js +7 -0
- package/.medusa/server/src/workflows/steps/create-bold-link.js +43 -0
- package/.medusa/server/src/workflows/steps/get-bold-payment-methods.js +12 -0
- package/.medusa/server/src/workflows/steps/get-payment-link-methods.js +12 -0
- package/.medusa/server/src/workflows/steps/get-terminals.js +13 -0
- package/.medusa/server/src/workflows/steps/refund-bold-payment.js +15 -0
- package/README.md +106 -2
- package/package.json +115 -3
- package/src/__tests__/bold-contract.spec.ts +40 -0
- package/src/__tests__/bold-qa-emulator.ts +582 -0
- package/src/__tests__/bold-use-cases.spec.ts +773 -0
- package/src/__tests__/medusa-payment-shim.mjs +334 -0
- package/src/__tests__/register-ts-resolver.mjs +101 -0
- package/src/admin/README.md +71 -0
- package/src/admin/components/bold-terminal-status.tsx +166 -0
- package/src/admin/components/icons/bold-fav.tsx +45 -0
- package/src/admin/components/icons/copy.tsx +23 -0
- package/src/admin/components/icons/index.ts +3 -0
- package/src/admin/components/icons/qr-code.tsx +33 -0
- package/src/admin/components/icons/smart-phone.tsx +23 -0
- package/src/admin/components/index.ts +4 -0
- package/src/admin/components/logos/bold.tsx +45 -0
- package/src/admin/components/method-item.tsx +19 -0
- package/src/admin/components/provider-card.tsx +21 -0
- package/src/admin/components/section.tsx +23 -0
- package/src/admin/i18n/README.md +58 -0
- package/src/admin/i18n/index.ts +11 -0
- package/src/admin/i18n/json/ar.json +99 -0
- package/src/admin/i18n/json/en.json +99 -0
- package/src/admin/i18n/json/es.json +99 -0
- package/src/admin/i18n/json/tr.json +99 -0
- package/src/admin/routes/settings/bold/components/bold-methods-sidebar.tsx +96 -0
- package/src/admin/routes/settings/bold/components/bold-providers-section.tsx +67 -0
- package/src/admin/routes/settings/bold/components/index.ts +2 -0
- package/src/admin/routes/settings/bold/page.tsx +109 -0
- package/src/admin/tsconfig.json +24 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/bold-audit-timeline.tsx +155 -0
- package/src/admin/widgets/bold-payment-actions/bold-payment-actions.tsx +114 -0
- package/src/admin/widgets/bold-payment-actions/components/bold-payment-tabs.tsx +127 -0
- package/src/admin/widgets/bold-payment-actions/components/bold-remote-payment-panel.tsx +104 -0
- package/src/admin/widgets/bold-payment-actions/components/bold-terminal-section.tsx +71 -0
- package/src/admin/widgets/bold-payment-actions/hooks/bold-order-balance.ts +126 -0
- package/src/admin/widgets/bold-payment-actions/hooks/use-bold-widget-state.tsx +181 -0
- package/src/admin/widgets/bold-refund-actions.tsx +151 -0
- package/src/admin/widgets/index.ts +12 -0
- package/src/api/README.md +37 -0
- package/src/api/admin/bold/banks/route.ts +23 -0
- package/src/api/admin/bold/payment-button/route.ts +57 -0
- package/src/api/admin/bold/payment-link/route.ts +68 -0
- package/src/api/admin/bold/payment-link-methods/route.ts +24 -0
- package/src/api/admin/bold/payment-methods/route.ts +27 -0
- package/src/api/admin/bold/payment-qr/route.ts +68 -0
- package/src/api/admin/bold/payment-status/route.ts +48 -0
- package/src/api/admin/bold/payment-stream/route.ts +157 -0
- package/src/api/admin/bold/pos-push/route.ts +59 -0
- package/src/api/admin/bold/refund/route.ts +51 -0
- package/src/api/admin/bold/terminals/route.ts +26 -0
- package/src/api/admin/plugin/route.ts +14 -0
- package/src/api/hooks/payment/bold/route.ts +156 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/hooks/index.ts +9 -0
- package/src/hooks/use-bold-checkout-handler.ts +172 -0
- package/src/hooks/use-bold-payment-methods.ts +36 -0
- package/src/hooks/use-bold-payment-status-stream.ts +55 -0
- package/src/hooks/use-bold-refund.ts +33 -0
- package/src/hooks/use-bold-terminals.ts +22 -0
- package/src/hooks/use-create-bold-button.ts +34 -0
- package/src/hooks/use-create-bold-link.ts +33 -0
- package/src/hooks/use-create-bold-qr.ts +33 -0
- package/src/hooks/use-create-bold-terminal.ts +37 -0
- package/src/index.ts +31 -0
- package/src/lib/error-utils.ts +29 -0
- package/src/lib/get-active-providers.ts +52 -0
- package/src/lib/get-provider-options.ts +54 -0
- package/src/lib/index.ts +3 -0
- package/src/lib/session-helpers.ts +16 -0
- package/src/providers/README.md +30 -0
- package/src/providers/bold-button/index.ts +244 -0
- package/src/providers/bold-link/index.ts +350 -0
- package/src/providers/bold-online/index.ts +254 -0
- package/src/providers/bold-terminal/index.ts +258 -0
- package/src/rbac.ts +72 -0
- package/src/sdk/client.ts +157 -0
- package/src/sdk/query-keys.ts +13 -0
- package/src/services/bold-client.ts +470 -0
- package/src/services/integrity.ts +74 -0
- package/src/types/constants.ts +63 -0
- package/src/types/enums.ts +13 -0
- package/src/types/errors.ts +88 -0
- package/src/types/index.ts +517 -0
- package/src/workflows/create-bold-payment-link.ts +16 -0
- package/src/workflows/create-bold-refund.ts +16 -0
- package/src/workflows/get-bold-payment-methods.ts +13 -0
- package/src/workflows/get-integration-api-payment-methods.ts +13 -0
- package/src/workflows/get-payment-link-methods.ts +13 -0
- package/src/workflows/get-terminals.ts +10 -0
- package/src/workflows/index.ts +6 -0
- package/src/workflows/steps/create-bold-link.ts +74 -0
- package/src/workflows/steps/get-bold-payment-methods.ts +21 -0
- package/src/workflows/steps/get-payment-link-methods.ts +19 -0
- package/src/workflows/steps/get-terminals.ts +20 -0
- package/src/workflows/steps/refund-bold-payment.ts +28 -0
- package/tsconfig.json +41 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbstractPaymentProvider,
|
|
3
|
+
MedusaError,
|
|
4
|
+
PaymentActions,
|
|
5
|
+
PaymentSessionStatus,
|
|
6
|
+
} from "@medusajs/framework/utils"
|
|
7
|
+
import {
|
|
8
|
+
AuthorizePaymentInput,
|
|
9
|
+
AuthorizePaymentOutput,
|
|
10
|
+
CancelPaymentInput,
|
|
11
|
+
CancelPaymentOutput,
|
|
12
|
+
CapturePaymentInput,
|
|
13
|
+
CapturePaymentOutput,
|
|
14
|
+
DeletePaymentInput,
|
|
15
|
+
DeletePaymentOutput,
|
|
16
|
+
GetPaymentStatusInput,
|
|
17
|
+
GetPaymentStatusOutput,
|
|
18
|
+
InitiatePaymentInput,
|
|
19
|
+
InitiatePaymentOutput,
|
|
20
|
+
RefundPaymentInput,
|
|
21
|
+
RefundPaymentOutput,
|
|
22
|
+
UpdatePaymentInput,
|
|
23
|
+
UpdatePaymentOutput,
|
|
24
|
+
WebhookActionResult,
|
|
25
|
+
} from "@medusajs/framework/types"
|
|
26
|
+
import { Logger } from "@medusajs/medusa"
|
|
27
|
+
import { BoldBaseOptions, InjectedDependencies } from "@payment-bold/types"
|
|
28
|
+
import { BoldHttpClient } from "../../services/bold-client"
|
|
29
|
+
import { EBoldPaymentProvider } from "@payment-bold/types/enums"
|
|
30
|
+
|
|
31
|
+
export class BoldTerminalPaymentProviderService extends AbstractPaymentProvider<BoldBaseOptions> {
|
|
32
|
+
static identifier = EBoldPaymentProvider.TERMINAL
|
|
33
|
+
|
|
34
|
+
protected logger_: Logger
|
|
35
|
+
protected options_: BoldBaseOptions
|
|
36
|
+
protected client_: BoldHttpClient
|
|
37
|
+
|
|
38
|
+
constructor(container: InjectedDependencies, options: BoldBaseOptions) {
|
|
39
|
+
super(container, options)
|
|
40
|
+
this.logger_ = container.logger
|
|
41
|
+
this.options_ = options
|
|
42
|
+
this.client_ = new BoldHttpClient(options, this.logger_)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async initiatePayment(
|
|
46
|
+
input: InitiatePaymentInput
|
|
47
|
+
): Promise<InitiatePaymentOutput> {
|
|
48
|
+
const { amount, currency_code, data } = input
|
|
49
|
+
const extra = (data || {}) as Record<string, unknown>
|
|
50
|
+
const reference = (extra.reference as string) || `pos_ref_${Date.now()}`
|
|
51
|
+
const roundedAmount = Math.round(Number(amount))
|
|
52
|
+
const currency = (
|
|
53
|
+
currency_code ||
|
|
54
|
+
this.options_.defaultCurrency ||
|
|
55
|
+
"COP"
|
|
56
|
+
).toUpperCase()
|
|
57
|
+
const terminalModel = (extra.terminalModel ||
|
|
58
|
+
extra.terminal_model) as string
|
|
59
|
+
const terminalSerial = (extra.terminalSerial ||
|
|
60
|
+
extra.terminal_serial) as string
|
|
61
|
+
const userEmail = (extra.userEmail || extra.user_email || "") as string
|
|
62
|
+
|
|
63
|
+
if (!terminalModel || !terminalSerial) {
|
|
64
|
+
throw new MedusaError(
|
|
65
|
+
MedusaError.Types.INVALID_DATA,
|
|
66
|
+
"terminalModel and terminalSerial are required to dispatch POS terminal checkout"
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const dispatchRes = await this.client_.triggerTerminalCheckout({
|
|
71
|
+
terminalModel,
|
|
72
|
+
terminalSerial,
|
|
73
|
+
amount: roundedAmount,
|
|
74
|
+
currency,
|
|
75
|
+
reference,
|
|
76
|
+
vatAmount: extra.vatAmount != null ? Number(extra.vatAmount) : 0,
|
|
77
|
+
userEmail,
|
|
78
|
+
description:
|
|
79
|
+
(extra.description as string) || `Medusa POS Order ${reference}`,
|
|
80
|
+
payer: extra.payer as any,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
id: dispatchRes?.payload?.id || reference,
|
|
85
|
+
data: {
|
|
86
|
+
...data,
|
|
87
|
+
terminal_checkout_id: dispatchRes?.payload?.id,
|
|
88
|
+
reference,
|
|
89
|
+
terminal_model: terminalModel,
|
|
90
|
+
terminal_serial: terminalSerial,
|
|
91
|
+
status: "pending",
|
|
92
|
+
amount: roundedAmount,
|
|
93
|
+
currency,
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async authorizePayment(
|
|
99
|
+
input: AuthorizePaymentInput
|
|
100
|
+
): Promise<AuthorizePaymentOutput> {
|
|
101
|
+
const sessionData = (input.data || {}) as Record<string, unknown>
|
|
102
|
+
let status =
|
|
103
|
+
(sessionData.status as string) || (sessionData.payment_status as string)
|
|
104
|
+
const reference = (sessionData.reference ||
|
|
105
|
+
sessionData.reference_id) as string
|
|
106
|
+
|
|
107
|
+
if (["pending", "PROCESSING", "ACTIVE"].includes(status) && reference) {
|
|
108
|
+
try {
|
|
109
|
+
const voucherRes = (await this.client_.getPaymentVoucher(
|
|
110
|
+
reference
|
|
111
|
+
)) as any
|
|
112
|
+
const remoteStatus =
|
|
113
|
+
voucherRes?.payload?.status ||
|
|
114
|
+
voucherRes?.status ||
|
|
115
|
+
voucherRes?.payment_status
|
|
116
|
+
if (remoteStatus) {
|
|
117
|
+
status = remoteStatus
|
|
118
|
+
sessionData.status = remoteStatus
|
|
119
|
+
sessionData.transaction_id =
|
|
120
|
+
voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
|
|
121
|
+
sessionData.payment_method =
|
|
122
|
+
voucherRes?.payload?.payment_method ||
|
|
123
|
+
voucherRes?.payment_method ||
|
|
124
|
+
"POS"
|
|
125
|
+
}
|
|
126
|
+
} catch {
|
|
127
|
+
// Network fallback
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status)) {
|
|
132
|
+
return {
|
|
133
|
+
status: PaymentSessionStatus.AUTHORIZED,
|
|
134
|
+
data: {
|
|
135
|
+
...sessionData,
|
|
136
|
+
status: "captured",
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (
|
|
142
|
+
["REJECTED", "FAILED", "CANCELLED", "EXPIRED", "error"].includes(status)
|
|
143
|
+
) {
|
|
144
|
+
return {
|
|
145
|
+
status: PaymentSessionStatus.CANCELED,
|
|
146
|
+
data: sessionData,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
status: PaymentSessionStatus.PENDING,
|
|
152
|
+
data: sessionData,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async capturePayment(
|
|
157
|
+
input: CapturePaymentInput
|
|
158
|
+
): Promise<CapturePaymentOutput> {
|
|
159
|
+
return { data: { ...input.data, status: "captured" } }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
|
163
|
+
return { data: { ...input.data, status: "canceled" } }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
|
167
|
+
const { data, amount } = input
|
|
168
|
+
const pData = (data || {}) as Record<string, unknown>
|
|
169
|
+
const transactionId = (pData.transaction_id || pData.id) as string
|
|
170
|
+
|
|
171
|
+
if (!transactionId) {
|
|
172
|
+
throw new MedusaError(
|
|
173
|
+
MedusaError.Types.INVALID_DATA,
|
|
174
|
+
"Missing transaction_id for terminal refund operation"
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const refundRes = await this.client_.refundPayment({
|
|
179
|
+
transactionId,
|
|
180
|
+
amount: Math.round(Number(amount)),
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
data: {
|
|
185
|
+
...pData,
|
|
186
|
+
refund_id: refundRes.refund_id,
|
|
187
|
+
refunded_amount: amount,
|
|
188
|
+
status: "refunded",
|
|
189
|
+
},
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
|
194
|
+
return { data: { ...input.data, status: "deleted" } }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
|
198
|
+
return this.initiatePayment(input)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async getPaymentStatus(
|
|
202
|
+
input: GetPaymentStatusInput
|
|
203
|
+
): Promise<GetPaymentStatusOutput> {
|
|
204
|
+
const sessionData = (input.data || {}) as Record<string, unknown>
|
|
205
|
+
const reference = (sessionData.reference ||
|
|
206
|
+
sessionData.reference_id) as string
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
if (reference) {
|
|
210
|
+
const voucherRes = (await this.client_.getPaymentVoucher(
|
|
211
|
+
reference
|
|
212
|
+
)) as any
|
|
213
|
+
const status =
|
|
214
|
+
voucherRes?.payload?.status ||
|
|
215
|
+
voucherRes?.status ||
|
|
216
|
+
voucherRes?.payment_status
|
|
217
|
+
const transactionId =
|
|
218
|
+
voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
|
|
219
|
+
|
|
220
|
+
if (transactionId) {
|
|
221
|
+
sessionData.transaction_id = transactionId
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (
|
|
225
|
+
["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status || "")
|
|
226
|
+
) {
|
|
227
|
+
sessionData.status = "captured"
|
|
228
|
+
return { status: PaymentSessionStatus.AUTHORIZED }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (
|
|
232
|
+
["REJECTED", "FAILED", "CANCELLED", "EXPIRED"].includes(status || "")
|
|
233
|
+
) {
|
|
234
|
+
sessionData.status = "canceled"
|
|
235
|
+
return { status: PaymentSessionStatus.CANCELED }
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return { status: PaymentSessionStatus.PENDING }
|
|
240
|
+
} catch {
|
|
241
|
+
return { status: PaymentSessionStatus.PENDING }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async getWebhookActionAndData(_data: {
|
|
246
|
+
data: Record<string, unknown>
|
|
247
|
+
rawData: string | Buffer
|
|
248
|
+
headers: Record<string, unknown>
|
|
249
|
+
}): Promise<WebhookActionResult> {
|
|
250
|
+
return { action: PaymentActions.NOT_SUPPORTED }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async retrievePayment(
|
|
254
|
+
paymentSessionData: Record<string, unknown>
|
|
255
|
+
): Promise<Record<string, unknown>> {
|
|
256
|
+
return paymentSessionData
|
|
257
|
+
}
|
|
258
|
+
}
|
package/src/rbac.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Auto-generated RBAC descriptors compatible with @abhi1693/medusa-rbac AccessControlOptions.
|
|
2
|
+
export type RbacPermissionTuple = readonly [
|
|
3
|
+
id: string,
|
|
4
|
+
label: string,
|
|
5
|
+
group: string,
|
|
6
|
+
]
|
|
7
|
+
export type RbacRoutePolicy = {
|
|
8
|
+
method: string
|
|
9
|
+
path: string
|
|
10
|
+
permissions: string[]
|
|
11
|
+
superuser?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const RBAC_PERMISSIONS: readonly RbacPermissionTuple[] = [
|
|
15
|
+
[
|
|
16
|
+
"bold.read",
|
|
17
|
+
"Read Bold payment methods, banks, terminals & status",
|
|
18
|
+
"Bold Payments",
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
"bold.write",
|
|
22
|
+
"Create Bold payment links, QR/POS charges & refunds",
|
|
23
|
+
"Bold Payments",
|
|
24
|
+
],
|
|
25
|
+
] as const
|
|
26
|
+
|
|
27
|
+
export const RBAC_ROUTES: RbacRoutePolicy[] = [
|
|
28
|
+
{ method: "GET", path: "/admin/bold/banks", permissions: ["bold.read"] },
|
|
29
|
+
{
|
|
30
|
+
method: "POST",
|
|
31
|
+
path: "/admin/bold/payment-button",
|
|
32
|
+
permissions: ["bold.write"],
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
method: "GET",
|
|
36
|
+
path: "/admin/bold/payment-link-methods",
|
|
37
|
+
permissions: ["bold.read"],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
method: "POST",
|
|
41
|
+
path: "/admin/bold/payment-link",
|
|
42
|
+
permissions: ["bold.write"],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
method: "GET",
|
|
46
|
+
path: "/admin/bold/payment-methods",
|
|
47
|
+
permissions: ["bold.read"],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
method: "POST",
|
|
51
|
+
path: "/admin/bold/payment-qr",
|
|
52
|
+
permissions: ["bold.write"],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
method: "GET",
|
|
56
|
+
path: "/admin/bold/payment-status",
|
|
57
|
+
permissions: ["bold.read"],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
method: "GET",
|
|
61
|
+
path: "/admin/bold/payment-stream",
|
|
62
|
+
permissions: ["bold.read"],
|
|
63
|
+
},
|
|
64
|
+
{ method: "POST", path: "/admin/bold/pos-push", permissions: ["bold.write"] },
|
|
65
|
+
{ method: "POST", path: "/admin/bold/refund", permissions: ["bold.write"] },
|
|
66
|
+
{ method: "GET", path: "/admin/bold/terminals", permissions: ["bold.read"] },
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
export const rbacAccess = {
|
|
70
|
+
permissions: [...RBAC_PERMISSIONS],
|
|
71
|
+
routes: RBAC_ROUTES,
|
|
72
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BoldButtonPayload,
|
|
3
|
+
BoldButtonResponse,
|
|
4
|
+
BoldIntegrationAPIPaymentMethod,
|
|
5
|
+
BoldLinkPayload,
|
|
6
|
+
BoldLinkResponse,
|
|
7
|
+
BoldPaymentLinkPaymentMethodLimits,
|
|
8
|
+
BoldQrPayload,
|
|
9
|
+
BoldQrResponse,
|
|
10
|
+
BoldRefundPayload,
|
|
11
|
+
BoldRefundResponse,
|
|
12
|
+
BoldStatusStreamCallbacks,
|
|
13
|
+
BoldTerminalItem,
|
|
14
|
+
BoldTerminalPayload,
|
|
15
|
+
BoldTerminalResponse,
|
|
16
|
+
} from "../types"
|
|
17
|
+
import { BOLD_FAILED_STATUSES, BOLD_SUCCESS_STATUSES } from "../types/constants"
|
|
18
|
+
|
|
19
|
+
export class BoldApiClient {
|
|
20
|
+
private baseUrl: string
|
|
21
|
+
private prefix: string
|
|
22
|
+
|
|
23
|
+
constructor(prefix: "/admin" | "/store" = "/store", baseUrl: string = "") {
|
|
24
|
+
this.prefix = prefix
|
|
25
|
+
this.baseUrl = baseUrl
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private async fetch<T>(endpoint: string, options?: RequestInit): Promise<T> {
|
|
29
|
+
const response = await fetch(`${this.baseUrl}${this.prefix}${endpoint}`, {
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
...options?.headers,
|
|
33
|
+
},
|
|
34
|
+
...options,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const data = await response.json()
|
|
38
|
+
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
data.message || `Request failed with status ${response.status}`
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return data as T
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async createPaymentLink(payload: BoldLinkPayload) {
|
|
49
|
+
return this.fetch<BoldLinkResponse>("/bold/payment-link", {
|
|
50
|
+
method: "POST",
|
|
51
|
+
body: JSON.stringify(payload),
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async createPaymentButton(payload: BoldButtonPayload) {
|
|
56
|
+
return this.fetch<BoldButtonResponse>("/bold/payment-button", {
|
|
57
|
+
method: "POST",
|
|
58
|
+
body: JSON.stringify(payload),
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async createPaymentQr(payload: BoldQrPayload) {
|
|
63
|
+
return this.fetch<BoldQrResponse>("/bold/payment-qr", {
|
|
64
|
+
method: "POST",
|
|
65
|
+
body: JSON.stringify(payload),
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async pushToTerminal(payload: BoldTerminalPayload) {
|
|
70
|
+
return this.fetch<BoldTerminalResponse>("/bold/pos-push", {
|
|
71
|
+
method: "POST",
|
|
72
|
+
body: JSON.stringify(payload),
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
subscribeToPaymentStatus(
|
|
77
|
+
sessionId: string,
|
|
78
|
+
callbacks: BoldStatusStreamCallbacks
|
|
79
|
+
): () => void {
|
|
80
|
+
const url = `${this.baseUrl}${this.prefix}/bold/payment-stream?paymentSessionId=${encodeURIComponent(sessionId)}`
|
|
81
|
+
const eventSource = new EventSource(url)
|
|
82
|
+
|
|
83
|
+
callbacks.onStatusChange?.("pending", null)
|
|
84
|
+
|
|
85
|
+
eventSource.onmessage = (event) => {
|
|
86
|
+
try {
|
|
87
|
+
const data = JSON.parse(event.data)
|
|
88
|
+
const status = (data.status || "").toLowerCase()
|
|
89
|
+
|
|
90
|
+
const isSuccess = BOLD_SUCCESS_STATUSES.includes(status)
|
|
91
|
+
const isFailure = BOLD_FAILED_STATUSES.includes(status)
|
|
92
|
+
|
|
93
|
+
if (isSuccess) {
|
|
94
|
+
callbacks.onStatusChange?.("captured", data)
|
|
95
|
+
callbacks.onSuccess?.(data)
|
|
96
|
+
eventSource.close()
|
|
97
|
+
} else if (isFailure) {
|
|
98
|
+
callbacks.onStatusChange?.("error", data)
|
|
99
|
+
callbacks.onError?.(data)
|
|
100
|
+
eventSource.close()
|
|
101
|
+
} else {
|
|
102
|
+
callbacks.onStatusChange?.("pending", data)
|
|
103
|
+
}
|
|
104
|
+
} catch (err) {
|
|
105
|
+
callbacks.onError?.(err)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
eventSource.onerror = (err) => {
|
|
110
|
+
callbacks.onStatusChange?.("error", err)
|
|
111
|
+
callbacks.onError?.(err)
|
|
112
|
+
eventSource.close()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return () => {
|
|
116
|
+
eventSource.close()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves active registered POS data-phone / terminals associated with the Bold merchant account.
|
|
122
|
+
*/
|
|
123
|
+
async getTerminals(): Promise<Array<BoldTerminalItem>> {
|
|
124
|
+
return this.fetch<Array<BoldTerminalItem>>("/bold/terminals", {
|
|
125
|
+
method: "GET",
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async refundPayment(payload: BoldRefundPayload): Promise<BoldRefundResponse> {
|
|
130
|
+
return this.fetch<BoldRefundResponse>("/bold/refund", {
|
|
131
|
+
method: "POST",
|
|
132
|
+
body: JSON.stringify(payload),
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async getPaymentMethods(): Promise<Array<BoldIntegrationAPIPaymentMethod>> {
|
|
137
|
+
return this.fetch<Array<BoldIntegrationAPIPaymentMethod>>(
|
|
138
|
+
"/bold/payment-methods",
|
|
139
|
+
{
|
|
140
|
+
method: "GET",
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async getPaymentLinkPaymentMethods(): Promise<BoldPaymentLinkPaymentMethodLimits> {
|
|
146
|
+
return this.fetch<BoldPaymentLinkPaymentMethodLimits>(
|
|
147
|
+
"/bold/payment-link-methods",
|
|
148
|
+
{
|
|
149
|
+
method: "GET",
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Export pre-configured SDK instances
|
|
156
|
+
export const boldAdminSdk = new BoldApiClient("/admin")
|
|
157
|
+
export const boldStoreSdk = new BoldApiClient("/store")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const BOLD_QUERY_KEY = "bold-payment-actions" as const
|
|
2
|
+
|
|
3
|
+
export const boldQueryKeys = {
|
|
4
|
+
all: [BOLD_QUERY_KEY] as const,
|
|
5
|
+
button: () => [...boldQueryKeys.all, "payment-button"] as const,
|
|
6
|
+
link: () => [...boldQueryKeys.all, "payment-link"] as const,
|
|
7
|
+
orders: () => [...boldQueryKeys.all, "orders"] as const,
|
|
8
|
+
order: (id: string) => [...boldQueryKeys.orders(), id] as const,
|
|
9
|
+
paymentMethods: () => [...boldQueryKeys.all, "payment-methods"] as const,
|
|
10
|
+
qr: () => [...boldQueryKeys.all, "payment-qr"] as const,
|
|
11
|
+
refund: () => [...boldQueryKeys.all, "refund"] as const,
|
|
12
|
+
terminals: () => ["bold", "terminals"] as const,
|
|
13
|
+
}
|