@paid-ai/paid-node 1.0.1-alpha0 → 1.0.1-alpha2

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 (74) hide show
  1. package/dist/cjs/Client.js +2 -2
  2. package/dist/cjs/api/resources/plans/client/Client.d.ts +70 -0
  3. package/dist/cjs/api/resources/plans/client/Client.js +282 -2
  4. package/dist/cjs/api/resources/plans/client/requests/PlansSubscribeRequest.d.ts +13 -0
  5. package/dist/cjs/api/resources/plans/client/requests/PlansSubscribeRequest.js +3 -0
  6. package/dist/cjs/api/resources/plans/client/requests/PlansUnsubscribeRequest.d.ts +10 -0
  7. package/dist/cjs/api/resources/plans/client/requests/PlansUnsubscribeRequest.js +3 -0
  8. package/dist/cjs/api/resources/plans/client/requests/PlansUpgradeRequest.d.ts +16 -0
  9. package/dist/cjs/api/resources/plans/client/requests/PlansUpgradeRequest.js +3 -0
  10. package/dist/cjs/api/resources/plans/client/requests/index.d.ts +3 -0
  11. package/dist/cjs/api/resources/usage/client/Client.d.ts +3 -3
  12. package/dist/cjs/api/resources/usage/client/Client.js +3 -3
  13. package/dist/cjs/api/resources/usage/client/requests/UsageRecordBulkV2Request.d.ts +3 -3
  14. package/dist/cjs/api/types/Plan.d.ts +33 -0
  15. package/dist/cjs/api/types/Plan.js +19 -0
  16. package/dist/cjs/api/types/PlanWithFeatures.d.ts +67 -0
  17. package/dist/cjs/api/types/PlanWithFeatures.js +22 -0
  18. package/dist/cjs/api/types/SignalV2.d.ts +5 -5
  19. package/dist/cjs/api/types/index.d.ts +1 -0
  20. package/dist/cjs/api/types/index.js +1 -0
  21. package/dist/cjs/integrations/adapters/expressjs/index.d.ts +2 -2
  22. package/dist/cjs/integrations/adapters/expressjs/index.js +2 -1
  23. package/dist/cjs/integrations/adapters/expressjs/routes.d.ts +16 -0
  24. package/dist/cjs/integrations/adapters/expressjs/routes.js +28 -0
  25. package/dist/cjs/integrations/adapters/nextjs/index.d.ts +1 -1
  26. package/dist/cjs/integrations/adapters/nextjs/index.js +6 -1
  27. package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +75 -0
  28. package/dist/cjs/integrations/adapters/nextjs/routes.js +101 -0
  29. package/dist/cjs/integrations/controllers/index.d.ts +1 -0
  30. package/dist/cjs/integrations/controllers/index.js +13 -1
  31. package/dist/cjs/integrations/controllers/orders.js +52 -17
  32. package/dist/cjs/integrations/controllers/plans.d.ts +208 -0
  33. package/dist/cjs/integrations/controllers/plans.js +365 -0
  34. package/dist/cjs/integrations/types.d.ts +5 -2
  35. package/dist/cjs/version.d.ts +1 -1
  36. package/dist/cjs/version.js +1 -1
  37. package/dist/esm/Client.mjs +2 -2
  38. package/dist/esm/api/resources/plans/client/Client.d.mts +70 -0
  39. package/dist/esm/api/resources/plans/client/Client.mjs +282 -2
  40. package/dist/esm/api/resources/plans/client/requests/PlansSubscribeRequest.d.mts +13 -0
  41. package/dist/esm/api/resources/plans/client/requests/PlansSubscribeRequest.mjs +2 -0
  42. package/dist/esm/api/resources/plans/client/requests/PlansUnsubscribeRequest.d.mts +10 -0
  43. package/dist/esm/api/resources/plans/client/requests/PlansUnsubscribeRequest.mjs +2 -0
  44. package/dist/esm/api/resources/plans/client/requests/PlansUpgradeRequest.d.mts +16 -0
  45. package/dist/esm/api/resources/plans/client/requests/PlansUpgradeRequest.mjs +2 -0
  46. package/dist/esm/api/resources/plans/client/requests/index.d.mts +3 -0
  47. package/dist/esm/api/resources/usage/client/Client.d.mts +3 -3
  48. package/dist/esm/api/resources/usage/client/Client.mjs +3 -3
  49. package/dist/esm/api/resources/usage/client/requests/UsageRecordBulkV2Request.d.mts +3 -3
  50. package/dist/esm/api/types/Plan.d.mts +33 -0
  51. package/dist/esm/api/types/Plan.mjs +18 -1
  52. package/dist/esm/api/types/PlanWithFeatures.d.mts +67 -0
  53. package/dist/esm/api/types/PlanWithFeatures.mjs +19 -0
  54. package/dist/esm/api/types/SignalV2.d.mts +5 -5
  55. package/dist/esm/api/types/index.d.mts +1 -0
  56. package/dist/esm/api/types/index.mjs +1 -0
  57. package/dist/esm/integrations/adapters/expressjs/index.d.mts +2 -2
  58. package/dist/esm/integrations/adapters/expressjs/index.mjs +2 -2
  59. package/dist/esm/integrations/adapters/expressjs/routes.d.mts +16 -0
  60. package/dist/esm/integrations/adapters/expressjs/routes.mjs +27 -0
  61. package/dist/esm/integrations/adapters/nextjs/index.d.mts +1 -1
  62. package/dist/esm/integrations/adapters/nextjs/index.mjs +1 -1
  63. package/dist/esm/integrations/adapters/nextjs/routes.d.mts +75 -0
  64. package/dist/esm/integrations/adapters/nextjs/routes.mjs +96 -0
  65. package/dist/esm/integrations/controllers/index.d.mts +1 -0
  66. package/dist/esm/integrations/controllers/index.mjs +1 -0
  67. package/dist/esm/integrations/controllers/orders.mjs +52 -17
  68. package/dist/esm/integrations/controllers/plans.d.mts +208 -0
  69. package/dist/esm/integrations/controllers/plans.mjs +353 -0
  70. package/dist/esm/integrations/types.d.mts +5 -2
  71. package/dist/esm/version.d.mts +1 -1
  72. package/dist/esm/version.mjs +1 -1
  73. package/package.json +36 -2
  74. package/reference.md +280 -3
@@ -0,0 +1,19 @@
1
+ // This file was auto-generated by Fern from our API Definition.
2
+ export var PlanWithFeatures;
3
+ (function (PlanWithFeatures) {
4
+ /** The type of plan (flat for fixed pricing, usage for usage-based, credit for prepaid credits) */
5
+ PlanWithFeatures.Type = {
6
+ Flat: "flat",
7
+ Usage: "usage",
8
+ Credit: "credit",
9
+ };
10
+ let Pricing;
11
+ (function (Pricing) {
12
+ /** The billing frequency for the plan */
13
+ Pricing.BillingFrequency = {
14
+ Monthly: "monthly",
15
+ Quarterly: "quarterly",
16
+ Annual: "annual",
17
+ };
18
+ })(Pricing = PlanWithFeatures.Pricing || (PlanWithFeatures.Pricing = {}));
19
+ })(PlanWithFeatures || (PlanWithFeatures = {}));
@@ -1,16 +1,16 @@
1
1
  /**
2
- * V2 signal schema with clean field names. Use either internal IDs (product_id, customer_id) or external IDs (external_product_id, external_customer_id).
2
+ * V2 signal schema with clean field names. IMPORTANT: Exactly one customer ID field (customer_id OR external_customer_id) and exactly one product ID field (product_id OR external_product_id) must be provided per signal.
3
3
  */
4
4
  export interface SignalV2 {
5
5
  /** The name of the event being tracked */
6
6
  event_name: string;
7
- /** Paid's internal product ID. Use either this OR external_product_id. */
7
+ /** Paid's display ID for the product (e.g., prod_abc123). Cannot be used with external_product_id. Either this or external_product_id is required. */
8
8
  product_id?: string;
9
- /** Your system's product ID. Use either this OR product_id. */
9
+ /** Your system's product ID. Cannot be used with product_id. Either this or product_id is required. */
10
10
  external_product_id?: string;
11
- /** Paid's internal customer ID. Use either this OR external_customer_id. */
11
+ /** Paid's display ID for the customer (e.g., cus_xyz789). Cannot be used with external_customer_id. Either this or external_customer_id is required. */
12
12
  customer_id?: string;
13
- /** Your system's customer ID. Use either this OR customer_id. */
13
+ /** Your system's customer ID. Cannot be used with customer_id. Either this or customer_id is required. */
14
14
  external_customer_id?: string;
15
15
  /** Optional additional data/metadata for the signal */
16
16
  data?: Record<string, unknown>;
@@ -30,6 +30,7 @@ export * from "./PaginationMeta.mjs";
30
30
  export * from "./PaymentMethod.mjs";
31
31
  export * from "./Plan.mjs";
32
32
  export * from "./PlanGroup.mjs";
33
+ export * from "./PlanWithFeatures.mjs";
33
34
  export * from "./PricePoint.mjs";
34
35
  export * from "./Pricing.mjs";
35
36
  export * from "./PricingModelType.mjs";
@@ -30,6 +30,7 @@ export * from "./PaginationMeta.mjs";
30
30
  export * from "./PaymentMethod.mjs";
31
31
  export * from "./Plan.mjs";
32
32
  export * from "./PlanGroup.mjs";
33
+ export * from "./PlanWithFeatures.mjs";
33
34
  export * from "./PricePoint.mjs";
34
35
  export * from "./Pricing.mjs";
35
36
  export * from "./PricingModelType.mjs";
@@ -2,5 +2,5 @@
2
2
  * ExpressJS integration for Paid SDK
3
3
  * Drop-in handlers for ExpressJS controllers using Express Request/Response
4
4
  */
5
- export { expressjsAdapter, createExpressJSResponseContext } from "./base-adapter.mjs";
6
- export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, type ActivateOrderSyncRouteConfig, type OrdersRouteConfig, type ProvisioningRouteConfig, type SetupIntentRouteConfig, } from "./routes.mjs";
5
+ export { expressjsAdapter, createExpressJSResponseContext, } from "./base-adapter.mjs";
6
+ export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createGetPlanGroupByIdRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, type ActivateOrderSyncRouteConfig, type OrdersRouteConfig, type ProvisioningRouteConfig, type SetupIntentRouteConfig, } from "./routes.mjs";
@@ -2,5 +2,5 @@
2
2
  * ExpressJS integration for Paid SDK
3
3
  * Drop-in handlers for ExpressJS controllers using Express Request/Response
4
4
  */
5
- export { expressjsAdapter, createExpressJSResponseContext } from "./base-adapter.mjs";
6
- export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, } from "./routes.mjs";
5
+ export { expressjsAdapter, createExpressJSResponseContext, } from "./base-adapter.mjs";
6
+ export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createGetPlanGroupByIdRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, } from "./routes.mjs";
@@ -161,4 +161,20 @@ export interface SetupIntentRouteConfig extends BaseHandlerConfig {
161
161
  * ```
162
162
  */
163
163
  export declare function createSetupIntentRoute(config?: SetupIntentRouteConfig): ExpressHandler;
164
+ /**
165
+ * Create an Express handler for getting a plan group by ID
166
+ *
167
+ * @param config - Handler configuration options
168
+ * @returns Express route handler function
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * import express from 'express';
173
+ * import { createGetPlanGroupByIdRoute } from '@paid-ai/paid-node/integrations/express';
174
+ *
175
+ * const app = express();
176
+ * app.get('/paid/plans/:planGroupId', createGetPlanGroupByIdRoute());
177
+ * ```
178
+ */
179
+ export declare function createGetPlanGroupByIdRoute(config?: BaseHandlerConfig): (req: Request, res: Response) => Promise<any>;
164
180
  export {};
@@ -9,6 +9,7 @@ import { createCustomersHandler, createGetCustomerHandler } from "../../controll
9
9
  import { createOrdersHandler } from "../../controllers/orders.mjs";
10
10
  import { createProvisioningHandler } from "../../controllers/provision-users.mjs";
11
11
  import { createActivateOrderSyncHandler, createPayInvoiceHandler, createSetupIntentHandler, } from "../../controllers/billing.mjs";
12
+ import { createGetPlanGroupByIdHandler } from "../../controllers/plans.mjs";
12
13
  /**
13
14
  * Helper to extract headers from Express Request
14
15
  */
@@ -265,3 +266,29 @@ export function createSetupIntentRoute(config = {}) {
265
266
  };
266
267
  return route;
267
268
  }
269
+ /**
270
+ * Create an Express handler for getting a plan group by ID
271
+ *
272
+ * @param config - Handler configuration options
273
+ * @returns Express route handler function
274
+ *
275
+ * @example
276
+ * ```typescript
277
+ * import express from 'express';
278
+ * import { createGetPlanGroupByIdRoute } from '@paid-ai/paid-node/integrations/express';
279
+ *
280
+ * const app = express();
281
+ * app.get('/paid/plans/:planGroupId', createGetPlanGroupByIdRoute());
282
+ * ```
283
+ */
284
+ export function createGetPlanGroupByIdRoute(config = {}) {
285
+ const handler = createGetPlanGroupByIdHandler();
286
+ return (req, res) => {
287
+ return handler({
288
+ body: req.body,
289
+ headers: extractHeaders(req),
290
+ method: "GET",
291
+ params: req.params,
292
+ }, createExpressJSResponseContext(res), config);
293
+ };
294
+ }
@@ -2,4 +2,4 @@
2
2
  * Next.js API Route handlers for Paid integration
3
3
  * Drop-in handlers for Next.js App Router
4
4
  */
5
- export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, type ActivateOrderSyncRouteConfig, type OrdersRouteConfig, type ProvisioningRouteConfig, type SetupIntentRouteConfig, } from './routes.mjs';
5
+ export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createGetPlanGroupByIdRoute, createGetGroupPlansRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, createSubscribeRoute, createUnsubscribeRoute, createUpgradeRoute, type ActivateOrderSyncRouteConfig, type OrdersRouteConfig, type ProvisioningRouteConfig, type SetupIntentRouteConfig, } from "./routes.mjs";
@@ -2,4 +2,4 @@
2
2
  * Next.js API Route handlers for Paid integration
3
3
  * Drop-in handlers for Next.js App Router
4
4
  */
5
- export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, } from './routes.mjs';
5
+ export { createActivateOrderSyncRoute, createContactsRoute, createCustomerInvoicesRoute, createCustomersRoute, createGetCustomerRoute, createGetPlanGroupByIdRoute, createGetGroupPlansRoute, createOrdersRoute, createPayInvoiceRoute, createProvisioningRoute, createSetupIntentRoute, createSubscribeRoute, createUnsubscribeRoute, createUpgradeRoute, } from "./routes.mjs";
@@ -224,4 +224,79 @@ export interface SetupIntentRouteConfig extends BaseHandlerConfig {
224
224
  * ```
225
225
  */
226
226
  export declare function createSetupIntentRoute(config?: SetupIntentRouteConfig): NextJsHandler;
227
+ /**
228
+ * Create a Next.js API route handler for getting a plan group by ID
229
+ *
230
+ * @param config - Handler configuration options
231
+ * @returns Next.js route handler function
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * // src/app/api/plans/[planGroupId]/route.ts
236
+ * import { createGetPlanGroupByIdRoute } from '@paid-ai/paid-node/integrations/nextjs';
237
+ *
238
+ * export const GET = createGetPlanGroupByIdRoute();
239
+ * ```
240
+ */
241
+ export declare function createGetPlanGroupByIdRoute(config?: BaseHandlerConfig): (request: any, context?: any) => Promise<any>;
242
+ /**
243
+ * Create a Next.js API route handler for getting all plans in a plan group
244
+ *
245
+ * @param config - Handler configuration options
246
+ * @returns Next.js route handler function
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * // src/app/api/plans/[planGroupId]/plans/route.ts
251
+ * import { createGetGroupPlansRoute } from '@paid-ai/paid-node/integrations/nextjs';
252
+ *
253
+ * export const GET = createGetGroupPlansRoute();
254
+ * ```
255
+ */
256
+ export declare function createGetGroupPlansRoute(config?: BaseHandlerConfig): (request: any, context?: any) => Promise<any>;
257
+ /**
258
+ * Create a Next.js API route handler for subscribing a customer to a plan
259
+ *
260
+ * @param config - Handler configuration options
261
+ * @returns Next.js route handler function
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * // src/app/api/plans/[planId]/subscribe/route.ts
266
+ * import { createSubscribeRoute } from '@paid-ai/paid-node/integrations/nextjs';
267
+ *
268
+ * export const POST = createSubscribeRoute();
269
+ * ```
270
+ */
271
+ export declare function createSubscribeRoute(config?: BaseHandlerConfig): (request: any, context?: any) => Promise<any>;
272
+ /**
273
+ * Create a Next.js API route handler for unsubscribing a customer from a plan
274
+ *
275
+ * @param config - Handler configuration options
276
+ * @returns Next.js route handler function
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * // src/app/api/plans/[planId]/unsubscribe/route.ts
281
+ * import { createUnsubscribeRoute } from '@paid-ai/paid-node/integrations/nextjs';
282
+ *
283
+ * export const POST = createUnsubscribeRoute();
284
+ * ```
285
+ */
286
+ export declare function createUnsubscribeRoute(config?: BaseHandlerConfig): (request: any, context?: any) => Promise<any>;
287
+ /**
288
+ * Create a Next.js API route handler for upgrading a customer's plan
289
+ *
290
+ * @param config - Handler configuration options
291
+ * @returns Next.js route handler function
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * // src/app/api/plans/upgrade/route.ts
296
+ * import { createUpgradeRoute } from '@paid-ai/paid-node/integrations/nextjs';
297
+ *
298
+ * export const POST = createUpgradeRoute();
299
+ * ```
300
+ */
301
+ export declare function createUpgradeRoute(config?: BaseHandlerConfig): (request: any, context?: any) => Promise<any>;
227
302
  export {};
@@ -5,6 +5,7 @@ import { createCustomersHandler, createGetCustomerHandler } from "../../controll
5
5
  import { createOrdersHandler } from "../../controllers/orders.mjs";
6
6
  import { createProvisioningHandler } from "../../controllers/provision-users.mjs";
7
7
  import { createActivateOrderSyncHandler, createPayInvoiceHandler, createSetupIntentHandler } from "../../controllers/billing.mjs";
8
+ import { createGetPlanGroupByIdHandler, createGetGroupPlansHandler, createSubscribeHandler, createUnsubscribeHandler, createUpgradeHandler } from "../../controllers/plans.mjs";
8
9
  /**
9
10
  * Create a Next.js API route handler for synchronous order activation
10
11
  *
@@ -242,3 +243,98 @@ export function createSetupIntentRoute(config = {}) {
242
243
  const route = (request, context) => adaptedHandler(request, context, config);
243
244
  return route;
244
245
  }
246
+ /**
247
+ * Create a Next.js API route handler for getting a plan group by ID
248
+ *
249
+ * @param config - Handler configuration options
250
+ * @returns Next.js route handler function
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * // src/app/api/plans/[planGroupId]/route.ts
255
+ * import { createGetPlanGroupByIdRoute } from '@paid-ai/paid-node/integrations/nextjs';
256
+ *
257
+ * export const GET = createGetPlanGroupByIdRoute();
258
+ * ```
259
+ */
260
+ export function createGetPlanGroupByIdRoute(config = {}) {
261
+ const handler = createGetPlanGroupByIdHandler();
262
+ const adaptedHandler = nextjsAdapter(handler);
263
+ return (request, context) => adaptedHandler(request, context, config);
264
+ }
265
+ /**
266
+ * Create a Next.js API route handler for getting all plans in a plan group
267
+ *
268
+ * @param config - Handler configuration options
269
+ * @returns Next.js route handler function
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * // src/app/api/plans/[planGroupId]/plans/route.ts
274
+ * import { createGetGroupPlansRoute } from '@paid-ai/paid-node/integrations/nextjs';
275
+ *
276
+ * export const GET = createGetGroupPlansRoute();
277
+ * ```
278
+ */
279
+ export function createGetGroupPlansRoute(config = {}) {
280
+ const handler = createGetGroupPlansHandler();
281
+ const adaptedHandler = nextjsAdapter(handler);
282
+ return (request, context) => adaptedHandler(request, context, config);
283
+ }
284
+ /**
285
+ * Create a Next.js API route handler for subscribing a customer to a plan
286
+ *
287
+ * @param config - Handler configuration options
288
+ * @returns Next.js route handler function
289
+ *
290
+ * @example
291
+ * ```typescript
292
+ * // src/app/api/plans/[planId]/subscribe/route.ts
293
+ * import { createSubscribeRoute } from '@paid-ai/paid-node/integrations/nextjs';
294
+ *
295
+ * export const POST = createSubscribeRoute();
296
+ * ```
297
+ */
298
+ export function createSubscribeRoute(config = {}) {
299
+ const handler = createSubscribeHandler();
300
+ const adaptedHandler = nextjsAdapter(handler);
301
+ return (request, context) => adaptedHandler(request, context, config);
302
+ }
303
+ /**
304
+ * Create a Next.js API route handler for unsubscribing a customer from a plan
305
+ *
306
+ * @param config - Handler configuration options
307
+ * @returns Next.js route handler function
308
+ *
309
+ * @example
310
+ * ```typescript
311
+ * // src/app/api/plans/[planId]/unsubscribe/route.ts
312
+ * import { createUnsubscribeRoute } from '@paid-ai/paid-node/integrations/nextjs';
313
+ *
314
+ * export const POST = createUnsubscribeRoute();
315
+ * ```
316
+ */
317
+ export function createUnsubscribeRoute(config = {}) {
318
+ const handler = createUnsubscribeHandler();
319
+ const adaptedHandler = nextjsAdapter(handler);
320
+ return (request, context) => adaptedHandler(request, context, config);
321
+ }
322
+ /**
323
+ * Create a Next.js API route handler for upgrading a customer's plan
324
+ *
325
+ * @param config - Handler configuration options
326
+ * @returns Next.js route handler function
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * // src/app/api/plans/upgrade/route.ts
331
+ * import { createUpgradeRoute } from '@paid-ai/paid-node/integrations/nextjs';
332
+ *
333
+ * export const POST = createUpgradeRoute();
334
+ * ```
335
+ */
336
+ export function createUpgradeRoute(config = {}) {
337
+ const handler = createUpgradeHandler();
338
+ const adaptedHandler = nextjsAdapter(handler);
339
+ return (request, context) => adaptedHandler(request, context, config);
340
+ }
@@ -4,3 +4,4 @@ export { createOrder, createOrderWithDefaults, createOrdersHandler, } from "./or
4
4
  export { payInvoice, createPayInvoiceHandler, type PayInvoiceConfig, type PayInvoiceRequest, type PayInvoiceResult, activateOrderSync, createActivateOrderSyncHandler, type ActivateOrderSyncConfig, type ActivateOrderSyncRequest, type ActivateOrderSyncResult, createSetupIntent, createSetupIntentHandler, type SetupIntentConfig, type SetupIntentRequest, type SetupIntentResponse, } from "./billing.mjs";
5
5
  export { getCustomerInvoices, createCustomerInvoicesHandler, type CustomerInvoicesResult, } from "./invoices.mjs";
6
6
  export { provisionNewUser, createProvisioningHandler, } from "./provision-users.mjs";
7
+ export { getPlanById, getPlanGroupById, createGetPlanGroupByIdHandler, getGroupPlans, createGetGroupPlansHandler, subscribe, createSubscribeHandler, unsubscribe, createUnsubscribeHandler, upgrade, createUpgradeHandler, } from "./plans.mjs";
@@ -4,3 +4,4 @@ export { createOrder, createOrderWithDefaults, createOrdersHandler, } from "./or
4
4
  export { payInvoice, createPayInvoiceHandler, activateOrderSync, createActivateOrderSyncHandler, createSetupIntent, createSetupIntentHandler, } from "./billing.mjs";
5
5
  export { getCustomerInvoices, createCustomerInvoicesHandler, } from "./invoices.mjs";
6
6
  export { provisionNewUser, createProvisioningHandler, } from "./provision-users.mjs";
7
+ export { getPlanById, getPlanGroupById, createGetPlanGroupByIdHandler, getGroupPlans, createGetGroupPlansHandler, subscribe, createSubscribeHandler, unsubscribe, createUnsubscribeHandler, upgrade, createUpgradeHandler, } from "./plans.mjs";
@@ -8,11 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { createHandler } from "../utils/base-handler.mjs";
11
+ import { getPlanById } from "./plans.mjs";
12
+ function hasProductWithExternalId(pp) {
13
+ if (!pp.product) {
14
+ throw new Error(`Plan product ${pp.id} is missing nested product data`);
15
+ }
16
+ return !!pp.product.externalId;
17
+ }
11
18
  /**
12
19
  * Generate default values for missing order fields
13
20
  */
14
21
  function generateOrderDefaults(config, options = {}) {
15
- var _a;
16
22
  const { defaultDurationDays = 365 } = options;
17
23
  const startDate = config.startDate || new Date().toISOString().split("T")[0];
18
24
  const endDate = config.endDate ||
@@ -22,20 +28,31 @@ function generateOrderDefaults(config, options = {}) {
22
28
  const name = config.name || "Subscription Order";
23
29
  const description = config.description || "Annual subscription";
24
30
  const currency = config.currency || "USD";
25
- const orderLines = ((_a = config.orderLines) === null || _a === void 0 ? void 0 : _a.map(line => ({
26
- agentExternalId: line.agentExternalId,
27
- name: line.name || name,
28
- description: line.description || description,
29
- }))) || [{
30
- agentExternalId: config.agentExternalId,
31
- name,
32
- description,
33
- }];
31
+ // defensive guard
32
+ if (!config.orderLines && !config.planId && !config.agentExternalId) {
33
+ throw new Error("Either orderLines, planId, or agentExternalId must be provided");
34
+ }
35
+ const orderLines = config.orderLines
36
+ ? config.orderLines.map(line => {
37
+ var _a;
38
+ return ({
39
+ agentExternalId: line.agentExternalId,
40
+ name: line.name || name,
41
+ description: line.description || description,
42
+ planProductId: (_a = line.planProductId) !== null && _a !== void 0 ? _a : undefined,
43
+ });
44
+ })
45
+ : [{
46
+ agentExternalId: config.agentExternalId,
47
+ name,
48
+ description,
49
+ }];
34
50
  return {
35
51
  customerId: config.customerId,
36
52
  customerExternalId: config.customerExternalId,
37
53
  billingContactId: config.billingContactId,
38
54
  agentExternalId: config.agentExternalId,
55
+ planId: config.planId,
39
56
  name,
40
57
  description,
41
58
  startDate,
@@ -64,8 +81,12 @@ function createAndActivateOrder(client, config, autoActivate) {
64
81
  orderLine.name = line.name;
65
82
  if (line.description)
66
83
  orderLine.description = line.description;
84
+ // planProductId is not in the create orders api spec - this is functioning but greptile will complain
85
+ if (line.planProductId)
86
+ orderLine.planProductId = line.planProductId;
67
87
  return orderLine;
68
88
  }),
89
+ planId: config.planId,
69
90
  });
70
91
  if (!order.id) {
71
92
  throw new Error("Order created but missing ID");
@@ -172,18 +193,32 @@ export function createOrder(client_1, config_1) {
172
193
  */
173
194
  export function createOrdersHandler(helperOptions) {
174
195
  return createHandler((client, body) => __awaiter(this, void 0, void 0, function* () {
175
- var _a, _b;
176
- if (!body.customerId) {
177
- throw new Error("customerId is required");
178
- }
196
+ var _a, _b, _c;
179
197
  if (!body.customerExternalId) {
180
198
  throw new Error("customerExternalId is required");
181
199
  }
182
- if (!body.agentExternalId) {
183
- throw new Error("agentExternalId is required");
200
+ if (!body.agentExternalId && !body.planId) {
201
+ throw new Error("agentExternalId or planId are required");
184
202
  }
185
203
  const orderOptions = Object.assign(Object.assign({}, helperOptions), { autoActivate: (_b = (_a = body.autoActivate) !== null && _a !== void 0 ? _a : helperOptions === null || helperOptions === void 0 ? void 0 : helperOptions.autoActivate) !== null && _b !== void 0 ? _b : true });
204
+ if (body.planId) {
205
+ const plan = yield getPlanById(client, body.planId);
206
+ if (!((_c = plan.planProducts) === null || _c === void 0 ? void 0 : _c.length)) {
207
+ throw new Error(`Plan ${body.planId} has no products`);
208
+ }
209
+ body.orderLines = plan.planProducts
210
+ .filter(hasProductWithExternalId)
211
+ .map((pp) => ({
212
+ agentExternalId: pp.product.externalId,
213
+ name: pp.product.name,
214
+ description: pp.product.description,
215
+ planProductId: pp.id,
216
+ }));
217
+ if (body.orderLines.length === 0) {
218
+ throw new Error(`Plan ${body.planId} has no products with externalId`);
219
+ }
220
+ }
186
221
  const order = yield createOrderWithDefaults(client, body, orderOptions);
187
222
  return { success: true, data: order };
188
- }), { requiredFields: ['customerId', 'customerExternalId', 'agentExternalId'] });
223
+ }), { requiredFields: ['customerExternalId'] });
189
224
  }