@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,151 @@
|
|
|
1
|
+
import { defineWidgetConfig } from "@medusajs/admin-sdk"
|
|
2
|
+
import {
|
|
3
|
+
Container,
|
|
4
|
+
Heading,
|
|
5
|
+
Button,
|
|
6
|
+
CurrencyInput,
|
|
7
|
+
Badge,
|
|
8
|
+
toast,
|
|
9
|
+
} from "@medusajs/ui"
|
|
10
|
+
import { useState } from "react"
|
|
11
|
+
import { useTranslation } from "react-i18next"
|
|
12
|
+
|
|
13
|
+
import { useBoldRefund } from "../../hooks/use-bold-refund"
|
|
14
|
+
|
|
15
|
+
const BoldRefundActionsWidget = ({ data: order }: { data: any }) => {
|
|
16
|
+
const { t } = useTranslation()
|
|
17
|
+
const [refundAmount, setRefundAmount] = useState<number | undefined>(
|
|
18
|
+
undefined
|
|
19
|
+
)
|
|
20
|
+
const { mutateAsync: refundPayment, isPending: loading } = useBoldRefund()
|
|
21
|
+
|
|
22
|
+
const boldPayment = order?.payment_collections
|
|
23
|
+
?.flatMap((pc: any) => pc.payments || [])
|
|
24
|
+
?.find((p: any) => p.provider_id?.startsWith("pp_bold") && p.captured_at)
|
|
25
|
+
|
|
26
|
+
if (!boldPayment) {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const currencyCode = (
|
|
31
|
+
order?.currency_code ||
|
|
32
|
+
boldPayment?.currency_code ||
|
|
33
|
+
"COP"
|
|
34
|
+
).toUpperCase()
|
|
35
|
+
|
|
36
|
+
const capturedAmount = boldPayment.amount || 0
|
|
37
|
+
const refundedAmount = boldPayment.refunded_amount || 0
|
|
38
|
+
const maxRefundable = capturedAmount - refundedAmount
|
|
39
|
+
|
|
40
|
+
if (maxRefundable <= 0) {
|
|
41
|
+
return (
|
|
42
|
+
<Container className="p-4 rounded-lg border bg-card space-y-2">
|
|
43
|
+
<div className="flex items-center justify-between">
|
|
44
|
+
<Heading level="h2" className="text-base font-semibold">
|
|
45
|
+
{t("bold.admin.refund.title")}
|
|
46
|
+
</Heading>
|
|
47
|
+
<Badge color="grey">{t("bold.admin.refund.fully_refunded")}</Badge>
|
|
48
|
+
</div>
|
|
49
|
+
<p className="text-xs text-ui-fg-subtle">
|
|
50
|
+
{t("bold.admin.refund.fully_refunded_desc", {
|
|
51
|
+
amount: capturedAmount.toLocaleString(),
|
|
52
|
+
currency: currencyCode,
|
|
53
|
+
})}
|
|
54
|
+
</p>
|
|
55
|
+
</Container>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const handleRefund = async () => {
|
|
60
|
+
const numericAmount = Number(refundAmount)
|
|
61
|
+
|
|
62
|
+
if (!numericAmount || numericAmount <= 0 || numericAmount > maxRefundable) {
|
|
63
|
+
toast.error(t("bold.admin.refund.invalid_amount_title"), {
|
|
64
|
+
description: t("bold.admin.refund.invalid_amount_desc", {
|
|
65
|
+
max: maxRefundable.toLocaleString(),
|
|
66
|
+
currency: currencyCode,
|
|
67
|
+
}),
|
|
68
|
+
})
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
await refundPayment({
|
|
74
|
+
payment_id: boldPayment.id,
|
|
75
|
+
amount: numericAmount,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
toast.success(t("bold.admin.refund.success_title"), {
|
|
79
|
+
description: t("bold.admin.refund.success_desc", {
|
|
80
|
+
amount: numericAmount.toLocaleString(),
|
|
81
|
+
currency: currencyCode,
|
|
82
|
+
}),
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
setRefundAmount(undefined)
|
|
86
|
+
window.location.reload()
|
|
87
|
+
} catch (err: any) {
|
|
88
|
+
toast.error(t("bold.admin.refund.error_title"), {
|
|
89
|
+
description: err.message,
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<Container className="p-4 rounded-lg border bg-card space-y-4">
|
|
96
|
+
<div className="flex items-center justify-between border-b pb-3">
|
|
97
|
+
<Heading level="h2" className="text-base font-semibold">
|
|
98
|
+
{t("bold.admin.refund.title")}
|
|
99
|
+
</Heading>
|
|
100
|
+
<Badge color="green">{t("bold.admin.refund.captured")}</Badge>
|
|
101
|
+
</div>
|
|
102
|
+
|
|
103
|
+
<div className="flex flex-col gap-y-1.5 text-xs text-ui-fg-subtle">
|
|
104
|
+
<div className="flex items-center justify-between">
|
|
105
|
+
<span>{t("bold.admin.refund.captured_total")}:</span>
|
|
106
|
+
<strong className="text-ui-fg-base font-mono">
|
|
107
|
+
${capturedAmount.toLocaleString()} {currencyCode}
|
|
108
|
+
</strong>
|
|
109
|
+
</div>
|
|
110
|
+
<div className="flex items-center justify-between">
|
|
111
|
+
<span>{t("bold.admin.refund.remaining")}:</span>
|
|
112
|
+
<strong className="text-ui-fg-base font-mono">
|
|
113
|
+
${maxRefundable.toLocaleString()} {currencyCode}
|
|
114
|
+
</strong>
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
|
|
118
|
+
<div className="space-y-3 pt-1">
|
|
119
|
+
<CurrencyInput
|
|
120
|
+
size="small"
|
|
121
|
+
symbol="$"
|
|
122
|
+
code={currencyCode}
|
|
123
|
+
value={refundAmount}
|
|
124
|
+
onValueChange={(val) =>
|
|
125
|
+
setRefundAmount(val ? Number(val) : undefined)
|
|
126
|
+
}
|
|
127
|
+
placeholder="0"
|
|
128
|
+
disabled={loading}
|
|
129
|
+
/>
|
|
130
|
+
|
|
131
|
+
<div className="flex justify-end">
|
|
132
|
+
<Button
|
|
133
|
+
size="small"
|
|
134
|
+
variant="secondary"
|
|
135
|
+
isLoading={loading}
|
|
136
|
+
onClick={handleRefund}
|
|
137
|
+
disabled={loading || !refundAmount}
|
|
138
|
+
>
|
|
139
|
+
{t("bold.admin.refund.action")}
|
|
140
|
+
</Button>
|
|
141
|
+
</div>
|
|
142
|
+
</div>
|
|
143
|
+
</Container>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const config = defineWidgetConfig({
|
|
148
|
+
zone: "order.details.side",
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
export default BoldRefundActionsWidget
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export {
|
|
2
|
+
default as BoldPaymentActionsWidget,
|
|
3
|
+
config as boldPaymentActionsConfig,
|
|
4
|
+
} from "./bold-payment-actions/bold-payment-actions"
|
|
5
|
+
export {
|
|
6
|
+
default as BoldAuditTimelineWidget,
|
|
7
|
+
config as boldAuditTimelineConfig,
|
|
8
|
+
} from "./bold-audit-timeline"
|
|
9
|
+
export {
|
|
10
|
+
default as BoldRefundActionsWidget,
|
|
11
|
+
config as boldRefundActionsConfig,
|
|
12
|
+
} from "./bold-refund-actions"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# API Routes (`src/api`)
|
|
2
|
+
|
|
3
|
+
Admin REST endpoints and Zod validation middlewares for `@vicacha-devs/medusa-plugin-payment-bold`.
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
api/
|
|
7
|
+
├── middlewares.ts # Registers validateAndTransformBody middlewares for /admin/bold/*
|
|
8
|
+
├── validators.ts # AdminBoldPosPushSchema & AdminBoldPaymentLinkSchema
|
|
9
|
+
└── admin/
|
|
10
|
+
└── bold/
|
|
11
|
+
├── terminals/
|
|
12
|
+
│ └── route.ts # GET /admin/bold/terminals
|
|
13
|
+
├── pos-push/
|
|
14
|
+
│ └── route.ts # POST /admin/bold/pos-push
|
|
15
|
+
└── payment-link/
|
|
16
|
+
└── route.ts # POST /admin/bold/payment-link
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Endpoints
|
|
22
|
+
|
|
23
|
+
| Method | Path | Body Schema | Description |
|
|
24
|
+
| :----- | :------------------------- | :--------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
25
|
+
| `GET` | `/admin/bold/terminals` | — | Invokes `listActiveTerminals()` on the registered Bold payment provider to list available physical POS Dataphones. |
|
|
26
|
+
| `POST` | `/admin/bold/pos-push` | `AdminBoldPosPushSchema` | Creates a `pp_bold-terminal_bold` payment session on `payment_collection_id` for `amount` (remaining `payableAmount`) and pushes the charge to `terminal_serial`. |
|
|
27
|
+
| `POST` | `/admin/bold/payment-link` | `AdminBoldPaymentLinkSchema` | Creates a `pp_bold-link_bold` payment session on `payment_collection_id` for `amount` (remaining `payableAmount`) and returns `payment_link_url` and `payment_link_id`. |
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Native Medusa Webhooks
|
|
32
|
+
|
|
33
|
+
Webhook notifications from Bold are handled natively by Medusa v2's Payment Module router via `getWebhookActionAndData` in `BoldBaseProvider`:
|
|
34
|
+
|
|
35
|
+
- `POST /hooks/payment/bold-online_bold`
|
|
36
|
+
- `POST /hooks/payment/bold-terminal_bold`
|
|
37
|
+
- `POST /hooks/payment/bold-link_bold`
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { BoldOnlinePaymentProviderService } from "@payment-bold/providers/bold-online"
|
|
3
|
+
|
|
4
|
+
export async function GET(
|
|
5
|
+
req: MedusaRequest,
|
|
6
|
+
res: MedusaResponse
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
try {
|
|
9
|
+
// Resolve the bold-online provider instance from the container
|
|
10
|
+
const boldOnlineService =
|
|
11
|
+
req.scope.resolve<BoldOnlinePaymentProviderService>("bold-online")
|
|
12
|
+
|
|
13
|
+
// Access the PSE banks list via the encapsulated client instance
|
|
14
|
+
const banks = await (boldOnlineService as any).client_.getPseBanks()
|
|
15
|
+
|
|
16
|
+
res.status(200).json({ banks })
|
|
17
|
+
} catch (error: any) {
|
|
18
|
+
res.status(500).json({
|
|
19
|
+
message:
|
|
20
|
+
error.message || "Failed to fetch active PSE banks from Bold API",
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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 CreateBoldButtonSchema = 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
|
+
description: z.string().min(1).max(500).optional(),
|
|
13
|
+
email: z.string().email().optional(),
|
|
14
|
+
taxAmount: z.coerce.number().finite().min(0).optional(),
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export async function POST(
|
|
18
|
+
req: MedusaRequest,
|
|
19
|
+
res: MedusaResponse
|
|
20
|
+
): Promise<void> {
|
|
21
|
+
const parsed = CreateBoldButtonSchema.safeParse(req.body ?? {})
|
|
22
|
+
if (!parsed.success) {
|
|
23
|
+
res.status(400).json({
|
|
24
|
+
message: "Invalid payment button payload",
|
|
25
|
+
errors: parsed.error.flatten().fieldErrors,
|
|
26
|
+
})
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const paymentModule: IPaymentModuleService = req.scope.resolve(
|
|
31
|
+
Modules.PAYMENT
|
|
32
|
+
)
|
|
33
|
+
const input = parsed.data
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const paymentSession = await paymentModule.createPaymentSession(
|
|
37
|
+
input.paymentCollectionId,
|
|
38
|
+
{
|
|
39
|
+
provider_id: EMedusaFormatBoldPaymentProvider.BUTTON,
|
|
40
|
+
currency_code: input.currency.toLowerCase(),
|
|
41
|
+
amount: input.amount,
|
|
42
|
+
data: {
|
|
43
|
+
reference: input.reference,
|
|
44
|
+
description: input.description,
|
|
45
|
+
email: input.email,
|
|
46
|
+
taxAmount: input.taxAmount,
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
res.status(200).json({ paymentSession })
|
|
52
|
+
} catch {
|
|
53
|
+
res
|
|
54
|
+
.status(500)
|
|
55
|
+
.json({ message: "Failed to initialize Bold payment button session" })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { z } from "@medusajs/framework/zod"
|
|
3
|
+
import { createBoldPaymentLinkWorkflow } from "@payment-bold/workflows/create-bold-payment-link"
|
|
4
|
+
|
|
5
|
+
const CreateBoldPaymentLinkSchema = z.object({
|
|
6
|
+
paymentCollectionId: z.string().min(1).optional(),
|
|
7
|
+
amount: z.coerce.number().finite().positive(),
|
|
8
|
+
currency: z
|
|
9
|
+
.string()
|
|
10
|
+
.min(3)
|
|
11
|
+
.max(3)
|
|
12
|
+
.transform((s) => s.toUpperCase()),
|
|
13
|
+
reference: z.string().min(1).max(255),
|
|
14
|
+
description: z.string().min(1).max(500),
|
|
15
|
+
email: z.string().email(),
|
|
16
|
+
callbackUrl: z
|
|
17
|
+
.string()
|
|
18
|
+
.url()
|
|
19
|
+
.refine((u) => u.startsWith("https://") || u.startsWith("http://"), {
|
|
20
|
+
message: "callbackUrl must use http:// or https://",
|
|
21
|
+
}),
|
|
22
|
+
vatAmount: z.coerce.number().finite().min(0).optional().default(0),
|
|
23
|
+
consumptionTaxAmount: z.coerce.number().finite().min(0).optional(),
|
|
24
|
+
imageUrl: z
|
|
25
|
+
.string()
|
|
26
|
+
.url()
|
|
27
|
+
.refine((u) => u.startsWith("https://") || u.startsWith("http://"), {
|
|
28
|
+
message: "imageUrl must use http:// or https://",
|
|
29
|
+
})
|
|
30
|
+
.optional(),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export async function POST(
|
|
34
|
+
req: MedusaRequest,
|
|
35
|
+
res: MedusaResponse
|
|
36
|
+
): Promise<void> {
|
|
37
|
+
const parsed = CreateBoldPaymentLinkSchema.safeParse(req.body ?? {})
|
|
38
|
+
if (!parsed.success) {
|
|
39
|
+
res.status(400).json({
|
|
40
|
+
message: "Invalid payment link payload",
|
|
41
|
+
errors: parsed.error.flatten().fieldErrors,
|
|
42
|
+
})
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const input = parsed.data
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const { result } = await createBoldPaymentLinkWorkflow(req.scope).run({
|
|
50
|
+
input: {
|
|
51
|
+
paymentCollectionId: input.paymentCollectionId,
|
|
52
|
+
amount: input.amount,
|
|
53
|
+
currency: input.currency,
|
|
54
|
+
reference: input.reference,
|
|
55
|
+
description: input.description,
|
|
56
|
+
email: input.email,
|
|
57
|
+
callbackUrl: input.callbackUrl,
|
|
58
|
+
vatAmount: input.vatAmount,
|
|
59
|
+
consumptionTaxAmount: input.consumptionTaxAmount ?? input.vatAmount,
|
|
60
|
+
imageUrl: input.imageUrl,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
res.status(200).json(result)
|
|
65
|
+
} catch {
|
|
66
|
+
res.status(500).json({ message: "Failed to execute payment workflow" })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
2
|
+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
3
|
+
import { getPaymentLinkMethodsWorkflow } from "@payment-bold/workflows/get-payment-link-methods"
|
|
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 } = await getPaymentLinkMethodsWorkflow(req.scope).run()
|
|
13
|
+
return res.json(result)
|
|
14
|
+
} catch (err: any) {
|
|
15
|
+
logger.error(
|
|
16
|
+
`[Bold Payment Link Methods Route] Failed to fetch available payment methods: ${err.message}`,
|
|
17
|
+
err
|
|
18
|
+
)
|
|
19
|
+
return res.status(500).json({
|
|
20
|
+
message:
|
|
21
|
+
err.message || "Failed to retrieve Bold Payment Link payment methods",
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
2
|
+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
3
|
+
import { getIntegrationApiPaymentMethodsWorkflow } from "@payment-bold/workflows/get-integration-api-payment-methods"
|
|
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 } = await getIntegrationApiPaymentMethodsWorkflow(
|
|
13
|
+
req.scope
|
|
14
|
+
).run()
|
|
15
|
+
return res.json(result)
|
|
16
|
+
} catch (err: any) {
|
|
17
|
+
logger.error(
|
|
18
|
+
`[Bold Integration API Payment Methods Route] Failed to fetch available payment methods: ${err.message}`,
|
|
19
|
+
err
|
|
20
|
+
)
|
|
21
|
+
return res.status(500).json({
|
|
22
|
+
message:
|
|
23
|
+
err.message ||
|
|
24
|
+
"Failed to retrieve Bold Integration API payment methods",
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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 CreateBoldQrSchema = z.object({
|
|
8
|
+
paymentCollectionId: z.string().min(1),
|
|
9
|
+
amount: z.coerce.number().finite().positive(),
|
|
10
|
+
currency: z.string().min(3).max(3).optional(),
|
|
11
|
+
currency_code: z.string().min(3).max(3).optional(),
|
|
12
|
+
reference: z.string().min(1).max(255).optional(),
|
|
13
|
+
description: z.string().max(500).optional(),
|
|
14
|
+
callbackUrl: z.string().url().optional(),
|
|
15
|
+
qrFormat: z.enum(["BOLD_BASE64", "PLAIN_BASE64", "TEXT"]).optional(),
|
|
16
|
+
email: z.string().email().optional(),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export async function POST(
|
|
20
|
+
req: MedusaRequest,
|
|
21
|
+
res: MedusaResponse
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const parsed = CreateBoldQrSchema.safeParse(req.body ?? {})
|
|
24
|
+
if (!parsed.success) {
|
|
25
|
+
res.status(400).json({
|
|
26
|
+
message: "Invalid QR payment 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
|
+
const currency = (
|
|
37
|
+
input.currency ||
|
|
38
|
+
input.currency_code ||
|
|
39
|
+
"COP"
|
|
40
|
+
).toLowerCase()
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const paymentSession = await paymentModule.createPaymentSession(
|
|
44
|
+
input.paymentCollectionId,
|
|
45
|
+
{
|
|
46
|
+
provider_id: EMedusaFormatBoldPaymentProvider.ONLINE,
|
|
47
|
+
currency_code: currency,
|
|
48
|
+
amount: input.amount,
|
|
49
|
+
data: {
|
|
50
|
+
methodType: "qr",
|
|
51
|
+
reference: input.reference,
|
|
52
|
+
description: input.description,
|
|
53
|
+
callbackUrl: input.callbackUrl,
|
|
54
|
+
qrFormat: input.qrFormat || "BOLD_BASE64",
|
|
55
|
+
email: input.email,
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const sessionData = (paymentSession?.data || {}) as Record<string, unknown>
|
|
61
|
+
res.status(200).json({
|
|
62
|
+
qr_payload: (sessionData.qr_payload as string) || "",
|
|
63
|
+
paymentSession,
|
|
64
|
+
})
|
|
65
|
+
} catch {
|
|
66
|
+
res.status(500).json({ message: "Failed to generate QR payment session" })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
3
|
+
import { IPaymentModuleService } from "@medusajs/framework/types"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* GET /admin/bold/payment-status
|
|
7
|
+
*
|
|
8
|
+
* Fetches the status and metadata of a payment session using Medusa v2's Payment Module.
|
|
9
|
+
*
|
|
10
|
+
* Query Parameters:
|
|
11
|
+
* - paymentSessionId / payment_session_id: The unique ID of the Medusa payment session.
|
|
12
|
+
*/
|
|
13
|
+
export async function GET(
|
|
14
|
+
req: MedusaRequest,
|
|
15
|
+
res: MedusaResponse
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
const paymentModule: IPaymentModuleService = req.scope.resolve(
|
|
18
|
+
Modules.PAYMENT
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
const paymentSessionId = (req.query.paymentSessionId ||
|
|
22
|
+
req.query.payment_session_id) as string
|
|
23
|
+
|
|
24
|
+
if (!paymentSessionId) {
|
|
25
|
+
res
|
|
26
|
+
.status(400)
|
|
27
|
+
.json({ message: "Missing required 'paymentSessionId' query parameter." })
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
// In Medusa v2, payment session details are retrieved directly from the Payment Module
|
|
33
|
+
const paymentSession =
|
|
34
|
+
await paymentModule.retrievePaymentSession(paymentSessionId)
|
|
35
|
+
|
|
36
|
+
res.status(200).json({
|
|
37
|
+
id: paymentSession.id,
|
|
38
|
+
status: paymentSession.status, // e.g., 'authorized', 'captured', 'pending', 'canceled'
|
|
39
|
+
amount: paymentSession.amount,
|
|
40
|
+
currency_code: paymentSession.currency_code,
|
|
41
|
+
data: paymentSession.data,
|
|
42
|
+
})
|
|
43
|
+
} catch (error: any) {
|
|
44
|
+
res.status(500).json({
|
|
45
|
+
message: error.message || "Failed to retrieve payment session status",
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { Modules, PaymentSessionStatus } from "@medusajs/framework/utils"
|
|
3
|
+
import { IPaymentModuleService, Logger } from "@medusajs/framework/types"
|
|
4
|
+
import { capturePaymentWorkflow } from "@medusajs/medusa/core-flows"
|
|
5
|
+
|
|
6
|
+
export async function GET(
|
|
7
|
+
req: MedusaRequest,
|
|
8
|
+
res: MedusaResponse
|
|
9
|
+
): Promise<void> {
|
|
10
|
+
const logger: Logger = req.scope.resolve("logger")
|
|
11
|
+
const paymentModule: IPaymentModuleService = req.scope.resolve(
|
|
12
|
+
Modules.PAYMENT
|
|
13
|
+
)
|
|
14
|
+
const remoteQuery = req.scope.resolve("remoteQuery")
|
|
15
|
+
|
|
16
|
+
const paymentSessionId = (req.query.paymentSessionId ||
|
|
17
|
+
req.query.payment_session_id) as string
|
|
18
|
+
|
|
19
|
+
if (!paymentSessionId) {
|
|
20
|
+
logger.warn(
|
|
21
|
+
"[Bold Stream] Connection attempt missing paymentSessionId query parameter."
|
|
22
|
+
)
|
|
23
|
+
res.status(400).json({ message: "Missing paymentSessionId parameter" })
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
res.setHeader("Content-Type", "text/event-stream")
|
|
28
|
+
res.setHeader("Cache-Control", "no-cache")
|
|
29
|
+
res.setHeader("Connection", "keep-alive")
|
|
30
|
+
|
|
31
|
+
logger.info(
|
|
32
|
+
`[Bold Stream] SSE connection established for session '${paymentSessionId}'`
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const intervalId = setInterval(async () => {
|
|
36
|
+
try {
|
|
37
|
+
let session = await paymentModule.retrievePaymentSession(paymentSessionId)
|
|
38
|
+
|
|
39
|
+
// 1. If still pending, query status using the active registered provider instance
|
|
40
|
+
if (session.status === "pending" && session.provider_id) {
|
|
41
|
+
const providerInstance =
|
|
42
|
+
(paymentModule as any).getPaymentProvider?.(session.provider_id) ||
|
|
43
|
+
(paymentModule as any).paymentProviderService_?.retrieveProvider?.(
|
|
44
|
+
session.provider_id
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if (providerInstance) {
|
|
48
|
+
let isAuthorized = false
|
|
49
|
+
|
|
50
|
+
if (typeof providerInstance.getPaymentStatus === "function") {
|
|
51
|
+
const statusRes = await providerInstance.getPaymentStatus({
|
|
52
|
+
data: session.data,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
if (
|
|
56
|
+
statusRes?.status === PaymentSessionStatus.AUTHORIZED ||
|
|
57
|
+
statusRes?.status === PaymentSessionStatus.CAPTURED
|
|
58
|
+
) {
|
|
59
|
+
isAuthorized = true
|
|
60
|
+
}
|
|
61
|
+
} else if (typeof providerInstance.getStatus === "function") {
|
|
62
|
+
const remoteStatus = await providerInstance.getStatus(session.data)
|
|
63
|
+
|
|
64
|
+
if (remoteStatus === "captured" || remoteStatus === "authorized") {
|
|
65
|
+
isAuthorized = true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isAuthorized) {
|
|
70
|
+
logger.info(
|
|
71
|
+
`[Bold Stream] Confirming payment for session '${session.id}'...`
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
const collectionId = session.payment_collection_id
|
|
75
|
+
|
|
76
|
+
if (collectionId) {
|
|
77
|
+
// Medusa v2 remoteQuery syntax
|
|
78
|
+
await remoteQuery({
|
|
79
|
+
entryPoint: "payment_collection",
|
|
80
|
+
fields: [
|
|
81
|
+
"id",
|
|
82
|
+
"status",
|
|
83
|
+
"amount",
|
|
84
|
+
"payment_sessions.*",
|
|
85
|
+
"payments.*",
|
|
86
|
+
],
|
|
87
|
+
variables: { id: collectionId },
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Step A: Authorize the session in Medusa Payment Module
|
|
92
|
+
const authorizedPayment =
|
|
93
|
+
await paymentModule.authorizePaymentSession(session.id, {})
|
|
94
|
+
|
|
95
|
+
const paymentId =
|
|
96
|
+
(authorizedPayment as any)?.id ||
|
|
97
|
+
(authorizedPayment as any)?.payment?.id
|
|
98
|
+
|
|
99
|
+
// Step B: Execute capture workflow
|
|
100
|
+
if (paymentId) {
|
|
101
|
+
await capturePaymentWorkflow(req.scope).run({
|
|
102
|
+
input: {
|
|
103
|
+
payment_id: paymentId,
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
logger.info(
|
|
107
|
+
`[Bold Stream] Successfully captured payment '${paymentId}' with provider '${session.provider_id}'`
|
|
108
|
+
)
|
|
109
|
+
} else {
|
|
110
|
+
logger.warn(
|
|
111
|
+
`[Bold Stream] No payment ID returned from authorizePaymentSession for session '${session.id}'`
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
session =
|
|
116
|
+
await paymentModule.retrievePaymentSession(paymentSessionId)
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
logger.warn(
|
|
120
|
+
`[Bold Stream] Provider instance for '${session.provider_id}' not found on Payment Module.`
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Write event stream payload to client
|
|
126
|
+
res.write(
|
|
127
|
+
`data: ${JSON.stringify({ status: session.status, session })}\n\n`
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
if (
|
|
131
|
+
["authorized", "captured", "completed", "canceled", "error"].includes(
|
|
132
|
+
session.status
|
|
133
|
+
)
|
|
134
|
+
) {
|
|
135
|
+
logger.info(
|
|
136
|
+
`[Bold Stream] Stream completed for session '${session.id}' with status '${session.status}'. Closing connection.`
|
|
137
|
+
)
|
|
138
|
+
clearInterval(intervalId)
|
|
139
|
+
res.end()
|
|
140
|
+
}
|
|
141
|
+
} catch (error: any) {
|
|
142
|
+
logger.error(
|
|
143
|
+
`[Bold Stream] Error during SSE polling interval: ${error.message}`
|
|
144
|
+
)
|
|
145
|
+
res.write(`data: ${JSON.stringify({ error: error.message })}\n\n`)
|
|
146
|
+
clearInterval(intervalId)
|
|
147
|
+
res.end()
|
|
148
|
+
}
|
|
149
|
+
}, 3000)
|
|
150
|
+
|
|
151
|
+
req.on("close", () => {
|
|
152
|
+
logger.info(
|
|
153
|
+
`[Bold Stream] Client disconnected SSE stream for session '${paymentSessionId}'`
|
|
154
|
+
)
|
|
155
|
+
clearInterval(intervalId)
|
|
156
|
+
})
|
|
157
|
+
}
|