@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,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Shared Paid client initialization utility
|
|
4
|
+
* Framework-agnostic
|
|
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.initializePaidClient = initializePaidClient;
|
|
17
|
+
exports.getOrganizationId = getOrganizationId;
|
|
18
|
+
const Client_js_1 = require("../Client.js");
|
|
19
|
+
/**
|
|
20
|
+
* Initialize a Paid client with environment variable fallbacks
|
|
21
|
+
*
|
|
22
|
+
* @param config - Optional configuration overrides
|
|
23
|
+
* @returns Configured PaidClient instance
|
|
24
|
+
* @throws Error if API key is not provided or found in environment
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const client = initializePaidClient();
|
|
29
|
+
* // Uses process.env.PAID_API_KEY
|
|
30
|
+
*
|
|
31
|
+
* const client = initializePaidClient({ apiKey: 'pk_xxx' });
|
|
32
|
+
* // Uses provided key
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function initializePaidClient(config = {}) {
|
|
36
|
+
const apiKey = config.apiKey || process.env.PAID_API_KEY;
|
|
37
|
+
if (!apiKey) {
|
|
38
|
+
throw new Error('PAID_API_KEY not configured. Set process.env.PAID_API_KEY or pass apiKey in config.');
|
|
39
|
+
}
|
|
40
|
+
return new Client_js_1.PaidClient({
|
|
41
|
+
token: apiKey,
|
|
42
|
+
baseUrl: config.apiUrl
|
|
43
|
+
? `${config.apiUrl}/api/v1`
|
|
44
|
+
: process.env.PAID_API_URL
|
|
45
|
+
? `${process.env.PAID_API_URL}/api/v1`
|
|
46
|
+
: undefined,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get organization ID from API key
|
|
51
|
+
*
|
|
52
|
+
* Calls the Paid API to resolve the organization ID associated with the API key.
|
|
53
|
+
*
|
|
54
|
+
* @param config - API configuration
|
|
55
|
+
* @returns Organization ID or null if not found
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const orgId = await getOrganizationId({
|
|
60
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
61
|
+
* apiUrl: 'https://api.agentpaid.io'
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
function getOrganizationId() {
|
|
66
|
+
return __awaiter(this, arguments, void 0, function* (config = {}) {
|
|
67
|
+
var _a;
|
|
68
|
+
const apiKey = config.apiKey || process.env.PAID_API_KEY;
|
|
69
|
+
const apiUrl = config.apiUrl || process.env.PAID_API_URL || 'https://api.agentpaid.io';
|
|
70
|
+
if (!apiKey) {
|
|
71
|
+
throw new Error('PAID_API_KEY not configured');
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const response = yield fetch(`${apiUrl}/api/organizations/organizationId`, {
|
|
75
|
+
method: 'GET',
|
|
76
|
+
headers: {
|
|
77
|
+
Authorization: `Bearer ${apiKey}`,
|
|
78
|
+
'Content-Type': 'application/json',
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
console.error('Failed to get organization ID:', yield response.text());
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const data = yield response.json();
|
|
86
|
+
return ((_a = data.data) === null || _a === void 0 ? void 0 : _a.organizationId) || null;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error('Error fetching organization ID:', error);
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
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.js").BaseRequestContext<PayInvoiceRequestWithBody, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").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.js").BaseRequestContext<ActivateOrderSyncRequestBody, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;
|
|
113
|
+
export interface SetupIntentRequest {
|
|
114
|
+
customerId: string;
|
|
115
|
+
confirmationToken: string;
|
|
116
|
+
returnUrl?: string;
|
|
117
|
+
metadata?: Record<string, any>;
|
|
118
|
+
}
|
|
119
|
+
export interface SetupIntentResponse {
|
|
120
|
+
setupIntent: {
|
|
121
|
+
id: string;
|
|
122
|
+
client_secret: string;
|
|
123
|
+
status: string;
|
|
124
|
+
usage: string;
|
|
125
|
+
customer: string;
|
|
126
|
+
};
|
|
127
|
+
message: string;
|
|
128
|
+
}
|
|
129
|
+
export interface SetupIntentConfig {
|
|
130
|
+
apiUrl: string;
|
|
131
|
+
apiKey: string;
|
|
132
|
+
organizationId: string;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a setup intent for adding payment methods without immediate charge
|
|
136
|
+
*
|
|
137
|
+
* Setup intents are used to save payment methods for future use without
|
|
138
|
+
* charging the customer immediately. This is useful for:
|
|
139
|
+
* - Adding payment methods for subscription billing
|
|
140
|
+
* - Updating payment methods
|
|
141
|
+
* - Pre-authorizing payment methods
|
|
142
|
+
*
|
|
143
|
+
* @param config - API configuration with organization context
|
|
144
|
+
* @param request - Setup intent request with customer ID and confirmation token
|
|
145
|
+
* @returns Setup intent result with payment method details
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const result = await createSetupIntent(
|
|
150
|
+
* {
|
|
151
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
152
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
153
|
+
* organizationId: 'org_123'
|
|
154
|
+
* },
|
|
155
|
+
* {
|
|
156
|
+
* customerId: 'cus_123',
|
|
157
|
+
* confirmationToken: 'seti_123_secret_456',
|
|
158
|
+
* returnUrl: 'https://example.com/return',
|
|
159
|
+
* metadata: { source: 'web' }
|
|
160
|
+
* }
|
|
161
|
+
* );
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export declare function createSetupIntent(config: SetupIntentConfig, request: SetupIntentRequest): Promise<SetupIntentResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Create a framework-agnostic handler for setup intent creation
|
|
167
|
+
*
|
|
168
|
+
* This handler can be used with any framework adapter.
|
|
169
|
+
*
|
|
170
|
+
* @param defaultReturnUrl - Optional default return URL
|
|
171
|
+
* @returns Handler function
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* // Next.js API route
|
|
176
|
+
* import { createSetupIntentHandler } from '@paid-ai/paid-node/integrations';
|
|
177
|
+
*
|
|
178
|
+
* const handler = createSetupIntentHandler('https://myapp.com/billing');
|
|
179
|
+
*
|
|
180
|
+
* export async function POST(req: Request) {
|
|
181
|
+
* return handler(
|
|
182
|
+
* {
|
|
183
|
+
* body: await req.json(),
|
|
184
|
+
* headers: Object.fromEntries(req.headers),
|
|
185
|
+
* method: 'POST'
|
|
186
|
+
* },
|
|
187
|
+
* {
|
|
188
|
+
* json: (data, status = 200) => Response.json(data, { status }),
|
|
189
|
+
* error: (message, status) => Response.json({ error: message }, { status })
|
|
190
|
+
* }
|
|
191
|
+
* );
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export declare function createSetupIntentHandler(defaultReturnUrl?: string): (request: import("../utils/base-handler.js").BaseRequestContext<SetupIntentRequest, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;
|
|
196
|
+
export {};
|
|
@@ -0,0 +1,267 @@
|
|
|
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.payInvoice = payInvoice;
|
|
13
|
+
exports.createPayInvoiceHandler = createPayInvoiceHandler;
|
|
14
|
+
exports.activateOrderSync = activateOrderSync;
|
|
15
|
+
exports.createActivateOrderSyncHandler = createActivateOrderSyncHandler;
|
|
16
|
+
exports.createSetupIntent = createSetupIntent;
|
|
17
|
+
exports.createSetupIntentHandler = createSetupIntentHandler;
|
|
18
|
+
const api_fetch_js_1 = require("../utils/api-fetch.js");
|
|
19
|
+
const base_handler_js_1 = require("../utils/base-handler.js");
|
|
20
|
+
/**
|
|
21
|
+
* Pay an invoice with a payment confirmation token
|
|
22
|
+
* This helper processes payment for an existing invoice using a Stripe confirmation token.
|
|
23
|
+
*
|
|
24
|
+
* @param config - API configuration with organization context
|
|
25
|
+
* @param request - Payment request details
|
|
26
|
+
* @returns Payment result
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const result = await payInvoice(
|
|
31
|
+
* {
|
|
32
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
33
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
34
|
+
* organizationId: 'org_123'
|
|
35
|
+
* },
|
|
36
|
+
* {
|
|
37
|
+
* invoiceId: 'inv_123',
|
|
38
|
+
* confirmationToken: 'pm_tok_xxx',
|
|
39
|
+
* returnUrl: 'https://example.com/thanks'
|
|
40
|
+
* }
|
|
41
|
+
* );
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function payInvoice(config, request) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const { invoiceId, confirmationToken, returnUrl } = request;
|
|
47
|
+
if (!invoiceId || !confirmationToken) {
|
|
48
|
+
throw new Error("invoiceId and confirmationToken are required");
|
|
49
|
+
}
|
|
50
|
+
const data = yield (0, api_fetch_js_1.paidApiFetch)(config, `/api/organizations/${config.organizationId}/invoices/${invoiceId}/pay`, {
|
|
51
|
+
method: "PUT",
|
|
52
|
+
body: Object.assign({ confirmationToken }, (returnUrl && { returnUrl })),
|
|
53
|
+
});
|
|
54
|
+
return { success: true, data };
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a framework-agnostic handler for invoice payment
|
|
59
|
+
*
|
|
60
|
+
* This handler can be used with any framework adapter.
|
|
61
|
+
* Organization ID is automatically fetched from the API key.
|
|
62
|
+
*
|
|
63
|
+
* @returns Handler function
|
|
64
|
+
*/
|
|
65
|
+
function createPayInvoiceHandler() {
|
|
66
|
+
return (0, base_handler_js_1.createHandler)((_client, body, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
if (!organizationId) {
|
|
68
|
+
return { success: false, error: 'Organization ID not found', status: 500 };
|
|
69
|
+
}
|
|
70
|
+
const invoiceId = (params === null || params === void 0 ? void 0 : params.invoiceId) || body.invoiceId;
|
|
71
|
+
const { confirmationToken, returnUrl } = body;
|
|
72
|
+
if (!invoiceId) {
|
|
73
|
+
return { success: false, error: 'invoiceId is required', status: 400 };
|
|
74
|
+
}
|
|
75
|
+
if (!confirmationToken) {
|
|
76
|
+
return { success: false, error: 'confirmationToken is required', status: 400 };
|
|
77
|
+
}
|
|
78
|
+
const apiKey = process.env.PAID_API_KEY || '';
|
|
79
|
+
const apiUrl = process.env.PAID_API_URL || 'https://api.agentpaid.io';
|
|
80
|
+
const result = yield payInvoice({ apiUrl, apiKey, organizationId }, { invoiceId, confirmationToken, returnUrl });
|
|
81
|
+
return {
|
|
82
|
+
success: true,
|
|
83
|
+
data: result.data,
|
|
84
|
+
};
|
|
85
|
+
}), { requiredFields: ['confirmationToken'], requireOrganizationId: true });
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Activate an order synchronously with payment
|
|
89
|
+
*
|
|
90
|
+
* This operation activates an order and immediately charges the customer
|
|
91
|
+
* using the provided confirmation token (from Stripe or other payment provider).
|
|
92
|
+
*
|
|
93
|
+
* @param config - API configuration with organization context
|
|
94
|
+
* @param request - Activation request with order ID and confirmation token
|
|
95
|
+
* @returns Activation result with order and billing information
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const result = await activateOrderSync(
|
|
100
|
+
* {
|
|
101
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
102
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
103
|
+
* organizationId: 'org_123'
|
|
104
|
+
* },
|
|
105
|
+
* {
|
|
106
|
+
* orderId: 'ord_123',
|
|
107
|
+
* confirmationToken: 'pi_123_secret_456',
|
|
108
|
+
* returnUrl: 'https://example.com/return'
|
|
109
|
+
* }
|
|
110
|
+
* );
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
function activateOrderSync(config, request) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
const { orderId, confirmationToken, returnUrl } = request;
|
|
116
|
+
if (!orderId) {
|
|
117
|
+
throw new Error("orderId is required");
|
|
118
|
+
}
|
|
119
|
+
if (!confirmationToken) {
|
|
120
|
+
throw new Error("confirmationToken is required");
|
|
121
|
+
}
|
|
122
|
+
const data = yield (0, api_fetch_js_1.paidApiFetch)(config, `/api/organizations/${config.organizationId}/orders/${orderId}/activate-and-pay`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: Object.assign({ confirmationToken }, (returnUrl && { returnUrl })),
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
success: true,
|
|
128
|
+
order: data.order,
|
|
129
|
+
billing: data.billing,
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Create a framework-agnostic handler for synchronous order activation
|
|
135
|
+
*
|
|
136
|
+
* This handler can be used with any framework adapter.
|
|
137
|
+
*
|
|
138
|
+
* @param defaultReturnUrl - Optional default return URL
|
|
139
|
+
* @returns Handler function
|
|
140
|
+
*/
|
|
141
|
+
function createActivateOrderSyncHandler(defaultReturnUrl) {
|
|
142
|
+
return (0, base_handler_js_1.createHandler)((client, body, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
var _a, _b;
|
|
144
|
+
const apiKey = (_a = client._options) === null || _a === void 0 ? void 0 : _a.token;
|
|
145
|
+
const baseUrl = ((_b = client._options) === null || _b === void 0 ? void 0 : _b.baseUrl) || 'https://api.agentpaid.io/api/v1';
|
|
146
|
+
const apiUrl = baseUrl.replace('/api/v1', '');
|
|
147
|
+
const orderId = (params === null || params === void 0 ? void 0 : params.orderId) || body.orderId;
|
|
148
|
+
const result = yield activateOrderSync({ apiUrl, apiKey, organizationId: organizationId }, {
|
|
149
|
+
orderId: orderId,
|
|
150
|
+
confirmationToken: body.confirmationToken,
|
|
151
|
+
returnUrl: body.returnUrl || defaultReturnUrl,
|
|
152
|
+
});
|
|
153
|
+
return {
|
|
154
|
+
success: true,
|
|
155
|
+
data: {
|
|
156
|
+
success: true,
|
|
157
|
+
order: result.order,
|
|
158
|
+
billing: result.billing,
|
|
159
|
+
message: 'Order activated and charged successfully',
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}), {
|
|
163
|
+
requiredFields: ['confirmationToken'],
|
|
164
|
+
requireOrganizationId: true,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Create a setup intent for adding payment methods without immediate charge
|
|
169
|
+
*
|
|
170
|
+
* Setup intents are used to save payment methods for future use without
|
|
171
|
+
* charging the customer immediately. This is useful for:
|
|
172
|
+
* - Adding payment methods for subscription billing
|
|
173
|
+
* - Updating payment methods
|
|
174
|
+
* - Pre-authorizing payment methods
|
|
175
|
+
*
|
|
176
|
+
* @param config - API configuration with organization context
|
|
177
|
+
* @param request - Setup intent request with customer ID and confirmation token
|
|
178
|
+
* @returns Setup intent result with payment method details
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const result = await createSetupIntent(
|
|
183
|
+
* {
|
|
184
|
+
* apiUrl: 'https://api.agentpaid.io',
|
|
185
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
186
|
+
* organizationId: 'org_123'
|
|
187
|
+
* },
|
|
188
|
+
* {
|
|
189
|
+
* customerId: 'cus_123',
|
|
190
|
+
* confirmationToken: 'seti_123_secret_456',
|
|
191
|
+
* returnUrl: 'https://example.com/return',
|
|
192
|
+
* metadata: { source: 'web' }
|
|
193
|
+
* }
|
|
194
|
+
* );
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
function createSetupIntent(config, request) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const { customerId, confirmationToken, returnUrl, metadata } = request;
|
|
200
|
+
if (!customerId) {
|
|
201
|
+
throw new Error("customerId is required");
|
|
202
|
+
}
|
|
203
|
+
if (!confirmationToken) {
|
|
204
|
+
throw new Error("confirmationToken is required");
|
|
205
|
+
}
|
|
206
|
+
const data = yield (0, api_fetch_js_1.paidApiFetch)(config, `/api/organizations/${config.organizationId}/payments/setup-intents-external`, {
|
|
207
|
+
method: "POST",
|
|
208
|
+
body: Object.assign(Object.assign({ customerId,
|
|
209
|
+
confirmationToken }, (returnUrl && { returnUrl })), (metadata && { metadata })),
|
|
210
|
+
});
|
|
211
|
+
return {
|
|
212
|
+
setupIntent: data.setupIntent || data,
|
|
213
|
+
message: "Setup intent created successfully",
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Create a framework-agnostic handler for setup intent creation
|
|
219
|
+
*
|
|
220
|
+
* This handler can be used with any framework adapter.
|
|
221
|
+
*
|
|
222
|
+
* @param defaultReturnUrl - Optional default return URL
|
|
223
|
+
* @returns Handler function
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* // Next.js API route
|
|
228
|
+
* import { createSetupIntentHandler } from '@paid-ai/paid-node/integrations';
|
|
229
|
+
*
|
|
230
|
+
* const handler = createSetupIntentHandler('https://myapp.com/billing');
|
|
231
|
+
*
|
|
232
|
+
* export async function POST(req: Request) {
|
|
233
|
+
* return handler(
|
|
234
|
+
* {
|
|
235
|
+
* body: await req.json(),
|
|
236
|
+
* headers: Object.fromEntries(req.headers),
|
|
237
|
+
* method: 'POST'
|
|
238
|
+
* },
|
|
239
|
+
* {
|
|
240
|
+
* json: (data, status = 200) => Response.json(data, { status }),
|
|
241
|
+
* error: (message, status) => Response.json({ error: message }, { status })
|
|
242
|
+
* }
|
|
243
|
+
* );
|
|
244
|
+
* }
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
function createSetupIntentHandler(defaultReturnUrl) {
|
|
248
|
+
return (0, base_handler_js_1.createHandler)((client, body, _params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
249
|
+
var _a, _b;
|
|
250
|
+
const apiKey = (_a = client._options) === null || _a === void 0 ? void 0 : _a.token;
|
|
251
|
+
const baseUrl = ((_b = client._options) === null || _b === void 0 ? void 0 : _b.baseUrl) || 'https://api.agentpaid.io/api/v1';
|
|
252
|
+
const apiUrl = baseUrl.replace('/api/v1', '');
|
|
253
|
+
const result = yield createSetupIntent({ apiUrl, apiKey, organizationId: organizationId }, {
|
|
254
|
+
customerId: body.customerId,
|
|
255
|
+
confirmationToken: body.confirmationToken,
|
|
256
|
+
returnUrl: body.returnUrl || defaultReturnUrl,
|
|
257
|
+
metadata: body.metadata,
|
|
258
|
+
});
|
|
259
|
+
return {
|
|
260
|
+
success: true,
|
|
261
|
+
data: result,
|
|
262
|
+
};
|
|
263
|
+
}), {
|
|
264
|
+
requiredFields: ['customerId', 'confirmationToken'],
|
|
265
|
+
requireOrganizationId: true,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { PaidClient } from "../../Client.js";
|
|
2
|
+
import type { ContactData, ContactCreationResult, Address } from "../types.js";
|
|
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.js").BaseRequestContext<ContactData, Record<string, string>>, response: import("../utils/base-handler.js").BaseResponseContext, config?: import("../utils/base-handler.js").BaseHandlerConfig) => Promise<any>;
|