@vicacha-devs/medusa-payment-bold 0.0.0-stage → 0.1.0
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 +120 -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 +70 -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 +161 -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,470 @@
|
|
|
1
|
+
import { MedusaError } from "@medusajs/framework/utils"
|
|
2
|
+
import { Logger } from "@medusajs/medusa"
|
|
3
|
+
import {
|
|
4
|
+
BankItem,
|
|
5
|
+
BoldBaseOptions,
|
|
6
|
+
BoldCreatePaymentLinkResponse,
|
|
7
|
+
BoldIntegrationAPIPaymentMethod,
|
|
8
|
+
BoldIntegrationApiPaymentMethodsResponse,
|
|
9
|
+
BoldLinkPaymentStatusResponse,
|
|
10
|
+
BoldPaymentAttemptResponse,
|
|
11
|
+
BoldPaymentIntentPayload,
|
|
12
|
+
BoldPaymentLinkPaymentLimitsResponse,
|
|
13
|
+
BoldPaymentLinkPaymentMethodLimits,
|
|
14
|
+
BoldPaymentMethod,
|
|
15
|
+
BoldRefundApiRequest,
|
|
16
|
+
BoldRefundApiResponse,
|
|
17
|
+
BoldTerminalItem,
|
|
18
|
+
BoldTerminalPayload,
|
|
19
|
+
BoldTerminalsResponse,
|
|
20
|
+
CreatePaymentLinkPayload,
|
|
21
|
+
CreateRefundPayload,
|
|
22
|
+
TBoldPaymentMethodQrFormat,
|
|
23
|
+
} from "@payment-bold/types"
|
|
24
|
+
|
|
25
|
+
const HTTP_ERROR_MAP: Record<number, string> = {
|
|
26
|
+
400: MedusaError.Types.INVALID_DATA,
|
|
27
|
+
401: MedusaError.Types.UNAUTHORIZED,
|
|
28
|
+
403: MedusaError.Types.UNAUTHORIZED,
|
|
29
|
+
404: MedusaError.Types.NOT_FOUND,
|
|
30
|
+
409: MedusaError.Types.DUPLICATE_ERROR,
|
|
31
|
+
422: MedusaError.Types.INVALID_DATA,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const DEFAULT_INTEGRATIONS_URL = "https://integrations.api.bold.co"
|
|
35
|
+
const DEFAULT_ONLINE_URL = "https://api.online.payments.bold.co"
|
|
36
|
+
const DEFAULT_PAYMENTS_URL = "https://payments.api.bold.co/v2/payment-voucher/"
|
|
37
|
+
|
|
38
|
+
function resolveSafeBaseUrl(
|
|
39
|
+
override: string | undefined,
|
|
40
|
+
envKey: string,
|
|
41
|
+
fallback: string
|
|
42
|
+
): string {
|
|
43
|
+
const candidate =
|
|
44
|
+
override ||
|
|
45
|
+
(process.env.NODE_ENV !== "production" ? process.env[envKey] : undefined) ||
|
|
46
|
+
fallback
|
|
47
|
+
const parsed = new URL(candidate)
|
|
48
|
+
const isLoopback =
|
|
49
|
+
parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost"
|
|
50
|
+
if (
|
|
51
|
+
parsed.protocol !== "https:" &&
|
|
52
|
+
!(parsed.protocol === "http:" && isLoopback)
|
|
53
|
+
) {
|
|
54
|
+
throw new MedusaError(
|
|
55
|
+
MedusaError.Types.INVALID_DATA,
|
|
56
|
+
`Unsafe Bold base URL "${candidate}": only https:// or loopback http://127.0.0.1 is allowed`
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
return candidate.replace(/\/+$/, "")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class BoldHttpClient {
|
|
63
|
+
private readonly integrationsUrl: string
|
|
64
|
+
private readonly onlineUrl: string
|
|
65
|
+
private readonly paymentsUrl: string
|
|
66
|
+
|
|
67
|
+
private readonly buttonApiKey: string
|
|
68
|
+
private readonly integrationApiKey: string
|
|
69
|
+
private readonly logger: Logger
|
|
70
|
+
private readonly linkExpirationMinutes: number
|
|
71
|
+
private readonly defaultCurrency: string
|
|
72
|
+
|
|
73
|
+
constructor(options: BoldBaseOptions, logger: Logger) {
|
|
74
|
+
this.integrationsUrl = resolveSafeBaseUrl(
|
|
75
|
+
options.integrationsUrl,
|
|
76
|
+
"BOLD_INTEGRATIONS_URL",
|
|
77
|
+
DEFAULT_INTEGRATIONS_URL
|
|
78
|
+
)
|
|
79
|
+
this.onlineUrl = resolveSafeBaseUrl(
|
|
80
|
+
options.onlineUrl,
|
|
81
|
+
"BOLD_ONLINE_URL",
|
|
82
|
+
DEFAULT_ONLINE_URL
|
|
83
|
+
)
|
|
84
|
+
const rawPaymentsUrl = resolveSafeBaseUrl(
|
|
85
|
+
options.paymentsUrl,
|
|
86
|
+
"BOLD_PAYMENTS_URL",
|
|
87
|
+
DEFAULT_PAYMENTS_URL
|
|
88
|
+
)
|
|
89
|
+
this.paymentsUrl = `${rawPaymentsUrl}/`
|
|
90
|
+
|
|
91
|
+
// Resolve integration key (with general apiKey fallback)
|
|
92
|
+
this.integrationApiKey = options.integrationApiKey || ""
|
|
93
|
+
|
|
94
|
+
// Resolve button key (with general apiKey fallback)
|
|
95
|
+
this.buttonApiKey = options.buttonApiKey || ""
|
|
96
|
+
|
|
97
|
+
this.defaultCurrency = (options.defaultCurrency || "cop").toUpperCase()
|
|
98
|
+
this.linkExpirationMinutes = options.linkExpirationMinutes || 10
|
|
99
|
+
this.logger = logger
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Universal HTTP Request method with explicit API key resolution.
|
|
104
|
+
*/
|
|
105
|
+
private async request<T>(
|
|
106
|
+
baseUrl: string,
|
|
107
|
+
endpoint: string,
|
|
108
|
+
apiKey: string,
|
|
109
|
+
options: RequestInit = {}
|
|
110
|
+
): Promise<T> {
|
|
111
|
+
const url = `${baseUrl}${endpoint}`
|
|
112
|
+
const headers = {
|
|
113
|
+
"Content-Type": "application/json",
|
|
114
|
+
Authorization: `x-api-key ${apiKey}`,
|
|
115
|
+
...options.headers,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.logger?.debug(
|
|
119
|
+
`[BoldHttpClient] Requesting ${options.method || "GET"} ${url}`
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const response = await fetch(url, { ...options, headers })
|
|
124
|
+
const data = await response.json().catch(() => ({}))
|
|
125
|
+
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
this.logger?.error(`[BoldHttpClient] API Error on ${endpoint}`, data)
|
|
128
|
+
|
|
129
|
+
const errorType =
|
|
130
|
+
HTTP_ERROR_MAP[response.status] || MedusaError.Types.UNEXPECTED_STATE
|
|
131
|
+
|
|
132
|
+
throw new MedusaError(
|
|
133
|
+
errorType,
|
|
134
|
+
data?.message ||
|
|
135
|
+
`Bold API request failed with status ${response.status}`
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return data as T
|
|
140
|
+
} catch (error: any) {
|
|
141
|
+
if (error instanceof MedusaError) {
|
|
142
|
+
throw error
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this.logger?.error(
|
|
146
|
+
`[BoldHttpClient] Network error on ${endpoint}: ${error.message}`
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
throw new MedusaError(
|
|
150
|
+
MedusaError.Types.UNEXPECTED_STATE,
|
|
151
|
+
`Bold API network request failed: ${error.message}`
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Helper to construct the target Bold payment_method payload based on input type.
|
|
158
|
+
*/
|
|
159
|
+
private buildPaymentMethodPayload(
|
|
160
|
+
payload: BoldPaymentIntentPayload
|
|
161
|
+
): BoldPaymentMethod {
|
|
162
|
+
switch (payload.methodType) {
|
|
163
|
+
case "card":
|
|
164
|
+
return {
|
|
165
|
+
name: "CREDIT_CARD",
|
|
166
|
+
card_number: payload.cardNumber || "",
|
|
167
|
+
cardholder_name: payload.cardholderName || "",
|
|
168
|
+
expiration_month: payload.expirationMonth || "",
|
|
169
|
+
expiration_year: payload.expirationYear || "",
|
|
170
|
+
installments: payload.installments || 1,
|
|
171
|
+
cvc: payload.cvc || "",
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
case "pse":
|
|
175
|
+
return {
|
|
176
|
+
name: "PSE",
|
|
177
|
+
bank_code: payload.bankCode || "",
|
|
178
|
+
bank_name: payload.bankName || "",
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
case "nequi":
|
|
182
|
+
return {
|
|
183
|
+
name: "NEQUI",
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
case "bancolombia":
|
|
187
|
+
return {
|
|
188
|
+
name: "BOTON_BANCOLOMBIA",
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
case "qr":
|
|
192
|
+
default:
|
|
193
|
+
return {
|
|
194
|
+
name: "QR",
|
|
195
|
+
qr_format:
|
|
196
|
+
(payload.qrFormat as unknown as TBoldPaymentMethodQrFormat) ||
|
|
197
|
+
"TEXT",
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Generates an online payment link with tax breakdown.
|
|
204
|
+
* Key: integrationApiKey
|
|
205
|
+
* Target: integrationsUrl (/online/link/v1)
|
|
206
|
+
*/
|
|
207
|
+
async createPaymentLink(payload: CreatePaymentLinkPayload) {
|
|
208
|
+
const nowMs = BigInt(Date.now())
|
|
209
|
+
const minutesInNs =
|
|
210
|
+
BigInt(this.linkExpirationMinutes) * 60n * 1000n * 1000n * 1000n
|
|
211
|
+
const expirationNs = nowMs * 1000000n + minutesInNs
|
|
212
|
+
|
|
213
|
+
const currency = (payload.currency || this.defaultCurrency).toUpperCase()
|
|
214
|
+
const totalAmount = this.formatAmount(payload.amount)
|
|
215
|
+
const vatAmount = payload.vatAmount
|
|
216
|
+
? this.formatAmount(payload.vatAmount)
|
|
217
|
+
: 0
|
|
218
|
+
|
|
219
|
+
const body = {
|
|
220
|
+
amount_type: "CLOSE",
|
|
221
|
+
amount: {
|
|
222
|
+
currency,
|
|
223
|
+
total_amount: totalAmount,
|
|
224
|
+
taxes:
|
|
225
|
+
vatAmount > 0
|
|
226
|
+
? [{ type: "VAT", base: totalAmount - vatAmount, value: vatAmount }]
|
|
227
|
+
: [],
|
|
228
|
+
tip_amount: 0,
|
|
229
|
+
},
|
|
230
|
+
reference: payload.reference,
|
|
231
|
+
description: payload.description || `Medusa Order ${payload.reference}`,
|
|
232
|
+
expiration_date: Number(expirationNs),
|
|
233
|
+
callback_url: payload.callbackUrl,
|
|
234
|
+
payer_email: payload.email,
|
|
235
|
+
image_url: payload.imageUrl,
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return this.request<BoldCreatePaymentLinkResponse>(
|
|
239
|
+
this.integrationsUrl,
|
|
240
|
+
"/online/link/v1",
|
|
241
|
+
this.integrationApiKey,
|
|
242
|
+
{
|
|
243
|
+
method: "POST",
|
|
244
|
+
body: JSON.stringify(body),
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Queries payment status and transaction details by Bold link ID.
|
|
251
|
+
* Key: integrationApiKey
|
|
252
|
+
* Target: integrationsUrl (/online/link/v1/{linkId})
|
|
253
|
+
*/
|
|
254
|
+
async getLinkStatus(linkId: string) {
|
|
255
|
+
return this.request<BoldLinkPaymentStatusResponse>(
|
|
256
|
+
this.integrationsUrl,
|
|
257
|
+
`/online/link/v1/${encodeURIComponent(linkId)}`,
|
|
258
|
+
this.integrationApiKey,
|
|
259
|
+
{
|
|
260
|
+
method: "GET",
|
|
261
|
+
}
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Queries available POS payment terminals associated with the account.
|
|
267
|
+
* Key: integrationApiKey
|
|
268
|
+
* Target: integrationsUrl (/payments/binded-terminals)
|
|
269
|
+
*/
|
|
270
|
+
async getTerminals(): Promise<Array<BoldTerminalItem>> {
|
|
271
|
+
const response = await this.request<BoldTerminalsResponse>(
|
|
272
|
+
this.integrationsUrl,
|
|
273
|
+
"/payments/binded-terminals",
|
|
274
|
+
this.integrationApiKey,
|
|
275
|
+
{
|
|
276
|
+
method: "GET",
|
|
277
|
+
}
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
return response?.payload.available_terminals || []
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Triggers terminal checkout dispatch to a physical data-phone POS.
|
|
285
|
+
* Key: integrationApiKey
|
|
286
|
+
* Target: integrationsUrl (/payments/app-checkout)
|
|
287
|
+
*/
|
|
288
|
+
async triggerTerminalCheckout(payload: BoldTerminalPayload) {
|
|
289
|
+
const vatAmount = payload.vatAmount
|
|
290
|
+
? this.formatAmount(payload.vatAmount)
|
|
291
|
+
: 0
|
|
292
|
+
|
|
293
|
+
const payer = payload.payer
|
|
294
|
+
? {
|
|
295
|
+
email: payload.payer.email,
|
|
296
|
+
phone_number: payload.payer.phoneNumber,
|
|
297
|
+
document: {
|
|
298
|
+
document_type: payload.payer.document.documentType,
|
|
299
|
+
document_number: payload.payer.document.documentNumber,
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
: null
|
|
303
|
+
|
|
304
|
+
const body = {
|
|
305
|
+
amount: {
|
|
306
|
+
currency: (payload.currency || this.defaultCurrency).toUpperCase(),
|
|
307
|
+
total: this.formatAmount(payload.amount),
|
|
308
|
+
taxes: vatAmount > 0 ? [{ type: "VAT", value: vatAmount }] : [],
|
|
309
|
+
tip: 0,
|
|
310
|
+
},
|
|
311
|
+
payment_method: "POS",
|
|
312
|
+
terminal_model: payload.terminalModel,
|
|
313
|
+
terminal_serial: payload.terminalSerial,
|
|
314
|
+
reference: payload.reference,
|
|
315
|
+
user_email: payload.userEmail,
|
|
316
|
+
description: payload.description || "Medusa POS Order",
|
|
317
|
+
payer,
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return this.request<{ payload: { id: string } }>(
|
|
321
|
+
this.integrationsUrl,
|
|
322
|
+
"/payments/app-checkout",
|
|
323
|
+
this.integrationApiKey,
|
|
324
|
+
{
|
|
325
|
+
method: "POST",
|
|
326
|
+
body: JSON.stringify(body),
|
|
327
|
+
}
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Queries available payment methods enabled for the merchant (Integration API).
|
|
333
|
+
* Key: integrationApiKey
|
|
334
|
+
* Target: integrationsUrl (/payments/payment-methods)
|
|
335
|
+
*/
|
|
336
|
+
async getAvailableIntegrationApiPaymentMethods(): Promise<
|
|
337
|
+
Array<BoldIntegrationAPIPaymentMethod>
|
|
338
|
+
> {
|
|
339
|
+
const response =
|
|
340
|
+
await this.request<BoldIntegrationApiPaymentMethodsResponse>(
|
|
341
|
+
this.integrationsUrl,
|
|
342
|
+
"/payments/payment-methods",
|
|
343
|
+
this.integrationApiKey,
|
|
344
|
+
{
|
|
345
|
+
method: "GET",
|
|
346
|
+
}
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
return response?.payment_methods || []
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Queries available payment methods for Payment Link API.
|
|
354
|
+
* The Payment Link API supports: QR, Cards, PSE, Nequi, Bancolombia Button
|
|
355
|
+
* Key: integrationApiKey
|
|
356
|
+
* Target: integrationsUrl (/online/link/v1/payment_methods)
|
|
357
|
+
*/
|
|
358
|
+
async getAvailablePaymentLinkPaymentMethods(): Promise<BoldPaymentLinkPaymentMethodLimits> {
|
|
359
|
+
const response = await this.request<BoldPaymentLinkPaymentLimitsResponse>(
|
|
360
|
+
this.integrationsUrl,
|
|
361
|
+
"/online/link/v1/payment_methods",
|
|
362
|
+
this.integrationApiKey,
|
|
363
|
+
{
|
|
364
|
+
method: "GET",
|
|
365
|
+
}
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
return response?.payload.payment_methods || {}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Creates an online payment intent.
|
|
373
|
+
* Key: buttonApiKey
|
|
374
|
+
* Target: onlineUrl (/v1/payment-intent)
|
|
375
|
+
*/
|
|
376
|
+
async createPaymentIntent(
|
|
377
|
+
payload: BoldPaymentIntentPayload
|
|
378
|
+
): Promise<BoldPaymentAttemptResponse> {
|
|
379
|
+
const body = {
|
|
380
|
+
reference_id: payload.reference,
|
|
381
|
+
...(payload.metadata && { metadata: payload.metadata }),
|
|
382
|
+
payer: {
|
|
383
|
+
person_type: payload.payer?.personType || "NATURAL_PERSON",
|
|
384
|
+
name: payload.payer?.name,
|
|
385
|
+
phone: payload.payer?.phone,
|
|
386
|
+
email: payload.payer?.email,
|
|
387
|
+
document_type: payload.payer?.documentType || "CEDULA",
|
|
388
|
+
document_number: payload.payer?.documentNumber,
|
|
389
|
+
...(payload.payer?.billingAddress && {
|
|
390
|
+
billing_address: payload.payer.billingAddress,
|
|
391
|
+
}),
|
|
392
|
+
},
|
|
393
|
+
payment_method: this.buildPaymentMethodPayload(payload),
|
|
394
|
+
...(payload.deviceFingerprint && {
|
|
395
|
+
device_fingerprint: payload.deviceFingerprint,
|
|
396
|
+
}),
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return this.request<BoldPaymentAttemptResponse>(
|
|
400
|
+
this.onlineUrl,
|
|
401
|
+
"/v1/payment-intent",
|
|
402
|
+
this.buttonApiKey,
|
|
403
|
+
{
|
|
404
|
+
method: "POST",
|
|
405
|
+
body: JSON.stringify(body),
|
|
406
|
+
}
|
|
407
|
+
)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Requests a full or partial refund for a transaction via Bold API Online.
|
|
412
|
+
* Key: buttonApiKey
|
|
413
|
+
* Target: onlineUrl (/v1/refunds)
|
|
414
|
+
*/
|
|
415
|
+
async refundPayment(
|
|
416
|
+
payload: CreateRefundPayload
|
|
417
|
+
): Promise<BoldRefundApiResponse> {
|
|
418
|
+
const body: BoldRefundApiRequest = {
|
|
419
|
+
transaction_id: payload.transactionId,
|
|
420
|
+
amount: this.formatAmount(payload.amount),
|
|
421
|
+
...(payload.reason && { reason: payload.reason }),
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return this.request<BoldRefundApiResponse>(
|
|
425
|
+
this.onlineUrl,
|
|
426
|
+
"/v1/refunds",
|
|
427
|
+
this.buttonApiKey,
|
|
428
|
+
{
|
|
429
|
+
method: "POST",
|
|
430
|
+
body: JSON.stringify(body),
|
|
431
|
+
}
|
|
432
|
+
)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Queries payment status and voucher details by order reference ID.
|
|
437
|
+
* Key: buttonApiKey
|
|
438
|
+
* Target: paymentsUrl (reference)
|
|
439
|
+
*/
|
|
440
|
+
async getPaymentVoucher(reference: string) {
|
|
441
|
+
return this.request<BoldLinkPaymentStatusResponse>(
|
|
442
|
+
this.paymentsUrl,
|
|
443
|
+
encodeURIComponent(reference),
|
|
444
|
+
this.buttonApiKey,
|
|
445
|
+
{
|
|
446
|
+
method: "GET",
|
|
447
|
+
}
|
|
448
|
+
)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Fetches the list of active PSE financial institutions/banks.
|
|
453
|
+
* Key: buttonApiKey
|
|
454
|
+
* Target: onlineUrl (/v1/payment/pse/banks)
|
|
455
|
+
*/
|
|
456
|
+
async getPseBanks() {
|
|
457
|
+
return this.request<Array<BankItem>>(
|
|
458
|
+
this.onlineUrl,
|
|
459
|
+
"/v1/payment/pse/banks",
|
|
460
|
+
this.buttonApiKey,
|
|
461
|
+
{
|
|
462
|
+
method: "GET",
|
|
463
|
+
}
|
|
464
|
+
)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private formatAmount(amount: number): number {
|
|
468
|
+
return Math.max(0, Math.round(amount))
|
|
469
|
+
}
|
|
470
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import crypto from "crypto"
|
|
2
|
+
|
|
3
|
+
export class BoldIntegrityService {
|
|
4
|
+
/**
|
|
5
|
+
* Timing-safe verification of Bold Webhook HMAC-SHA256 signatures.
|
|
6
|
+
* Concatenates the timestamp header with the raw UTF-8 body string
|
|
7
|
+
* and computes the expected HMAC digest using constant-time comparison.
|
|
8
|
+
*
|
|
9
|
+
* @param rawBody - The raw HTTP request body (string or Buffer) before JSON parsing.
|
|
10
|
+
* @param signatureHeader - The signature string from `x-bold-signature` or `x-signature` header.
|
|
11
|
+
* @param timestampHeader - The timestamp string from `x-bold-timestamp` or `x-timestamp` header.
|
|
12
|
+
* @param secretKey - The Webhook Signing Secret Key provided in the Bold Merchant Dashboard.
|
|
13
|
+
* @returns `true` if the computed signature matches the header signature; otherwise `false`.
|
|
14
|
+
*/
|
|
15
|
+
static verifyWebhookSignature(
|
|
16
|
+
rawBody: string | Buffer,
|
|
17
|
+
signatureHeader: string,
|
|
18
|
+
timestampHeader: string,
|
|
19
|
+
secretKey: string
|
|
20
|
+
): boolean {
|
|
21
|
+
// 1. Fail immediately if secret key, signature, or timestamp headers are missing
|
|
22
|
+
if (!secretKey || !signatureHeader || !timestampHeader) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 2. Ensure raw body is a clean UTF-8 string
|
|
27
|
+
const bodyString = Buffer.isBuffer(rawBody)
|
|
28
|
+
? rawBody.toString("utf-8")
|
|
29
|
+
: rawBody
|
|
30
|
+
|
|
31
|
+
// 3. Bold webhook signature format: timestamp + raw UTF-8 JSON payload
|
|
32
|
+
const payloadToSign = `${timestampHeader}${bodyString}`
|
|
33
|
+
|
|
34
|
+
// 4. Compute expected HMAC-SHA256 signature hash
|
|
35
|
+
const computedHash = crypto
|
|
36
|
+
.createHmac("sha256", secretKey)
|
|
37
|
+
.update(payloadToSign)
|
|
38
|
+
.digest("hex")
|
|
39
|
+
|
|
40
|
+
const signatureBuffer = Buffer.from(signatureHeader, "utf-8")
|
|
41
|
+
const computedBuffer = Buffer.from(computedHash, "utf-8")
|
|
42
|
+
|
|
43
|
+
// 5. Length check prior to constant-time comparison
|
|
44
|
+
if (signatureBuffer.length !== computedBuffer.length) {
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 6. Timing-safe equality check to prevent timing side-channel attacks
|
|
49
|
+
return crypto.timingSafeEqual(
|
|
50
|
+
new Uint8Array(signatureBuffer),
|
|
51
|
+
new Uint8Array(computedBuffer)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Calculates the SHA-256 integrity signature hash for the storefront Bold Payment Button.
|
|
57
|
+
* Concatenates reference, amount, currency, and secret key.
|
|
58
|
+
*
|
|
59
|
+
* @param reference - The unique payment reference identifier.
|
|
60
|
+
* @param amount - The total payment amount in base currency units (e.g., COP integer).
|
|
61
|
+
* @param currency - The 3-letter currency code (e.g., "COP").
|
|
62
|
+
* @param secretKey - The Integrity / API Secret Key provided in the Bold Merchant Dashboard.
|
|
63
|
+
* @returns SHA-256 hex digest string used to validate the checkout button payload.
|
|
64
|
+
*/
|
|
65
|
+
static generateButtonHash(
|
|
66
|
+
reference: string,
|
|
67
|
+
amount: number,
|
|
68
|
+
currency: string,
|
|
69
|
+
secretKey: string
|
|
70
|
+
): string {
|
|
71
|
+
const rawString = `${reference}${amount}${currency}${secretKey}`
|
|
72
|
+
return crypto.createHash("sha256").update(rawString).digest("hex")
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const BOLD_ENVIRONMENT = ["sandbox", "production"] as const
|
|
2
|
+
|
|
3
|
+
export const BOLD_PAYMENT_METHOD_QR_FORMAT = [
|
|
4
|
+
"BOLD_BASE64",
|
|
5
|
+
"PLAIN_BASE64",
|
|
6
|
+
"TEXT",
|
|
7
|
+
] as const
|
|
8
|
+
|
|
9
|
+
export const BOLD_PAYMENT_PROVIDERS = [
|
|
10
|
+
"pp_bold-link_bold",
|
|
11
|
+
"pp_bold-online_bold",
|
|
12
|
+
"pp_bold-button_bold",
|
|
13
|
+
"pp_bold-terminal_bold",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
export const BOLD_PAYMENT_ACTIONS_SUCCESSFUL = [
|
|
17
|
+
"PAID",
|
|
18
|
+
"APPROVED",
|
|
19
|
+
"SUCCESSFUL",
|
|
20
|
+
"captured",
|
|
21
|
+
] as const
|
|
22
|
+
|
|
23
|
+
export const BOLD_PAYMENT_ACTIONS_FAILED = [
|
|
24
|
+
"REJECTED",
|
|
25
|
+
"FAILED",
|
|
26
|
+
"CANCELLED",
|
|
27
|
+
"EXPIRED",
|
|
28
|
+
"error",
|
|
29
|
+
] as const
|
|
30
|
+
|
|
31
|
+
export const BOLD_PAYMENT_METHOD_TYPE = [
|
|
32
|
+
"card",
|
|
33
|
+
"pse",
|
|
34
|
+
"nequi",
|
|
35
|
+
"bancolombia",
|
|
36
|
+
"qr",
|
|
37
|
+
] as const
|
|
38
|
+
|
|
39
|
+
// TODO: if lowercased does not work, this is the right
|
|
40
|
+
// export const BOLD_PAYMENT_METHOD_TYPE = ["CARD" , "PSE" , "NEQUI" , "BANCOLOMBIA" , "QR"] as const
|
|
41
|
+
|
|
42
|
+
export const BOLD_PLUGIN_PROVIDERS = [
|
|
43
|
+
"bold-online",
|
|
44
|
+
"bold-link",
|
|
45
|
+
"bold-button",
|
|
46
|
+
"bold-terminal",
|
|
47
|
+
] as const
|
|
48
|
+
|
|
49
|
+
export const BOLD_FAILED_STATUSES = [
|
|
50
|
+
"canceled",
|
|
51
|
+
"cancelled",
|
|
52
|
+
"error",
|
|
53
|
+
"rejected",
|
|
54
|
+
"failed",
|
|
55
|
+
] as const
|
|
56
|
+
|
|
57
|
+
export const BOLD_SUCCESS_STATUSES = [
|
|
58
|
+
"captured",
|
|
59
|
+
"authorized",
|
|
60
|
+
"approved",
|
|
61
|
+
"completed",
|
|
62
|
+
"paid",
|
|
63
|
+
] as const
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export enum EBoldPaymentProvider {
|
|
2
|
+
BUTTON = "bold-button",
|
|
3
|
+
LINK = "bold-link",
|
|
4
|
+
ONLINE = "bold-online",
|
|
5
|
+
TERMINAL = "bold-terminal",
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export enum EMedusaFormatBoldPaymentProvider {
|
|
9
|
+
BUTTON = "pp_bold-button_bold",
|
|
10
|
+
LINK = "pp_bold-link_bold",
|
|
11
|
+
ONLINE = "pp_bold-online_bold",
|
|
12
|
+
TERMINAL = "pp_bold-terminal_bold",
|
|
13
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { MappedPaymentError } from "."
|
|
2
|
+
|
|
3
|
+
export const BOLD_ERROR_MAP: Record<string, MappedPaymentError> = {
|
|
4
|
+
EXPIRED_CARD: {
|
|
5
|
+
code: "EXPIRED_CARD",
|
|
6
|
+
userMessage: "bold.errors.expired_card",
|
|
7
|
+
adminReason: "Card expiration date is in the past.",
|
|
8
|
+
retryable: false,
|
|
9
|
+
},
|
|
10
|
+
INVALID_CARD_NUMBER: {
|
|
11
|
+
code: "INVALID_CARD_NUMBER",
|
|
12
|
+
userMessage: "bold.errors.invalid_card_number",
|
|
13
|
+
adminReason: "Luhn check or card bin validation failed.",
|
|
14
|
+
retryable: true,
|
|
15
|
+
},
|
|
16
|
+
INVALID_CVV: {
|
|
17
|
+
code: "INVALID_CVV",
|
|
18
|
+
userMessage: "bold.errors.invalid_cvv",
|
|
19
|
+
adminReason: "CVV verification failed at issuer.",
|
|
20
|
+
retryable: true,
|
|
21
|
+
},
|
|
22
|
+
INSUFFICIENT_FUNDS: {
|
|
23
|
+
code: "INSUFFICIENT_FUNDS",
|
|
24
|
+
userMessage: "bold.errors.insufficient_funds",
|
|
25
|
+
adminReason: "Declined by issuing bank due to insufficient funds.",
|
|
26
|
+
retryable: false,
|
|
27
|
+
},
|
|
28
|
+
EXCEEDS_TRANSACTION_LIMIT: {
|
|
29
|
+
code: "EXCEEDS_TRANSACTION_LIMIT",
|
|
30
|
+
userMessage: "bold.errors.exceeds_transaction_limit",
|
|
31
|
+
adminReason: "Transaction amount exceeds card limit.",
|
|
32
|
+
retryable: false,
|
|
33
|
+
},
|
|
34
|
+
"3DS_AUTHENTICATION_FAILED": {
|
|
35
|
+
code: "3DS_AUTHENTICATION_FAILED",
|
|
36
|
+
userMessage: "bold.errors.3ds_authentication_failed",
|
|
37
|
+
adminReason: "Issuer 3DS OTP validation failed or timed out.",
|
|
38
|
+
retryable: true,
|
|
39
|
+
},
|
|
40
|
+
FRAUD_SUSPECTED: {
|
|
41
|
+
code: "FRAUD_SUSPECTED",
|
|
42
|
+
userMessage: "bold.errors.fraud_suspected",
|
|
43
|
+
adminReason: "Bold or issuing bank anti-fraud filter flagged transaction.",
|
|
44
|
+
retryable: false,
|
|
45
|
+
},
|
|
46
|
+
RESTRICTED_CARD: {
|
|
47
|
+
code: "RESTRICTED_CARD",
|
|
48
|
+
userMessage: "bold.errors.restricted_card",
|
|
49
|
+
adminReason: "Card is reported stolen, lost, or blocked for e-commerce.",
|
|
50
|
+
retryable: false,
|
|
51
|
+
},
|
|
52
|
+
NEQUI_TIMEOUT: {
|
|
53
|
+
code: "NEQUI_TIMEOUT",
|
|
54
|
+
userMessage: "bold.errors.nequi_timeout",
|
|
55
|
+
adminReason:
|
|
56
|
+
"Push notification was not approved within the timeout window.",
|
|
57
|
+
retryable: true,
|
|
58
|
+
},
|
|
59
|
+
PSE_BANK_UNAVAILABLE: {
|
|
60
|
+
code: "PSE_BANK_UNAVAILABLE",
|
|
61
|
+
userMessage: "bold.errors.pse_bank_unavailable",
|
|
62
|
+
adminReason: "Financial institution bank node down or unreachable.",
|
|
63
|
+
retryable: true,
|
|
64
|
+
},
|
|
65
|
+
DEFAULT_ERROR: {
|
|
66
|
+
code: "GENERIC_DECLINE",
|
|
67
|
+
userMessage: "bold.errors.generic_decline",
|
|
68
|
+
adminReason: "Generic or unspecified gateway error.",
|
|
69
|
+
retryable: true,
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function parseBoldError(boldErrorData: any): MappedPaymentError {
|
|
74
|
+
const rawCode =
|
|
75
|
+
boldErrorData?.code ||
|
|
76
|
+
boldErrorData?.failure_code ||
|
|
77
|
+
boldErrorData?.errors?.[0]?.code ||
|
|
78
|
+
""
|
|
79
|
+
|
|
80
|
+
const uppercaseCode = rawCode.toUpperCase().replace(/\s+/g, "_")
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
BOLD_ERROR_MAP[uppercaseCode] || {
|
|
84
|
+
...BOLD_ERROR_MAP.DEFAULT_ERROR,
|
|
85
|
+
adminReason: `Unmapped error: ${JSON.stringify(boldErrorData)}`,
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
}
|