@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,59 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { IPaymentModuleService } from "@medusajs/framework/types"
|
|
3
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
4
|
+
import { z } from "@medusajs/framework/zod"
|
|
5
|
+
import { EMedusaFormatBoldPaymentProvider } from "@payment-bold/types/enums"
|
|
6
|
+
|
|
7
|
+
const CreateBoldPosPushSchema = z.object({
|
|
8
|
+
paymentCollectionId: z.string().min(1),
|
|
9
|
+
amount: z.coerce.number().finite().positive(),
|
|
10
|
+
currency: z.string().min(3).max(3).default("COP"),
|
|
11
|
+
reference: z.string().min(1).max(255),
|
|
12
|
+
terminalModel: z.string().min(1).max(100),
|
|
13
|
+
terminalSerial: z.string().min(1).max(100),
|
|
14
|
+
userEmail: z.string().email(),
|
|
15
|
+
vatAmount: z.coerce.number().finite().min(0).optional(),
|
|
16
|
+
description: z.string().max(500).optional(),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export async function POST(
|
|
20
|
+
req: MedusaRequest,
|
|
21
|
+
res: MedusaResponse
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const parsed = CreateBoldPosPushSchema.safeParse(req.body ?? {})
|
|
24
|
+
if (!parsed.success) {
|
|
25
|
+
res.status(400).json({
|
|
26
|
+
message: "Invalid POS terminal checkout payload",
|
|
27
|
+
errors: parsed.error.flatten().fieldErrors,
|
|
28
|
+
})
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const paymentModule: IPaymentModuleService = req.scope.resolve(
|
|
33
|
+
Modules.PAYMENT
|
|
34
|
+
)
|
|
35
|
+
const input = parsed.data
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const paymentSession = await paymentModule.createPaymentSession(
|
|
39
|
+
input.paymentCollectionId,
|
|
40
|
+
{
|
|
41
|
+
provider_id: EMedusaFormatBoldPaymentProvider.TERMINAL,
|
|
42
|
+
currency_code: input.currency.toLowerCase(),
|
|
43
|
+
amount: input.amount,
|
|
44
|
+
data: {
|
|
45
|
+
reference: input.reference,
|
|
46
|
+
terminalModel: input.terminalModel,
|
|
47
|
+
terminalSerial: input.terminalSerial,
|
|
48
|
+
userEmail: input.userEmail,
|
|
49
|
+
vatAmount: input.vatAmount ?? 0,
|
|
50
|
+
description: input.description,
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
res.status(200).json({ paymentSession })
|
|
56
|
+
} catch {
|
|
57
|
+
res.status(500).json({ message: "Failed to trigger terminal checkout" })
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
2
|
+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
3
|
+
import { z } from "@medusajs/framework/zod"
|
|
4
|
+
import { refundBoldPaymentWorkflow } from "@payment-bold/workflows/create-bold-refund"
|
|
5
|
+
|
|
6
|
+
const RefundBoldPaymentSchema = z.object({
|
|
7
|
+
payment_id: z.string().min(1),
|
|
8
|
+
amount: z.coerce.number().finite().positive(),
|
|
9
|
+
note: z.string().max(500).optional(),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export const POST = async (
|
|
13
|
+
req: AuthenticatedMedusaRequest<{
|
|
14
|
+
payment_id: string
|
|
15
|
+
amount: number
|
|
16
|
+
note?: string
|
|
17
|
+
}>,
|
|
18
|
+
res: MedusaResponse
|
|
19
|
+
) => {
|
|
20
|
+
const logger = req.scope.resolve(ContainerRegistrationKeys.LOGGER)
|
|
21
|
+
const parsed = RefundBoldPaymentSchema.safeParse(req.body ?? {})
|
|
22
|
+
|
|
23
|
+
if (!parsed.success) {
|
|
24
|
+
return res.status(400).json({
|
|
25
|
+
message: "Invalid refund payload",
|
|
26
|
+
errors: parsed.error.flatten().fieldErrors,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { payment_id, amount, note } = parsed.data
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const { result } = await refundBoldPaymentWorkflow(req.scope).run({
|
|
34
|
+
input: {
|
|
35
|
+
paymentId: payment_id,
|
|
36
|
+
amount,
|
|
37
|
+
note,
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return res.json(result)
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
44
|
+
logger.error(
|
|
45
|
+
`[Bold Refund Route] Error executing refund workflow: ${message}`
|
|
46
|
+
)
|
|
47
|
+
return res.status(500).json({
|
|
48
|
+
message: "Failed to process refund",
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
2
|
+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
3
|
+
import { getTerminalsWorkflow } from "@payment-bold/workflows/get-terminals"
|
|
4
|
+
|
|
5
|
+
export const GET = async (
|
|
6
|
+
req: AuthenticatedMedusaRequest,
|
|
7
|
+
res: MedusaResponse
|
|
8
|
+
) => {
|
|
9
|
+
const logger = req.scope.resolve(ContainerRegistrationKeys.LOGGER)
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const { result: terminals } = await getTerminalsWorkflow(req.scope).run()
|
|
13
|
+
|
|
14
|
+
return res.json({ terminals: terminals || [] })
|
|
15
|
+
} catch (err: any) {
|
|
16
|
+
logger.error(
|
|
17
|
+
`[Bold Terminals Route] Failed executing getTerminalsWorkflow: ${err.message}`,
|
|
18
|
+
err
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
return res.status(500).json({
|
|
22
|
+
message:
|
|
23
|
+
err.message || "Failed to retrieve terminal fleet status from Bold API",
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { BOLD_PLUGIN_PROVIDERS } from "@payment-bold/types/constants"
|
|
3
|
+
|
|
4
|
+
export async function GET(
|
|
5
|
+
req: MedusaRequest,
|
|
6
|
+
res: MedusaResponse
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
res.status(200).json({
|
|
9
|
+
plugin: "medusa-payment-bold",
|
|
10
|
+
version: "2.0.0",
|
|
11
|
+
status: "active",
|
|
12
|
+
providers: BOLD_PLUGIN_PROVIDERS,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { Modules, ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
3
|
+
import { IPaymentModuleService, Logger } from "@medusajs/framework/types"
|
|
4
|
+
import { BoldIntegrityService } from "../../../../services/integrity"
|
|
5
|
+
import { BOLD_PAYMENT_PROVIDERS } from "@payment-bold/types/constants"
|
|
6
|
+
|
|
7
|
+
export async function POST(
|
|
8
|
+
req: MedusaRequest,
|
|
9
|
+
res: MedusaResponse
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
const logger: Logger = req.scope.resolve(ContainerRegistrationKeys.LOGGER)
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const signatureHeader = (req.headers["x-bold-signature"] ||
|
|
15
|
+
req.headers["x-signature"]) as string
|
|
16
|
+
|
|
17
|
+
const timestampHeader = (req.headers["x-bold-timestamp"] ||
|
|
18
|
+
req.headers["x-timestamp"]) as string
|
|
19
|
+
|
|
20
|
+
// 1. Resolve Bold configuration from any available provider (provider-agnostic)
|
|
21
|
+
let options: any = {}
|
|
22
|
+
for (const providerId of BOLD_PAYMENT_PROVIDERS) {
|
|
23
|
+
try {
|
|
24
|
+
const provider = req.scope.resolve(providerId) as any
|
|
25
|
+
if (provider?.options_) {
|
|
26
|
+
options = provider.options_
|
|
27
|
+
logger.debug(
|
|
28
|
+
`[Bold Webhook] Configuration loaded from provider: ${providerId}`
|
|
29
|
+
)
|
|
30
|
+
break
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
// Provider not registered, try next one
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 2. Collect all configured Bold webhook signing secrets (fail-closed if none)
|
|
39
|
+
const possibleSecrets = [
|
|
40
|
+
options?.buttonSecretKey,
|
|
41
|
+
options?.integrationSecretKey,
|
|
42
|
+
].filter(
|
|
43
|
+
(secret): secret is string =>
|
|
44
|
+
typeof secret === "string" && secret.length > 0
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if (possibleSecrets.length === 0) {
|
|
48
|
+
logger.error(
|
|
49
|
+
"[Bold Webhook] No Bold payment provider signing key configured"
|
|
50
|
+
)
|
|
51
|
+
res.status(500).json({ message: "Webhook signing key is not configured" })
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const rawBody = (req as any).rawBody || JSON.stringify(req.body ?? {})
|
|
56
|
+
|
|
57
|
+
// 3. Verify webhook signature against configured secret keys (fail-closed)
|
|
58
|
+
if (!signatureHeader || !timestampHeader) {
|
|
59
|
+
logger.warn("[Bold Webhook] Missing signature or timestamp header.")
|
|
60
|
+
res.status(401).json({ message: "Missing webhook signature headers" })
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const isValid = possibleSecrets.some((secretKey) =>
|
|
65
|
+
BoldIntegrityService.verifyWebhookSignature(
|
|
66
|
+
rawBody,
|
|
67
|
+
signatureHeader,
|
|
68
|
+
timestampHeader,
|
|
69
|
+
secretKey
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
if (!isValid) {
|
|
74
|
+
logger.warn(
|
|
75
|
+
"[Bold Webhook] Signature verification failed against configured secret keys."
|
|
76
|
+
)
|
|
77
|
+
res.status(401).json({ message: "Invalid webhook signature" })
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const payload = req.body as Record<string, any>
|
|
82
|
+
const eventType = payload?.event
|
|
83
|
+
const data = payload?.data
|
|
84
|
+
|
|
85
|
+
logger.info(
|
|
86
|
+
`[Bold Webhook] Received event '${eventType}' for reference '${data?.reference}'`
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
// 4. Capture payment in Medusa DB on successful sale
|
|
90
|
+
if (
|
|
91
|
+
eventType === "sale.successful" &&
|
|
92
|
+
(data?.status === "APPROVED" || data?.status === "PAID")
|
|
93
|
+
) {
|
|
94
|
+
const reference = data?.reference
|
|
95
|
+
const paymentModule: IPaymentModuleService = req.scope.resolve(
|
|
96
|
+
Modules.PAYMENT
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
if (reference) {
|
|
100
|
+
const paymentSessions = await paymentModule.listPaymentSessions({
|
|
101
|
+
status: "pending",
|
|
102
|
+
} as any)
|
|
103
|
+
|
|
104
|
+
const matchingSession = paymentSessions.find((session) => {
|
|
105
|
+
const sData = (session.data || {}) as Record<string, any>
|
|
106
|
+
const isMatch =
|
|
107
|
+
sData.reference === reference ||
|
|
108
|
+
sData.payment_link_id === reference ||
|
|
109
|
+
sData.payment_link === reference ||
|
|
110
|
+
session.id === reference
|
|
111
|
+
|
|
112
|
+
if (!isMatch || session.status !== "pending") {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Verify amount/currency when provided by webhook payload
|
|
117
|
+
const webhookAmount = Number(data?.amount?.total ?? data?.amount)
|
|
118
|
+
if (Number.isFinite(webhookAmount) && session.amount !== undefined) {
|
|
119
|
+
if (
|
|
120
|
+
Math.round(Number(session.amount)) !== Math.round(webhookAmount)
|
|
121
|
+
) {
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (typeof data?.currency === "string" && session.currency_code) {
|
|
126
|
+
if (
|
|
127
|
+
session.currency_code.toUpperCase() !==
|
|
128
|
+
data.currency.toUpperCase()
|
|
129
|
+
) {
|
|
130
|
+
return false
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return true
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
if (matchingSession) {
|
|
138
|
+
await paymentModule.authorizePaymentSession(matchingSession.id, {})
|
|
139
|
+
logger.info(
|
|
140
|
+
`[Bold Webhook] Payment session authorized successfully: ${matchingSession.id}`
|
|
141
|
+
)
|
|
142
|
+
} else {
|
|
143
|
+
logger.warn(
|
|
144
|
+
`[Bold Webhook] No matching pending session found for reference: ${reference}`
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
res.status(200).json({ received: true })
|
|
151
|
+
} catch (error: unknown) {
|
|
152
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
153
|
+
logger.error(`[Bold Webhook Error] ${message}`)
|
|
154
|
+
res.status(500).json({ message: "Webhook processing failed" })
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./use-bold-checkout-handler"
|
|
2
|
+
export * from "./use-bold-payment-methods"
|
|
3
|
+
export * from "./use-bold-payment-status-stream"
|
|
4
|
+
export * from "./use-bold-refund"
|
|
5
|
+
export * from "./use-bold-terminals"
|
|
6
|
+
export * from "./use-create-bold-button"
|
|
7
|
+
export * from "./use-create-bold-link"
|
|
8
|
+
export * from "./use-create-bold-qr"
|
|
9
|
+
export * from "./use-create-bold-terminal"
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { extractSessionId } from "../lib/session-helpers"
|
|
2
|
+
import {
|
|
3
|
+
BoldCheckoutData,
|
|
4
|
+
BoldCheckoutResult,
|
|
5
|
+
BoldMutationHookOptions,
|
|
6
|
+
} from "../types"
|
|
7
|
+
import { useCreateBoldButton } from "./use-create-bold-button"
|
|
8
|
+
import { useCreateBoldLink } from "./use-create-bold-link"
|
|
9
|
+
import { useCreateBoldQr } from "./use-create-bold-qr"
|
|
10
|
+
import { usePushToBoldTerminal } from "./use-create-bold-terminal"
|
|
11
|
+
|
|
12
|
+
export const useBoldCheckoutHandler = (options?: BoldMutationHookOptions) => {
|
|
13
|
+
const createLink = useCreateBoldLink(options)
|
|
14
|
+
const createButton = useCreateBoldButton(options)
|
|
15
|
+
const createQr = useCreateBoldQr(options)
|
|
16
|
+
const pushTerminal = usePushToBoldTerminal(options)
|
|
17
|
+
|
|
18
|
+
const handleCheckout = async (
|
|
19
|
+
data: BoldCheckoutData
|
|
20
|
+
): Promise<BoldCheckoutResult> => {
|
|
21
|
+
const { details, client, currencyCode, paymentMode } = data
|
|
22
|
+
const email =
|
|
23
|
+
client.email || `${(client.phone || "").replace(/\D/g, "")}@noemail.local`
|
|
24
|
+
|
|
25
|
+
// Mode: Link
|
|
26
|
+
if (details.boldMode === "link") {
|
|
27
|
+
const timestamp = Date.now()
|
|
28
|
+
const rawName = `${client.first_name || ""}_${client.last_name || ""}`
|
|
29
|
+
const cleanSlug =
|
|
30
|
+
rawName
|
|
31
|
+
.toUpperCase()
|
|
32
|
+
.replace(/[^A-Z0-9_]/g, "")
|
|
33
|
+
.slice(0, 20) || "GUEST"
|
|
34
|
+
|
|
35
|
+
const reference = `TRV_${cleanSlug}_${timestamp}`.slice(0, 60)
|
|
36
|
+
const isInstallment = paymentMode === "installments"
|
|
37
|
+
const fullName =
|
|
38
|
+
`${client.first_name || ""} ${client.last_name || ""}`.trim()
|
|
39
|
+
const rawDescription = isInstallment
|
|
40
|
+
? `Travel deposit payment - ${fullName || email}`
|
|
41
|
+
: `Full travel package payment - ${fullName || email}`
|
|
42
|
+
|
|
43
|
+
const response = await createLink.mutateAsync({
|
|
44
|
+
amount: details.amount,
|
|
45
|
+
currency: currencyCode,
|
|
46
|
+
reference,
|
|
47
|
+
description: rawDescription.trim().slice(0, 100),
|
|
48
|
+
email,
|
|
49
|
+
vatAmount: details.vatAmount || 0,
|
|
50
|
+
consumptionTaxAmount: details.consumptionTaxAmount || 0,
|
|
51
|
+
callbackUrl: details.callbackUrl,
|
|
52
|
+
...(details.expirationMinutes
|
|
53
|
+
? { expirationMinutes: details.expirationMinutes }
|
|
54
|
+
: {}),
|
|
55
|
+
...(details.imageUrl ? { imageUrl: details.imageUrl } : {}),
|
|
56
|
+
...(details.paymentCollectionId
|
|
57
|
+
? { paymentCollectionId: details.paymentCollectionId }
|
|
58
|
+
: {}),
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const session = response.paymentSession
|
|
62
|
+
return {
|
|
63
|
+
type: "link",
|
|
64
|
+
url: session?.data?.url || session?.data?.payment_url,
|
|
65
|
+
sessionId: extractSessionId(session),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Mode: Button
|
|
70
|
+
if (details.boldMode === "button") {
|
|
71
|
+
const timestamp = Date.now()
|
|
72
|
+
const rawName = `${client.first_name || ""}_${client.last_name || ""}`
|
|
73
|
+
const cleanSlug =
|
|
74
|
+
rawName
|
|
75
|
+
.toUpperCase()
|
|
76
|
+
.replace(/[^A-Z0-9_]/g, "")
|
|
77
|
+
.slice(0, 20) || "GUEST"
|
|
78
|
+
|
|
79
|
+
const reference = `BTN_${cleanSlug}_${timestamp}`.slice(0, 60)
|
|
80
|
+
|
|
81
|
+
const response = await createButton.mutateAsync({
|
|
82
|
+
amount: details.amount,
|
|
83
|
+
currency: currencyCode,
|
|
84
|
+
reference,
|
|
85
|
+
description: `Payment for ${client.first_name || email}`.slice(0, 100),
|
|
86
|
+
email,
|
|
87
|
+
taxAmount: details.vatAmount || 0,
|
|
88
|
+
...(details.paymentCollectionId
|
|
89
|
+
? { paymentCollectionId: details.paymentCollectionId }
|
|
90
|
+
: {}),
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const session = response.paymentSession
|
|
94
|
+
return {
|
|
95
|
+
type: "button",
|
|
96
|
+
hash: session?.data?.hash,
|
|
97
|
+
sessionId: extractSessionId(session),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Mode: QR
|
|
102
|
+
if (details.boldMode === "qr") {
|
|
103
|
+
const res = await createQr.mutateAsync({
|
|
104
|
+
amount: details.amount,
|
|
105
|
+
currency_code: currencyCode,
|
|
106
|
+
...(details.paymentCollectionId
|
|
107
|
+
? { paymentCollectionId: details.paymentCollectionId }
|
|
108
|
+
: {}),
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const session = res.paymentSession
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
type: "qr",
|
|
115
|
+
qrPayload: res.qr_payload,
|
|
116
|
+
sessionId: extractSessionId(session) || "",
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Mode: Terminal
|
|
121
|
+
if (details.boldMode === "terminal") {
|
|
122
|
+
if (!details.terminalModel || !details.terminalSerial) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
"Missing terminalModel or terminalSerial for POS payment mode"
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const timestamp = Date.now()
|
|
129
|
+
const rawName = `${client.first_name || ""}_${client.last_name || ""}`
|
|
130
|
+
const cleanSlug =
|
|
131
|
+
rawName
|
|
132
|
+
.toUpperCase()
|
|
133
|
+
.replace(/[^A-Z0-9_]/g, "")
|
|
134
|
+
.slice(0, 20) || "GUEST"
|
|
135
|
+
|
|
136
|
+
const reference = `POS_${cleanSlug}_${timestamp}`.slice(0, 60)
|
|
137
|
+
|
|
138
|
+
const res = await pushTerminal.mutateAsync({
|
|
139
|
+
amount: details.amount,
|
|
140
|
+
currency: currencyCode,
|
|
141
|
+
reference,
|
|
142
|
+
userEmail: email,
|
|
143
|
+
terminalModel: details.terminalModel,
|
|
144
|
+
terminalSerial: details.terminalSerial,
|
|
145
|
+
...(details.paymentCollectionId
|
|
146
|
+
? { paymentCollectionId: details.paymentCollectionId }
|
|
147
|
+
: {}),
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const session = res.paymentSession
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
type: "terminal",
|
|
154
|
+
sessionId: extractSessionId(session) || "",
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Explicit fallback to satisfy TypeScript's return path check
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Unsupported bold payment mode: ${(details as any)?.boldMode}`
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
handleCheckout,
|
|
166
|
+
isPending:
|
|
167
|
+
createLink.isPending ||
|
|
168
|
+
createButton.isPending ||
|
|
169
|
+
createQr.isPending ||
|
|
170
|
+
pushTerminal.isPending,
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useQuery, UseQueryOptions } from "@tanstack/react-query"
|
|
2
|
+
|
|
3
|
+
import { boldAdminSdk } from "../sdk/client"
|
|
4
|
+
import { boldQueryKeys } from "../sdk/query-keys"
|
|
5
|
+
import {
|
|
6
|
+
BoldIntegrationAPIPaymentMethod,
|
|
7
|
+
BoldPaymentLinkPaymentMethodLimits,
|
|
8
|
+
} from "@payment-bold/types"
|
|
9
|
+
|
|
10
|
+
export const useBoldIntegrationApiPaymentMethods = (
|
|
11
|
+
options?: Omit<
|
|
12
|
+
UseQueryOptions<Array<BoldIntegrationAPIPaymentMethod>, Error>,
|
|
13
|
+
"queryKey" | "queryFn"
|
|
14
|
+
>
|
|
15
|
+
) => {
|
|
16
|
+
return useQuery({
|
|
17
|
+
queryKey: [...boldQueryKeys.paymentMethods(), "integration-api"],
|
|
18
|
+
queryFn: () => boldAdminSdk.getPaymentMethods(),
|
|
19
|
+
staleTime: 1000 * 60 * 15,
|
|
20
|
+
...options,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const useBoldPaymentLinkPaymentMethods = (
|
|
25
|
+
options?: Omit<
|
|
26
|
+
UseQueryOptions<BoldPaymentLinkPaymentMethodLimits, Error>,
|
|
27
|
+
"queryKey" | "queryFn"
|
|
28
|
+
>
|
|
29
|
+
) => {
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: [...boldQueryKeys.paymentMethods(), "payment-link"],
|
|
32
|
+
queryFn: () => boldAdminSdk.getPaymentLinkPaymentMethods(),
|
|
33
|
+
staleTime: 1000 * 60 * 15,
|
|
34
|
+
...options,
|
|
35
|
+
})
|
|
36
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect } from "react"
|
|
2
|
+
import { QueryKey } from "@tanstack/react-query"
|
|
3
|
+
|
|
4
|
+
import { boldAdminSdk, BoldApiClient } from "../sdk/client"
|
|
5
|
+
import { BoldMutationHookOptions, BoldStatusStreamCallbacks } from "../types"
|
|
6
|
+
|
|
7
|
+
export interface UseBoldStatusStreamOptions {
|
|
8
|
+
sessionId?: string | null
|
|
9
|
+
enabled?: boolean
|
|
10
|
+
/**
|
|
11
|
+
* The API client instance to use (e.g. boldAdminSdk or boldStoreSdk)
|
|
12
|
+
* @default boldAdminSdk
|
|
13
|
+
*/
|
|
14
|
+
client?: BoldApiClient
|
|
15
|
+
/**
|
|
16
|
+
* Custom TanStack Query key to invalidate upon successful payment capture
|
|
17
|
+
* @default ["orders"]
|
|
18
|
+
*/
|
|
19
|
+
queryKey?: QueryKey
|
|
20
|
+
onSuccess?: (data: any) => void
|
|
21
|
+
onError?: (error: any) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface UseBoldPaymentStatusStreamProps
|
|
25
|
+
extends BoldStatusStreamCallbacks, BoldMutationHookOptions {
|
|
26
|
+
sessionId: string | null
|
|
27
|
+
enabled?: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const useBoldPaymentStatusStream = ({
|
|
31
|
+
sessionId,
|
|
32
|
+
enabled = true,
|
|
33
|
+
client,
|
|
34
|
+
onStatusChange,
|
|
35
|
+
onSuccess,
|
|
36
|
+
onError,
|
|
37
|
+
}: UseBoldPaymentStatusStreamProps) => {
|
|
38
|
+
const sdk = client || boldAdminSdk
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!sessionId || !enabled) {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const unsubscribe = sdk.subscribeToPaymentStatus(sessionId, {
|
|
46
|
+
onStatusChange,
|
|
47
|
+
onSuccess,
|
|
48
|
+
onError,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return () => {
|
|
52
|
+
unsubscribe()
|
|
53
|
+
}
|
|
54
|
+
}, [sessionId, enabled, sdk])
|
|
55
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMutation,
|
|
3
|
+
useQueryClient,
|
|
4
|
+
UseMutationOptions,
|
|
5
|
+
} from "@tanstack/react-query"
|
|
6
|
+
|
|
7
|
+
import { boldAdminSdk } from "../sdk/client"
|
|
8
|
+
import { boldQueryKeys } from "../sdk/query-keys"
|
|
9
|
+
import {
|
|
10
|
+
BoldRefundPayload,
|
|
11
|
+
BoldRefundResponse,
|
|
12
|
+
BoldMutationHookOptions,
|
|
13
|
+
} from "../types"
|
|
14
|
+
|
|
15
|
+
export const useBoldRefund = (
|
|
16
|
+
options?: UseMutationOptions<BoldRefundResponse, Error, BoldRefundPayload> &
|
|
17
|
+
BoldMutationHookOptions
|
|
18
|
+
) => {
|
|
19
|
+
const queryClient = useQueryClient()
|
|
20
|
+
const sdk = options?.client || boldAdminSdk
|
|
21
|
+
const targetKey = options?.queryKey || boldQueryKeys.refund()
|
|
22
|
+
|
|
23
|
+
return useMutation({
|
|
24
|
+
mutationFn: (payload: BoldRefundPayload) => sdk.refundPayment(payload),
|
|
25
|
+
onSuccess: (data, variables, onMutateResult, context) => {
|
|
26
|
+
if (targetKey && targetKey.length > 0) {
|
|
27
|
+
queryClient.invalidateQueries({ queryKey: targetKey as any })
|
|
28
|
+
}
|
|
29
|
+
options?.onSuccess?.(data, variables, onMutateResult, context)
|
|
30
|
+
},
|
|
31
|
+
...options,
|
|
32
|
+
})
|
|
33
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useQuery, UseQueryOptions } from "@tanstack/react-query"
|
|
2
|
+
|
|
3
|
+
import { boldAdminSdk } from "../sdk/client"
|
|
4
|
+
import { boldQueryKeys } from "../sdk/query-keys"
|
|
5
|
+
import { BoldMutationHookOptions, BoldTerminalItem } from "../types"
|
|
6
|
+
|
|
7
|
+
export const useBoldTerminals = (
|
|
8
|
+
options?: Omit<
|
|
9
|
+
UseQueryOptions<BoldTerminalItem[], Error, BoldTerminalItem[]>,
|
|
10
|
+
"queryKey" | "queryFn"
|
|
11
|
+
> &
|
|
12
|
+
BoldMutationHookOptions
|
|
13
|
+
) => {
|
|
14
|
+
const sdk = options?.client || boldAdminSdk
|
|
15
|
+
const queryKey = options?.queryKey || boldQueryKeys.terminals()
|
|
16
|
+
|
|
17
|
+
return useQuery({
|
|
18
|
+
queryKey,
|
|
19
|
+
queryFn: () => sdk.getTerminals(),
|
|
20
|
+
...options,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMutation,
|
|
3
|
+
useQueryClient,
|
|
4
|
+
UseMutationOptions,
|
|
5
|
+
} from "@tanstack/react-query"
|
|
6
|
+
|
|
7
|
+
import { boldAdminSdk } from "../sdk/client"
|
|
8
|
+
import { boldQueryKeys } from "../sdk/query-keys"
|
|
9
|
+
import {
|
|
10
|
+
BoldButtonPayload,
|
|
11
|
+
BoldButtonResponse,
|
|
12
|
+
BoldMutationHookOptions,
|
|
13
|
+
} from "../types"
|
|
14
|
+
|
|
15
|
+
export const useCreateBoldButton = (
|
|
16
|
+
options?: UseMutationOptions<BoldButtonResponse, Error, BoldButtonPayload> &
|
|
17
|
+
BoldMutationHookOptions
|
|
18
|
+
) => {
|
|
19
|
+
const queryClient = useQueryClient()
|
|
20
|
+
const sdk = options?.client || boldAdminSdk
|
|
21
|
+
const targetKey = options?.queryKey || boldQueryKeys.button()
|
|
22
|
+
|
|
23
|
+
return useMutation({
|
|
24
|
+
mutationFn: (payload: BoldButtonPayload) =>
|
|
25
|
+
sdk.createPaymentButton(payload),
|
|
26
|
+
onSuccess: (data, variables, onMutateResult, context) => {
|
|
27
|
+
if (targetKey && targetKey.length > 0) {
|
|
28
|
+
queryClient.invalidateQueries({ queryKey: targetKey as any })
|
|
29
|
+
}
|
|
30
|
+
options?.onSuccess?.(data, variables, onMutateResult, context)
|
|
31
|
+
},
|
|
32
|
+
...options,
|
|
33
|
+
})
|
|
34
|
+
}
|