@paid-ai/paid-node 0.4.1 → 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 (75) hide show
  1. package/dist/cjs/Client.js +2 -2
  2. package/dist/cjs/index.d.ts +1 -0
  3. package/dist/cjs/index.js +2 -1
  4. package/dist/cjs/integrations/adapters/nextjs/base-adapter.d.ts +10 -0
  5. package/dist/cjs/integrations/adapters/nextjs/base-adapter.js +58 -0
  6. package/dist/cjs/integrations/adapters/nextjs/index.d.ts +5 -0
  7. package/dist/cjs/integrations/adapters/nextjs/index.js +15 -0
  8. package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +174 -0
  9. package/dist/cjs/integrations/adapters/nextjs/routes.js +192 -0
  10. package/dist/cjs/integrations/client.d.ts +49 -0
  11. package/dist/cjs/integrations/client.js +93 -0
  12. package/dist/cjs/integrations/controllers/billing.d.ts +113 -0
  13. package/dist/cjs/integrations/controllers/billing.js +164 -0
  14. package/dist/cjs/integrations/controllers/contacts.d.ts +66 -0
  15. package/dist/cjs/integrations/controllers/contacts.js +145 -0
  16. package/dist/cjs/integrations/controllers/customers.d.ts +63 -0
  17. package/dist/cjs/integrations/controllers/customers.js +150 -0
  18. package/dist/cjs/integrations/controllers/index.d.ts +6 -0
  19. package/dist/cjs/integrations/controllers/index.js +26 -0
  20. package/dist/cjs/integrations/controllers/invoices.d.ts +40 -0
  21. package/dist/cjs/integrations/controllers/invoices.js +72 -0
  22. package/dist/cjs/integrations/controllers/orders.d.ts +72 -0
  23. package/dist/cjs/integrations/controllers/orders.js +194 -0
  24. package/dist/cjs/integrations/controllers/provision-users.d.ts +62 -0
  25. package/dist/cjs/integrations/controllers/provision-users.js +130 -0
  26. package/dist/cjs/integrations/index.d.ts +29 -0
  27. package/dist/cjs/integrations/index.js +44 -0
  28. package/dist/cjs/integrations/types.d.ts +119 -0
  29. package/dist/cjs/integrations/types.js +2 -0
  30. package/dist/cjs/integrations/utils/api-fetch.d.ts +18 -0
  31. package/dist/cjs/integrations/utils/api-fetch.js +53 -0
  32. package/dist/cjs/integrations/utils/base-handler.d.ts +79 -0
  33. package/dist/cjs/integrations/utils/base-handler.js +168 -0
  34. package/dist/cjs/integrations/utils/create-adapter.d.ts +29 -0
  35. package/dist/cjs/integrations/utils/create-adapter.js +56 -0
  36. package/dist/cjs/version.d.ts +1 -1
  37. package/dist/cjs/version.js +1 -1
  38. package/dist/esm/Client.mjs +2 -2
  39. package/dist/esm/index.d.mts +1 -0
  40. package/dist/esm/index.mjs +1 -0
  41. package/dist/esm/integrations/adapters/nextjs/base-adapter.d.mts +10 -0
  42. package/dist/esm/integrations/adapters/nextjs/base-adapter.mjs +55 -0
  43. package/dist/esm/integrations/adapters/nextjs/index.d.mts +5 -0
  44. package/dist/esm/integrations/adapters/nextjs/index.mjs +5 -0
  45. package/dist/esm/integrations/adapters/nextjs/routes.d.mts +174 -0
  46. package/dist/esm/integrations/adapters/nextjs/routes.mjs +183 -0
  47. package/dist/esm/integrations/client.d.mts +49 -0
  48. package/dist/esm/integrations/client.mjs +89 -0
  49. package/dist/esm/integrations/controllers/billing.d.mts +113 -0
  50. package/dist/esm/integrations/controllers/billing.mjs +158 -0
  51. package/dist/esm/integrations/controllers/contacts.d.mts +66 -0
  52. package/dist/esm/integrations/controllers/contacts.mjs +140 -0
  53. package/dist/esm/integrations/controllers/customers.d.mts +63 -0
  54. package/dist/esm/integrations/controllers/customers.mjs +145 -0
  55. package/dist/esm/integrations/controllers/index.d.mts +6 -0
  56. package/dist/esm/integrations/controllers/index.mjs +6 -0
  57. package/dist/esm/integrations/controllers/invoices.d.mts +40 -0
  58. package/dist/esm/integrations/controllers/invoices.mjs +68 -0
  59. package/dist/esm/integrations/controllers/orders.d.mts +72 -0
  60. package/dist/esm/integrations/controllers/orders.mjs +189 -0
  61. package/dist/esm/integrations/controllers/provision-users.d.mts +62 -0
  62. package/dist/esm/integrations/controllers/provision-users.mjs +126 -0
  63. package/dist/esm/integrations/index.d.mts +29 -0
  64. package/dist/esm/integrations/index.mjs +28 -0
  65. package/dist/esm/integrations/types.d.mts +119 -0
  66. package/dist/esm/integrations/types.mjs +1 -0
  67. package/dist/esm/integrations/utils/api-fetch.d.mts +18 -0
  68. package/dist/esm/integrations/utils/api-fetch.mjs +50 -0
  69. package/dist/esm/integrations/utils/base-handler.d.mts +79 -0
  70. package/dist/esm/integrations/utils/base-handler.mjs +164 -0
  71. package/dist/esm/integrations/utils/create-adapter.d.mts +29 -0
  72. package/dist/esm/integrations/utils/create-adapter.mjs +53 -0
  73. package/dist/esm/version.d.mts +1 -1
  74. package/dist/esm/version.mjs +1 -1
  75. package/package.json +32 -3
@@ -0,0 +1,113 @@
1
+ export interface PayInvoiceConfig {
2
+ apiUrl: string;
3
+ apiKey: string;
4
+ organizationId: string;
5
+ }
6
+ export interface PayInvoiceRequest {
7
+ invoiceId: string;
8
+ confirmationToken: string;
9
+ returnUrl?: string;
10
+ }
11
+ export interface PayInvoiceResult {
12
+ success: boolean;
13
+ data?: any;
14
+ }
15
+ /**
16
+ * Pay an invoice with a payment confirmation token
17
+ * This helper processes payment for an existing invoice using a Stripe confirmation token.
18
+ *
19
+ * @param config - API configuration with organization context
20
+ * @param request - Payment request details
21
+ * @returns Payment result
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const result = await payInvoice(
26
+ * {
27
+ * apiUrl: 'https://api.agentpaid.io',
28
+ * apiKey: process.env.PAID_API_KEY,
29
+ * organizationId: 'org_123'
30
+ * },
31
+ * {
32
+ * invoiceId: 'inv_123',
33
+ * confirmationToken: 'pm_tok_xxx',
34
+ * returnUrl: 'https://example.com/thanks'
35
+ * }
36
+ * );
37
+ * ```
38
+ */
39
+ export declare function payInvoice(config: PayInvoiceConfig, request: PayInvoiceRequest): Promise<PayInvoiceResult>;
40
+ interface PayInvoiceRequestWithBody {
41
+ invoiceId?: string;
42
+ confirmationToken: string;
43
+ returnUrl?: string;
44
+ }
45
+ /**
46
+ * Create a framework-agnostic handler for invoice payment
47
+ *
48
+ * This handler can be used with any framework adapter.
49
+ * Organization ID is automatically fetched from the API key.
50
+ *
51
+ * @returns Handler function
52
+ */
53
+ export declare function createPayInvoiceHandler(): (request: import("../utils/base-handler.mjs").BaseRequestContext<PayInvoiceRequestWithBody, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
54
+ export interface ActivateOrderSyncConfig {
55
+ apiUrl: string;
56
+ apiKey: string;
57
+ organizationId: string;
58
+ }
59
+ export interface ActivateOrderSyncRequest {
60
+ orderId: string;
61
+ confirmationToken: string;
62
+ returnUrl?: string;
63
+ }
64
+ export interface ActivateOrderSyncResult {
65
+ success: boolean;
66
+ order: any;
67
+ billing: any;
68
+ }
69
+ /**
70
+ * Activate an order synchronously with payment
71
+ *
72
+ * This operation activates an order and immediately charges the customer
73
+ * using the provided confirmation token (from Stripe or other payment provider).
74
+ *
75
+ * @param config - API configuration with organization context
76
+ * @param request - Activation request with order ID and confirmation token
77
+ * @returns Activation result with order and billing information
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const result = await activateOrderSync(
82
+ * {
83
+ * apiUrl: 'https://api.agentpaid.io',
84
+ * apiKey: process.env.PAID_API_KEY,
85
+ * organizationId: 'org_123'
86
+ * },
87
+ * {
88
+ * orderId: 'ord_123',
89
+ * confirmationToken: 'pi_123_secret_456',
90
+ * returnUrl: 'https://example.com/return'
91
+ * }
92
+ * );
93
+ * ```
94
+ */
95
+ export declare function activateOrderSync(config: ActivateOrderSyncConfig, request: ActivateOrderSyncRequest): Promise<ActivateOrderSyncResult>;
96
+ /**
97
+ * Internal request body type for the handler
98
+ */
99
+ interface ActivateOrderSyncRequestBody {
100
+ orderId?: string;
101
+ confirmationToken: string;
102
+ returnUrl?: string;
103
+ }
104
+ /**
105
+ * Create a framework-agnostic handler for synchronous order activation
106
+ *
107
+ * This handler can be used with any framework adapter.
108
+ *
109
+ * @param defaultReturnUrl - Optional default return URL
110
+ * @returns Handler function
111
+ */
112
+ export declare function createActivateOrderSyncHandler(defaultReturnUrl?: string): (request: import("../utils/base-handler.mjs").BaseRequestContext<ActivateOrderSyncRequestBody, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
113
+ export {};
@@ -0,0 +1,158 @@
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-sync`, {
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
+ }
@@ -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,63 @@
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>;
@@ -0,0 +1,145 @@
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
+ /**
12
+ * Generate placeholder values for missing customer fields
13
+ * Creates valid but fake data for rapid prototyping
14
+ */
15
+ function generateCustomerDefaults(data) {
16
+ var _a, _b, _c, _d, _e, _f;
17
+ const timestamp = Date.now();
18
+ const randomId = Math.random().toString(36).substring(7);
19
+ const email = data.email || `user_${timestamp}@example.com`;
20
+ const name = data.name || `User ${randomId}`;
21
+ const nameParts = name.split(" ");
22
+ const firstName = data.firstName || nameParts[0] || "Joe";
23
+ const lastName = data.lastName || nameParts.slice(1).join(" ") || "Bloggs";
24
+ const billingAddress = {
25
+ line1: ((_a = data.billingAddress) === null || _a === void 0 ? void 0 : _a.line1) || "123 Placeholder Street",
26
+ line2: ((_b = data.billingAddress) === null || _b === void 0 ? void 0 : _b.line2) || "",
27
+ city: ((_c = data.billingAddress) === null || _c === void 0 ? void 0 : _c.city) || "London",
28
+ state: ((_d = data.billingAddress) === null || _d === void 0 ? void 0 : _d.state) || "London",
29
+ zipCode: ((_e = data.billingAddress) === null || _e === void 0 ? void 0 : _e.zipCode) || "SW1A 1AA",
30
+ country: ((_f = data.billingAddress) === null || _f === void 0 ? void 0 : _f.country) || "UK",
31
+ };
32
+ return {
33
+ externalId: data.externalId,
34
+ email,
35
+ name,
36
+ firstName,
37
+ lastName,
38
+ company: data.company || "",
39
+ phone: data.phone || "",
40
+ billingAddress,
41
+ metadata: data.metadata || {},
42
+ };
43
+ }
44
+ /**
45
+ * Internal helper to create a customer
46
+ */
47
+ function createCustomerInternal(client, data) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const customer = yield client.customers.create({
50
+ externalId: data.externalId,
51
+ name: data.name,
52
+ billingAddress: {
53
+ line1: data.billingAddress.line1,
54
+ line2: data.billingAddress.line2,
55
+ city: data.billingAddress.city,
56
+ state: data.billingAddress.state,
57
+ zipCode: data.billingAddress.zipCode,
58
+ country: data.billingAddress.country,
59
+ },
60
+ });
61
+ if (!customer.id) {
62
+ throw new Error("Customer created but missing ID");
63
+ }
64
+ return {
65
+ id: customer.id,
66
+ externalId: data.externalId,
67
+ };
68
+ });
69
+ }
70
+ /**
71
+ * Create a customer with defaults for missing fields
72
+ *
73
+ * This helper generates reasonable placeholder values for any missing data,
74
+ * making it easy to create customers for prototyping and development.
75
+ *
76
+ * @param client - PaidClient instance
77
+ * @param data - Customer data (only externalId is required)
78
+ * @returns Created customer with ID
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * const customer = await createCustomerWithDefaults(client, {
83
+ * externalId: 'user-123',
84
+ * email: 'user@example.com'
85
+ * });
86
+ * // Generates: name, address, etc.
87
+ * ```
88
+ */
89
+ export function createCustomerWithDefaults(client, data) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const completeData = generateCustomerDefaults(data);
92
+ return createCustomerInternal(client, completeData);
93
+ });
94
+ }
95
+ /**
96
+ * Create a customer with explicit data (NO defaults applied)
97
+ *
98
+ * Required fields: externalId, name, email, and complete billingAddress
99
+ * (line1, line2, city, state, zipCode, country)
100
+ *
101
+ * Use createCustomerWithDefaults() if you want automatic default values for missing fields.
102
+ *
103
+ * @param client - PaidClient instance
104
+ * @param data - Complete customer data with ALL required fields
105
+ * @returns Created customer with ID
106
+ * @throws Error if any required fields are missing
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * const customer = await createCustomer(client, {
111
+ * externalId: 'user-123',
112
+ * email: 'user@example.com',
113
+ * name: 'John Doe',
114
+ * billingAddress: {
115
+ * line1: '123 Main St',
116
+ * line2: '',
117
+ * city: 'San Francisco',
118
+ * state: 'CA',
119
+ * zipCode: '94105',
120
+ * country: 'US'
121
+ * }
122
+ * });
123
+ * ```
124
+ */
125
+ export function createCustomer(client, data) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ return createCustomerInternal(client, data);
128
+ });
129
+ }
130
+ /**
131
+ * Create a framework-agnostic handler for customer creation
132
+ *
133
+ * This handler can be used with any framework adapter.
134
+ *
135
+ * @returns Handler function
136
+ */
137
+ export function createCustomersHandler() {
138
+ return createHandler((client, body) => __awaiter(this, void 0, void 0, function* () {
139
+ if (!body.externalId) {
140
+ throw new Error("externalId is required");
141
+ }
142
+ const customer = yield createCustomerWithDefaults(client, body);
143
+ return { success: true, data: customer };
144
+ }), { requiredFields: ['externalId'] });
145
+ }