@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.
Files changed (155) hide show
  1. package/.medusa/server/src/admin/index.cjs +1732 -0
  2. package/.medusa/server/src/admin/index.js +1732 -0
  3. package/.medusa/server/src/api/admin/bold/banks/route.js +15 -0
  4. package/.medusa/server/src/api/admin/bold/payment-button/route.js +44 -0
  5. package/.medusa/server/src/api/admin/bold/payment-link/route.js +61 -0
  6. package/.medusa/server/src/api/admin/bold/payment-link-methods/route.js +16 -0
  7. package/.medusa/server/src/api/admin/bold/payment-methods/route.js +17 -0
  8. package/.medusa/server/src/api/admin/bold/payment-qr/route.js +53 -0
  9. package/.medusa/server/src/api/admin/bold/payment-status/route.js +37 -0
  10. package/.medusa/server/src/api/admin/bold/payment-stream/route.js +103 -0
  11. package/.medusa/server/src/api/admin/bold/pos-push/route.js +46 -0
  12. package/.medusa/server/src/api/admin/bold/refund/route.js +37 -0
  13. package/.medusa/server/src/api/admin/bold/terminals/route.js +16 -0
  14. package/.medusa/server/src/api/admin/plugin/route.js +10 -0
  15. package/.medusa/server/src/api/hooks/payment/bold/route.js +104 -0
  16. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  17. package/.medusa/server/src/hooks/index.js +10 -0
  18. package/.medusa/server/src/hooks/use-bold-checkout-handler.js +135 -0
  19. package/.medusa/server/src/hooks/use-bold-payment-methods.js +20 -0
  20. package/.medusa/server/src/hooks/use-bold-payment-status-stream.js +19 -0
  21. package/.medusa/server/src/hooks/use-bold-refund.js +19 -0
  22. package/.medusa/server/src/hooks/use-bold-terminals.js +13 -0
  23. package/.medusa/server/src/hooks/use-create-bold-button.js +19 -0
  24. package/.medusa/server/src/hooks/use-create-bold-link.js +19 -0
  25. package/.medusa/server/src/hooks/use-create-bold-qr.js +19 -0
  26. package/.medusa/server/src/hooks/use-create-bold-terminal.js +19 -0
  27. package/.medusa/server/src/index.js +21 -0
  28. package/.medusa/server/src/lib/error-utils.js +25 -0
  29. package/.medusa/server/src/lib/get-active-providers.js +34 -0
  30. package/.medusa/server/src/lib/get-provider-options.js +20 -0
  31. package/.medusa/server/src/lib/index.js +4 -0
  32. package/.medusa/server/src/lib/session-helpers.js +11 -0
  33. package/.medusa/server/src/providers/bold-button/index.js +157 -0
  34. package/.medusa/server/src/providers/bold-link/index.js +249 -0
  35. package/.medusa/server/src/providers/bold-online/index.js +169 -0
  36. package/.medusa/server/src/providers/bold-terminal/index.js +170 -0
  37. package/.medusa/server/src/rbac.js +58 -0
  38. package/.medusa/server/src/sdk/client.js +112 -0
  39. package/.medusa/server/src/sdk/query-keys.js +13 -0
  40. package/.medusa/server/src/services/bold-client.js +304 -0
  41. package/.medusa/server/src/services/integrity.js +54 -0
  42. package/.medusa/server/src/types/constants.js +55 -0
  43. package/.medusa/server/src/types/enums.js +15 -0
  44. package/.medusa/server/src/types/errors.js +80 -0
  45. package/.medusa/server/src/types/index.js +3 -0
  46. package/.medusa/server/src/workflows/create-bold-payment-link.js +7 -0
  47. package/.medusa/server/src/workflows/create-bold-refund.js +7 -0
  48. package/.medusa/server/src/workflows/get-bold-payment-methods.js +7 -0
  49. package/.medusa/server/src/workflows/get-integration-api-payment-methods.js +7 -0
  50. package/.medusa/server/src/workflows/get-payment-link-methods.js +7 -0
  51. package/.medusa/server/src/workflows/get-terminals.js +7 -0
  52. package/.medusa/server/src/workflows/index.js +7 -0
  53. package/.medusa/server/src/workflows/steps/create-bold-link.js +43 -0
  54. package/.medusa/server/src/workflows/steps/get-bold-payment-methods.js +12 -0
  55. package/.medusa/server/src/workflows/steps/get-payment-link-methods.js +12 -0
  56. package/.medusa/server/src/workflows/steps/get-terminals.js +13 -0
  57. package/.medusa/server/src/workflows/steps/refund-bold-payment.js +15 -0
  58. package/README.md +106 -2
  59. package/package.json +115 -3
  60. package/src/__tests__/bold-contract.spec.ts +40 -0
  61. package/src/__tests__/bold-qa-emulator.ts +582 -0
  62. package/src/__tests__/bold-use-cases.spec.ts +773 -0
  63. package/src/__tests__/medusa-payment-shim.mjs +334 -0
  64. package/src/__tests__/register-ts-resolver.mjs +101 -0
  65. package/src/admin/README.md +71 -0
  66. package/src/admin/components/bold-terminal-status.tsx +166 -0
  67. package/src/admin/components/icons/bold-fav.tsx +45 -0
  68. package/src/admin/components/icons/copy.tsx +23 -0
  69. package/src/admin/components/icons/index.ts +3 -0
  70. package/src/admin/components/icons/qr-code.tsx +33 -0
  71. package/src/admin/components/icons/smart-phone.tsx +23 -0
  72. package/src/admin/components/index.ts +4 -0
  73. package/src/admin/components/logos/bold.tsx +45 -0
  74. package/src/admin/components/method-item.tsx +19 -0
  75. package/src/admin/components/provider-card.tsx +21 -0
  76. package/src/admin/components/section.tsx +23 -0
  77. package/src/admin/i18n/README.md +58 -0
  78. package/src/admin/i18n/index.ts +11 -0
  79. package/src/admin/i18n/json/ar.json +99 -0
  80. package/src/admin/i18n/json/en.json +99 -0
  81. package/src/admin/i18n/json/es.json +99 -0
  82. package/src/admin/i18n/json/tr.json +99 -0
  83. package/src/admin/routes/settings/bold/components/bold-methods-sidebar.tsx +96 -0
  84. package/src/admin/routes/settings/bold/components/bold-providers-section.tsx +67 -0
  85. package/src/admin/routes/settings/bold/components/index.ts +2 -0
  86. package/src/admin/routes/settings/bold/page.tsx +109 -0
  87. package/src/admin/tsconfig.json +24 -0
  88. package/src/admin/vite-env.d.ts +1 -0
  89. package/src/admin/widgets/bold-audit-timeline.tsx +155 -0
  90. package/src/admin/widgets/bold-payment-actions/bold-payment-actions.tsx +114 -0
  91. package/src/admin/widgets/bold-payment-actions/components/bold-payment-tabs.tsx +127 -0
  92. package/src/admin/widgets/bold-payment-actions/components/bold-remote-payment-panel.tsx +104 -0
  93. package/src/admin/widgets/bold-payment-actions/components/bold-terminal-section.tsx +71 -0
  94. package/src/admin/widgets/bold-payment-actions/hooks/bold-order-balance.ts +126 -0
  95. package/src/admin/widgets/bold-payment-actions/hooks/use-bold-widget-state.tsx +181 -0
  96. package/src/admin/widgets/bold-refund-actions.tsx +151 -0
  97. package/src/admin/widgets/index.ts +12 -0
  98. package/src/api/README.md +37 -0
  99. package/src/api/admin/bold/banks/route.ts +23 -0
  100. package/src/api/admin/bold/payment-button/route.ts +57 -0
  101. package/src/api/admin/bold/payment-link/route.ts +68 -0
  102. package/src/api/admin/bold/payment-link-methods/route.ts +24 -0
  103. package/src/api/admin/bold/payment-methods/route.ts +27 -0
  104. package/src/api/admin/bold/payment-qr/route.ts +68 -0
  105. package/src/api/admin/bold/payment-status/route.ts +48 -0
  106. package/src/api/admin/bold/payment-stream/route.ts +157 -0
  107. package/src/api/admin/bold/pos-push/route.ts +59 -0
  108. package/src/api/admin/bold/refund/route.ts +51 -0
  109. package/src/api/admin/bold/terminals/route.ts +26 -0
  110. package/src/api/admin/plugin/route.ts +14 -0
  111. package/src/api/hooks/payment/bold/route.ts +156 -0
  112. package/src/api/store/plugin/route.ts +5 -0
  113. package/src/hooks/index.ts +9 -0
  114. package/src/hooks/use-bold-checkout-handler.ts +172 -0
  115. package/src/hooks/use-bold-payment-methods.ts +36 -0
  116. package/src/hooks/use-bold-payment-status-stream.ts +55 -0
  117. package/src/hooks/use-bold-refund.ts +33 -0
  118. package/src/hooks/use-bold-terminals.ts +22 -0
  119. package/src/hooks/use-create-bold-button.ts +34 -0
  120. package/src/hooks/use-create-bold-link.ts +33 -0
  121. package/src/hooks/use-create-bold-qr.ts +33 -0
  122. package/src/hooks/use-create-bold-terminal.ts +37 -0
  123. package/src/index.ts +31 -0
  124. package/src/lib/error-utils.ts +29 -0
  125. package/src/lib/get-active-providers.ts +52 -0
  126. package/src/lib/get-provider-options.ts +54 -0
  127. package/src/lib/index.ts +3 -0
  128. package/src/lib/session-helpers.ts +16 -0
  129. package/src/providers/README.md +30 -0
  130. package/src/providers/bold-button/index.ts +244 -0
  131. package/src/providers/bold-link/index.ts +350 -0
  132. package/src/providers/bold-online/index.ts +254 -0
  133. package/src/providers/bold-terminal/index.ts +258 -0
  134. package/src/rbac.ts +72 -0
  135. package/src/sdk/client.ts +157 -0
  136. package/src/sdk/query-keys.ts +13 -0
  137. package/src/services/bold-client.ts +470 -0
  138. package/src/services/integrity.ts +74 -0
  139. package/src/types/constants.ts +63 -0
  140. package/src/types/enums.ts +13 -0
  141. package/src/types/errors.ts +88 -0
  142. package/src/types/index.ts +517 -0
  143. package/src/workflows/create-bold-payment-link.ts +16 -0
  144. package/src/workflows/create-bold-refund.ts +16 -0
  145. package/src/workflows/get-bold-payment-methods.ts +13 -0
  146. package/src/workflows/get-integration-api-payment-methods.ts +13 -0
  147. package/src/workflows/get-payment-link-methods.ts +13 -0
  148. package/src/workflows/get-terminals.ts +10 -0
  149. package/src/workflows/index.ts +6 -0
  150. package/src/workflows/steps/create-bold-link.ts +74 -0
  151. package/src/workflows/steps/get-bold-payment-methods.ts +21 -0
  152. package/src/workflows/steps/get-payment-link-methods.ts +19 -0
  153. package/src/workflows/steps/get-terminals.ts +20 -0
  154. package/src/workflows/steps/refund-bold-payment.ts +28 -0
  155. package/tsconfig.json +41 -0
@@ -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
+ BoldLinkPayload,
11
+ BoldLinkResponse,
12
+ BoldMutationHookOptions,
13
+ } from "../types"
14
+
15
+ export const useCreateBoldLink = (
16
+ options?: UseMutationOptions<BoldLinkResponse, Error, BoldLinkPayload> &
17
+ BoldMutationHookOptions
18
+ ) => {
19
+ const queryClient = useQueryClient()
20
+ const sdk = options?.client || boldAdminSdk
21
+ const targetKey = options?.queryKey || boldQueryKeys.link()
22
+
23
+ return useMutation({
24
+ mutationFn: (payload: BoldLinkPayload) => sdk.createPaymentLink(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,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
+ BoldMutationHookOptions,
11
+ BoldQrPayload,
12
+ BoldQrResponse,
13
+ } from "../types"
14
+
15
+ export const useCreateBoldQr = (
16
+ options?: UseMutationOptions<BoldQrResponse, Error, BoldQrPayload> &
17
+ BoldMutationHookOptions
18
+ ) => {
19
+ const queryClient = useQueryClient()
20
+ const sdk = options?.client || boldAdminSdk
21
+ const targetKey = options?.queryKey || boldQueryKeys.qr()
22
+
23
+ return useMutation({
24
+ mutationFn: (payload: BoldQrPayload) => sdk.createPaymentQr(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,37 @@
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
+ BoldMutationHookOptions,
11
+ BoldTerminalPayload,
12
+ BoldTerminalResponse,
13
+ } from "../types"
14
+
15
+ export const usePushToBoldTerminal = (
16
+ options?: UseMutationOptions<
17
+ BoldTerminalResponse,
18
+ Error,
19
+ BoldTerminalPayload
20
+ > &
21
+ BoldMutationHookOptions
22
+ ) => {
23
+ const queryClient = useQueryClient()
24
+ const sdk = options?.client || boldAdminSdk
25
+ const targetKey = options?.queryKey || boldQueryKeys.orders()
26
+
27
+ return useMutation({
28
+ mutationFn: (payload: BoldTerminalPayload) => sdk.pushToTerminal(payload),
29
+ onSuccess: (data, variables, onMutateResult, context) => {
30
+ if (targetKey && targetKey.length > 0) {
31
+ queryClient.invalidateQueries({ queryKey: targetKey as any })
32
+ }
33
+ options?.onSuccess?.(data, variables, onMutateResult, context)
34
+ },
35
+ ...options,
36
+ })
37
+ }
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { ModuleProviderExports } from "@medusajs/framework/types"
2
+ import { BoldOnlinePaymentProviderService } from "./providers/bold-online"
3
+ import { BoldLinkPaymentProviderService } from "./providers/bold-link"
4
+ import { BoldButtonPaymentProviderService } from "./providers/bold-button"
5
+ import { BoldTerminalPaymentProviderService } from "./providers/bold-terminal"
6
+
7
+ export * from "./types"
8
+ export * from "./types/errors"
9
+ export * from "./lib"
10
+ export * from "./sdk/client"
11
+ export * from "./sdk/query-keys"
12
+
13
+ export {
14
+ BoldButtonPaymentProviderService,
15
+ BoldLinkPaymentProviderService,
16
+ BoldOnlinePaymentProviderService,
17
+ BoldTerminalPaymentProviderService,
18
+ }
19
+
20
+ const services = [
21
+ BoldButtonPaymentProviderService,
22
+ BoldLinkPaymentProviderService,
23
+ BoldOnlinePaymentProviderService,
24
+ BoldTerminalPaymentProviderService,
25
+ ]
26
+
27
+ const providerExports: ModuleProviderExports = {
28
+ services,
29
+ }
30
+
31
+ export default providerExports
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Utility to inspect standard JS errors, Axios/Fetch responses,
3
+ * and Medusa error payloads for 401/403 API authorization denials.
4
+ */
5
+ export const isUnauthorizedError = (error: unknown): boolean => {
6
+ if (!error) return false
7
+
8
+ const err = error as Record<string, any>
9
+ const status = err?.status || err?.statusCode || err?.response?.status
10
+ const message = (
11
+ err?.message ||
12
+ err?.response?.data?.message ||
13
+ err?.response?.data?.Message ||
14
+ ""
15
+ ).toLowerCase()
16
+ const type = (err?.type || err?.response?.data?.type || "").toLowerCase()
17
+
18
+ return (
19
+ status === 401 ||
20
+ status === 403 ||
21
+ type === "unauthorized" ||
22
+ message.includes("unauthorized") ||
23
+ message.includes("not authorized") ||
24
+ message.includes("403") ||
25
+ message.includes("401") ||
26
+ message.includes("deny") ||
27
+ message.includes("forbidden")
28
+ )
29
+ }
@@ -0,0 +1,52 @@
1
+ import { EBoldPaymentProvider } from "../types"
2
+
3
+ /**
4
+ * Shape representing a Medusa payment provider object or string identifier.
5
+ */
6
+ export interface ProviderLike {
7
+ /** Unique provider identifier string (e.g. `pp_bold-online_01H...`) */
8
+ id?: string
9
+ /** Alternative provider identifier property used in certain Medusa API responses */
10
+ provider_id?: string
11
+ }
12
+
13
+ /**
14
+ * Array of all supported Bold payment provider enums.
15
+ */
16
+ export const BOLD_PROVIDER_VALUES = Object.values(EBoldPaymentProvider)
17
+
18
+ /**
19
+ * Parses and returns an array of currently active Bold payment providers from a list of
20
+ * Medusa registered payment provider instances.
21
+ *
22
+ * This function inspects each provider record against the known `EBoldPaymentProvider` enums
23
+ * (`bold-online`, `bold-link`, `bold-button`, `bold-terminal`) to identify matches.
24
+ *
25
+ * @param providers - An array of Medusa payment provider objects or raw ID strings.
26
+ * @returns An array containing only the matching `EBoldPaymentProvider` enum values.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const medusaProviders = [
31
+ * { id: "pp_bold-link_01J0A1" },
32
+ * { id: "pp_bold-terminal_01J0A2" },
33
+ * { id: "pp_stripe_stripe" }
34
+ * ]
35
+ *
36
+ * const activeBold = getActiveBoldProviders(medusaProviders)
37
+ * // Result: [EBoldPaymentProvider.LINK, EBoldPaymentProvider.TERMINAL]
38
+ * ```
39
+ */
40
+ export const getActiveBoldProviders = (
41
+ providers: (string | ProviderLike)[] = []
42
+ ): EBoldPaymentProvider[] => {
43
+ const providerIds = providers.map((provider) =>
44
+ typeof provider === "string"
45
+ ? provider
46
+ : provider?.id || provider?.provider_id || ""
47
+ )
48
+
49
+ return BOLD_PROVIDER_VALUES.filter((boldProvider) =>
50
+ providerIds.some((id) => id.includes(boldProvider))
51
+ )
52
+ }
@@ -0,0 +1,54 @@
1
+ import { ConfigModule, MedusaContainer } from "@medusajs/framework/types"
2
+ import {
3
+ ContainerRegistrationKeys,
4
+ MedusaError,
5
+ } from "@medusajs/framework/utils"
6
+ import { BoldBaseOptions } from "@payment-bold/types"
7
+
8
+ interface ModulesOptionsConfigWithBoldProvider {
9
+ options: {
10
+ providers?: Array<{
11
+ id: string
12
+ resolve: string
13
+ options: BoldBaseOptions
14
+ }>
15
+ }
16
+ }
17
+
18
+ export function getProviderOptionsFromContainer(
19
+ container: MedusaContainer
20
+ ): BoldBaseOptions {
21
+ // 1. Resolve configModule from container
22
+ const configModule = container.resolve<ConfigModule>(
23
+ ContainerRegistrationKeys.CONFIG_MODULE
24
+ )
25
+
26
+ // 2. Safely extract payment module options
27
+ const paymentModuleConfig = configModule?.modules?.payment as unknown as
28
+ | ModulesOptionsConfigWithBoldProvider
29
+ | undefined
30
+
31
+ const providers = paymentModuleConfig?.options?.providers
32
+
33
+ if (!paymentModuleConfig || !Array.isArray(providers)) {
34
+ throw new MedusaError(
35
+ MedusaError.Types.INVALID_DATA,
36
+ "The Payment Module is not configured in medusa-config.ts. Please register '@medusajs/medusa/payment' under modules in your project configuration."
37
+ )
38
+ }
39
+
40
+ // 3. Find the Bold provider entry in the providers array
41
+ const boldProvider = providers.find((p) => p.id && p.id.includes("bold"))
42
+
43
+ if (!boldProvider || !boldProvider.options) {
44
+ throw new MedusaError(
45
+ MedusaError.Types.NOT_FOUND,
46
+ "Unable to resolve Bold Payment Provider options. Please ensure a payment provider with an ID containing 'bold' (e.g., 'pp_bold-link') is configured in medusa-config.ts under modules.payment.options.providers."
47
+ )
48
+ }
49
+
50
+ // 4. Return merged options prioritizing medusa-config options with process.env fallbacks
51
+ const options = boldProvider.options
52
+
53
+ return options
54
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./error-utils"
2
+ export * from "./get-active-providers"
3
+ export * from "./session-helpers"
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Helper utilities for extracting payment session data consistently
3
+ */
4
+
5
+ export function extractSessionId(paymentSession: any): string | undefined {
6
+ return paymentSession?.id
7
+ }
8
+
9
+ export function extractPaymentCollectionId(
10
+ paymentSession: any
11
+ ): string | undefined {
12
+ return (
13
+ paymentSession?.payment_collection_id ||
14
+ paymentSession?.payment_collection?.id
15
+ )
16
+ }
@@ -0,0 +1,30 @@
1
+ # Payment Providers (`src/providers`)
2
+
3
+ Medusa v2 Payment Module provider implementations for Bold.co (`@vicacha-devs/medusa-plugin-payment-bold/providers/payment-bold`).
4
+
5
+ ```text
6
+ providers/
7
+ └── payment-bold/
8
+ ├── index.ts # ModuleProvider(Modules.PAYMENT, { services: [...] })
9
+ ├── core/
10
+ │ └── bold-base.ts # Abstract BoldBaseProvider (HTTP client, HMAC verification, terminals)
11
+ ├── services/
12
+ │ ├── bold-online.ts # BoldOnlineProviderService (identifier: "bold-online")
13
+ │ ├── bold-terminal.ts # BoldTerminalProviderService (identifier: "bold-terminal")
14
+ │ └── bold-link.ts # BoldLinkProviderService (identifier: "bold-link")
15
+ ├── types/
16
+ │ └── index.ts # BoldOptions, BoldWebhookPayload, BoldTerminal, BoldCreateLinkPayload
17
+ └── __tests__/
18
+ └── bold-provider.spec.ts # Unit tests for integrity hash, POS push, payment links, and HMAC webhooks
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Registered Provider Services
24
+
25
+ 1. **`BoldOnlineProviderService` (`bold-online`)**:
26
+ - Generates Bold Checkout widget parameters (`orderId`, `amount`, `currency`, `apiKey`, and SHA-256 `integritySignature`) during `initiatePayment`.
27
+ 2. **`BoldTerminalProviderService` (`bold-terminal`)**:
28
+ - Dispatches physical Smart POS Dataphone payment requests (`POST /v1/payments/app-checkout`) with `terminal_model`, `terminal_serial`, `user_email`, and `reference`.
29
+ 3. **`BoldLinkProviderService` (`bold-link`)**:
30
+ - Generates remote payment links (`POST /online/link/v1`) and supports on-demand status polling (`GET /online/link/v1/:linkId`) in `getPaymentStatus` and `authorizePayment`.
@@ -0,0 +1,244 @@
1
+ import {
2
+ AbstractPaymentProvider,
3
+ MedusaError,
4
+ PaymentActions,
5
+ PaymentSessionStatus,
6
+ } from "@medusajs/framework/utils"
7
+ import {
8
+ AuthorizePaymentInput,
9
+ AuthorizePaymentOutput,
10
+ CancelPaymentInput,
11
+ CancelPaymentOutput,
12
+ CapturePaymentInput,
13
+ CapturePaymentOutput,
14
+ DeletePaymentInput,
15
+ DeletePaymentOutput,
16
+ GetPaymentStatusInput,
17
+ GetPaymentStatusOutput,
18
+ InitiatePaymentInput,
19
+ InitiatePaymentOutput,
20
+ RefundPaymentInput,
21
+ RefundPaymentOutput,
22
+ UpdatePaymentInput,
23
+ UpdatePaymentOutput,
24
+ WebhookActionResult,
25
+ } from "@medusajs/framework/types"
26
+ import { Logger } from "@medusajs/medusa"
27
+ import { BoldBaseOptions, InjectedDependencies } from "@payment-bold/types"
28
+ import { BoldHttpClient } from "../../services/bold-client"
29
+ import { BoldIntegrityService } from "../../services/integrity"
30
+ import { EBoldPaymentProvider } from "@payment-bold/types/enums"
31
+
32
+ export class BoldButtonPaymentProviderService extends AbstractPaymentProvider<BoldBaseOptions> {
33
+ static identifier = EBoldPaymentProvider.BUTTON
34
+
35
+ protected logger_: Logger
36
+ protected options_: BoldBaseOptions
37
+ protected client_: BoldHttpClient
38
+
39
+ constructor(container: InjectedDependencies, options: BoldBaseOptions) {
40
+ super(container, options)
41
+ this.logger_ = container.logger
42
+ this.options_ = options
43
+ this.client_ = new BoldHttpClient(options, this.logger_)
44
+ }
45
+
46
+ async initiatePayment(
47
+ input: InitiatePaymentInput
48
+ ): Promise<InitiatePaymentOutput> {
49
+ const { amount, currency_code, data } = input
50
+ const extra = (data || {}) as Record<string, unknown>
51
+ const reference = (extra.reference as string) || `btn_ref_${Date.now()}`
52
+ const roundedAmount = Math.round(Number(amount))
53
+ const currency = (
54
+ currency_code ||
55
+ this.options_.defaultCurrency ||
56
+ "COP"
57
+ ).toUpperCase()
58
+ const secretKey = this.options_.buttonSecretKey || ""
59
+ const integritySignature = secretKey
60
+ ? BoldIntegrityService.generateButtonHash(
61
+ reference,
62
+ roundedAmount,
63
+ currency,
64
+ secretKey
65
+ )
66
+ : undefined
67
+
68
+ return {
69
+ id: reference,
70
+ data: {
71
+ ...data,
72
+ order_id: reference,
73
+ reference,
74
+ hash: integritySignature,
75
+ integrity_signature: integritySignature,
76
+ api_key: this.options_.buttonApiKey,
77
+ status: "pending",
78
+ amount: roundedAmount,
79
+ currency,
80
+ },
81
+ }
82
+ }
83
+
84
+ async authorizePayment(
85
+ input: AuthorizePaymentInput
86
+ ): Promise<AuthorizePaymentOutput> {
87
+ const sessionData = (input.data || {}) as Record<string, unknown>
88
+ let status =
89
+ (sessionData.status as string) || (sessionData.payment_status as string)
90
+ const reference = (sessionData.reference ||
91
+ sessionData.reference_id) as string
92
+
93
+ if (["pending", "ACTIVE"].includes(status) && reference) {
94
+ try {
95
+ const voucherRes = (await this.client_.getPaymentVoucher(
96
+ reference
97
+ )) as any
98
+ const remoteStatus =
99
+ voucherRes?.payload?.status ||
100
+ voucherRes?.status ||
101
+ voucherRes?.payment_status
102
+ if (remoteStatus) {
103
+ status = remoteStatus
104
+ sessionData.status = remoteStatus
105
+ sessionData.transaction_id =
106
+ voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
107
+ sessionData.payment_method =
108
+ voucherRes?.payload?.payment_method || voucherRes?.payment_method
109
+ }
110
+ } catch {
111
+ // Retain local status on network timeout
112
+ }
113
+ }
114
+
115
+ if (["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status)) {
116
+ return {
117
+ status: PaymentSessionStatus.AUTHORIZED,
118
+ data: {
119
+ ...sessionData,
120
+ status: "captured",
121
+ },
122
+ }
123
+ }
124
+
125
+ if (
126
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED", "error"].includes(status)
127
+ ) {
128
+ return {
129
+ status: PaymentSessionStatus.CANCELED,
130
+ data: sessionData,
131
+ }
132
+ }
133
+
134
+ return {
135
+ status: PaymentSessionStatus.PENDING,
136
+ data: sessionData,
137
+ }
138
+ }
139
+
140
+ async capturePayment(
141
+ input: CapturePaymentInput
142
+ ): Promise<CapturePaymentOutput> {
143
+ const paymentData = (input.data || {}) as Record<string, unknown>
144
+ return { data: { ...paymentData, status: "captured" } }
145
+ }
146
+
147
+ async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
148
+ const paymentData = (input.data || {}) as Record<string, unknown>
149
+ return { data: { ...paymentData, status: "canceled" } }
150
+ }
151
+
152
+ async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
153
+ const { data, amount } = input
154
+ const pData = (data || {}) as Record<string, unknown>
155
+ const transactionId = (pData.transaction_id || pData.id) as string
156
+
157
+ if (!transactionId) {
158
+ throw new MedusaError(
159
+ MedusaError.Types.INVALID_DATA,
160
+ "Missing transaction_id for refund operation"
161
+ )
162
+ }
163
+
164
+ const refundRes = await this.client_.refundPayment({
165
+ transactionId,
166
+ amount: Math.round(Number(amount)),
167
+ })
168
+
169
+ return {
170
+ data: {
171
+ ...pData,
172
+ refund_id: refundRes.refund_id,
173
+ refunded_amount: amount,
174
+ status: "refunded",
175
+ },
176
+ }
177
+ }
178
+
179
+ async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
180
+ return { data: { ...input.data, status: "deleted" } }
181
+ }
182
+
183
+ async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
184
+ return this.initiatePayment(input)
185
+ }
186
+
187
+ async getPaymentStatus(
188
+ input: GetPaymentStatusInput
189
+ ): Promise<GetPaymentStatusOutput> {
190
+ const sessionData = (input.data || {}) as Record<string, unknown>
191
+ const reference = (sessionData.reference ||
192
+ sessionData.reference_id) as string
193
+
194
+ try {
195
+ if (reference) {
196
+ const voucherRes = (await this.client_.getPaymentVoucher(
197
+ reference
198
+ )) as any
199
+ const status =
200
+ voucherRes?.payload?.status ||
201
+ voucherRes?.status ||
202
+ voucherRes?.payment_status
203
+ const transactionId =
204
+ voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
205
+
206
+ if (transactionId) {
207
+ sessionData.transaction_id = transactionId
208
+ }
209
+
210
+ if (
211
+ ["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status || "")
212
+ ) {
213
+ sessionData.status = "captured"
214
+ return { status: PaymentSessionStatus.AUTHORIZED }
215
+ }
216
+
217
+ if (
218
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED"].includes(status || "")
219
+ ) {
220
+ sessionData.status = "canceled"
221
+ return { status: PaymentSessionStatus.CANCELED }
222
+ }
223
+ }
224
+
225
+ return { status: PaymentSessionStatus.PENDING }
226
+ } catch {
227
+ return { status: PaymentSessionStatus.PENDING }
228
+ }
229
+ }
230
+
231
+ async getWebhookActionAndData(_data: {
232
+ data: Record<string, unknown>
233
+ rawData: string | Buffer
234
+ headers: Record<string, unknown>
235
+ }): Promise<WebhookActionResult> {
236
+ return { action: PaymentActions.NOT_SUPPORTED }
237
+ }
238
+
239
+ async retrievePayment(
240
+ paymentSessionData: Record<string, unknown>
241
+ ): Promise<Record<string, unknown>> {
242
+ return paymentSessionData
243
+ }
244
+ }