@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,235 @@
|
|
|
1
|
+
import { nextjsAdapter } from "./base-adapter.mjs";
|
|
2
|
+
import { createContactsHandler } from "../../controllers/contacts.mjs";
|
|
3
|
+
import { createCustomerInvoicesHandler } from "../../controllers/invoices.mjs";
|
|
4
|
+
import { createCustomersHandler, createGetCustomerHandler } from "../../controllers/customers.mjs";
|
|
5
|
+
import { createOrdersHandler } from "../../controllers/orders.mjs";
|
|
6
|
+
import { createProvisioningHandler } from "../../controllers/provision-users.mjs";
|
|
7
|
+
import { createActivateOrderSyncHandler, createPayInvoiceHandler, createSetupIntentHandler } from "../../controllers/billing.mjs";
|
|
8
|
+
/**
|
|
9
|
+
* Create a Next.js API route handler for synchronous order activation
|
|
10
|
+
*
|
|
11
|
+
* This creates an endpoint that activates an order and immediately charges
|
|
12
|
+
* the customer using a confirmation token from a payment provider.
|
|
13
|
+
*
|
|
14
|
+
* Organization ID is automatically fetched from the API key.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // src/app/api/orders/[orderId]/activate-sync/route.ts
|
|
19
|
+
* import { createActivateOrderSyncRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
20
|
+
*
|
|
21
|
+
* export const POST = createActivateOrderSyncRoute({
|
|
22
|
+
* defaultReturnUrl: process.env.NEXT_PUBLIC_BASE_URL + '/orders'
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example Client-side usage with route params
|
|
27
|
+
* ```typescript
|
|
28
|
+
* await fetch('/api/orders/ord_123/activate-sync', {
|
|
29
|
+
* method: 'POST',
|
|
30
|
+
* body: JSON.stringify({
|
|
31
|
+
* confirmationToken: 'pi_123_secret_456',
|
|
32
|
+
* returnUrl: 'https://example.com/return'
|
|
33
|
+
* })
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example Client-side usage with body params
|
|
38
|
+
* ```typescript
|
|
39
|
+
* await fetch('/api/orders/activate-sync', {
|
|
40
|
+
* method: 'POST',
|
|
41
|
+
* body: JSON.stringify({
|
|
42
|
+
* orderId: 'ord_123',
|
|
43
|
+
* confirmationToken: 'pi_123_secret_456'
|
|
44
|
+
* })
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function createActivateOrderSyncRoute(config = {}) {
|
|
49
|
+
const handler = createActivateOrderSyncHandler(config.defaultReturnUrl);
|
|
50
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
51
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create a Next.js API route handler for contact creation
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // src/app/api/contacts/route.ts
|
|
59
|
+
* import { createContactsRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
60
|
+
*
|
|
61
|
+
* export const POST = createContactsRoute();
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function createContactsRoute(config = {}) {
|
|
65
|
+
const handler = createContactsHandler();
|
|
66
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
67
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a Next.js API route handler for fetching customer invoices
|
|
71
|
+
*
|
|
72
|
+
* Organization ID is automatically fetched from the API key.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // src/app/api/customers/[customerExternalId]/invoices/route.ts
|
|
77
|
+
* import { createCustomerInvoicesRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
78
|
+
*
|
|
79
|
+
* export const GET = createCustomerInvoicesRoute();
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function createCustomerInvoicesRoute(config = {}) {
|
|
83
|
+
const handler = createCustomerInvoicesHandler();
|
|
84
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
85
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a Next.js API route handler for fetching a customer by external ID
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* // src/app/api/customers/[customerExternalId]/route.ts
|
|
93
|
+
* import { createGetCustomerRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
94
|
+
*
|
|
95
|
+
* export const GET = createGetCustomerRoute();
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function createGetCustomerRoute(config = {}) {
|
|
99
|
+
const handler = createGetCustomerHandler();
|
|
100
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
101
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Create a Next.js API route handler for customer creation
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // src/app/api/customers/route.ts
|
|
109
|
+
* import { createCustomersRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
110
|
+
*
|
|
111
|
+
* export const POST = createCustomersRoute();
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @example With custom config
|
|
115
|
+
* ```typescript
|
|
116
|
+
* export const POST = createCustomersRoute({
|
|
117
|
+
* validate: async (body) => {
|
|
118
|
+
* if (!body.email.includes('@company.com')) {
|
|
119
|
+
* throw new Error('Only company emails allowed');
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export function createCustomersRoute(config = {}) {
|
|
126
|
+
const handler = createCustomersHandler();
|
|
127
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
128
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Create a Next.js API route handler for order creation
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* // src/app/api/orders/route.ts
|
|
136
|
+
* import { createOrdersRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
137
|
+
*
|
|
138
|
+
* export const POST = createOrdersRoute();
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* @example With auto-activation disabled
|
|
142
|
+
* ```typescript
|
|
143
|
+
* export const POST = createOrdersRoute({
|
|
144
|
+
* helperOptions: { autoActivate: false }
|
|
145
|
+
* });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export function createOrdersRoute(config = {}) {
|
|
149
|
+
const handler = createOrdersHandler(config.helperOptions);
|
|
150
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
151
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Create a Next.js API route handler for invoice payment
|
|
155
|
+
*
|
|
156
|
+
* Organization ID is automatically fetched from the API key.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* // src/app/api/invoices/[invoiceId]/pay/route.ts
|
|
161
|
+
* import { createPayInvoiceRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
162
|
+
*
|
|
163
|
+
* export const POST = createPayInvoiceRoute();
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export function createPayInvoiceRoute(config = {}) {
|
|
167
|
+
const handler = createPayInvoiceHandler();
|
|
168
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
169
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a Next.js API route handler for user provisioning
|
|
173
|
+
*
|
|
174
|
+
* This creates a single endpoint that provisions a complete user
|
|
175
|
+
* with customer, contact, and optional order.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* // src/app/api/provision/route.ts
|
|
180
|
+
* import { createProvisioningRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
181
|
+
*
|
|
182
|
+
* export const POST = createProvisioningRoute({
|
|
183
|
+
* defaultAgentExternalId: process.env.PAID_AGENT_ID
|
|
184
|
+
* });
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @example Client-side usage
|
|
188
|
+
* ```typescript
|
|
189
|
+
* await fetch('/api/provision', {
|
|
190
|
+
* method: 'POST',
|
|
191
|
+
* body: JSON.stringify({ email: 'user@example.com' })
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export function createProvisioningRoute(config = {}) {
|
|
196
|
+
const handler = createProvisioningHandler(config.orderOptions, config.defaultAgentExternalId);
|
|
197
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
198
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Create a Next.js API route handler for setup intent creation
|
|
202
|
+
*
|
|
203
|
+
* Setup intents are used to add payment methods without immediate charge.
|
|
204
|
+
* This is useful for saving payment methods for future billing.
|
|
205
|
+
*
|
|
206
|
+
* Organization ID is automatically fetched from the API key.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* // src/app/api/payment-methods/setup/route.ts
|
|
211
|
+
* import { createSetupIntentRoute } from '@paid-ai/paid-node/integrations/nextjs';
|
|
212
|
+
*
|
|
213
|
+
* export const POST = createSetupIntentRoute({
|
|
214
|
+
* defaultReturnUrl: process.env.NEXT_PUBLIC_BASE_URL + '/billing'
|
|
215
|
+
* });
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
218
|
+
* @example Client-side usage
|
|
219
|
+
* ```typescript
|
|
220
|
+
* await fetch('/api/payment-methods/setup', {
|
|
221
|
+
* method: 'POST',
|
|
222
|
+
* body: JSON.stringify({
|
|
223
|
+
* customerId: 'cus_123',
|
|
224
|
+
* confirmationToken: 'seti_123_secret_456',
|
|
225
|
+
* returnUrl: 'https://example.com/billing',
|
|
226
|
+
* metadata: { source: 'settings_page' }
|
|
227
|
+
* })
|
|
228
|
+
* });
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export function createSetupIntentRoute(config = {}) {
|
|
232
|
+
const handler = createSetupIntentHandler(config.defaultReturnUrl);
|
|
233
|
+
const adaptedHandler = nextjsAdapter(handler);
|
|
234
|
+
return (request, context) => adaptedHandler(request, context, config);
|
|
235
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Paid client initialization utility
|
|
3
|
+
* Framework-agnostic
|
|
4
|
+
*/
|
|
5
|
+
import { PaidClient } from '../Client.mjs';
|
|
6
|
+
export interface PaidClientConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Paid API key - defaults to process.env.PAID_API_KEY
|
|
9
|
+
*/
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Paid API URL - defaults to production
|
|
13
|
+
*/
|
|
14
|
+
apiUrl?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initialize a Paid client with environment variable fallbacks
|
|
18
|
+
*
|
|
19
|
+
* @param config - Optional configuration overrides
|
|
20
|
+
* @returns Configured PaidClient instance
|
|
21
|
+
* @throws Error if API key is not provided or found in environment
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const client = initializePaidClient();
|
|
26
|
+
* // Uses process.env.PAID_API_KEY
|
|
27
|
+
*
|
|
28
|
+
* const client = initializePaidClient({ apiKey: 'pk_xxx' });
|
|
29
|
+
* // Uses provided key
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function initializePaidClient(config?: PaidClientConfig): PaidClient;
|
|
33
|
+
/**
|
|
34
|
+
* Get organization ID from API key
|
|
35
|
+
*
|
|
36
|
+
* Calls the Paid API to resolve the organization ID associated with the API key.
|
|
37
|
+
*
|
|
38
|
+
* @param config - API configuration
|
|
39
|
+
* @returns Organization ID or null if not found
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const orgId = await getOrganizationId({
|
|
44
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
45
|
+
* apiUrl: 'https://api.agentpaid.io'
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function getOrganizationId(config?: PaidClientConfig): Promise<string | null>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Paid client initialization utility
|
|
3
|
+
* Framework-agnostic
|
|
4
|
+
*/
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
import { PaidClient } from '../Client.mjs';
|
|
15
|
+
/**
|
|
16
|
+
* Initialize a Paid client with environment variable fallbacks
|
|
17
|
+
*
|
|
18
|
+
* @param config - Optional configuration overrides
|
|
19
|
+
* @returns Configured PaidClient instance
|
|
20
|
+
* @throws Error if API key is not provided or found in environment
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const client = initializePaidClient();
|
|
25
|
+
* // Uses process.env.PAID_API_KEY
|
|
26
|
+
*
|
|
27
|
+
* const client = initializePaidClient({ apiKey: 'pk_xxx' });
|
|
28
|
+
* // Uses provided key
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function initializePaidClient(config = {}) {
|
|
32
|
+
const apiKey = config.apiKey || process.env.PAID_API_KEY;
|
|
33
|
+
if (!apiKey) {
|
|
34
|
+
throw new Error('PAID_API_KEY not configured. Set process.env.PAID_API_KEY or pass apiKey in config.');
|
|
35
|
+
}
|
|
36
|
+
return new PaidClient({
|
|
37
|
+
token: apiKey,
|
|
38
|
+
baseUrl: config.apiUrl
|
|
39
|
+
? `${config.apiUrl}/api/v1`
|
|
40
|
+
: process.env.PAID_API_URL
|
|
41
|
+
? `${process.env.PAID_API_URL}/api/v1`
|
|
42
|
+
: undefined,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get organization ID from API key
|
|
47
|
+
*
|
|
48
|
+
* Calls the Paid API to resolve the organization ID associated with the API key.
|
|
49
|
+
*
|
|
50
|
+
* @param config - API configuration
|
|
51
|
+
* @returns Organization ID or null if not found
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const orgId = await getOrganizationId({
|
|
56
|
+
* apiKey: process.env.PAID_API_KEY,
|
|
57
|
+
* apiUrl: 'https://api.agentpaid.io'
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function getOrganizationId() {
|
|
62
|
+
return __awaiter(this, arguments, void 0, function* (config = {}) {
|
|
63
|
+
var _a;
|
|
64
|
+
const apiKey = config.apiKey || process.env.PAID_API_KEY;
|
|
65
|
+
const apiUrl = config.apiUrl || process.env.PAID_API_URL || 'https://api.agentpaid.io';
|
|
66
|
+
if (!apiKey) {
|
|
67
|
+
throw new Error('PAID_API_KEY not configured');
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const response = yield fetch(`${apiUrl}/api/organizations/organizationId`, {
|
|
71
|
+
method: 'GET',
|
|
72
|
+
headers: {
|
|
73
|
+
Authorization: `Bearer ${apiKey}`,
|
|
74
|
+
'Content-Type': 'application/json',
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
console.error('Failed to get organization ID:', yield response.text());
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const data = yield response.json();
|
|
82
|
+
return ((_a = data.data) === null || _a === void 0 ? void 0 : _a.organizationId) || null;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error('Error fetching organization ID:', error);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -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.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 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.mjs").BaseRequestContext<SetupIntentRequest, Record<string, string>>, response: import("../utils/base-handler.mjs").BaseResponseContext, config?: import("../utils/base-handler.mjs").BaseHandlerConfig) => Promise<any>;
|
|
196
|
+
export {};
|