{"version":3,"sources":["../src/server.ts"],"sourcesContent":["/**\n * Server-side entry point for @waffo/pancake-nextjs.\n *\n * All functions in this module use the private key for RSA signing\n * and must only run on the server (Next.js Server Actions / Route Handlers).\n *\n * @example\n * ```ts\n * // app/lib/waffo.ts\n * \"use server\";\n * import { createCheckoutAction, createCustomerTokenAction, createMerchantQueryAction } from \"@waffo/pancake-nextjs/server\";\n *\n * const config = {\n *   merchantId: process.env.WAFFO_MERCHANT_ID!,\n *   privateKey: process.env.WAFFO_PRIVATE_KEY!,\n * };\n *\n * export const checkout = createCheckoutAction(config);\n * export const issueCustomerToken = createCustomerTokenAction(config);\n * export const merchantQuery = createMerchantQueryAction(config);\n * ```\n */\nimport { WaffoPancake } from \"@waffo/pancake-ts\";\n\nimport type {\n  RequestOptions,\n  WaffoPancakeConfig,\n  AnonymousCheckoutParams,\n  AuthenticatedCheckoutParams,\n  AuthenticatedPlanChangeParams,\n  CreatePlanChangeSessionParams,\n  CheckoutSessionResult,\n  AuthenticatedCheckoutResult,\n  IssueSessionTokenParams,\n  SessionToken,\n  GraphQLParams,\n  GraphQLResponse,\n} from \"@waffo/pancake-ts\";\n\n// ============================================================\n// Checkout Action\n// ============================================================\n\n/**\n * Parameters for the checkout server action.\n *\n * The `type` field selects the flow: a new purchase (`anonymous` / `authenticated`)\n * or a plan change for an existing subscription (`planChange` /\n * `authenticatedPlanChange`, which carry the required `originOrderId`).\n */\nexport type CheckoutActionParams =\n  | ({ type?: \"anonymous\" } & AnonymousCheckoutParams)\n  | ({ type: \"authenticated\" } & AuthenticatedCheckoutParams)\n  | ({ type: \"planChange\" } & CreatePlanChangeSessionParams)\n  | ({ type: \"authenticatedPlanChange\" } & AuthenticatedPlanChangeParams);\n\n/** Result of the checkout server action */\nexport type CheckoutActionResult = CheckoutSessionResult | AuthenticatedCheckoutResult;\n\n/**\n * Server action signature for checkout.\n *\n * The optional `options` carries an `idempotencyKey`; without one no key is sent\n * and a retried call creates a second session.\n */\nexport type CheckoutAction = (params: CheckoutActionParams, options?: RequestOptions) => Promise<CheckoutActionResult>;\n\n/**\n * Create a server action that handles checkout session creation.\n *\n * The private key is captured in the closure and never sent to the client.\n *\n * Handles both new purchases and plan changes; `params.type` selects the flow.\n *\n * @param config - WaffoPancake client configuration (merchantId + privateKey)\n * @returns A server action function\n *\n * @example\n * ```ts\n * \"use server\";\n * import { createCheckoutAction } from \"@waffo/pancake-nextjs/server\";\n *\n * export const checkout = createCheckoutAction({\n *   merchantId: process.env.WAFFO_MERCHANT_ID!,\n *   privateKey: process.env.WAFFO_PRIVATE_KEY!,\n * });\n * ```\n *\n * @example\n * ```ts\n * // Plan change link for an existing subscription — originOrderId is required\n * const session = await checkout({\n *   type: \"planChange\",\n *   originOrderId: \"ORD_xxx\",\n *   productId: \"PROD_target_plan\",\n *   currency: \"USD\",\n * });\n * // session.checkoutUrl points at the change confirmation page\n * ```\n */\nexport function createCheckoutAction(config: WaffoPancakeConfig): CheckoutAction {\n  const client = new WaffoPancake(config);\n\n  return async (params: CheckoutActionParams, options?: RequestOptions): Promise<CheckoutActionResult> => {\n    if (params.type === \"authenticated\") {\n      // eslint-disable-next-line @typescript-eslint/no-unused-vars -- remove type field before passing to SDK\n      const { type, ...sdkParams } = params;\n      return client.checkout.authenticated.create(sdkParams, options);\n    }\n    if (params.type === \"planChange\") {\n      // eslint-disable-next-line @typescript-eslint/no-unused-vars -- remove type field before passing to SDK\n      const { type, ...sdkParams } = params;\n      return client.checkout.createPlanChangeSession(sdkParams, options);\n    }\n    if (params.type === \"authenticatedPlanChange\") {\n      // eslint-disable-next-line @typescript-eslint/no-unused-vars -- remove type field before passing to SDK\n      const { type, ...sdkParams } = params;\n      return client.checkout.authenticated.createPlanChange(sdkParams, options);\n    }\n    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- remove type field before passing to SDK\n    const { type, ...sdkParams } = params;\n    return client.checkout.anonymous.create(sdkParams, options);\n  };\n}\n\n// ============================================================\n// Customer Token Action\n// ============================================================\n\n/** Server action signature for issuing customer tokens */\nexport type CustomerTokenAction = (params: IssueSessionTokenParams, options?: RequestOptions) => Promise<SessionToken>;\n\n/**\n * Create a server action that issues customer session tokens.\n *\n * @param config - WaffoPancake client configuration\n * @returns A server action function\n *\n * @example\n * ```ts\n * \"use server\";\n * import { createCustomerTokenAction } from \"@waffo/pancake-nextjs/server\";\n *\n * export const issueCustomerToken = createCustomerTokenAction({\n *   merchantId: process.env.WAFFO_MERCHANT_ID!,\n *   privateKey: process.env.WAFFO_PRIVATE_KEY!,\n * });\n * ```\n */\nexport function createCustomerTokenAction(config: WaffoPancakeConfig): CustomerTokenAction {\n  const client = new WaffoPancake(config);\n\n  return async (params: IssueSessionTokenParams, options?: RequestOptions): Promise<SessionToken> => {\n    return client.auth.issueSessionToken(params, options);\n  };\n}\n\n// ============================================================\n// Customer Session Action\n// ============================================================\n\n/** Customer action types */\nexport type CustomerSessionActionType =\n  | \"cancelSubscription\"\n  | \"cancelOnetimeOrder\"\n  | \"reactivateSubscription\"\n  | \"createRefundTicket\"\n  | \"resubmitRefundTicket\"\n  | \"createPlanChangeSession\"\n  | \"query\";\n\n/** Server action signature for customer session operations */\nexport type CustomerSessionAction = (\n  token: string,\n  actionType: CustomerSessionActionType,\n  params: unknown,\n  options?: RequestOptions,\n) => Promise<unknown>;\n\n/**\n * Create a server action that executes customer self-service operations.\n *\n * `config.environment` is required — session tokens carry no environment, and the\n * gateway rejects a session request without the matching header.\n *\n * `createPlanChangeSession` is the customer-driven half of a plan change: it returns\n * a confirmation-page URL, and the platform allows it only when the subscription\n * belongs to that customer, the target plan is in the same product group, and that\n * group's `selfServicePlanChange` is on — otherwise 403. The merchant-only pricing\n * fields are not part of its params; the platform drops them on this path silently.\n *\n * No call sends an idempotency key unless you pass one (`options.idempotencyKey`,\n * the last argument) — the same rule as every other method in the SDK — so a write\n * retried after a timeout executes twice.\n *\n * @param config - WaffoPancake client configuration\n * @returns A server action function\n *\n * @example\n * ```ts\n * \"use server\";\n * import { createCustomerSessionAction } from \"@waffo/pancake-nextjs/server\";\n *\n * export const customerAction = createCustomerSessionAction({\n *   merchantId: process.env.WAFFO_MERCHANT_ID!,\n *   privateKey: process.env.WAFFO_PRIVATE_KEY!,\n *   environment: \"test\",\n * });\n * ```\n */\nexport function createCustomerSessionAction(config: WaffoPancakeConfig): CustomerSessionAction {\n  const client = new WaffoPancake(config);\n\n  return async (token: string, actionType: CustomerSessionActionType, params: unknown, options?: RequestOptions): Promise<unknown> => {\n    const customer = client.buyer(token);\n    switch (actionType) {\n      case \"cancelSubscription\":\n        return customer.cancelSubscription(params as Parameters<typeof customer.cancelSubscription>[0], options);\n      case \"cancelOnetimeOrder\":\n        return customer.cancelOnetimeOrder(params as Parameters<typeof customer.cancelOnetimeOrder>[0], options);\n      case \"reactivateSubscription\":\n        return customer.reactivateSubscription(params as Parameters<typeof customer.reactivateSubscription>[0], options);\n      case \"createRefundTicket\":\n        return customer.createRefundTicket(params as Parameters<typeof customer.createRefundTicket>[0], options);\n      case \"resubmitRefundTicket\":\n        return customer.resubmitRefundTicket(params as Parameters<typeof customer.resubmitRefundTicket>[0], options);\n      case \"createPlanChangeSession\":\n        return customer.createPlanChangeSession(params as Parameters<typeof customer.createPlanChangeSession>[0], options);\n      case \"query\":\n        // Reads take no key: a cached replay would serve stale data.\n        return customer.graphql.query(params as GraphQLParams);\n      default:\n        throw new Error(`Unknown customer action: ${actionType}`);\n    }\n  };\n}\n\n// ============================================================\n// Merchant Query Action\n// ============================================================\n\n/** Server action signature for merchant GraphQL queries */\nexport type MerchantQueryAction = (params: GraphQLParams) => Promise<GraphQLResponse>;\n\n/**\n * Create a server action that executes merchant GraphQL queries.\n *\n * @param config - WaffoPancake client configuration\n * @returns A server action function\n *\n * @example\n * ```ts\n * \"use server\";\n * import { createMerchantQueryAction } from \"@waffo/pancake-nextjs/server\";\n *\n * export const merchantQuery = createMerchantQueryAction({\n *   merchantId: process.env.WAFFO_MERCHANT_ID!,\n *   privateKey: process.env.WAFFO_PRIVATE_KEY!,\n * });\n * ```\n */\nexport function createMerchantQueryAction(config: WaffoPancakeConfig): MerchantQueryAction {\n  const client = new WaffoPancake(config);\n\n  return async (params: GraphQLParams): Promise<GraphQLResponse> => {\n    return client.graphql.query(params);\n  };\n}\n\n// ============================================================\n// Deprecated Aliases\n// ============================================================\n\n/** @deprecated Use {@link CustomerTokenAction} instead. */\nexport type BuyerTokenAction = CustomerTokenAction;\n\n/** @deprecated Use {@link createCustomerTokenAction} instead. */\nexport const createBuyerTokenAction = createCustomerTokenAction;\n\n/** @deprecated Use {@link CustomerSessionActionType} instead. */\nexport type BuyerSessionActionType = CustomerSessionActionType;\n\n/** @deprecated Use {@link CustomerSessionAction} instead. */\nexport type BuyerSessionAction = CustomerSessionAction;\n\n/** @deprecated Use {@link createCustomerSessionAction} instead. */\nexport const createBuyerSessionAction = createCustomerSessionAction;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA,wBAA6B;AA8EtB,SAAS,qBAAqB,QAA4C;AAC/E,QAAM,SAAS,IAAI,+BAAa,MAAM;AAEtC,SAAO,OAAO,QAA8B,YAA4D;AACtG,QAAI,OAAO,SAAS,iBAAiB;AAEnC,YAAM,EAAE,MAAAA,OAAM,GAAGC,WAAU,IAAI;AAC/B,aAAO,OAAO,SAAS,cAAc,OAAOA,YAAW,OAAO;AAAA,IAChE;AACA,QAAI,OAAO,SAAS,cAAc;AAEhC,YAAM,EAAE,MAAAD,OAAM,GAAGC,WAAU,IAAI;AAC/B,aAAO,OAAO,SAAS,wBAAwBA,YAAW,OAAO;AAAA,IACnE;AACA,QAAI,OAAO,SAAS,2BAA2B;AAE7C,YAAM,EAAE,MAAAD,OAAM,GAAGC,WAAU,IAAI;AAC/B,aAAO,OAAO,SAAS,cAAc,iBAAiBA,YAAW,OAAO;AAAA,IAC1E;AAEA,UAAM,EAAE,MAAM,GAAG,UAAU,IAAI;AAC/B,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