@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,350 @@
1
+ import {
2
+ AbstractPaymentProvider,
3
+ MedusaError,
4
+ PaymentActions,
5
+ PaymentSessionStatus,
6
+ } from "@medusajs/framework/utils"
7
+ import {
8
+ InitiatePaymentInput,
9
+ InitiatePaymentOutput,
10
+ AuthorizePaymentInput,
11
+ AuthorizePaymentOutput,
12
+ CapturePaymentInput,
13
+ CapturePaymentOutput,
14
+ CancelPaymentInput,
15
+ CancelPaymentOutput,
16
+ RefundPaymentInput,
17
+ RefundPaymentOutput,
18
+ DeletePaymentInput,
19
+ DeletePaymentOutput,
20
+ UpdatePaymentInput,
21
+ UpdatePaymentOutput,
22
+ GetPaymentStatusInput,
23
+ GetPaymentStatusOutput,
24
+ WebhookActionResult,
25
+ } from "@medusajs/framework/types"
26
+ import { Logger } from "@medusajs/medusa"
27
+ import {
28
+ BoldBaseOptions,
29
+ CreatePaymentLinkPayload,
30
+ InjectedDependencies,
31
+ } from "@payment-bold/types"
32
+ import { EBoldPaymentProvider } from "@payment-bold/types/enums"
33
+
34
+ import { BoldHttpClient } from "../../services/bold-client"
35
+
36
+ export class BoldLinkPaymentProviderService extends AbstractPaymentProvider<BoldBaseOptions> {
37
+ static identifier = EBoldPaymentProvider.LINK
38
+
39
+ protected logger_: Logger
40
+ protected options_: BoldBaseOptions
41
+ protected client_: BoldHttpClient
42
+
43
+ constructor(container: InjectedDependencies, options: BoldBaseOptions) {
44
+ super(container, options)
45
+ this.logger_ = container.logger
46
+ this.options_ = options
47
+ this.client_ = new BoldHttpClient(options, this.logger_)
48
+ }
49
+
50
+ /**
51
+ * Initializes a Bold Payment Link session.
52
+ */
53
+ async initiatePayment(
54
+ input: InitiatePaymentInput
55
+ ): Promise<InitiatePaymentOutput> {
56
+ const { amount, currency_code, data } = input
57
+
58
+ try {
59
+ const extra = (data || {}) as Record<string, unknown>
60
+ const reference = (extra.reference as string) || `link_ref_${Date.now()}`
61
+ const numAmount = Math.round(Number(amount))
62
+ const currency = (
63
+ currency_code ||
64
+ this.options_.defaultCurrency ||
65
+ "COP"
66
+ ).toUpperCase()
67
+
68
+ const payload: CreatePaymentLinkPayload = {
69
+ amount: numAmount,
70
+ currency,
71
+ reference,
72
+ description: (extra.description as string) || `Order ${reference}`,
73
+ email: extra.email as string,
74
+ callbackUrl: extra.callbackUrl as string | undefined,
75
+
76
+ vatAmount: extra.vatAmount != null ? Number(extra.vatAmount) : 0,
77
+ consumptionTaxAmount:
78
+ extra.consumptionTaxAmount != null
79
+ ? Number(extra.consumptionTaxAmount)
80
+ : 0,
81
+
82
+ imageUrl: extra.imageUrl as string | undefined,
83
+ expirationMinutes: extra.expirationMinutes
84
+ ? Number(extra.expirationMinutes)
85
+ : this.options_.linkExpirationMinutes,
86
+ }
87
+
88
+ const linkResponse = await this.client_.createPaymentLink(payload)
89
+
90
+ return {
91
+ id: linkResponse.payload.payment_link,
92
+ data: {
93
+ ...data,
94
+ payment_link_id: linkResponse.payload.payment_link,
95
+ payment_url: linkResponse.payload.url,
96
+ reference,
97
+ status: "pending",
98
+ },
99
+ }
100
+ } catch (error: any) {
101
+ this.logger_.error(
102
+ `[BoldLink] Error generating payment link: ${error.message}`
103
+ )
104
+
105
+ if (error instanceof MedusaError) {
106
+ throw error
107
+ }
108
+
109
+ throw new MedusaError(
110
+ MedusaError.Types.UNEXPECTED_STATE,
111
+ `[BoldLink] Failed to initiate payment link: ${error.message}`
112
+ )
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Authorizes a payment link session.
118
+ */
119
+ async authorizePayment(
120
+ input: AuthorizePaymentInput
121
+ ): Promise<AuthorizePaymentOutput> {
122
+ const sessionData = (input.data || {}) as Record<string, unknown>
123
+
124
+ // 1. Check current session status or verify against Bold if still pending
125
+ let status =
126
+ (sessionData.status as string) || (sessionData.payment_status as string)
127
+
128
+ const linkId = (sessionData.payment_link_id ||
129
+ sessionData.id ||
130
+ sessionData.payment_link) as string
131
+
132
+ if (["pending", "ACTIVE"].includes(status) && linkId) {
133
+ try {
134
+ const linkStatusRes = (await this.client_.getLinkStatus(linkId)) as any
135
+ const remoteStatus =
136
+ linkStatusRes?.payload?.status || linkStatusRes?.status
137
+ if (remoteStatus) {
138
+ status = remoteStatus
139
+ sessionData.status = remoteStatus
140
+ sessionData.transaction_id =
141
+ linkStatusRes?.payload?.transaction_id ||
142
+ linkStatusRes?.transaction_id
143
+ sessionData.payment_method =
144
+ linkStatusRes?.payload?.payment_method ||
145
+ linkStatusRes?.payment_method
146
+ }
147
+ } catch {
148
+ // Keep existing status if network fails
149
+ }
150
+ }
151
+
152
+ // 2. Return AUTHORIZED if status is PAID, APPROVED, SUCCESSFUL, or captured
153
+ if (["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status)) {
154
+ return {
155
+ status: PaymentSessionStatus.AUTHORIZED,
156
+ data: {
157
+ ...sessionData,
158
+ status: "captured",
159
+ },
160
+ }
161
+ }
162
+
163
+ if (
164
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED", "error"].includes(status)
165
+ ) {
166
+ return {
167
+ status: PaymentSessionStatus.CANCELED,
168
+ data: sessionData,
169
+ }
170
+ }
171
+
172
+ return {
173
+ status: PaymentSessionStatus.PENDING,
174
+ data: sessionData,
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Captures the payment.
180
+ */
181
+ async capturePayment(
182
+ input: CapturePaymentInput
183
+ ): Promise<CapturePaymentOutput> {
184
+ const paymentData = (input.data || {}) as Record<string, unknown>
185
+ return {
186
+ data: {
187
+ ...paymentData,
188
+ status: "captured",
189
+ },
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Cancels the payment session.
195
+ */
196
+ async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
197
+ const paymentData = (input.data || {}) as Record<string, unknown>
198
+ return {
199
+ data: {
200
+ ...paymentData,
201
+ status: "canceled",
202
+ },
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Refunds a payment made via Bold Link.
208
+ */
209
+ async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
210
+ const { data, amount } = input
211
+ const pData = (data || {}) as Record<string, unknown>
212
+ const transactionId = (pData.transaction_id || pData.id) as string
213
+
214
+ if (!transactionId) {
215
+ throw new MedusaError(
216
+ MedusaError.Types.INVALID_DATA,
217
+ "Missing transaction_id for refund operation"
218
+ )
219
+ }
220
+
221
+ try {
222
+ const refundRes = await this.client_.refundPayment({
223
+ transactionId,
224
+ amount: Math.round(Number(amount)),
225
+ })
226
+
227
+ return {
228
+ data: {
229
+ ...pData,
230
+ refund_id: refundRes.refund_id,
231
+ refunded_amount: amount,
232
+ status: "refunded",
233
+ },
234
+ }
235
+ } catch (error: any) {
236
+ this.logger_.error(`[BoldLink] Refund error: ${error.message}`)
237
+
238
+ if (error instanceof MedusaError) {
239
+ throw error
240
+ }
241
+
242
+ throw new MedusaError(
243
+ MedusaError.Types.UNEXPECTED_STATE,
244
+ `[BoldLink] Refund failed: ${error.message}`
245
+ )
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Deletes a payment session.
251
+ */
252
+ async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
253
+ const sessionData = (input.data || {}) as Record<string, unknown>
254
+ return {
255
+ data: {
256
+ ...sessionData,
257
+ status: "deleted",
258
+ },
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Updates a payment session.
264
+ */
265
+ async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
266
+ return this.initiatePayment(input)
267
+ }
268
+
269
+ /**
270
+ * Checks the status of a payment link via link ID or reference voucher.
271
+ */
272
+ async getPaymentStatus(
273
+ input: GetPaymentStatusInput
274
+ ): Promise<GetPaymentStatusOutput> {
275
+ const sessionData = (input.data || {}) as Record<string, unknown>
276
+ const linkId = (sessionData.payment_link_id ||
277
+ sessionData.id ||
278
+ sessionData.payment_link) as string
279
+ const reference = (sessionData.reference ||
280
+ sessionData.reference_id) as string
281
+
282
+ try {
283
+ let status: string | undefined
284
+ let transactionId: string | undefined
285
+
286
+ if (linkId) {
287
+ const linkStatusRes = (await this.client_.getLinkStatus(linkId)) as any
288
+ status =
289
+ linkStatusRes?.payload?.status ||
290
+ linkStatusRes?.status ||
291
+ linkStatusRes?.payment_status
292
+ transactionId =
293
+ linkStatusRes?.payload?.transaction_id ||
294
+ linkStatusRes?.transaction_id
295
+ } else if (reference) {
296
+ const voucherRes = (await this.client_.getPaymentVoucher(
297
+ reference
298
+ )) as any
299
+ status =
300
+ voucherRes?.payload?.status ||
301
+ voucherRes?.status ||
302
+ voucherRes?.payment_status
303
+ transactionId =
304
+ voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
305
+ }
306
+
307
+ if (transactionId) {
308
+ sessionData.transaction_id = transactionId
309
+ }
310
+
311
+ if (
312
+ ["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status || "")
313
+ ) {
314
+ sessionData.status = "captured"
315
+ return { status: PaymentSessionStatus.AUTHORIZED }
316
+ }
317
+
318
+ if (
319
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED"].includes(status || "")
320
+ ) {
321
+ sessionData.status = "canceled"
322
+ return { status: PaymentSessionStatus.CANCELED }
323
+ }
324
+
325
+ return { status: PaymentSessionStatus.PENDING }
326
+ } catch {
327
+ return { status: PaymentSessionStatus.PENDING }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Processes incoming Bold webhook notifications natively in Medusa v2.
333
+ * Verifies the HMAC-SHA256 signature and maps the event to PaymentActions.
334
+ */
335
+ async getWebhookActionAndData(_data: {
336
+ data: Record<string, unknown>
337
+ rawData: string | Buffer
338
+ headers: Record<string, unknown>
339
+ }): Promise<WebhookActionResult> {
340
+ return {
341
+ action: PaymentActions.NOT_SUPPORTED,
342
+ }
343
+ }
344
+
345
+ async retrievePayment(
346
+ paymentSessionData: Record<string, unknown>
347
+ ): Promise<Record<string, unknown>> {
348
+ return paymentSessionData
349
+ }
350
+ }
@@ -0,0 +1,254 @@
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 { EBoldPaymentProvider } from "@payment-bold/types/enums"
30
+
31
+ export class BoldOnlinePaymentProviderService extends AbstractPaymentProvider<BoldBaseOptions> {
32
+ static identifier = EBoldPaymentProvider.ONLINE
33
+
34
+ protected logger_: Logger
35
+ protected options_: BoldBaseOptions
36
+ protected client_: BoldHttpClient
37
+
38
+ constructor(container: InjectedDependencies, options: BoldBaseOptions) {
39
+ super(container, options)
40
+ this.logger_ = container.logger
41
+ this.options_ = options
42
+ this.client_ = new BoldHttpClient(options, this.logger_)
43
+ }
44
+
45
+ async initiatePayment(
46
+ input: InitiatePaymentInput
47
+ ): Promise<InitiatePaymentOutput> {
48
+ const { amount, currency_code, data } = input
49
+ const extra = (data || {}) as Record<string, unknown>
50
+ const reference = (extra.reference as string) || `online_ref_${Date.now()}`
51
+ const roundedAmount = Math.round(Number(amount))
52
+ const currency = (
53
+ currency_code ||
54
+ this.options_.defaultCurrency ||
55
+ "COP"
56
+ ).toUpperCase()
57
+ const methodType = (extra.methodType as string) || "qr"
58
+
59
+ const intentRes = await this.client_.createPaymentIntent({
60
+ reference,
61
+ amount: roundedAmount,
62
+ currency,
63
+ methodType: methodType as any,
64
+ description: (extra.description as string) || `Order ${reference}`,
65
+ callbackUrl: extra.callbackUrl as string | undefined,
66
+ qrFormat: (extra.qrFormat as string) || "BOLD_BASE64",
67
+ bankCode: extra.bankCode as string | undefined,
68
+ bankName: extra.bankName as string | undefined,
69
+ payer: extra.email
70
+ ? { email: extra.email as string }
71
+ : (extra.payer as any),
72
+ })
73
+
74
+ const qrPayload =
75
+ (intentRes?.payment_method?.qr_payload as string | undefined) ||
76
+ (intentRes?.payment_method?.qr_code as string | undefined) ||
77
+ intentRes?.payment_url ||
78
+ ""
79
+
80
+ return {
81
+ id: intentRes.id || reference,
82
+ data: {
83
+ ...data,
84
+ intent_id: intentRes.id,
85
+ reference,
86
+ payment_url: intentRes.payment_url,
87
+ qr_payload: qrPayload,
88
+ payment_method: intentRes.payment_method,
89
+ status: "pending",
90
+ amount: roundedAmount,
91
+ currency,
92
+ },
93
+ }
94
+ }
95
+
96
+ async authorizePayment(
97
+ input: AuthorizePaymentInput
98
+ ): Promise<AuthorizePaymentOutput> {
99
+ const sessionData = (input.data || {}) as Record<string, unknown>
100
+ let status =
101
+ (sessionData.status as string) || (sessionData.payment_status as string)
102
+ const reference = (sessionData.reference ||
103
+ sessionData.reference_id) as string
104
+
105
+ if (["pending", "PROCESSING", "ACTIVE"].includes(status) && reference) {
106
+ try {
107
+ const voucherRes = (await this.client_.getPaymentVoucher(
108
+ reference
109
+ )) as any
110
+ const remoteStatus =
111
+ voucherRes?.payload?.status ||
112
+ voucherRes?.status ||
113
+ voucherRes?.payment_status
114
+ if (remoteStatus) {
115
+ status = remoteStatus
116
+ sessionData.status = remoteStatus
117
+ sessionData.transaction_id =
118
+ voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
119
+ sessionData.payment_method =
120
+ voucherRes?.payload?.payment_method || voucherRes?.payment_method
121
+ }
122
+ } catch {
123
+ // Network fallback
124
+ }
125
+ }
126
+
127
+ if (["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status)) {
128
+ return {
129
+ status: PaymentSessionStatus.AUTHORIZED,
130
+ data: {
131
+ ...sessionData,
132
+ status: "captured",
133
+ },
134
+ }
135
+ }
136
+
137
+ if (
138
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED", "error"].includes(status)
139
+ ) {
140
+ return {
141
+ status: PaymentSessionStatus.CANCELED,
142
+ data: sessionData,
143
+ }
144
+ }
145
+
146
+ return {
147
+ status: PaymentSessionStatus.PENDING,
148
+ data: sessionData,
149
+ }
150
+ }
151
+
152
+ async capturePayment(
153
+ input: CapturePaymentInput
154
+ ): Promise<CapturePaymentOutput> {
155
+ return { data: { ...input.data, status: "captured" } }
156
+ }
157
+
158
+ async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
159
+ return { data: { ...input.data, status: "canceled" } }
160
+ }
161
+
162
+ async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
163
+ const { data, amount } = input
164
+ const pData = (data || {}) as Record<string, unknown>
165
+ const transactionId = (pData.transaction_id || pData.id) as string
166
+
167
+ if (!transactionId) {
168
+ throw new MedusaError(
169
+ MedusaError.Types.INVALID_DATA,
170
+ "Missing transaction_id for refund operation"
171
+ )
172
+ }
173
+
174
+ const refundRes = await this.client_.refundPayment({
175
+ transactionId,
176
+ amount: Math.round(Number(amount)),
177
+ })
178
+
179
+ return {
180
+ data: {
181
+ ...pData,
182
+ refund_id: refundRes.refund_id,
183
+ refunded_amount: amount,
184
+ status: "refunded",
185
+ },
186
+ }
187
+ }
188
+
189
+ async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
190
+ return { data: { ...input.data, status: "deleted" } }
191
+ }
192
+
193
+ async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
194
+ return this.initiatePayment(input)
195
+ }
196
+
197
+ async getPaymentStatus(
198
+ input: GetPaymentStatusInput
199
+ ): Promise<GetPaymentStatusOutput> {
200
+ const sessionData = (input.data || {}) as Record<string, unknown>
201
+ const reference = (sessionData.reference ||
202
+ sessionData.reference_id) as string
203
+
204
+ try {
205
+ if (reference) {
206
+ const voucherRes = (await this.client_.getPaymentVoucher(
207
+ reference
208
+ )) as any
209
+ const status =
210
+ voucherRes?.payload?.status ||
211
+ voucherRes?.status ||
212
+ voucherRes?.payment_status
213
+ const transactionId =
214
+ voucherRes?.payload?.transaction_id || voucherRes?.transaction_id
215
+
216
+ if (transactionId) {
217
+ sessionData.transaction_id = transactionId
218
+ }
219
+
220
+ if (
221
+ ["PAID", "APPROVED", "SUCCESSFUL", "captured"].includes(status || "")
222
+ ) {
223
+ sessionData.status = "captured"
224
+ return { status: PaymentSessionStatus.AUTHORIZED }
225
+ }
226
+
227
+ if (
228
+ ["REJECTED", "FAILED", "CANCELLED", "EXPIRED"].includes(status || "")
229
+ ) {
230
+ sessionData.status = "canceled"
231
+ return { status: PaymentSessionStatus.CANCELED }
232
+ }
233
+ }
234
+
235
+ return { status: PaymentSessionStatus.PENDING }
236
+ } catch {
237
+ return { status: PaymentSessionStatus.PENDING }
238
+ }
239
+ }
240
+
241
+ async getWebhookActionAndData(_data: {
242
+ data: Record<string, unknown>
243
+ rawData: string | Buffer
244
+ headers: Record<string, unknown>
245
+ }): Promise<WebhookActionResult> {
246
+ return { action: PaymentActions.NOT_SUPPORTED }
247
+ }
248
+
249
+ async retrievePayment(
250
+ paymentSessionData: Record<string, unknown>
251
+ ): Promise<Record<string, unknown>> {
252
+ return paymentSessionData
253
+ }
254
+ }