@paid-ai/paid-node 0.4.0 → 0.5.0-alpha1

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 (77) hide show
  1. package/dist/cjs/Client.js +2 -2
  2. package/dist/cjs/api/types/Signal.d.ts +5 -0
  3. package/dist/cjs/index.d.ts +1 -0
  4. package/dist/cjs/index.js +2 -1
  5. package/dist/cjs/integrations/adapters/nextjs/base-adapter.d.ts +10 -0
  6. package/dist/cjs/integrations/adapters/nextjs/base-adapter.js +58 -0
  7. package/dist/cjs/integrations/adapters/nextjs/index.d.ts +5 -0
  8. package/dist/cjs/integrations/adapters/nextjs/index.js +15 -0
  9. package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +174 -0
  10. package/dist/cjs/integrations/adapters/nextjs/routes.js +192 -0
  11. package/dist/cjs/integrations/client.d.ts +49 -0
  12. package/dist/cjs/integrations/client.js +93 -0
  13. package/dist/cjs/integrations/controllers/billing.d.ts +113 -0
  14. package/dist/cjs/integrations/controllers/billing.js +164 -0
  15. package/dist/cjs/integrations/controllers/contacts.d.ts +66 -0
  16. package/dist/cjs/integrations/controllers/contacts.js +145 -0
  17. package/dist/cjs/integrations/controllers/customers.d.ts +63 -0
  18. package/dist/cjs/integrations/controllers/customers.js +150 -0
  19. package/dist/cjs/integrations/controllers/index.d.ts +6 -0
  20. package/dist/cjs/integrations/controllers/index.js +26 -0
  21. package/dist/cjs/integrations/controllers/invoices.d.ts +40 -0
  22. package/dist/cjs/integrations/controllers/invoices.js +72 -0
  23. package/dist/cjs/integrations/controllers/orders.d.ts +72 -0
  24. package/dist/cjs/integrations/controllers/orders.js +194 -0
  25. package/dist/cjs/integrations/controllers/provision-users.d.ts +62 -0
  26. package/dist/cjs/integrations/controllers/provision-users.js +130 -0
  27. package/dist/cjs/integrations/index.d.ts +29 -0
  28. package/dist/cjs/integrations/index.js +44 -0
  29. package/dist/cjs/integrations/types.d.ts +119 -0
  30. package/dist/cjs/integrations/types.js +2 -0
  31. package/dist/cjs/integrations/utils/api-fetch.d.ts +18 -0
  32. package/dist/cjs/integrations/utils/api-fetch.js +53 -0
  33. package/dist/cjs/integrations/utils/base-handler.d.ts +79 -0
  34. package/dist/cjs/integrations/utils/base-handler.js +168 -0
  35. package/dist/cjs/integrations/utils/create-adapter.d.ts +29 -0
  36. package/dist/cjs/integrations/utils/create-adapter.js +56 -0
  37. package/dist/cjs/version.d.ts +1 -1
  38. package/dist/cjs/version.js +1 -1
  39. package/dist/esm/Client.mjs +2 -2
  40. package/dist/esm/api/types/Signal.d.mts +5 -0
  41. package/dist/esm/index.d.mts +1 -0
  42. package/dist/esm/index.mjs +1 -0
  43. package/dist/esm/integrations/adapters/nextjs/base-adapter.d.mts +10 -0
  44. package/dist/esm/integrations/adapters/nextjs/base-adapter.mjs +55 -0
  45. package/dist/esm/integrations/adapters/nextjs/index.d.mts +5 -0
  46. package/dist/esm/integrations/adapters/nextjs/index.mjs +5 -0
  47. package/dist/esm/integrations/adapters/nextjs/routes.d.mts +174 -0
  48. package/dist/esm/integrations/adapters/nextjs/routes.mjs +183 -0
  49. package/dist/esm/integrations/client.d.mts +49 -0
  50. package/dist/esm/integrations/client.mjs +89 -0
  51. package/dist/esm/integrations/controllers/billing.d.mts +113 -0
  52. package/dist/esm/integrations/controllers/billing.mjs +158 -0
  53. package/dist/esm/integrations/controllers/contacts.d.mts +66 -0
  54. package/dist/esm/integrations/controllers/contacts.mjs +140 -0
  55. package/dist/esm/integrations/controllers/customers.d.mts +63 -0
  56. package/dist/esm/integrations/controllers/customers.mjs +145 -0
  57. package/dist/esm/integrations/controllers/index.d.mts +6 -0
  58. package/dist/esm/integrations/controllers/index.mjs +6 -0
  59. package/dist/esm/integrations/controllers/invoices.d.mts +40 -0
  60. package/dist/esm/integrations/controllers/invoices.mjs +68 -0
  61. package/dist/esm/integrations/controllers/orders.d.mts +72 -0
  62. package/dist/esm/integrations/controllers/orders.mjs +189 -0
  63. package/dist/esm/integrations/controllers/provision-users.d.mts +62 -0
  64. package/dist/esm/integrations/controllers/provision-users.mjs +126 -0
  65. package/dist/esm/integrations/index.d.mts +29 -0
  66. package/dist/esm/integrations/index.mjs +28 -0
  67. package/dist/esm/integrations/types.d.mts +119 -0
  68. package/dist/esm/integrations/types.mjs +1 -0
  69. package/dist/esm/integrations/utils/api-fetch.d.mts +18 -0
  70. package/dist/esm/integrations/utils/api-fetch.mjs +50 -0
  71. package/dist/esm/integrations/utils/base-handler.d.mts +79 -0
  72. package/dist/esm/integrations/utils/base-handler.mjs +164 -0
  73. package/dist/esm/integrations/utils/create-adapter.d.mts +29 -0
  74. package/dist/esm/integrations/utils/create-adapter.mjs +53 -0
  75. package/dist/esm/version.d.mts +1 -1
  76. package/dist/esm/version.mjs +1 -1
  77. package/package.json +32 -3
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createCustomerWithDefaults = createCustomerWithDefaults;
13
+ exports.createCustomer = createCustomer;
14
+ exports.createCustomersHandler = createCustomersHandler;
15
+ const base_handler_js_1 = require("../utils/base-handler.js");
16
+ /**
17
+ * Generate placeholder values for missing customer fields
18
+ * Creates valid but fake data for rapid prototyping
19
+ */
20
+ function generateCustomerDefaults(data) {
21
+ var _a, _b, _c, _d, _e, _f;
22
+ const timestamp = Date.now();
23
+ const randomId = Math.random().toString(36).substring(7);
24
+ const email = data.email || `user_${timestamp}@example.com`;
25
+ const name = data.name || `User ${randomId}`;
26
+ const nameParts = name.split(" ");
27
+ const firstName = data.firstName || nameParts[0] || "Joe";
28
+ const lastName = data.lastName || nameParts.slice(1).join(" ") || "Bloggs";
29
+ const billingAddress = {
30
+ line1: ((_a = data.billingAddress) === null || _a === void 0 ? void 0 : _a.line1) || "123 Placeholder Street",
31
+ line2: ((_b = data.billingAddress) === null || _b === void 0 ? void 0 : _b.line2) || "",
32
+ city: ((_c = data.billingAddress) === null || _c === void 0 ? void 0 : _c.city) || "London",
33
+ state: ((_d = data.billingAddress) === null || _d === void 0 ? void 0 : _d.state) || "London",
34
+ zipCode: ((_e = data.billingAddress) === null || _e === void 0 ? void 0 : _e.zipCode) || "SW1A 1AA",
35
+ country: ((_f = data.billingAddress) === null || _f === void 0 ? void 0 : _f.country) || "UK",
36
+ };
37
+ return {
38
+ externalId: data.externalId,
39
+ email,
40
+ name,
41
+ firstName,
42
+ lastName,
43
+ company: data.company || "",
44
+ phone: data.phone || "",
45
+ billingAddress,
46
+ metadata: data.metadata || {},
47
+ };
48
+ }
49
+ /**
50
+ * Internal helper to create a customer
51
+ */
52
+ function createCustomerInternal(client, data) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const customer = yield client.customers.create({
55
+ externalId: data.externalId,
56
+ name: data.name,
57
+ billingAddress: {
58
+ line1: data.billingAddress.line1,
59
+ line2: data.billingAddress.line2,
60
+ city: data.billingAddress.city,
61
+ state: data.billingAddress.state,
62
+ zipCode: data.billingAddress.zipCode,
63
+ country: data.billingAddress.country,
64
+ },
65
+ });
66
+ if (!customer.id) {
67
+ throw new Error("Customer created but missing ID");
68
+ }
69
+ return {
70
+ id: customer.id,
71
+ externalId: data.externalId,
72
+ };
73
+ });
74
+ }
75
+ /**
76
+ * Create a customer with defaults for missing fields
77
+ *
78
+ * This helper generates reasonable placeholder values for any missing data,
79
+ * making it easy to create customers for prototyping and development.
80
+ *
81
+ * @param client - PaidClient instance
82
+ * @param data - Customer data (only externalId is required)
83
+ * @returns Created customer with ID
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const customer = await createCustomerWithDefaults(client, {
88
+ * externalId: 'user-123',
89
+ * email: 'user@example.com'
90
+ * });
91
+ * // Generates: name, address, etc.
92
+ * ```
93
+ */
94
+ function createCustomerWithDefaults(client, data) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ const completeData = generateCustomerDefaults(data);
97
+ return createCustomerInternal(client, completeData);
98
+ });
99
+ }
100
+ /**
101
+ * Create a customer with explicit data (NO defaults applied)
102
+ *
103
+ * Required fields: externalId, name, email, and complete billingAddress
104
+ * (line1, line2, city, state, zipCode, country)
105
+ *
106
+ * Use createCustomerWithDefaults() if you want automatic default values for missing fields.
107
+ *
108
+ * @param client - PaidClient instance
109
+ * @param data - Complete customer data with ALL required fields
110
+ * @returns Created customer with ID
111
+ * @throws Error if any required fields are missing
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const customer = await createCustomer(client, {
116
+ * externalId: 'user-123',
117
+ * email: 'user@example.com',
118
+ * name: 'John Doe',
119
+ * billingAddress: {
120
+ * line1: '123 Main St',
121
+ * line2: '',
122
+ * city: 'San Francisco',
123
+ * state: 'CA',
124
+ * zipCode: '94105',
125
+ * country: 'US'
126
+ * }
127
+ * });
128
+ * ```
129
+ */
130
+ function createCustomer(client, data) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ return createCustomerInternal(client, data);
133
+ });
134
+ }
135
+ /**
136
+ * Create a framework-agnostic handler for customer creation
137
+ *
138
+ * This handler can be used with any framework adapter.
139
+ *
140
+ * @returns Handler function
141
+ */
142
+ function createCustomersHandler() {
143
+ return (0, base_handler_js_1.createHandler)((client, body) => __awaiter(this, void 0, void 0, function* () {
144
+ if (!body.externalId) {
145
+ throw new Error("externalId is required");
146
+ }
147
+ const customer = yield createCustomerWithDefaults(client, body);
148
+ return { success: true, data: customer };
149
+ }), { requiredFields: ['externalId'] });
150
+ }
@@ -0,0 +1,6 @@
1
+ export { createCustomer, createCustomerWithDefaults, createCustomersHandler, } from "./customers.js";
2
+ export { createContact, createContactWithDefaults, createContactsHandler, } from "./contacts.js";
3
+ export { createOrder, createOrderWithDefaults, createOrdersHandler, } from "./orders.js";
4
+ export { payInvoice, createPayInvoiceHandler, type PayInvoiceConfig, type PayInvoiceRequest, type PayInvoiceResult, activateOrderSync, createActivateOrderSyncHandler, type ActivateOrderSyncConfig, type ActivateOrderSyncRequest, type ActivateOrderSyncResult, } from "./billing.js";
5
+ export { getCustomerInvoices, createCustomerInvoicesHandler, type CustomerInvoicesResult, } from "./invoices.js";
6
+ export { provisionNewUser, createProvisioningHandler, } from "./provision-users.js";
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createProvisioningHandler = exports.provisionNewUser = exports.createCustomerInvoicesHandler = exports.getCustomerInvoices = exports.createActivateOrderSyncHandler = exports.activateOrderSync = exports.createPayInvoiceHandler = exports.payInvoice = exports.createOrdersHandler = exports.createOrderWithDefaults = exports.createOrder = exports.createContactsHandler = exports.createContactWithDefaults = exports.createContact = exports.createCustomersHandler = exports.createCustomerWithDefaults = exports.createCustomer = void 0;
4
+ var customers_js_1 = require("./customers.js");
5
+ Object.defineProperty(exports, "createCustomer", { enumerable: true, get: function () { return customers_js_1.createCustomer; } });
6
+ Object.defineProperty(exports, "createCustomerWithDefaults", { enumerable: true, get: function () { return customers_js_1.createCustomerWithDefaults; } });
7
+ Object.defineProperty(exports, "createCustomersHandler", { enumerable: true, get: function () { return customers_js_1.createCustomersHandler; } });
8
+ var contacts_js_1 = require("./contacts.js");
9
+ Object.defineProperty(exports, "createContact", { enumerable: true, get: function () { return contacts_js_1.createContact; } });
10
+ Object.defineProperty(exports, "createContactWithDefaults", { enumerable: true, get: function () { return contacts_js_1.createContactWithDefaults; } });
11
+ Object.defineProperty(exports, "createContactsHandler", { enumerable: true, get: function () { return contacts_js_1.createContactsHandler; } });
12
+ var orders_js_1 = require("./orders.js");
13
+ Object.defineProperty(exports, "createOrder", { enumerable: true, get: function () { return orders_js_1.createOrder; } });
14
+ Object.defineProperty(exports, "createOrderWithDefaults", { enumerable: true, get: function () { return orders_js_1.createOrderWithDefaults; } });
15
+ Object.defineProperty(exports, "createOrdersHandler", { enumerable: true, get: function () { return orders_js_1.createOrdersHandler; } });
16
+ var billing_js_1 = require("./billing.js");
17
+ Object.defineProperty(exports, "payInvoice", { enumerable: true, get: function () { return billing_js_1.payInvoice; } });
18
+ Object.defineProperty(exports, "createPayInvoiceHandler", { enumerable: true, get: function () { return billing_js_1.createPayInvoiceHandler; } });
19
+ Object.defineProperty(exports, "activateOrderSync", { enumerable: true, get: function () { return billing_js_1.activateOrderSync; } });
20
+ Object.defineProperty(exports, "createActivateOrderSyncHandler", { enumerable: true, get: function () { return billing_js_1.createActivateOrderSyncHandler; } });
21
+ var invoices_js_1 = require("./invoices.js");
22
+ Object.defineProperty(exports, "getCustomerInvoices", { enumerable: true, get: function () { return invoices_js_1.getCustomerInvoices; } });
23
+ Object.defineProperty(exports, "createCustomerInvoicesHandler", { enumerable: true, get: function () { return invoices_js_1.createCustomerInvoicesHandler; } });
24
+ var provision_users_js_1 = require("./provision-users.js");
25
+ Object.defineProperty(exports, "provisionNewUser", { enumerable: true, get: function () { return provision_users_js_1.provisionNewUser; } });
26
+ Object.defineProperty(exports, "createProvisioningHandler", { enumerable: true, get: function () { return provision_users_js_1.createProvisioningHandler; } });
@@ -0,0 +1,40 @@
1
+ import { PayInvoiceConfig } from "./billing.js";
2
+ export interface CustomerInvoicesResult {
3
+ data: any[];
4
+ }
5
+ /**
6
+ * Get customer invoices
7
+ * Fetches all invoices for a customer by their external ID.
8
+ *
9
+ * @param config - API configuration with organization context
10
+ * @param customerExternalId - Customer's external ID
11
+ * @returns Customer invoices
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const result = await getCustomerInvoices(
16
+ * {
17
+ * apiUrl: 'https://api.agentpaid.io',
18
+ * apiKey: process.env.PAID_API_KEY,
19
+ * organizationId: 'org_123'
20
+ * },
21
+ * 'user-123'
22
+ * );
23
+ *
24
+ * console.log(`Found ${result.data.length} invoices`);
25
+ * ```
26
+ */
27
+ export declare function getCustomerInvoices(config: PayInvoiceConfig, customerExternalId: string): Promise<CustomerInvoicesResult>;
28
+ interface CustomerInvoicesRequest {
29
+ customerExternalId: string;
30
+ }
31
+ /**
32
+ * Create a framework-agnostic handler for fetching customer invoices
33
+ *
34
+ * This handler can be used with any framework adapter.
35
+ * Organization ID is automatically fetched from the API key.
36
+ *
37
+ * @returns Handler function
38
+ */
39
+ export declare function createCustomerInvoicesHandler(): (request: import("../utils/base-handler.js").BaseRequestContext<CustomerInvoicesRequest, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;
40
+ export {};
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getCustomerInvoices = getCustomerInvoices;
13
+ exports.createCustomerInvoicesHandler = createCustomerInvoicesHandler;
14
+ const api_fetch_js_1 = require("../utils/api-fetch.js");
15
+ const base_handler_js_1 = require("../utils/base-handler.js");
16
+ /**
17
+ * Get customer invoices
18
+ * Fetches all invoices for a customer by their external ID.
19
+ *
20
+ * @param config - API configuration with organization context
21
+ * @param customerExternalId - Customer's external ID
22
+ * @returns Customer invoices
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * const result = await getCustomerInvoices(
27
+ * {
28
+ * apiUrl: 'https://api.agentpaid.io',
29
+ * apiKey: process.env.PAID_API_KEY,
30
+ * organizationId: 'org_123'
31
+ * },
32
+ * 'user-123'
33
+ * );
34
+ *
35
+ * console.log(`Found ${result.data.length} invoices`);
36
+ * ```
37
+ */
38
+ function getCustomerInvoices(config, customerExternalId) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if (!customerExternalId) {
41
+ throw new Error("customerExternalId is required");
42
+ }
43
+ const data = yield (0, api_fetch_js_1.paidApiFetch)(config, `/api/organizations/${config.organizationId}/customer/external/${customerExternalId}/invoices`, { method: "GET" });
44
+ return { data: data.data || data || [] };
45
+ });
46
+ }
47
+ /**
48
+ * Create a framework-agnostic handler for fetching customer invoices
49
+ *
50
+ * This handler can be used with any framework adapter.
51
+ * Organization ID is automatically fetched from the API key.
52
+ *
53
+ * @returns Handler function
54
+ */
55
+ function createCustomerInvoicesHandler() {
56
+ return (0, base_handler_js_1.createHandler)((_client, _body, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
57
+ if (!organizationId) {
58
+ return { success: false, error: 'Organization ID not found', status: 500 };
59
+ }
60
+ const customerExternalId = params === null || params === void 0 ? void 0 : params.customerExternalId;
61
+ if (!customerExternalId) {
62
+ return { success: false, error: 'customerExternalId is required', status: 400 };
63
+ }
64
+ const apiKey = process.env.PAID_API_KEY || '';
65
+ const apiUrl = process.env.PAID_API_URL || 'https://api.agentpaid.io';
66
+ const result = yield getCustomerInvoices({ apiUrl, apiKey, organizationId }, customerExternalId);
67
+ return {
68
+ success: true,
69
+ data: result.data,
70
+ };
71
+ }), { allowedMethods: ['GET'], requireOrganizationId: true });
72
+ }
@@ -0,0 +1,72 @@
1
+ import type { PaidClient } from "../../Client.js";
2
+ import type { OrderConfig, OrderCreationResult, OrderOptions } from "../types.js";
3
+ /**
4
+ * Create an order with defaults for missing fields
5
+ *
6
+ * This helper generates reasonable defaults for dates, names, and other fields,
7
+ * making it easy to create orders for prototyping and development.
8
+ *
9
+ * @param client - PaidClient instance
10
+ * @param config - Order configuration (customerId, customerExternalId, agentExternalId required)
11
+ * @param options - Additional options for order creation
12
+ * @returns Created order with ID
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const order = await createOrderWithDefaults(client, {
17
+ * customerId: 'cus_123',
18
+ * customerExternalId: 'user-123',
19
+ * agentExternalId: 'agent-prod'
20
+ * });
21
+ * // Generates: startDate, endDate (1 year), name, description, currency (USD)
22
+ * ```
23
+ */
24
+ export declare function createOrderWithDefaults(client: PaidClient, config: OrderConfig, options?: OrderOptions): Promise<OrderCreationResult>;
25
+ /**
26
+ * Create an order with explicit configuration (NO defaults applied)
27
+ *
28
+ * Required fields: customerId, customerExternalId, agentExternalId, name, description,
29
+ * startDate, endDate, currency, orderLines
30
+ *
31
+ * Use createOrderWithDefaults() if you want automatic default values for missing fields.
32
+ *
33
+ * @param client - PaidClient instance
34
+ * @param config - Complete order configuration with ALL required fields
35
+ * @param options - Additional options for order creation
36
+ * @returns Created order with ID
37
+ * @throws Error if any required fields are missing
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const order = await createOrder(client, {
42
+ * customerId: 'cus_123',
43
+ * customerExternalId: 'user-123',
44
+ * billingContactId: 'contact_123',
45
+ * agentExternalId: 'agent-prod',
46
+ * name: 'Pro Plan Subscription',
47
+ * description: 'Monthly subscription to Pro plan',
48
+ * startDate: '2024-01-01',
49
+ * endDate: '2025-01-01',
50
+ * currency: 'USD',
51
+ * orderLines: [{
52
+ * agentExternalId: 'agent-prod',
53
+ * name: 'Pro Plan',
54
+ * description: 'Pro plan features'
55
+ * }]
56
+ * }, { autoActivate: true });
57
+ * ```
58
+ */
59
+ export declare function createOrder(client: PaidClient, config: Required<OrderConfig>, options?: OrderOptions): Promise<OrderCreationResult>;
60
+ interface OrderRequest extends OrderConfig {
61
+ autoActivate?: boolean;
62
+ }
63
+ /**
64
+ * Create a framework-agnostic handler for order creation
65
+ *
66
+ * This handler can be used with any framework adapter.
67
+ *
68
+ * @param helperOptions - Default options for order creation
69
+ * @returns Handler function
70
+ */
71
+ export declare function createOrdersHandler(helperOptions?: OrderOptions): (request: import("../utils/base-handler.js").BaseRequestContext<OrderRequest, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;
72
+ export {};
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createOrderWithDefaults = createOrderWithDefaults;
13
+ exports.createOrder = createOrder;
14
+ exports.createOrdersHandler = createOrdersHandler;
15
+ const base_handler_js_1 = require("../utils/base-handler.js");
16
+ /**
17
+ * Generate default values for missing order fields
18
+ */
19
+ function generateOrderDefaults(config, options = {}) {
20
+ var _a;
21
+ const { defaultDurationDays = 365 } = options;
22
+ const startDate = config.startDate || new Date().toISOString().split("T")[0];
23
+ const endDate = config.endDate ||
24
+ new Date(Date.now() + defaultDurationDays * 24 * 60 * 60 * 1000)
25
+ .toISOString()
26
+ .split("T")[0];
27
+ const name = config.name || "Subscription Order";
28
+ const description = config.description || "Annual subscription";
29
+ const currency = config.currency || "USD";
30
+ const orderLines = ((_a = config.orderLines) === null || _a === void 0 ? void 0 : _a.map(line => ({
31
+ agentExternalId: line.agentExternalId,
32
+ name: line.name || name,
33
+ description: line.description || description,
34
+ }))) || [{
35
+ agentExternalId: config.agentExternalId,
36
+ name,
37
+ description,
38
+ }];
39
+ return {
40
+ customerId: config.customerId,
41
+ customerExternalId: config.customerExternalId,
42
+ billingContactId: config.billingContactId,
43
+ agentExternalId: config.agentExternalId,
44
+ name,
45
+ description,
46
+ startDate,
47
+ endDate,
48
+ currency,
49
+ orderLines,
50
+ };
51
+ }
52
+ /**
53
+ * Internal helper to create and optionally activate an order
54
+ */
55
+ function createAndActivateOrder(client, config, autoActivate) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const order = yield client.orders.create({
58
+ customerId: config.customerId,
59
+ customerExternalId: config.customerExternalId,
60
+ billingContactId: config.billingContactId,
61
+ name: config.name,
62
+ description: config.description,
63
+ startDate: config.startDate,
64
+ endDate: config.endDate,
65
+ currency: config.currency,
66
+ orderLines: config.orderLines.map(line => {
67
+ const orderLine = { agentExternalId: line.agentExternalId };
68
+ if (line.name)
69
+ orderLine.name = line.name;
70
+ if (line.description)
71
+ orderLine.description = line.description;
72
+ return orderLine;
73
+ }),
74
+ });
75
+ if (!order.id) {
76
+ throw new Error("Order created but missing ID");
77
+ }
78
+ if (autoActivate && order.creationState === "draft") {
79
+ try {
80
+ yield client.orders.activate(order.id);
81
+ return {
82
+ id: order.id,
83
+ creationState: "active",
84
+ };
85
+ }
86
+ catch (activationError) {
87
+ console.error(`Error activating order ${order.id}:`, activationError instanceof Error
88
+ ? activationError.message
89
+ : "Unknown error");
90
+ return {
91
+ id: order.id,
92
+ creationState: order.creationState,
93
+ };
94
+ }
95
+ }
96
+ return {
97
+ id: order.id,
98
+ creationState: order.creationState,
99
+ };
100
+ });
101
+ }
102
+ /**
103
+ * Create an order with defaults for missing fields
104
+ *
105
+ * This helper generates reasonable defaults for dates, names, and other fields,
106
+ * making it easy to create orders for prototyping and development.
107
+ *
108
+ * @param client - PaidClient instance
109
+ * @param config - Order configuration (customerId, customerExternalId, agentExternalId required)
110
+ * @param options - Additional options for order creation
111
+ * @returns Created order with ID
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const order = await createOrderWithDefaults(client, {
116
+ * customerId: 'cus_123',
117
+ * customerExternalId: 'user-123',
118
+ * agentExternalId: 'agent-prod'
119
+ * });
120
+ * // Generates: startDate, endDate (1 year), name, description, currency (USD)
121
+ * ```
122
+ */
123
+ function createOrderWithDefaults(client_1, config_1) {
124
+ return __awaiter(this, arguments, void 0, function* (client, config, options = {}) {
125
+ const { autoActivate = true } = options;
126
+ const completeConfig = generateOrderDefaults(config, options);
127
+ return createAndActivateOrder(client, completeConfig, autoActivate);
128
+ });
129
+ }
130
+ /**
131
+ * Create an order with explicit configuration (NO defaults applied)
132
+ *
133
+ * Required fields: customerId, customerExternalId, agentExternalId, name, description,
134
+ * startDate, endDate, currency, orderLines
135
+ *
136
+ * Use createOrderWithDefaults() if you want automatic default values for missing fields.
137
+ *
138
+ * @param client - PaidClient instance
139
+ * @param config - Complete order configuration with ALL required fields
140
+ * @param options - Additional options for order creation
141
+ * @returns Created order with ID
142
+ * @throws Error if any required fields are missing
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * const order = await createOrder(client, {
147
+ * customerId: 'cus_123',
148
+ * customerExternalId: 'user-123',
149
+ * billingContactId: 'contact_123',
150
+ * agentExternalId: 'agent-prod',
151
+ * name: 'Pro Plan Subscription',
152
+ * description: 'Monthly subscription to Pro plan',
153
+ * startDate: '2024-01-01',
154
+ * endDate: '2025-01-01',
155
+ * currency: 'USD',
156
+ * orderLines: [{
157
+ * agentExternalId: 'agent-prod',
158
+ * name: 'Pro Plan',
159
+ * description: 'Pro plan features'
160
+ * }]
161
+ * }, { autoActivate: true });
162
+ * ```
163
+ */
164
+ function createOrder(client_1, config_1) {
165
+ return __awaiter(this, arguments, void 0, function* (client, config, options = {}) {
166
+ const { autoActivate = true } = options;
167
+ return createAndActivateOrder(client, config, autoActivate);
168
+ });
169
+ }
170
+ /**
171
+ * Create a framework-agnostic handler for order creation
172
+ *
173
+ * This handler can be used with any framework adapter.
174
+ *
175
+ * @param helperOptions - Default options for order creation
176
+ * @returns Handler function
177
+ */
178
+ function createOrdersHandler(helperOptions) {
179
+ return (0, base_handler_js_1.createHandler)((client, body) => __awaiter(this, void 0, void 0, function* () {
180
+ var _a, _b;
181
+ if (!body.customerId) {
182
+ throw new Error("customerId is required");
183
+ }
184
+ if (!body.customerExternalId) {
185
+ throw new Error("customerExternalId is required");
186
+ }
187
+ if (!body.agentExternalId) {
188
+ throw new Error("agentExternalId is required");
189
+ }
190
+ 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 });
191
+ const order = yield createOrderWithDefaults(client, body, orderOptions);
192
+ return { success: true, data: order };
193
+ }), { requiredFields: ['customerId', 'customerExternalId', 'agentExternalId'] });
194
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Provisioning helpers for Paid SDK
3
+ * Orchestrates customer + contact + order creation in one operation
4
+ */
5
+ import type { PaidClient } from "../../Client.js";
6
+ import type { OrderOptions, ProvisioningConfig, ProvisioningResult } from "../types.js";
7
+ /**
8
+ * Provision a new user with customer, contact, and optionally order
9
+ *
10
+ * This is a high-level helper that orchestrates the creation of all
11
+ * necessary Paid entities for a new user in one operation.
12
+ *
13
+ * @param client - PaidClient instance
14
+ * @param config - Provisioning configuration
15
+ * @returns Result with created customer, contact, and optional order
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const result = await provisionNewUser(client, {
20
+ * customer: {
21
+ * externalId: 'user-123',
22
+ * email: 'user@example.com'
23
+ * },
24
+ * agentExternalId: 'agent-prod'
25
+ * });
26
+ * // Creates customer, contact, and order with defaults
27
+ * ```
28
+ */
29
+ export declare function provisionNewUser(client: PaidClient, config: ProvisioningConfig): Promise<ProvisioningResult>;
30
+ /**
31
+ * Create a framework-agnostic handler for user provisioning
32
+ *
33
+ * This handler can be used with any framework adapter.
34
+ * Accepts partial provisioning configuration and fills in defaults for missing fields.
35
+ *
36
+ * @param orderOptions - Optional default order creation options
37
+ * @param defaultAgentExternalId - Optional default agent external ID
38
+ * @returns Handler function
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Minimal - just externalId and email
43
+ * {
44
+ * customer: { externalId: 'org-123', email: 'user@example.com' }
45
+ * }
46
+ *
47
+ * // With full customer data
48
+ * {
49
+ * customer: {
50
+ * externalId: 'org-123',
51
+ * email: 'user@example.com',
52
+ * name: 'Acme Corp',
53
+ * billingAddress: { line1: '123 Main St', city: 'NYC', ... }
54
+ * },
55
+ * contact: {
56
+ * firstName: 'John',
57
+ * lastName: 'Doe'
58
+ * }
59
+ * }
60
+ * ```
61
+ */
62
+ export declare function createProvisioningHandler(orderOptions?: OrderOptions, defaultAgentExternalId?: string): (request: import("../utils/base-handler.js").BaseRequestContext<Partial<ProvisioningConfig>, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;