@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,259 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { paidApiFetch } from "../utils/api-fetch.mjs";
|
|
11
|
+
import { createHandler } from "../utils/base-handler.mjs";
|
|
12
|
+
/**
|
|
13
|
+
* Pay an invoice with a payment confirmation token
|
|
14
|
+
* This helper processes payment for an existing invoice using a Stripe confirmation token.
|
|
15
|
+
*
|
|
16
|
+
* @param config - API configuration with organization context
|
|
17
|
+
* @param request - Payment request details
|
|
18
|
+
* @returns Payment result
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const result = await payInvoice(
|
|
23
|
+
* {
|
|
24
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
25
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
26
|
+
* organizationId: 'org_123'
|
|
27
|
+
* },
|
|
28
|
+
* {
|
|
29
|
+
* invoiceId: 'inv_123',
|
|
30
|
+
* confirmationToken: 'pm_tok_xxx',
|
|
31
|
+
* returnUrl: 'https://example.com/thanks'
|
|
32
|
+
* }
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function payInvoice(config, request) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const { invoiceId, confirmationToken, returnUrl } = request;
|
|
39
|
+
if (!invoiceId || !confirmationToken) {
|
|
40
|
+
throw new Error("invoiceId and confirmationToken are required");
|
|
41
|
+
}
|
|
42
|
+
const data = yield paidApiFetch(config, `/api/organizations/${config.organizationId}/invoices/${invoiceId}/pay`, {
|
|
43
|
+
method: "PUT",
|
|
44
|
+
body: Object.assign({ confirmationToken }, (returnUrl && { returnUrl })),
|
|
45
|
+
});
|
|
46
|
+
return { success: true, data };
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create a framework-agnostic handler for invoice payment
|
|
51
|
+
*
|
|
52
|
+
* This handler can be used with any framework adapter.
|
|
53
|
+
* Organization ID is automatically fetched from the API key.
|
|
54
|
+
*
|
|
55
|
+
* @returns Handler function
|
|
56
|
+
*/
|
|
57
|
+
export function createPayInvoiceHandler() {
|
|
58
|
+
return createHandler((_client, body, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (!organizationId) {
|
|
60
|
+
return { success: false, error: 'Organization ID not found', status: 500 };
|
|
61
|
+
}
|
|
62
|
+
const invoiceId = (params === null || params === void 0 ? void 0 : params.invoiceId) || body.invoiceId;
|
|
63
|
+
const { confirmationToken, returnUrl } = body;
|
|
64
|
+
if (!invoiceId) {
|
|
65
|
+
return { success: false, error: 'invoiceId is required', status: 400 };
|
|
66
|
+
}
|
|
67
|
+
if (!confirmationToken) {
|
|
68
|
+
return { success: false, error: 'confirmationToken is required', status: 400 };
|
|
69
|
+
}
|
|
70
|
+
const apiKey = process.env.PAID_API_KEY || '';
|
|
71
|
+
const apiUrl = process.env.PAID_API_URL || 'https://api.agentpaid.io';
|
|
72
|
+
const result = yield payInvoice({ apiUrl, apiKey, organizationId }, { invoiceId, confirmationToken, returnUrl });
|
|
73
|
+
return {
|
|
74
|
+
success: true,
|
|
75
|
+
data: result.data,
|
|
76
|
+
};
|
|
77
|
+
}), { requiredFields: ['confirmationToken'], requireOrganizationId: true });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Activate an order synchronously with payment
|
|
81
|
+
*
|
|
82
|
+
* This operation activates an order and immediately charges the customer
|
|
83
|
+
* using the provided confirmation token (from Stripe or other payment provider).
|
|
84
|
+
*
|
|
85
|
+
* @param config - API configuration with organization context
|
|
86
|
+
* @param request - Activation request with order ID and confirmation token
|
|
87
|
+
* @returns Activation result with order and billing information
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const result = await activateOrderSync(
|
|
92
|
+
* {
|
|
93
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
94
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
95
|
+
* organizationId: 'org_123'
|
|
96
|
+
* },
|
|
97
|
+
* {
|
|
98
|
+
* orderId: 'ord_123',
|
|
99
|
+
* confirmationToken: 'pi_123_secret_456',
|
|
100
|
+
* returnUrl: 'https://example.com/return'
|
|
101
|
+
* }
|
|
102
|
+
* );
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function activateOrderSync(config, request) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const { orderId, confirmationToken, returnUrl } = request;
|
|
108
|
+
if (!orderId) {
|
|
109
|
+
throw new Error("orderId is required");
|
|
110
|
+
}
|
|
111
|
+
if (!confirmationToken) {
|
|
112
|
+
throw new Error("confirmationToken is required");
|
|
113
|
+
}
|
|
114
|
+
const data = yield paidApiFetch(config, `/api/organizations/${config.organizationId}/orders/${orderId}/activate-and-pay`, {
|
|
115
|
+
method: "POST",
|
|
116
|
+
body: Object.assign({ confirmationToken }, (returnUrl && { returnUrl })),
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
success: true,
|
|
120
|
+
order: data.order,
|
|
121
|
+
billing: data.billing,
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create a framework-agnostic handler for synchronous order activation
|
|
127
|
+
*
|
|
128
|
+
* This handler can be used with any framework adapter.
|
|
129
|
+
*
|
|
130
|
+
* @param defaultReturnUrl - Optional default return URL
|
|
131
|
+
* @returns Handler function
|
|
132
|
+
*/
|
|
133
|
+
export function createActivateOrderSyncHandler(defaultReturnUrl) {
|
|
134
|
+
return createHandler((client, body, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
var _a, _b;
|
|
136
|
+
const apiKey = (_a = client._options) === null || _a === void 0 ? void 0 : _a.token;
|
|
137
|
+
const baseUrl = ((_b = client._options) === null || _b === void 0 ? void 0 : _b.baseUrl) || 'https://api.agentpaid.io/api/v1';
|
|
138
|
+
const apiUrl = baseUrl.replace('/api/v1', '');
|
|
139
|
+
const orderId = (params === null || params === void 0 ? void 0 : params.orderId) || body.orderId;
|
|
140
|
+
const result = yield activateOrderSync({ apiUrl, apiKey, organizationId: organizationId }, {
|
|
141
|
+
orderId: orderId,
|
|
142
|
+
confirmationToken: body.confirmationToken,
|
|
143
|
+
returnUrl: body.returnUrl || defaultReturnUrl,
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
success: true,
|
|
147
|
+
data: {
|
|
148
|
+
success: true,
|
|
149
|
+
order: result.order,
|
|
150
|
+
billing: result.billing,
|
|
151
|
+
message: 'Order activated and charged successfully',
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}), {
|
|
155
|
+
requiredFields: ['confirmationToken'],
|
|
156
|
+
requireOrganizationId: true,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Create a setup intent for adding payment methods without immediate charge
|
|
161
|
+
*
|
|
162
|
+
* Setup intents are used to save payment methods for future use without
|
|
163
|
+
* charging the customer immediately. This is useful for:
|
|
164
|
+
* - Adding payment methods for subscription billing
|
|
165
|
+
* - Updating payment methods
|
|
166
|
+
* - Pre-authorizing payment methods
|
|
167
|
+
*
|
|
168
|
+
* @param config - API configuration with organization context
|
|
169
|
+
* @param request - Setup intent request with customer ID and confirmation token
|
|
170
|
+
* @returns Setup intent result with payment method details
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const result = await createSetupIntent(
|
|
175
|
+
* {
|
|
176
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
177
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
178
|
+
* organizationId: 'org_123'
|
|
179
|
+
* },
|
|
180
|
+
* {
|
|
181
|
+
* customerId: 'cus_123',
|
|
182
|
+
* confirmationToken: 'seti_123_secret_456',
|
|
183
|
+
* returnUrl: 'https://example.com/return',
|
|
184
|
+
* metadata: { source: 'web' }
|
|
185
|
+
* }
|
|
186
|
+
* );
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export function createSetupIntent(config, request) {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
191
|
+
const { customerId, confirmationToken, returnUrl, metadata } = request;
|
|
192
|
+
if (!customerId) {
|
|
193
|
+
throw new Error("customerId is required");
|
|
194
|
+
}
|
|
195
|
+
if (!confirmationToken) {
|
|
196
|
+
throw new Error("confirmationToken is required");
|
|
197
|
+
}
|
|
198
|
+
const data = yield paidApiFetch(config, `/api/organizations/${config.organizationId}/payments/setup-intents-external`, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
body: Object.assign(Object.assign({ customerId,
|
|
201
|
+
confirmationToken }, (returnUrl && { returnUrl })), (metadata && { metadata })),
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
setupIntent: data.setupIntent || data,
|
|
205
|
+
message: "Setup intent created successfully",
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Create a framework-agnostic handler for setup intent creation
|
|
211
|
+
*
|
|
212
|
+
* This handler can be used with any framework adapter.
|
|
213
|
+
*
|
|
214
|
+
* @param defaultReturnUrl - Optional default return URL
|
|
215
|
+
* @returns Handler function
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* // Next.js API route
|
|
220
|
+
* import { createSetupIntentHandler } from '@paid-ai/paid-node/integrations';
|
|
221
|
+
*
|
|
222
|
+
* const handler = createSetupIntentHandler('https://myapp.com/billing');
|
|
223
|
+
*
|
|
224
|
+
* export async function POST(req: Request) {
|
|
225
|
+
* return handler(
|
|
226
|
+
* {
|
|
227
|
+
* body: await req.json(),
|
|
228
|
+
* headers: Object.fromEntries(req.headers),
|
|
229
|
+
* method: 'POST'
|
|
230
|
+
* },
|
|
231
|
+
* {
|
|
232
|
+
* json: (data, status = 200) => Response.json(data, { status }),
|
|
233
|
+
* error: (message, status) => Response.json({ error: message }, { status })
|
|
234
|
+
* }
|
|
235
|
+
* );
|
|
236
|
+
* }
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export function createSetupIntentHandler(defaultReturnUrl) {
|
|
240
|
+
return createHandler((client, body, _params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
241
|
+
var _a, _b;
|
|
242
|
+
const apiKey = (_a = client._options) === null || _a === void 0 ? void 0 : _a.token;
|
|
243
|
+
const baseUrl = ((_b = client._options) === null || _b === void 0 ? void 0 : _b.baseUrl) || 'https://api.agentpaid.io/api/v1';
|
|
244
|
+
const apiUrl = baseUrl.replace('/api/v1', '');
|
|
245
|
+
const result = yield createSetupIntent({ apiUrl, apiKey, organizationId: organizationId }, {
|
|
246
|
+
customerId: body.customerId,
|
|
247
|
+
confirmationToken: body.confirmationToken,
|
|
248
|
+
returnUrl: body.returnUrl || defaultReturnUrl,
|
|
249
|
+
metadata: body.metadata,
|
|
250
|
+
});
|
|
251
|
+
return {
|
|
252
|
+
success: true,
|
|
253
|
+
data: result,
|
|
254
|
+
};
|
|
255
|
+
}), {
|
|
256
|
+
requiredFields: ['customerId', 'confirmationToken'],
|
|
257
|
+
requireOrganizationId: true,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { PaidClient } from "../../Client.mjs";
|
|
2
|
+
import type { ContactData, ContactCreationResult, Address } from "../types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Create a contact with defaults for missing fields
|
|
5
|
+
*
|
|
6
|
+
* This helper generates reasonable placeholder values for any missing data,
|
|
7
|
+
* making it easy to create contacts for prototyping and development.
|
|
8
|
+
*
|
|
9
|
+
* @param client - PaidClient instance
|
|
10
|
+
* @param data - Contact data (only customerExternalId is required)
|
|
11
|
+
* @returns Created contact with ID
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const contact = await createContactWithDefaults(client, {
|
|
16
|
+
* customerExternalId: 'user-123',
|
|
17
|
+
* email: 'user@example.com'
|
|
18
|
+
* });
|
|
19
|
+
* // Generates: firstName, lastName, address, etc.
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function createContactWithDefaults(client: PaidClient, data: ContactData): Promise<ContactCreationResult>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a contact with explicit data (NO defaults applied)
|
|
25
|
+
*
|
|
26
|
+
* Required fields: customerExternalId, email, firstName, lastName, phone, salutation,
|
|
27
|
+
* and complete billingAddress (line1, line2, city, state, zipCode, country)
|
|
28
|
+
*
|
|
29
|
+
* Use createContactWithDefaults() if you want automatic default values for missing fields.
|
|
30
|
+
*
|
|
31
|
+
* @param client - PaidClient instance
|
|
32
|
+
* @param data - Complete contact data with ALL required fields
|
|
33
|
+
* @returns Created contact with ID
|
|
34
|
+
* @throws Error if any required fields are missing
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const contact = await createContact(client, {
|
|
39
|
+
* customerExternalId: 'user-123',
|
|
40
|
+
* email: 'john@example.com',
|
|
41
|
+
* firstName: 'John',
|
|
42
|
+
* lastName: 'Doe',
|
|
43
|
+
* phone: '+1234567890',
|
|
44
|
+
* salutation: 'Mr',
|
|
45
|
+
* billingAddress: {
|
|
46
|
+
* line1: '123 Main St',
|
|
47
|
+
* line2: 'Suite 100',
|
|
48
|
+
* city: 'San Francisco',
|
|
49
|
+
* state: 'CA',
|
|
50
|
+
* zipCode: '94105',
|
|
51
|
+
* country: 'US'
|
|
52
|
+
* }
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function createContact(client: PaidClient, data: Required<Omit<ContactData, 'billingAddress'>> & {
|
|
57
|
+
billingAddress: Required<Address>;
|
|
58
|
+
}): Promise<ContactCreationResult>;
|
|
59
|
+
/**
|
|
60
|
+
* Create a framework-agnostic handler for contact creation
|
|
61
|
+
*
|
|
62
|
+
* This handler can be used with any framework adapter.
|
|
63
|
+
*
|
|
64
|
+
* @returns Handler function
|
|
65
|
+
*/
|
|
66
|
+
export declare function createContactsHandler(): (request: import("../utils/base-handler.mjs").BaseRequestContext<ContactData, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { createHandler } from "../utils/base-handler.mjs";
|
|
11
|
+
function generateContactDefaults(data) {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f;
|
|
13
|
+
const timestamp = Date.now();
|
|
14
|
+
const email = data.email || `contact_${timestamp}@example.com`;
|
|
15
|
+
const firstName = data.firstName || "Joe";
|
|
16
|
+
const lastName = data.lastName || "Bloggs";
|
|
17
|
+
const phone = data.phone || "";
|
|
18
|
+
const salutation = data.salutation || "Mr";
|
|
19
|
+
const billingAddress = {
|
|
20
|
+
line1: ((_a = data.billingAddress) === null || _a === void 0 ? void 0 : _a.line1) || "123 Placeholder Street",
|
|
21
|
+
line2: ((_b = data.billingAddress) === null || _b === void 0 ? void 0 : _b.line2) || "",
|
|
22
|
+
city: ((_c = data.billingAddress) === null || _c === void 0 ? void 0 : _c.city) || "London",
|
|
23
|
+
state: ((_d = data.billingAddress) === null || _d === void 0 ? void 0 : _d.state) || "London",
|
|
24
|
+
zipCode: ((_e = data.billingAddress) === null || _e === void 0 ? void 0 : _e.zipCode) || "SW1A 1AA",
|
|
25
|
+
country: ((_f = data.billingAddress) === null || _f === void 0 ? void 0 : _f.country) || "UK",
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
customerExternalId: data.customerExternalId,
|
|
29
|
+
email,
|
|
30
|
+
firstName,
|
|
31
|
+
lastName,
|
|
32
|
+
phone,
|
|
33
|
+
salutation,
|
|
34
|
+
billingAddress,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Internal helper to create a contact
|
|
39
|
+
*/
|
|
40
|
+
function createContactInternal(client, data) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const contact = yield client.contacts.create({
|
|
43
|
+
customerExternalId: data.customerExternalId,
|
|
44
|
+
salutation: data.salutation,
|
|
45
|
+
firstName: data.firstName,
|
|
46
|
+
lastName: data.lastName,
|
|
47
|
+
email: data.email,
|
|
48
|
+
phone: data.phone,
|
|
49
|
+
billingStreet: data.billingAddress.line1,
|
|
50
|
+
billingCity: data.billingAddress.city,
|
|
51
|
+
billingCountry: data.billingAddress.country,
|
|
52
|
+
billingPostalCode: data.billingAddress.zipCode,
|
|
53
|
+
});
|
|
54
|
+
if (!contact.id) {
|
|
55
|
+
throw new Error("Contact created but missing ID");
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
id: contact.id,
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a contact with defaults for missing fields
|
|
64
|
+
*
|
|
65
|
+
* This helper generates reasonable placeholder values for any missing data,
|
|
66
|
+
* making it easy to create contacts for prototyping and development.
|
|
67
|
+
*
|
|
68
|
+
* @param client - PaidClient instance
|
|
69
|
+
* @param data - Contact data (only customerExternalId is required)
|
|
70
|
+
* @returns Created contact with ID
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const contact = await createContactWithDefaults(client, {
|
|
75
|
+
* customerExternalId: 'user-123',
|
|
76
|
+
* email: 'user@example.com'
|
|
77
|
+
* });
|
|
78
|
+
* // Generates: firstName, lastName, address, etc.
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function createContactWithDefaults(client, data) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
if (!data.customerExternalId) {
|
|
84
|
+
throw new Error("customerExternalId is required");
|
|
85
|
+
}
|
|
86
|
+
const completeData = generateContactDefaults(data);
|
|
87
|
+
return createContactInternal(client, completeData);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Create a contact with explicit data (NO defaults applied)
|
|
92
|
+
*
|
|
93
|
+
* Required fields: customerExternalId, email, firstName, lastName, phone, salutation,
|
|
94
|
+
* and complete billingAddress (line1, line2, city, state, zipCode, country)
|
|
95
|
+
*
|
|
96
|
+
* Use createContactWithDefaults() if you want automatic default values for missing fields.
|
|
97
|
+
*
|
|
98
|
+
* @param client - PaidClient instance
|
|
99
|
+
* @param data - Complete contact data with ALL required fields
|
|
100
|
+
* @returns Created contact with ID
|
|
101
|
+
* @throws Error if any required fields are missing
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const contact = await createContact(client, {
|
|
106
|
+
* customerExternalId: 'user-123',
|
|
107
|
+
* email: 'john@example.com',
|
|
108
|
+
* firstName: 'John',
|
|
109
|
+
* lastName: 'Doe',
|
|
110
|
+
* phone: '+1234567890',
|
|
111
|
+
* salutation: 'Mr',
|
|
112
|
+
* billingAddress: {
|
|
113
|
+
* line1: '123 Main St',
|
|
114
|
+
* line2: 'Suite 100',
|
|
115
|
+
* city: 'San Francisco',
|
|
116
|
+
* state: 'CA',
|
|
117
|
+
* zipCode: '94105',
|
|
118
|
+
* country: 'US'
|
|
119
|
+
* }
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export function createContact(client, data) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
return createContactInternal(client, data);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Create a framework-agnostic handler for contact creation
|
|
130
|
+
*
|
|
131
|
+
* This handler can be used with any framework adapter.
|
|
132
|
+
*
|
|
133
|
+
* @returns Handler function
|
|
134
|
+
*/
|
|
135
|
+
export function createContactsHandler() {
|
|
136
|
+
return createHandler((client, body) => __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
const contact = yield createContactWithDefaults(client, body);
|
|
138
|
+
return { success: true, data: contact };
|
|
139
|
+
}), { requiredFields: ['customerExternalId'] });
|
|
140
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { PaidClient } from "../../Client.mjs";
|
|
2
|
+
import type { CustomerData, CustomerCreationResult, Address } from "../types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Create a customer with defaults for missing fields
|
|
5
|
+
*
|
|
6
|
+
* This helper generates reasonable placeholder values for any missing data,
|
|
7
|
+
* making it easy to create customers for prototyping and development.
|
|
8
|
+
*
|
|
9
|
+
* @param client - PaidClient instance
|
|
10
|
+
* @param data - Customer data (only externalId is required)
|
|
11
|
+
* @returns Created customer with ID
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const customer = await createCustomerWithDefaults(client, {
|
|
16
|
+
* externalId: 'user-123',
|
|
17
|
+
* email: 'user@example.com'
|
|
18
|
+
* });
|
|
19
|
+
* // Generates: name, address, etc.
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function createCustomerWithDefaults(client: PaidClient, data: CustomerData): Promise<CustomerCreationResult>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a customer with explicit data (NO defaults applied)
|
|
25
|
+
*
|
|
26
|
+
* Required fields: externalId, name, email, and complete billingAddress
|
|
27
|
+
* (line1, line2, city, state, zipCode, country)
|
|
28
|
+
*
|
|
29
|
+
* Use createCustomerWithDefaults() if you want automatic default values for missing fields.
|
|
30
|
+
*
|
|
31
|
+
* @param client - PaidClient instance
|
|
32
|
+
* @param data - Complete customer data with ALL required fields
|
|
33
|
+
* @returns Created customer with ID
|
|
34
|
+
* @throws Error if any required fields are missing
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const customer = await createCustomer(client, {
|
|
39
|
+
* externalId: 'user-123',
|
|
40
|
+
* email: 'user@example.com',
|
|
41
|
+
* name: 'John Doe',
|
|
42
|
+
* billingAddress: {
|
|
43
|
+
* line1: '123 Main St',
|
|
44
|
+
* line2: '',
|
|
45
|
+
* city: 'San Francisco',
|
|
46
|
+
* state: 'CA',
|
|
47
|
+
* zipCode: '94105',
|
|
48
|
+
* country: 'US'
|
|
49
|
+
* }
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function createCustomer(client: PaidClient, data: Required<Omit<CustomerData, 'firstName' | 'lastName' | 'company' | 'phone' | 'metadata' | 'billingAddress'>> & {
|
|
54
|
+
billingAddress: Required<Address>;
|
|
55
|
+
}): Promise<CustomerCreationResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Create a framework-agnostic handler for customer creation
|
|
58
|
+
*
|
|
59
|
+
* This handler can be used with any framework adapter.
|
|
60
|
+
*
|
|
61
|
+
* @returns Handler function
|
|
62
|
+
*/
|
|
63
|
+
export declare function createCustomersHandler(): (request: import("../utils/base-handler.mjs").BaseRequestContext<CustomerData, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
|
|
64
|
+
/**
|
|
65
|
+
* Get a customer by external ID
|
|
66
|
+
*
|
|
67
|
+
* @param client - PaidClient instance
|
|
68
|
+
* @param customerExternalId - Customer's external ID
|
|
69
|
+
* @returns Customer data
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const customer = await getCustomer(client, 'user-123');
|
|
74
|
+
* console.log(customer.name, customer.email);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function getCustomer(client: PaidClient, customerExternalId: string): Promise<any>;
|
|
78
|
+
/**
|
|
79
|
+
* Create a framework-agnostic handler for fetching a customer by external ID
|
|
80
|
+
*
|
|
81
|
+
* This handler can be used with any framework adapter.
|
|
82
|
+
* The customer external ID should be provided either in params or query string.
|
|
83
|
+
*
|
|
84
|
+
* @returns Handler function
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* // In Next.js route: /api/customers/[customerExternalId]/route.ts
|
|
89
|
+
* import { createGetCustomerHandler } from '@paid-ai/paid-node/integrations';
|
|
90
|
+
* import { nextjsAdapter } from '@paid-ai/paid-node/integrations/nextjs';
|
|
91
|
+
*
|
|
92
|
+
* const handler = createGetCustomerHandler();
|
|
93
|
+
* export const GET = nextjsAdapter(handler);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function createGetCustomerHandler(): (request: import("../utils/base-handler.mjs").BaseRequestContext<any, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
|