@paid-ai/paid-node 0.4.1 → 0.5.0-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.
- package/dist/cjs/Client.js +2 -2
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/integrations/adapters/nextjs/base-adapter.d.ts +10 -0
- package/dist/cjs/integrations/adapters/nextjs/base-adapter.js +58 -0
- package/dist/cjs/integrations/adapters/nextjs/index.d.ts +5 -0
- package/dist/cjs/integrations/adapters/nextjs/index.js +17 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +225 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.js +246 -0
- package/dist/cjs/integrations/client.d.ts +49 -0
- package/dist/cjs/integrations/client.js +93 -0
- package/dist/cjs/integrations/controllers/billing.d.ts +196 -0
- package/dist/cjs/integrations/controllers/billing.js +267 -0
- package/dist/cjs/integrations/controllers/contacts.d.ts +66 -0
- package/dist/cjs/integrations/controllers/contacts.js +145 -0
- package/dist/cjs/integrations/controllers/customers.d.ts +96 -0
- package/dist/cjs/integrations/controllers/customers.js +206 -0
- package/dist/cjs/integrations/controllers/index.d.ts +6 -0
- package/dist/cjs/integrations/controllers/index.js +30 -0
- package/dist/cjs/integrations/controllers/invoices.d.ts +40 -0
- package/dist/cjs/integrations/controllers/invoices.js +72 -0
- package/dist/cjs/integrations/controllers/orders.d.ts +72 -0
- package/dist/cjs/integrations/controllers/orders.js +194 -0
- package/dist/cjs/integrations/controllers/provision-users.d.ts +62 -0
- package/dist/cjs/integrations/controllers/provision-users.js +130 -0
- package/dist/cjs/integrations/index.d.ts +29 -0
- package/dist/cjs/integrations/index.js +44 -0
- package/dist/cjs/integrations/types.d.ts +119 -0
- package/dist/cjs/integrations/types.js +2 -0
- package/dist/cjs/integrations/utils/api-fetch.d.ts +18 -0
- package/dist/cjs/integrations/utils/api-fetch.js +53 -0
- package/dist/cjs/integrations/utils/base-handler.d.ts +79 -0
- package/dist/cjs/integrations/utils/base-handler.js +168 -0
- package/dist/cjs/integrations/utils/create-adapter.d.ts +29 -0
- package/dist/cjs/integrations/utils/create-adapter.js +56 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/Client.mjs +2 -2
- package/dist/esm/index.d.mts +1 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/integrations/adapters/nextjs/base-adapter.d.mts +10 -0
- package/dist/esm/integrations/adapters/nextjs/base-adapter.mjs +55 -0
- package/dist/esm/integrations/adapters/nextjs/index.d.mts +5 -0
- package/dist/esm/integrations/adapters/nextjs/index.mjs +5 -0
- package/dist/esm/integrations/adapters/nextjs/routes.d.mts +225 -0
- package/dist/esm/integrations/adapters/nextjs/routes.mjs +235 -0
- package/dist/esm/integrations/client.d.mts +49 -0
- package/dist/esm/integrations/client.mjs +89 -0
- package/dist/esm/integrations/controllers/billing.d.mts +196 -0
- package/dist/esm/integrations/controllers/billing.mjs +259 -0
- package/dist/esm/integrations/controllers/contacts.d.mts +66 -0
- package/dist/esm/integrations/controllers/contacts.mjs +140 -0
- package/dist/esm/integrations/controllers/customers.d.mts +96 -0
- package/dist/esm/integrations/controllers/customers.mjs +199 -0
- package/dist/esm/integrations/controllers/index.d.mts +6 -0
- package/dist/esm/integrations/controllers/index.mjs +6 -0
- package/dist/esm/integrations/controllers/invoices.d.mts +40 -0
- package/dist/esm/integrations/controllers/invoices.mjs +68 -0
- package/dist/esm/integrations/controllers/orders.d.mts +72 -0
- package/dist/esm/integrations/controllers/orders.mjs +189 -0
- package/dist/esm/integrations/controllers/provision-users.d.mts +62 -0
- package/dist/esm/integrations/controllers/provision-users.mjs +126 -0
- package/dist/esm/integrations/index.d.mts +29 -0
- package/dist/esm/integrations/index.mjs +28 -0
- package/dist/esm/integrations/types.d.mts +119 -0
- package/dist/esm/integrations/types.mjs +1 -0
- package/dist/esm/integrations/utils/api-fetch.d.mts +18 -0
- package/dist/esm/integrations/utils/api-fetch.mjs +50 -0
- package/dist/esm/integrations/utils/base-handler.d.mts +79 -0
- package/dist/esm/integrations/utils/base-handler.mjs +164 -0
- package/dist/esm/integrations/utils/create-adapter.d.mts +29 -0
- package/dist/esm/integrations/utils/create-adapter.mjs +53 -0
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +32 -3
|
@@ -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>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Provisioning helpers for Paid SDK
|
|
4
|
+
* Orchestrates customer + contact + order creation in one operation
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.provisionNewUser = provisionNewUser;
|
|
17
|
+
exports.createProvisioningHandler = createProvisioningHandler;
|
|
18
|
+
const customers_js_1 = require("./customers.js");
|
|
19
|
+
const contacts_js_1 = require("./contacts.js");
|
|
20
|
+
const orders_js_1 = require("./orders.js");
|
|
21
|
+
const base_handler_js_1 = require("../utils/base-handler.js");
|
|
22
|
+
/**
|
|
23
|
+
* Provision a new user with customer, contact, and optionally order
|
|
24
|
+
*
|
|
25
|
+
* This is a high-level helper that orchestrates the creation of all
|
|
26
|
+
* necessary Paid entities for a new user in one operation.
|
|
27
|
+
*
|
|
28
|
+
* @param client - PaidClient instance
|
|
29
|
+
* @param config - Provisioning configuration
|
|
30
|
+
* @returns Result with created customer, contact, and optional order
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const result = await provisionNewUser(client, {
|
|
35
|
+
* customer: {
|
|
36
|
+
* externalId: 'user-123',
|
|
37
|
+
* email: 'user@example.com'
|
|
38
|
+
* },
|
|
39
|
+
* agentExternalId: 'agent-prod'
|
|
40
|
+
* });
|
|
41
|
+
* // Creates customer, contact, and order with defaults
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function provisionNewUser(client, config) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
var _a, _b, _c, _d, _e, _f;
|
|
47
|
+
const customer = yield (0, customers_js_1.createCustomerWithDefaults)(client, config.customer);
|
|
48
|
+
const contactData = {
|
|
49
|
+
customerExternalId: config.customer.externalId,
|
|
50
|
+
email: ((_a = config.contact) === null || _a === void 0 ? void 0 : _a.email) || config.customer.email,
|
|
51
|
+
firstName: (_b = config.contact) === null || _b === void 0 ? void 0 : _b.firstName,
|
|
52
|
+
lastName: (_c = config.contact) === null || _c === void 0 ? void 0 : _c.lastName,
|
|
53
|
+
phone: (_d = config.contact) === null || _d === void 0 ? void 0 : _d.phone,
|
|
54
|
+
billingAddress: (_e = config.contact) === null || _e === void 0 ? void 0 : _e.billingAddress,
|
|
55
|
+
salutation: (_f = config.contact) === null || _f === void 0 ? void 0 : _f.salutation,
|
|
56
|
+
};
|
|
57
|
+
const contact = yield (0, contacts_js_1.createContactWithDefaults)(client, contactData);
|
|
58
|
+
let order;
|
|
59
|
+
if (config.agentExternalId) {
|
|
60
|
+
order = yield (0, orders_js_1.createOrderWithDefaults)(client, {
|
|
61
|
+
customerId: customer.id,
|
|
62
|
+
customerExternalId: config.customer.externalId,
|
|
63
|
+
agentExternalId: config.agentExternalId,
|
|
64
|
+
billingContactId: contact.id,
|
|
65
|
+
}, config.orderOptions);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
customer,
|
|
69
|
+
contact,
|
|
70
|
+
order,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Create a framework-agnostic handler for user provisioning
|
|
76
|
+
*
|
|
77
|
+
* This handler can be used with any framework adapter.
|
|
78
|
+
* Accepts partial provisioning configuration and fills in defaults for missing fields.
|
|
79
|
+
*
|
|
80
|
+
* @param orderOptions - Optional default order creation options
|
|
81
|
+
* @param defaultAgentExternalId - Optional default agent external ID
|
|
82
|
+
* @returns Handler function
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* // Minimal - just externalId and email
|
|
87
|
+
* {
|
|
88
|
+
* customer: { externalId: 'org-123', email: 'user@example.com' }
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* // With full customer data
|
|
92
|
+
* {
|
|
93
|
+
* customer: {
|
|
94
|
+
* externalId: 'org-123',
|
|
95
|
+
* email: 'user@example.com',
|
|
96
|
+
* name: 'Acme Corp',
|
|
97
|
+
* billingAddress: { line1: '123 Main St', city: 'NYC', ... }
|
|
98
|
+
* },
|
|
99
|
+
* contact: {
|
|
100
|
+
* firstName: 'John',
|
|
101
|
+
* lastName: 'Doe'
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function createProvisioningHandler(orderOptions, defaultAgentExternalId) {
|
|
107
|
+
return (0, base_handler_js_1.createHandler)((client, body) => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
if (!body.customer) {
|
|
109
|
+
return {
|
|
110
|
+
success: false,
|
|
111
|
+
error: "'customer' is required",
|
|
112
|
+
status: 400,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const effectiveAgentId = body.agentExternalId ||
|
|
116
|
+
defaultAgentExternalId ||
|
|
117
|
+
process.env.NEXT_PUBLIC_PAID_AGENT_ID ||
|
|
118
|
+
process.env.PAID_AGENT_ID;
|
|
119
|
+
const result = yield provisionNewUser(client, {
|
|
120
|
+
customer: body.customer,
|
|
121
|
+
contact: body.contact,
|
|
122
|
+
agentExternalId: effectiveAgentId,
|
|
123
|
+
orderOptions: body.orderOptions || orderOptions,
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
success: true,
|
|
127
|
+
data: result,
|
|
128
|
+
};
|
|
129
|
+
}), { requiredFields: [] });
|
|
130
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paid SDK Integrations
|
|
3
|
+
*
|
|
4
|
+
* This module provides simplified helpers for common Paid operations
|
|
5
|
+
* with defaults and reduced boilerplate.
|
|
6
|
+
*
|
|
7
|
+
* @module integrations
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { PaidClient } from '@paid-ai/paid-node';
|
|
12
|
+
* import { createCustomerWithDefaults, createOrderWithDefaults } from '@paid-ai/paid-node/integrations';
|
|
13
|
+
*
|
|
14
|
+
* const client = new PaidClient({ token: 'pk_xxx' });
|
|
15
|
+
*
|
|
16
|
+
* const customer = await createCustomerWithDefaults(client, {
|
|
17
|
+
* externalId: 'user-123',
|
|
18
|
+
* email: 'user@example.com'
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const order = await createOrderWithDefaults(client, {
|
|
22
|
+
* customerId: customer.id,
|
|
23
|
+
* customerExternalId: 'user-123',
|
|
24
|
+
* agentExternalId: 'agent-prod'
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export * from "./controllers/index.js";
|
|
29
|
+
export type { Address, CustomerData, CompleteCustomerData, ContactData, OrderConfig, CompleteOrderConfig, OrderLineConfig, OrderOptions as HelperOptions, CustomerCreationResult, ContactCreationResult, OrderCreationResult, ProvisioningConfig, ProvisioningResult, } from "./types.js";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Paid SDK Integrations
|
|
4
|
+
*
|
|
5
|
+
* This module provides simplified helpers for common Paid operations
|
|
6
|
+
* with defaults and reduced boilerplate.
|
|
7
|
+
*
|
|
8
|
+
* @module integrations
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { PaidClient } from '@paid-ai/paid-node';
|
|
13
|
+
* import { createCustomerWithDefaults, createOrderWithDefaults } from '@paid-ai/paid-node/integrations';
|
|
14
|
+
*
|
|
15
|
+
* const client = new PaidClient({ token: 'pk_xxx' });
|
|
16
|
+
*
|
|
17
|
+
* const customer = await createCustomerWithDefaults(client, {
|
|
18
|
+
* externalId: 'user-123',
|
|
19
|
+
* email: 'user@example.com'
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const order = await createOrderWithDefaults(client, {
|
|
23
|
+
* customerId: customer.id,
|
|
24
|
+
* customerExternalId: 'user-123',
|
|
25
|
+
* agentExternalId: 'agent-prod'
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
30
|
+
if (k2 === undefined) k2 = k;
|
|
31
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
32
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
33
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
34
|
+
}
|
|
35
|
+
Object.defineProperty(o, k2, desc);
|
|
36
|
+
}) : (function(o, m, k, k2) {
|
|
37
|
+
if (k2 === undefined) k2 = k;
|
|
38
|
+
o[k2] = m[k];
|
|
39
|
+
}));
|
|
40
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
41
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
__exportStar(require("./controllers/index.js"), exports);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export interface Address {
|
|
2
|
+
line1?: string;
|
|
3
|
+
line2?: string;
|
|
4
|
+
city?: string;
|
|
5
|
+
state?: string;
|
|
6
|
+
zipCode?: string;
|
|
7
|
+
country?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface CustomerData {
|
|
10
|
+
externalId: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
firstName?: string;
|
|
14
|
+
lastName?: string;
|
|
15
|
+
company?: string;
|
|
16
|
+
phone?: string;
|
|
17
|
+
billingAddress?: Address;
|
|
18
|
+
metadata?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
export interface CompleteCustomerData {
|
|
21
|
+
externalId: string;
|
|
22
|
+
email: string;
|
|
23
|
+
name: string;
|
|
24
|
+
firstName: string;
|
|
25
|
+
lastName: string;
|
|
26
|
+
company: string;
|
|
27
|
+
phone: string;
|
|
28
|
+
billingAddress: Required<Address>;
|
|
29
|
+
metadata: Record<string, any>;
|
|
30
|
+
}
|
|
31
|
+
export interface ContactData {
|
|
32
|
+
customerExternalId: string;
|
|
33
|
+
email?: string;
|
|
34
|
+
firstName?: string;
|
|
35
|
+
lastName?: string;
|
|
36
|
+
phone?: string;
|
|
37
|
+
billingAddress?: Address;
|
|
38
|
+
salutation?: "Mr" | "Mrs" | "Ms" | "Dr" | "Prof";
|
|
39
|
+
}
|
|
40
|
+
export interface OrderConfig {
|
|
41
|
+
customerId: string;
|
|
42
|
+
customerExternalId: string;
|
|
43
|
+
billingContactId?: string;
|
|
44
|
+
agentExternalId: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
startDate?: string;
|
|
48
|
+
endDate?: string;
|
|
49
|
+
currency?: string;
|
|
50
|
+
orderLines?: OrderLineConfig[];
|
|
51
|
+
}
|
|
52
|
+
export interface OrderLineConfig {
|
|
53
|
+
agentExternalId: string;
|
|
54
|
+
name?: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Note: orderLines is not required because if agentExternalId is provided,
|
|
59
|
+
* order lines can be derived from that agent.
|
|
60
|
+
*/
|
|
61
|
+
export interface CompleteOrderConfig {
|
|
62
|
+
customerId: string;
|
|
63
|
+
customerExternalId: string;
|
|
64
|
+
billingContactId?: string;
|
|
65
|
+
agentExternalId: string;
|
|
66
|
+
name: string;
|
|
67
|
+
description: string;
|
|
68
|
+
startDate: string;
|
|
69
|
+
endDate: string;
|
|
70
|
+
currency: string;
|
|
71
|
+
orderLines: OrderLineConfig[];
|
|
72
|
+
}
|
|
73
|
+
export interface OrderOptions {
|
|
74
|
+
/**
|
|
75
|
+
* Whether to auto-activate orders after creation
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
autoActivate?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Duration in days for order end date (if not specified)
|
|
81
|
+
* @default 365
|
|
82
|
+
*/
|
|
83
|
+
defaultDurationDays?: number;
|
|
84
|
+
}
|
|
85
|
+
export interface CustomerCreationResult {
|
|
86
|
+
id: string;
|
|
87
|
+
externalId: string;
|
|
88
|
+
}
|
|
89
|
+
export interface ContactCreationResult {
|
|
90
|
+
id: string;
|
|
91
|
+
}
|
|
92
|
+
export interface OrderCreationResult {
|
|
93
|
+
id: string;
|
|
94
|
+
creationState?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface ProvisioningConfig {
|
|
97
|
+
/**
|
|
98
|
+
* Customer data (externalId required)
|
|
99
|
+
*/
|
|
100
|
+
customer: CustomerData;
|
|
101
|
+
/**
|
|
102
|
+
* Contact data (optional, will use customer email if not provided)
|
|
103
|
+
*/
|
|
104
|
+
contact?: Partial<ContactData>;
|
|
105
|
+
/**
|
|
106
|
+
* Agent external ID for order creation (optional)
|
|
107
|
+
* If not provided, no order will be created
|
|
108
|
+
*/
|
|
109
|
+
agentExternalId?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Options for order creation
|
|
112
|
+
*/
|
|
113
|
+
orderOptions?: OrderOptions;
|
|
114
|
+
}
|
|
115
|
+
export interface ProvisioningResult {
|
|
116
|
+
customer: CustomerCreationResult;
|
|
117
|
+
contact: ContactCreationResult;
|
|
118
|
+
order?: OrderCreationResult;
|
|
119
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface PaidApiConfig {
|
|
2
|
+
apiUrl: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
organizationId: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Make a direct API call to ap-signals
|
|
8
|
+
*
|
|
9
|
+
* @param config - API configuration with organization context
|
|
10
|
+
* @param endpoint - API endpoint path (will be appended to apiUrl)
|
|
11
|
+
* @param options - Fetch options (method, body)
|
|
12
|
+
* @returns Parsed JSON response
|
|
13
|
+
* @throws Error if request fails
|
|
14
|
+
*/
|
|
15
|
+
export declare function paidApiFetch<T = any>(config: PaidApiConfig, endpoint: string, options?: {
|
|
16
|
+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
17
|
+
body?: any;
|
|
18
|
+
}): Promise<T>;
|
|
@@ -0,0 +1,53 @@
|
|
|
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.paidApiFetch = paidApiFetch;
|
|
13
|
+
/**
|
|
14
|
+
* Make a direct API call to ap-signals
|
|
15
|
+
*
|
|
16
|
+
* @param config - API configuration with organization context
|
|
17
|
+
* @param endpoint - API endpoint path (will be appended to apiUrl)
|
|
18
|
+
* @param options - Fetch options (method, body)
|
|
19
|
+
* @returns Parsed JSON response
|
|
20
|
+
* @throws Error if request fails
|
|
21
|
+
*/
|
|
22
|
+
function paidApiFetch(config, endpoint, options) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
if (!config.organizationId) {
|
|
25
|
+
throw new Error("organizationId is required");
|
|
26
|
+
}
|
|
27
|
+
if (!config.apiKey) {
|
|
28
|
+
throw new Error("apiKey is required");
|
|
29
|
+
}
|
|
30
|
+
if (!config.apiUrl) {
|
|
31
|
+
throw new Error("apiUrl is required");
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const response = yield fetch(`${config.apiUrl}${endpoint}`, {
|
|
35
|
+
method: (options === null || options === void 0 ? void 0 : options.method) || "GET",
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
},
|
|
40
|
+
body: (options === null || options === void 0 ? void 0 : options.body) ? JSON.stringify(options.body) : undefined,
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const errorText = yield response.text();
|
|
44
|
+
throw new Error(`API request failed: ${errorText}`);
|
|
45
|
+
}
|
|
46
|
+
return yield response.json();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.error("Error making Paid API request:", error);
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|