@easypayment/medusa-paypal 0.4.7 → 0.4.8

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 (70) hide show
  1. package/.medusa/server/src/admin/index.js +7 -7
  2. package/.medusa/server/src/admin/index.mjs +7 -7
  3. package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
  4. package/.medusa/server/src/api/store/paypal/create-order/route.js +62 -139
  5. package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
  6. package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js +22 -22
  7. package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js +11 -11
  8. package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js +18 -18
  9. package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js +16 -16
  10. package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js +20 -20
  11. package/.medusa/server/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.js +14 -14
  12. package/.medusa/server/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.js +15 -15
  13. package/README.md +142 -142
  14. package/package.json +75 -75
  15. package/src/admin/index.ts +7 -7
  16. package/src/admin/routes/settings/paypal/_components/Tabs.tsx +52 -52
  17. package/src/admin/routes/settings/paypal/_components/Toast.tsx +51 -51
  18. package/src/admin/routes/settings/paypal/additional-settings/page.tsx +200 -200
  19. package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +183 -183
  20. package/src/admin/routes/settings/paypal/apple-pay/page.tsx +5 -5
  21. package/src/admin/routes/settings/paypal/connection/page.tsx +754 -754
  22. package/src/admin/routes/settings/paypal/google-pay/page.tsx +5 -5
  23. package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +5 -5
  24. package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +376 -376
  25. package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +24 -24
  26. package/src/api/admin/paypal/disconnect/route.ts +8 -8
  27. package/src/api/admin/paypal/environment/route.ts +25 -25
  28. package/src/api/admin/paypal/onboard-complete/route.ts +44 -44
  29. package/src/api/admin/paypal/onboarding-link/route.ts +45 -45
  30. package/src/api/admin/paypal/onboarding-status/route.ts +18 -18
  31. package/src/api/admin/paypal/rotate-credentials/route.ts +8 -8
  32. package/src/api/admin/paypal/save-credentials/route.ts +14 -14
  33. package/src/api/admin/paypal/settings/route.ts +14 -14
  34. package/src/api/admin/paypal/status/route.ts +12 -12
  35. package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +65 -65
  36. package/src/api/store/paypal/capture-order/route.ts +276 -276
  37. package/src/api/store/paypal/config/route.ts +102 -102
  38. package/src/api/store/paypal/create-order/route.ts +77 -176
  39. package/src/api/store/paypal/settings/route.ts +19 -19
  40. package/src/api/store/paypal/webhook/route.ts +246 -246
  41. package/src/api/store/paypal-complete/route.ts +75 -75
  42. package/src/jobs/paypal-reconcile.ts +112 -112
  43. package/src/jobs/paypal-webhook-retry.ts +85 -85
  44. package/src/modules/paypal/clients/paypal-seller.client.ts +59 -59
  45. package/src/modules/paypal/index.ts +8 -8
  46. package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -33
  47. package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -22
  48. package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -29
  49. package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -27
  50. package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -31
  51. package/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.ts +25 -25
  52. package/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.ts +26 -26
  53. package/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.ts +11 -11
  54. package/src/modules/paypal/models/paypal_connection.ts +21 -21
  55. package/src/modules/paypal/models/paypal_metric.ts +9 -9
  56. package/src/modules/paypal/models/paypal_settings.ts +8 -8
  57. package/src/modules/paypal/models/paypal_webhook_event.ts +19 -19
  58. package/src/modules/paypal/payment-provider/README.md +22 -22
  59. package/src/modules/paypal/payment-provider/card-service.ts +760 -760
  60. package/src/modules/paypal/payment-provider/index.ts +19 -19
  61. package/src/modules/paypal/payment-provider/service.ts +1121 -1121
  62. package/src/modules/paypal/payment-provider/webhook-utils.ts +88 -88
  63. package/src/modules/paypal/service.ts +1247 -1247
  64. package/src/modules/paypal/types/config.ts +47 -47
  65. package/src/modules/paypal/utils/amounts.ts +41 -41
  66. package/src/modules/paypal/utils/crypto.ts +51 -51
  67. package/src/modules/paypal/utils/currencies.ts +84 -84
  68. package/src/modules/paypal/utils/paypal-auth.ts +32 -32
  69. package/src/modules/paypal/utils/provider-ids.ts +15 -15
  70. package/src/modules/paypal/webhook-processor.ts +215 -215
@@ -1,24 +1,24 @@
1
- import { createPaymentSessionsWorkflow } from "@medusajs/core-flows"
2
- import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
3
-
4
- type CreatePaymentSessionBody = {
5
- provider_id: string
6
- data?: Record<string, unknown>
7
- customer_id?: string
8
- }
9
-
10
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
11
- const collectionId = req.params.id
12
- const { provider_id, data, customer_id } = req.body as CreatePaymentSessionBody
13
-
14
- const { result } = await createPaymentSessionsWorkflow(req.scope).run({
15
- input: {
16
- payment_collection_id: collectionId,
17
- provider_id,
18
- customer_id,
19
- data,
20
- },
21
- })
22
-
23
- res.status(200).json({ payment_session: result })
24
- }
1
+ import { createPaymentSessionsWorkflow } from "@medusajs/core-flows"
2
+ import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
3
+
4
+ type CreatePaymentSessionBody = {
5
+ provider_id: string
6
+ data?: Record<string, unknown>
7
+ customer_id?: string
8
+ }
9
+
10
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
11
+ const collectionId = req.params.id
12
+ const { provider_id, data, customer_id } = req.body as CreatePaymentSessionBody
13
+
14
+ const { result } = await createPaymentSessionsWorkflow(req.scope).run({
15
+ input: {
16
+ payment_collection_id: collectionId,
17
+ provider_id,
18
+ customer_id,
19
+ data,
20
+ },
21
+ })
22
+
23
+ res.status(200).json({ payment_session: result })
24
+ }
@@ -1,8 +1,8 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
- await paypal.disconnect()
7
- return res.json({ ok: true })
8
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
+ await paypal.disconnect()
7
+ return res.json({ ok: true })
8
+ }
@@ -1,25 +1,25 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- type Body = {
5
- environment?: "sandbox" | "live"
6
- }
7
-
8
- export async function GET(req: MedusaRequest, res: MedusaResponse) {
9
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
10
- const status = await paypal.getStatus()
11
- return res.json({ environment: status.environment })
12
- }
13
-
14
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
15
- try {
16
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
17
- const body = (req.body || {}) as Body
18
- const env = body.environment === "sandbox" ? "sandbox" : "live"
19
- await paypal.setEnvironment(env)
20
- const status = await paypal.getStatus()
21
- return res.json(status)
22
- } catch (e: any) {
23
- return res.status(500).json({ message: e?.message || "Unknown error" })
24
- }
25
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ type Body = {
5
+ environment?: "sandbox" | "live"
6
+ }
7
+
8
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
9
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
10
+ const status = await paypal.getStatus()
11
+ return res.json({ environment: status.environment })
12
+ }
13
+
14
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
15
+ try {
16
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
17
+ const body = (req.body || {}) as Body
18
+ const env = body.environment === "sandbox" ? "sandbox" : "live"
19
+ await paypal.setEnvironment(env)
20
+ const status = await paypal.getStatus()
21
+ return res.json(status)
22
+ } catch (e: any) {
23
+ return res.status(500).json({ message: e?.message || "Unknown error" })
24
+ }
25
+ }
@@ -1,44 +1,44 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- /**
5
- * Helpful response for browser address-bar tests.
6
- * The onboarding callback must call this endpoint via POST.
7
- */
8
- export async function GET(_req: MedusaRequest, res: MedusaResponse) {
9
- return res.status(405).json({
10
- message:
11
- "Method Not Allowed. Use POST with JSON: { authCode, sharedId, env }. This endpoint is called by the PayPal onboarding callback.",
12
- })
13
- }
14
-
15
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
16
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
17
- const body = req.body as { authCode?: string; sharedId?: string; env?: "sandbox" | "live" }
18
-
19
- if (!body?.authCode || !body?.sharedId) {
20
- return res.status(400).json({ message: "Missing authCode/sharedId" })
21
- }
22
-
23
- try {
24
- // Useful for debugging "No response" / hanging requests
25
- console.log("[PayPal] /admin/paypal/onboard-complete", {
26
- env: body.env,
27
- hasAuthCode: !!body.authCode,
28
- hasSharedId: !!body.sharedId,
29
- })
30
-
31
- await paypal.exchangeAndSaveSellerCredentials({
32
- authCode: body.authCode,
33
- sharedId: body.sharedId,
34
- env: body.env,
35
- })
36
-
37
- return res.json({ ok: true })
38
- } catch (e: any) {
39
- console.error("[PayPal] onboard-complete failed", e)
40
- return res.status(500).json({
41
- message: e?.message || "Failed to exchange and save PayPal credentials",
42
- })
43
- }
44
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ /**
5
+ * Helpful response for browser address-bar tests.
6
+ * The onboarding callback must call this endpoint via POST.
7
+ */
8
+ export async function GET(_req: MedusaRequest, res: MedusaResponse) {
9
+ return res.status(405).json({
10
+ message:
11
+ "Method Not Allowed. Use POST with JSON: { authCode, sharedId, env }. This endpoint is called by the PayPal onboarding callback.",
12
+ })
13
+ }
14
+
15
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
16
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
17
+ const body = req.body as { authCode?: string; sharedId?: string; env?: "sandbox" | "live" }
18
+
19
+ if (!body?.authCode || !body?.sharedId) {
20
+ return res.status(400).json({ message: "Missing authCode/sharedId" })
21
+ }
22
+
23
+ try {
24
+ // Useful for debugging "No response" / hanging requests
25
+ console.log("[PayPal] /admin/paypal/onboard-complete", {
26
+ env: body.env,
27
+ hasAuthCode: !!body.authCode,
28
+ hasSharedId: !!body.sharedId,
29
+ })
30
+
31
+ await paypal.exchangeAndSaveSellerCredentials({
32
+ authCode: body.authCode,
33
+ sharedId: body.sharedId,
34
+ env: body.env,
35
+ })
36
+
37
+ return res.json({ ok: true })
38
+ } catch (e: any) {
39
+ console.error("[PayPal] onboard-complete failed", e)
40
+ return res.status(500).json({
41
+ message: e?.message || "Failed to exchange and save PayPal credentials",
42
+ })
43
+ }
44
+ }
@@ -1,45 +1,45 @@
1
- import type {
2
- AuthenticatedMedusaRequest,
3
- MedusaRequest,
4
- MedusaResponse,
5
- } from "@medusajs/framework/http"
6
- import type PayPalModuleService from "../../../../modules/paypal/service"
7
-
8
- type Body = {
9
- email?: string
10
- products?: string[]
11
- }
12
-
13
- export async function POST(req: AuthenticatedMedusaRequest, res: MedusaResponse) {
14
- try {
15
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
16
- const body = (req.body || {}) as Body
17
- const authEmail =
18
- typeof req.auth_context?.user_metadata?.email === "string"
19
- ? String(req.auth_context.user_metadata.email)
20
- : undefined
21
- const email = authEmail ?? body.email ?? "admin@paypal.com"
22
-
23
- const link = await paypal.createOnboardingLink({
24
- email,
25
- products: body.products,
26
- })
27
-
28
- return res.json({
29
- status: "pending",
30
- onboarding_url: link.onboarding_url,
31
- return_url: link.return_url,
32
- })
33
- } catch (e: any) {
34
- console.error("[paypal_onboarding] onboarding-link error:", e?.message || e, e?.stack)
35
- return res.status(500).json({
36
- message: e?.message || "Unknown error",
37
- })
38
- }
39
- }
40
-
41
- export async function GET(req: MedusaRequest, res: MedusaResponse) {
42
- return res.status(405).json({
43
- message: "Use POST /admin/paypal/onboarding-link",
44
- })
45
- }
1
+ import type {
2
+ AuthenticatedMedusaRequest,
3
+ MedusaRequest,
4
+ MedusaResponse,
5
+ } from "@medusajs/framework/http"
6
+ import type PayPalModuleService from "../../../../modules/paypal/service"
7
+
8
+ type Body = {
9
+ email?: string
10
+ products?: string[]
11
+ }
12
+
13
+ export async function POST(req: AuthenticatedMedusaRequest, res: MedusaResponse) {
14
+ try {
15
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
16
+ const body = (req.body || {}) as Body
17
+ const authEmail =
18
+ typeof req.auth_context?.user_metadata?.email === "string"
19
+ ? String(req.auth_context.user_metadata.email)
20
+ : undefined
21
+ const email = authEmail ?? body.email ?? "admin@paypal.com"
22
+
23
+ const link = await paypal.createOnboardingLink({
24
+ email,
25
+ products: body.products,
26
+ })
27
+
28
+ return res.json({
29
+ status: "pending",
30
+ onboarding_url: link.onboarding_url,
31
+ return_url: link.return_url,
32
+ })
33
+ } catch (e: any) {
34
+ console.error("[paypal_onboarding] onboarding-link error:", e?.message || e, e?.stack)
35
+ return res.status(500).json({
36
+ message: e?.message || "Unknown error",
37
+ })
38
+ }
39
+ }
40
+
41
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
42
+ return res.status(405).json({
43
+ message: "Use POST /admin/paypal/onboarding-link",
44
+ })
45
+ }
@@ -1,18 +1,18 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
- try {
6
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
7
- const status = await paypal.getStatus()
8
- return res.json(status)
9
- } catch (e: any) {
10
- // ✅ Avoid breaking Admin UI with Medusa's generic "unknown_error"
11
- console.error("[paypal_onboarding] onboarding-status error:", e?.message || e, e?.stack)
12
- return res.json({
13
- environment: "live",
14
- status: "disconnected",
15
- error: e?.message || "Unknown error",
16
- })
17
- }
18
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
+ try {
6
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
7
+ const status = await paypal.getStatus()
8
+ return res.json(status)
9
+ } catch (e: any) {
10
+ // ✅ Avoid breaking Admin UI with Medusa's generic "unknown_error"
11
+ console.error("[paypal_onboarding] onboarding-status error:", e?.message || e, e?.stack)
12
+ return res.json({
13
+ environment: "live",
14
+ status: "disconnected",
15
+ error: e?.message || "Unknown error",
16
+ })
17
+ }
18
+ }
@@ -1,8 +1,8 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
- const result = await paypal.rotateCredentialEncryptionKey()
7
- return res.json(result)
8
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
+ const result = await paypal.rotateCredentialEncryptionKey()
7
+ return res.json(result)
8
+ }
@@ -1,14 +1,14 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
- const body = req.body as { clientId?: string; clientSecret?: string }
7
-
8
- if (!body?.clientId || !body?.clientSecret) {
9
- return res.status(400).json({ message: "Missing clientId/clientSecret" })
10
- }
11
-
12
- await paypal.saveSellerCredentials({ clientId: body.clientId, clientSecret: body.clientSecret })
13
- return res.json({ ok: true })
14
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
5
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
+ const body = req.body as { clientId?: string; clientSecret?: string }
7
+
8
+ if (!body?.clientId || !body?.clientSecret) {
9
+ return res.status(400).json({ message: "Missing clientId/clientSecret" })
10
+ }
11
+
12
+ await paypal.saveSellerCredentials({ clientId: body.clientId, clientSecret: body.clientSecret })
13
+ return res.json({ ok: true })
14
+ }
@@ -1,14 +1,14 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
- return res.json(await paypal.getSettings())
7
- }
8
-
9
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
10
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
11
-
12
- const patch = (req.body && typeof req.body === "object") ? (req.body as Record<string, any>) : {}
13
- return res.json(await paypal.saveSettings(patch))
14
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
+ return res.json(await paypal.getSettings())
7
+ }
8
+
9
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
10
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
11
+
12
+ const patch = (req.body && typeof req.body === "object") ? (req.body as Record<string, any>) : {}
13
+ return res.json(await paypal.saveSettings(patch))
14
+ }
@@ -1,12 +1,12 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type PayPalModuleService from "../../../../modules/paypal/service"
3
-
4
- export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
- const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
-
7
- const q = (req.query || {}) as Record<string, any>
8
- const envParam = (q.environment || q.env) as string | undefined
9
- const env = envParam === "live" ? "live" : envParam === "sandbox" ? "sandbox" : undefined
10
-
11
- return res.json(await paypal.getStatus(env))
12
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type PayPalModuleService from "../../../../modules/paypal/service"
3
+
4
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
5
+ const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
6
+
7
+ const q = (req.query || {}) as Record<string, any>
8
+ const envParam = (q.environment || q.env) as string | undefined
9
+ const env = envParam === "live" ? "live" : envParam === "sandbox" ? "sandbox" : undefined
10
+
11
+ return res.json(await paypal.getStatus(env))
12
+ }
@@ -1,66 +1,66 @@
1
- import { createPaymentSessionsWorkflow } from "@medusajs/core-flows"
2
- import { MedusaResponse, MedusaStoreRequest, refetchEntity } from "@medusajs/framework/http"
3
- import { MedusaError } from "@medusajs/framework/utils"
4
-
5
- // FIX 6: Removed the broken import of isPayPalProviderId from provider-ids.
6
- // That import caused TS2307 because the relative path was wrong, and the
7
- // isPayPalProviderId guard was conflicting with the typed body cast below,
8
- // producing two const declarations of provider_id in the same scope.
9
- // This route does not need to filter by provider — it accepts any provider_id
10
- // and delegates to the standard createPaymentSessionsWorkflow. The
11
- // isPayPalProviderId guard (Fix 6b) belongs only in routes that need to
12
- // short-circuit for non-PayPal providers.
13
-
14
- type CreatePaymentSessionBody = {
15
- provider_id: string
16
- data?: Record<string, unknown>
17
- }
18
-
19
- const defaultPaymentCollectionFields = [
20
- "id",
21
- "currency_code",
22
- "amount",
23
- "*payment_sessions",
24
- ]
25
-
26
- export async function POST(req: MedusaStoreRequest, res: MedusaResponse) {
27
- const collectionId = req.params.id
28
-
29
- // Single typed destructure — no redeclaration, req.body cast once
30
- const { provider_id, data } = req.body as CreatePaymentSessionBody
31
-
32
- if (!provider_id || typeof provider_id !== "string") {
33
- throw new MedusaError(
34
- MedusaError.Types.INVALID_DATA,
35
- "provider_id is required to create a payment session"
36
- )
37
- }
38
-
39
- try {
40
- await createPaymentSessionsWorkflow(req.scope).run({
41
- input: {
42
- payment_collection_id: collectionId,
43
- provider_id,
44
- customer_id: req.auth_context?.actor_id,
45
- data,
46
- },
47
- })
48
- } catch (error) {
49
- const message = error instanceof Error ? error.message : String(error)
50
- throw new MedusaError(
51
- MedusaError.Types.UNEXPECTED_STATE,
52
- `Failed to create payment session for provider '${provider_id}': ${message}`
53
- )
54
- }
55
-
56
- const paymentCollection = await refetchEntity({
57
- entity: "payment_collection",
58
- idOrFilter: collectionId,
59
- scope: req.scope,
60
- fields: req.queryConfig?.fields ?? defaultPaymentCollectionFields,
61
- })
62
-
63
- res.status(200).json({
64
- payment_collection: paymentCollection,
65
- })
1
+ import { createPaymentSessionsWorkflow } from "@medusajs/core-flows"
2
+ import { MedusaResponse, MedusaStoreRequest, refetchEntity } from "@medusajs/framework/http"
3
+ import { MedusaError } from "@medusajs/framework/utils"
4
+
5
+ // FIX 6: Removed the broken import of isPayPalProviderId from provider-ids.
6
+ // That import caused TS2307 because the relative path was wrong, and the
7
+ // isPayPalProviderId guard was conflicting with the typed body cast below,
8
+ // producing two const declarations of provider_id in the same scope.
9
+ // This route does not need to filter by provider — it accepts any provider_id
10
+ // and delegates to the standard createPaymentSessionsWorkflow. The
11
+ // isPayPalProviderId guard (Fix 6b) belongs only in routes that need to
12
+ // short-circuit for non-PayPal providers.
13
+
14
+ type CreatePaymentSessionBody = {
15
+ provider_id: string
16
+ data?: Record<string, unknown>
17
+ }
18
+
19
+ const defaultPaymentCollectionFields = [
20
+ "id",
21
+ "currency_code",
22
+ "amount",
23
+ "*payment_sessions",
24
+ ]
25
+
26
+ export async function POST(req: MedusaStoreRequest, res: MedusaResponse) {
27
+ const collectionId = req.params.id
28
+
29
+ // Single typed destructure — no redeclaration, req.body cast once
30
+ const { provider_id, data } = req.body as CreatePaymentSessionBody
31
+
32
+ if (!provider_id || typeof provider_id !== "string") {
33
+ throw new MedusaError(
34
+ MedusaError.Types.INVALID_DATA,
35
+ "provider_id is required to create a payment session"
36
+ )
37
+ }
38
+
39
+ try {
40
+ await createPaymentSessionsWorkflow(req.scope).run({
41
+ input: {
42
+ payment_collection_id: collectionId,
43
+ provider_id,
44
+ customer_id: req.auth_context?.actor_id,
45
+ data,
46
+ },
47
+ })
48
+ } catch (error) {
49
+ const message = error instanceof Error ? error.message : String(error)
50
+ throw new MedusaError(
51
+ MedusaError.Types.UNEXPECTED_STATE,
52
+ `Failed to create payment session for provider '${provider_id}': ${message}`
53
+ )
54
+ }
55
+
56
+ const paymentCollection = await refetchEntity({
57
+ entity: "payment_collection",
58
+ idOrFilter: collectionId,
59
+ scope: req.scope,
60
+ fields: req.queryConfig?.fields ?? defaultPaymentCollectionFields,
61
+ })
62
+
63
+ res.status(200).json({
64
+ payment_collection: paymentCollection,
65
+ })
66
66
  }