@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,582 @@
|
|
|
1
|
+
import crypto from "node:crypto"
|
|
2
|
+
import http from "node:http"
|
|
3
|
+
|
|
4
|
+
export interface EmulatorTerminal {
|
|
5
|
+
terminal_serial: string
|
|
6
|
+
terminal_model: string
|
|
7
|
+
status: "READY" | "BUSY" | "OFFLINE"
|
|
8
|
+
name: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface RecordedRequest {
|
|
12
|
+
method: string
|
|
13
|
+
path: string
|
|
14
|
+
authorization?: string
|
|
15
|
+
body: Record<string, unknown> | null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface StoredTransaction {
|
|
19
|
+
id: string
|
|
20
|
+
reference: string
|
|
21
|
+
linkId?: string
|
|
22
|
+
channel: "LINK" | "QR" | "ONLINE" | "BUTTON" | "POS"
|
|
23
|
+
amount: number
|
|
24
|
+
currency: string
|
|
25
|
+
status:
|
|
26
|
+
| "ACTIVE"
|
|
27
|
+
| "PROCESSING"
|
|
28
|
+
| "APPROVED"
|
|
29
|
+
| "REJECTED"
|
|
30
|
+
| "EXPIRED"
|
|
31
|
+
| "CANCELLED"
|
|
32
|
+
transactionId: string
|
|
33
|
+
paymentMethod: string
|
|
34
|
+
refundedAmount: number
|
|
35
|
+
terminalSerial?: string
|
|
36
|
+
terminalModel?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BoldEmulatorConfig {
|
|
40
|
+
integrationApiKey: string
|
|
41
|
+
buttonApiKey: string
|
|
42
|
+
webhookSecret: string
|
|
43
|
+
terminals?: EmulatorTerminal[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SignedWebhookEnvelope {
|
|
47
|
+
rawBody: string
|
|
48
|
+
payload: Record<string, unknown>
|
|
49
|
+
headers: Record<string, string>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function signBoldWebhook(
|
|
53
|
+
payload: Record<string, unknown>,
|
|
54
|
+
secretKey: string,
|
|
55
|
+
timestamp = String(Date.now())
|
|
56
|
+
): SignedWebhookEnvelope {
|
|
57
|
+
const rawBody = JSON.stringify(payload)
|
|
58
|
+
const signature = crypto
|
|
59
|
+
.createHmac("sha256", secretKey)
|
|
60
|
+
.update(`${timestamp}${rawBody}`)
|
|
61
|
+
.digest("hex")
|
|
62
|
+
return {
|
|
63
|
+
rawBody,
|
|
64
|
+
payload,
|
|
65
|
+
headers: {
|
|
66
|
+
"content-type": "application/json",
|
|
67
|
+
"x-bold-signature": signature,
|
|
68
|
+
"x-bold-timestamp": timestamp,
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class BoldQaEmulator {
|
|
74
|
+
private server: http.Server | null = null
|
|
75
|
+
private baseUrl = ""
|
|
76
|
+
private seq = 1
|
|
77
|
+
private readonly cfg: BoldEmulatorConfig
|
|
78
|
+
private readonly transactionsByRef = new Map<string, StoredTransaction>()
|
|
79
|
+
private readonly transactionsByLinkId = new Map<string, StoredTransaction>()
|
|
80
|
+
private readonly transactionsByTxId = new Map<string, StoredTransaction>()
|
|
81
|
+
private readonly terminals: EmulatorTerminal[]
|
|
82
|
+
private nextFault: {
|
|
83
|
+
pathContains: string
|
|
84
|
+
status: number
|
|
85
|
+
message: string
|
|
86
|
+
} | null = null
|
|
87
|
+
public readonly requests: RecordedRequest[] = []
|
|
88
|
+
|
|
89
|
+
constructor(cfg: BoldEmulatorConfig) {
|
|
90
|
+
this.cfg = cfg
|
|
91
|
+
this.terminals = cfg.terminals ?? [
|
|
92
|
+
{
|
|
93
|
+
terminal_serial: "N86-QA-001",
|
|
94
|
+
terminal_model: "N86",
|
|
95
|
+
status: "READY",
|
|
96
|
+
name: "QA Dataphone Counter 1",
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
terminal_serial: "SMART-QA-002",
|
|
100
|
+
terminal_model: "SMART_PRO",
|
|
101
|
+
status: "READY",
|
|
102
|
+
name: "QA Dataphone Mobile 2",
|
|
103
|
+
},
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get url(): string {
|
|
108
|
+
return this.baseUrl
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
get clientOptions() {
|
|
112
|
+
return {
|
|
113
|
+
buttonApiKey: this.cfg.buttonApiKey,
|
|
114
|
+
buttonSecretKey: this.cfg.webhookSecret,
|
|
115
|
+
integrationApiKey: this.cfg.integrationApiKey,
|
|
116
|
+
integrationSecretKey: this.cfg.webhookSecret,
|
|
117
|
+
defaultCurrency: "COP",
|
|
118
|
+
linkExpirationMinutes: 15,
|
|
119
|
+
integrationsUrl: this.baseUrl,
|
|
120
|
+
onlineUrl: this.baseUrl,
|
|
121
|
+
paymentsUrl: `${this.baseUrl}/v2/payment-voucher`,
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
reset(): void {
|
|
126
|
+
this.requests.length = 0
|
|
127
|
+
this.transactionsByRef.clear()
|
|
128
|
+
this.transactionsByLinkId.clear()
|
|
129
|
+
this.transactionsByTxId.clear()
|
|
130
|
+
this.nextFault = null
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
injectFault(pathContains: string, status: number, message: string): void {
|
|
134
|
+
this.nextFault = { pathContains, status, message }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
seedButtonOrder(reference: string, amount: number, currency = "COP"): void {
|
|
138
|
+
const tx: StoredTransaction = {
|
|
139
|
+
id: `btn_${this.seq++}`,
|
|
140
|
+
reference,
|
|
141
|
+
channel: "BUTTON",
|
|
142
|
+
amount,
|
|
143
|
+
currency: currency.toUpperCase(),
|
|
144
|
+
status: "ACTIVE",
|
|
145
|
+
transactionId: `tx_btn_${this.seq}`,
|
|
146
|
+
paymentMethod: "BOLD_BUTTON",
|
|
147
|
+
refundedAmount: 0,
|
|
148
|
+
}
|
|
149
|
+
this.transactionsByRef.set(reference, tx)
|
|
150
|
+
this.transactionsByTxId.set(tx.transactionId, tx)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
simulatePaymentOutcome(
|
|
154
|
+
referenceOrLinkId: string,
|
|
155
|
+
outcome: "APPROVED" | "REJECTED" | "EXPIRED" | "CANCELLED",
|
|
156
|
+
overrides?: { amount?: number; currency?: string; paymentMethod?: string }
|
|
157
|
+
): SignedWebhookEnvelope {
|
|
158
|
+
const tx =
|
|
159
|
+
this.transactionsByRef.get(referenceOrLinkId) ||
|
|
160
|
+
this.transactionsByLinkId.get(referenceOrLinkId)
|
|
161
|
+
if (!tx) {
|
|
162
|
+
throw new Error(`Emulator has no transaction for "${referenceOrLinkId}"`)
|
|
163
|
+
}
|
|
164
|
+
tx.status = outcome
|
|
165
|
+
if (overrides?.paymentMethod) {
|
|
166
|
+
tx.paymentMethod = overrides.paymentMethod
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const eventMap: Record<typeof outcome, string> = {
|
|
170
|
+
APPROVED: "sale.successful",
|
|
171
|
+
REJECTED: "sale.rejected",
|
|
172
|
+
EXPIRED: "sale.failed",
|
|
173
|
+
CANCELLED: "sale.failed",
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const payload = {
|
|
177
|
+
event: eventMap[outcome],
|
|
178
|
+
data: {
|
|
179
|
+
id: tx.transactionId,
|
|
180
|
+
reference: tx.reference,
|
|
181
|
+
amount: overrides?.amount ?? tx.amount,
|
|
182
|
+
currency: overrides?.currency ?? tx.currency,
|
|
183
|
+
status: outcome,
|
|
184
|
+
payment_method: tx.paymentMethod,
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
return signBoldWebhook(payload, this.cfg.webhookSecret)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async start(port = 0): Promise<string> {
|
|
191
|
+
this.server = http.createServer((req, res) => {
|
|
192
|
+
void this.handleRequest(req, res)
|
|
193
|
+
})
|
|
194
|
+
await new Promise<void>((resolve) => {
|
|
195
|
+
this.server?.listen(port, "127.0.0.1", () => resolve())
|
|
196
|
+
})
|
|
197
|
+
const addr = this.server.address()
|
|
198
|
+
if (!addr || typeof addr === "string") {
|
|
199
|
+
throw new Error("Failed to bind BoldQaEmulator on 127.0.0.1")
|
|
200
|
+
}
|
|
201
|
+
this.baseUrl = `http://127.0.0.1:${addr.port}`
|
|
202
|
+
return this.baseUrl
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async stop(): Promise<void> {
|
|
206
|
+
if (!this.server) return
|
|
207
|
+
await new Promise<void>((resolve, reject) => {
|
|
208
|
+
this.server?.close((err) => (err ? reject(err) : resolve()))
|
|
209
|
+
})
|
|
210
|
+
this.server = null
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private sendJson(
|
|
214
|
+
res: http.ServerResponse,
|
|
215
|
+
status: number,
|
|
216
|
+
body: unknown
|
|
217
|
+
): void {
|
|
218
|
+
res.writeHead(status, { "content-type": "application/json" })
|
|
219
|
+
res.end(JSON.stringify(body))
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private async readBody(
|
|
223
|
+
req: http.IncomingMessage
|
|
224
|
+
): Promise<Record<string, unknown> | null> {
|
|
225
|
+
const chunks: Buffer[] = []
|
|
226
|
+
for await (const chunk of req) {
|
|
227
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
|
|
228
|
+
}
|
|
229
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim()
|
|
230
|
+
if (!raw) return null
|
|
231
|
+
try {
|
|
232
|
+
return JSON.parse(raw) as Record<string, unknown>
|
|
233
|
+
} catch {
|
|
234
|
+
return null
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private checkApiKey(
|
|
239
|
+
req: http.IncomingMessage,
|
|
240
|
+
expectedKey: string,
|
|
241
|
+
res: http.ServerResponse
|
|
242
|
+
): boolean {
|
|
243
|
+
const auth = req.headers.authorization || ""
|
|
244
|
+
if (auth !== `x-api-key ${expectedKey}`) {
|
|
245
|
+
this.sendJson(res, 401, { message: "Invalid Bold API key" })
|
|
246
|
+
return false
|
|
247
|
+
}
|
|
248
|
+
return true
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private async handleRequest(
|
|
252
|
+
req: http.IncomingMessage,
|
|
253
|
+
res: http.ServerResponse
|
|
254
|
+
): Promise<void> {
|
|
255
|
+
const url = new URL(req.url || "/", this.baseUrl || "http://127.0.0.1")
|
|
256
|
+
const method = (req.method || "GET").toUpperCase()
|
|
257
|
+
const pathname = url.pathname
|
|
258
|
+
const body = await this.readBody(req)
|
|
259
|
+
|
|
260
|
+
if (!pathname.startsWith("/__qa/")) {
|
|
261
|
+
this.requests.push({
|
|
262
|
+
method,
|
|
263
|
+
path: pathname,
|
|
264
|
+
authorization: req.headers.authorization,
|
|
265
|
+
body,
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (this.nextFault && pathname.includes(this.nextFault.pathContains)) {
|
|
270
|
+
const fault = this.nextFault
|
|
271
|
+
this.nextFault = null
|
|
272
|
+
this.sendJson(res, fault.status, { message: fault.message })
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// QA Control Endpoints
|
|
277
|
+
if (method === "GET" && pathname === "/__qa/health") {
|
|
278
|
+
this.sendJson(res, 200, { status: "ok", emulator: "bold-qa" })
|
|
279
|
+
return
|
|
280
|
+
}
|
|
281
|
+
if (method === "GET" && pathname === "/__qa/requests") {
|
|
282
|
+
this.sendJson(res, 200, { requests: this.requests })
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
if (method === "POST" && pathname === "/__qa/reset") {
|
|
286
|
+
this.reset()
|
|
287
|
+
this.sendJson(res, 200, { reset: true })
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
if (method === "POST" && pathname === "/__qa/fault") {
|
|
291
|
+
this.injectFault(
|
|
292
|
+
String(body?.pathContains || "/"),
|
|
293
|
+
Number(body?.status || 500),
|
|
294
|
+
String(body?.message || "Injected QA fault")
|
|
295
|
+
)
|
|
296
|
+
this.sendJson(res, 200, { injected: true })
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
if (method === "POST" && pathname === "/__qa/simulate-payment") {
|
|
300
|
+
const ref = String(body?.reference || body?.linkId || "")
|
|
301
|
+
const outcome = (body?.outcome || "APPROVED") as
|
|
302
|
+
| "APPROVED"
|
|
303
|
+
| "REJECTED"
|
|
304
|
+
| "EXPIRED"
|
|
305
|
+
| "CANCELLED"
|
|
306
|
+
if (body?.seedButton && ref) {
|
|
307
|
+
this.seedButtonOrder(
|
|
308
|
+
ref,
|
|
309
|
+
Number(body?.amount || 0),
|
|
310
|
+
String(body?.currency || "COP")
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
const envelope = this.simulatePaymentOutcome(ref, outcome)
|
|
314
|
+
const webhookUrl = body?.webhookUrl ? String(body.webhookUrl) : ""
|
|
315
|
+
let webhookStatus: number | null = null
|
|
316
|
+
if (webhookUrl) {
|
|
317
|
+
const parsedHook = new URL(webhookUrl)
|
|
318
|
+
if (
|
|
319
|
+
parsedHook.hostname !== "127.0.0.1" &&
|
|
320
|
+
parsedHook.hostname !== "localhost"
|
|
321
|
+
) {
|
|
322
|
+
this.sendJson(res, 400, {
|
|
323
|
+
message: "webhookUrl must target 127.0.0.1/localhost",
|
|
324
|
+
})
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
const hookRes = await fetch(webhookUrl, {
|
|
328
|
+
method: "POST",
|
|
329
|
+
headers: envelope.headers,
|
|
330
|
+
body: envelope.rawBody,
|
|
331
|
+
})
|
|
332
|
+
webhookStatus = hookRes.status
|
|
333
|
+
}
|
|
334
|
+
this.sendJson(res, 200, {
|
|
335
|
+
simulated: true,
|
|
336
|
+
outcome,
|
|
337
|
+
webhookStatus,
|
|
338
|
+
envelope,
|
|
339
|
+
})
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 1. Payment Link API (integrationApiKey)
|
|
344
|
+
if (method === "POST" && pathname === "/online/link/v1") {
|
|
345
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
346
|
+
const reference = String(body?.reference || `ref_${this.seq}`)
|
|
347
|
+
const amountObj = (body?.amount || {}) as Record<string, unknown>
|
|
348
|
+
const linkId = `LNK_QA_${this.seq++}`
|
|
349
|
+
const tx: StoredTransaction = {
|
|
350
|
+
id: linkId,
|
|
351
|
+
linkId,
|
|
352
|
+
reference,
|
|
353
|
+
channel: "LINK",
|
|
354
|
+
amount: Number(amountObj.total_amount || 0),
|
|
355
|
+
currency: String(amountObj.currency || "COP"),
|
|
356
|
+
status: "ACTIVE",
|
|
357
|
+
transactionId: `tx_link_${this.seq}`,
|
|
358
|
+
paymentMethod: "PAYMENT_LINK",
|
|
359
|
+
refundedAmount: 0,
|
|
360
|
+
}
|
|
361
|
+
this.transactionsByRef.set(reference, tx)
|
|
362
|
+
this.transactionsByLinkId.set(linkId, tx)
|
|
363
|
+
this.transactionsByTxId.set(tx.transactionId, tx)
|
|
364
|
+
this.sendJson(res, 200, {
|
|
365
|
+
payload: {
|
|
366
|
+
payment_link: linkId,
|
|
367
|
+
url: `${this.baseUrl}/checkout/${linkId}`,
|
|
368
|
+
},
|
|
369
|
+
errors: [],
|
|
370
|
+
})
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (method === "GET" && pathname === "/online/link/v1/payment_methods") {
|
|
375
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
376
|
+
this.sendJson(res, 200, {
|
|
377
|
+
payload: {
|
|
378
|
+
payment_methods: {
|
|
379
|
+
CREDIT_CARD: { min: 1000, max: 50000000 },
|
|
380
|
+
PSE: { min: 1000, max: 50000000 },
|
|
381
|
+
NEQUI: { min: 1000, max: 10000000 },
|
|
382
|
+
BOTON_BANCOLOMBIA: { min: 1000, max: 20000000 },
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
errors: [],
|
|
386
|
+
})
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (method === "GET" && pathname.startsWith("/online/link/v1/")) {
|
|
391
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
392
|
+
const linkId = decodeURIComponent(
|
|
393
|
+
pathname.slice("/online/link/v1/".length)
|
|
394
|
+
)
|
|
395
|
+
const tx = this.transactionsByLinkId.get(linkId)
|
|
396
|
+
if (!tx) {
|
|
397
|
+
this.sendJson(res, 404, { message: `Link ${linkId} not found` })
|
|
398
|
+
return
|
|
399
|
+
}
|
|
400
|
+
this.sendJson(res, 200, {
|
|
401
|
+
link_id: linkId,
|
|
402
|
+
reference_id: tx.reference,
|
|
403
|
+
total: tx.amount,
|
|
404
|
+
subtotal: tx.amount,
|
|
405
|
+
description: `Order ${tx.reference}`,
|
|
406
|
+
payment_status: tx.status,
|
|
407
|
+
status: tx.status,
|
|
408
|
+
transaction_id: tx.transactionId,
|
|
409
|
+
payment_method: tx.paymentMethod,
|
|
410
|
+
})
|
|
411
|
+
return
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// 2. POS Terminals API (integrationApiKey)
|
|
415
|
+
if (method === "GET" && pathname === "/payments/binded-terminals") {
|
|
416
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
417
|
+
this.sendJson(res, 200, {
|
|
418
|
+
payload: { available_terminals: this.terminals },
|
|
419
|
+
errors: [],
|
|
420
|
+
})
|
|
421
|
+
return
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (method === "GET" && pathname === "/payments/payment-methods") {
|
|
425
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
426
|
+
this.sendJson(res, 200, {
|
|
427
|
+
payment_methods: [
|
|
428
|
+
{ name: "POS", enabled: true },
|
|
429
|
+
{ name: "NEQUI", enabled: true },
|
|
430
|
+
{ name: "DAVIPLATA", enabled: true },
|
|
431
|
+
],
|
|
432
|
+
errors: [],
|
|
433
|
+
})
|
|
434
|
+
return
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (method === "POST" && pathname === "/payments/app-checkout") {
|
|
438
|
+
if (!this.checkApiKey(req, this.cfg.integrationApiKey, res)) return
|
|
439
|
+
const serial = String(body?.terminal_serial || "")
|
|
440
|
+
const model = String(body?.terminal_model || "")
|
|
441
|
+
const found = this.terminals.find(
|
|
442
|
+
(t) => t.terminal_serial === serial && t.terminal_model === model
|
|
443
|
+
)
|
|
444
|
+
if (!found) {
|
|
445
|
+
this.sendJson(res, 404, {
|
|
446
|
+
message: `Terminal ${model}/${serial} not bound to merchant account`,
|
|
447
|
+
})
|
|
448
|
+
return
|
|
449
|
+
}
|
|
450
|
+
const reference = String(body?.reference || `pos_${this.seq}`)
|
|
451
|
+
const amountObj = (body?.amount || {}) as Record<string, unknown>
|
|
452
|
+
const checkoutId = `pos_chk_${this.seq++}`
|
|
453
|
+
const tx: StoredTransaction = {
|
|
454
|
+
id: checkoutId,
|
|
455
|
+
reference,
|
|
456
|
+
channel: "POS",
|
|
457
|
+
amount: Number(amountObj.total || 0),
|
|
458
|
+
currency: String(amountObj.currency || "COP"),
|
|
459
|
+
status: "PROCESSING",
|
|
460
|
+
transactionId: `tx_pos_${this.seq}`,
|
|
461
|
+
paymentMethod: "POS",
|
|
462
|
+
refundedAmount: 0,
|
|
463
|
+
terminalSerial: serial,
|
|
464
|
+
terminalModel: model,
|
|
465
|
+
}
|
|
466
|
+
this.transactionsByRef.set(reference, tx)
|
|
467
|
+
this.transactionsByTxId.set(tx.transactionId, tx)
|
|
468
|
+
this.sendJson(res, 200, { payload: { id: checkoutId } })
|
|
469
|
+
return
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// 3. Online / QR / PSE Payment Intent API (buttonApiKey)
|
|
473
|
+
if (method === "POST" && pathname === "/v1/payment-intent") {
|
|
474
|
+
if (!this.checkApiKey(req, this.cfg.buttonApiKey, res)) return
|
|
475
|
+
const reference = String(body?.reference_id || `qr_${this.seq}`)
|
|
476
|
+
const pm = (body?.payment_method || {}) as Record<string, unknown>
|
|
477
|
+
const pmName = String(pm.name || "QR")
|
|
478
|
+
const intentId = `pi_qa_${this.seq++}`
|
|
479
|
+
const qrFormat = String(pm.qr_format || "BOLD_BASE64")
|
|
480
|
+
const qrPayload =
|
|
481
|
+
qrFormat === "TEXT"
|
|
482
|
+
? `00020101021226BOLDQA${reference}5802CO6304ABCD`
|
|
483
|
+
: Buffer.from(`BOLD_QR_IMAGE:${reference}:${qrFormat}`).toString(
|
|
484
|
+
"base64"
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
const tx: StoredTransaction = {
|
|
488
|
+
id: intentId,
|
|
489
|
+
reference,
|
|
490
|
+
channel: pmName === "QR" ? "QR" : "ONLINE",
|
|
491
|
+
amount: Number(
|
|
492
|
+
(body?.metadata as Record<string, unknown>)?.amount || 0
|
|
493
|
+
),
|
|
494
|
+
currency: "COP",
|
|
495
|
+
status: "ACTIVE",
|
|
496
|
+
transactionId: `tx_online_${this.seq}`,
|
|
497
|
+
paymentMethod: pmName,
|
|
498
|
+
refundedAmount: 0,
|
|
499
|
+
}
|
|
500
|
+
this.transactionsByRef.set(reference, tx)
|
|
501
|
+
this.transactionsByTxId.set(tx.transactionId, tx)
|
|
502
|
+
|
|
503
|
+
this.sendJson(res, 200, {
|
|
504
|
+
id: intentId,
|
|
505
|
+
status: "PENDING",
|
|
506
|
+
reference_id: reference,
|
|
507
|
+
payment_url: `${this.baseUrl}/pay/${intentId}`,
|
|
508
|
+
payment_method: {
|
|
509
|
+
name: pmName,
|
|
510
|
+
qr_format: qrFormat,
|
|
511
|
+
qr_payload: qrPayload,
|
|
512
|
+
},
|
|
513
|
+
})
|
|
514
|
+
return
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (method === "GET" && pathname === "/v1/payment/pse/banks") {
|
|
518
|
+
if (!this.checkApiKey(req, this.cfg.buttonApiKey, res)) return
|
|
519
|
+
this.sendJson(res, 200, [
|
|
520
|
+
{ bank_code: "1007", bank_name: "BANCOLOMBIA" },
|
|
521
|
+
{ bank_code: "1051", bank_name: "DAVIVIENDA" },
|
|
522
|
+
])
|
|
523
|
+
return
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// 4. Voucher lookup & Refunds (buttonApiKey)
|
|
527
|
+
if (method === "GET" && pathname.startsWith("/v2/payment-voucher/")) {
|
|
528
|
+
if (!this.checkApiKey(req, this.cfg.buttonApiKey, res)) return
|
|
529
|
+
const reference = decodeURIComponent(
|
|
530
|
+
pathname.slice("/v2/payment-voucher/".length)
|
|
531
|
+
)
|
|
532
|
+
const tx = this.transactionsByRef.get(reference)
|
|
533
|
+
if (!tx) {
|
|
534
|
+
this.sendJson(res, 404, {
|
|
535
|
+
message: `Voucher for ${reference} not found`,
|
|
536
|
+
})
|
|
537
|
+
return
|
|
538
|
+
}
|
|
539
|
+
this.sendJson(res, 200, {
|
|
540
|
+
payload: {
|
|
541
|
+
reference_id: tx.reference,
|
|
542
|
+
status: tx.status,
|
|
543
|
+
payment_status: tx.status,
|
|
544
|
+
transaction_id: tx.transactionId,
|
|
545
|
+
payment_method: tx.paymentMethod,
|
|
546
|
+
total: tx.amount,
|
|
547
|
+
},
|
|
548
|
+
})
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (method === "POST" && pathname === "/v1/refunds") {
|
|
553
|
+
if (!this.checkApiKey(req, this.cfg.buttonApiKey, res)) return
|
|
554
|
+
const txId = String(body?.transaction_id || "")
|
|
555
|
+
const amount = Number(body?.amount || 0)
|
|
556
|
+
const tx = this.transactionsByTxId.get(txId)
|
|
557
|
+
if (!tx) {
|
|
558
|
+
this.sendJson(res, 404, { message: `Transaction ${txId} not found` })
|
|
559
|
+
return
|
|
560
|
+
}
|
|
561
|
+
if (tx.amount > 0 && tx.refundedAmount + amount > tx.amount) {
|
|
562
|
+
this.sendJson(res, 422, {
|
|
563
|
+
message: "Refund amount exceeds remaining captured balance",
|
|
564
|
+
})
|
|
565
|
+
return
|
|
566
|
+
}
|
|
567
|
+
tx.refundedAmount += amount
|
|
568
|
+
this.sendJson(res, 200, {
|
|
569
|
+
refund_id: `rf_qa_${this.seq++}`,
|
|
570
|
+
transaction_id: txId,
|
|
571
|
+
amount,
|
|
572
|
+
status: "REFUNDED",
|
|
573
|
+
created_at: new Date().toISOString(),
|
|
574
|
+
})
|
|
575
|
+
return
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
this.sendJson(res, 404, {
|
|
579
|
+
message: `Unhandled route ${method} ${pathname}`,
|
|
580
|
+
})
|
|
581
|
+
}
|
|
582
|
+
}
|