import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" import type PayPalModuleService from "../../../../modules/paypal/service" /** GET /admin/paypal/settings — the full settings singleton. */ export async function GET(req: MedusaRequest, res: MedusaResponse) { try { const paypal = req.scope.resolve("paypal_onboarding") return res.json(await paypal.getSettings()) } catch (e: unknown) { console.error( "[PayPal] settings GET failed:", e instanceof Error ? e.message : e ) return res.status(500).json({ message: "Failed to load PayPal settings" }) } } /** Top-level settings sections an admin may write; anything else is dropped. */ const ALLOWED_SETTINGS_KEYS = new Set([ "additional_settings", "paypal_settings", "advanced_card_payments", "pay_later_messaging", "apple_pay", "google_pay", "api_details", ]) /** POST /admin/paypal/settings — deep-merge the allowed sections of the body. */ export async function POST(req: MedusaRequest, res: MedusaResponse) { const paypal = req.scope.resolve("paypal_onboarding") const raw = req.body && typeof req.body === "object" ? (req.body as Record) : {} const patch: Record = {} for (const key of Object.keys(raw)) { if (ALLOWED_SETTINGS_KEYS.has(key)) { patch[key] = raw[key] } } if (Object.keys(patch).length === 0) { return res .status(400) .json({ message: "No valid settings fields provided" }) } try { return res.json( await paypal.saveSettings( patch as Record> ) ) } catch (e: unknown) { console.error( "[PayPal] settings POST failed:", e instanceof Error ? e.message : e ) return res.status(500).json({ message: "Failed to save PayPal settings" }) } }