checkout-intents 0.2.0 → 0.3.0
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/CHANGELOG.md +14 -0
- package/README.md +55 -2
- package/client.d.mts.map +1 -1
- package/client.d.ts.map +1 -1
- package/client.js +18 -1
- package/client.js.map +1 -1
- package/client.mjs +18 -1
- package/client.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/checkout-intents.d.mts +124 -1
- package/resources/checkout-intents.d.mts.map +1 -1
- package/resources/checkout-intents.d.ts +124 -1
- package/resources/checkout-intents.d.ts.map +1 -1
- package/resources/checkout-intents.js +160 -0
- package/resources/checkout-intents.js.map +1 -1
- package/resources/checkout-intents.mjs +160 -0
- package/resources/checkout-intents.mjs.map +1 -1
- package/src/client.ts +23 -1
- package/src/resources/checkout-intents.ts +222 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -2,6 +2,19 @@ import { APIResource } from "../core/resource.js";
|
|
|
2
2
|
import * as CheckoutIntentsAPI from "./checkout-intents.js";
|
|
3
3
|
import { APIPromise } from "../core/api-promise.js";
|
|
4
4
|
import { RequestOptions } from "../internal/request-options.js";
|
|
5
|
+
/**
|
|
6
|
+
* Options for polling operations.
|
|
7
|
+
*/
|
|
8
|
+
export interface PollOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The interval in milliseconds between polling attempts.
|
|
11
|
+
*/
|
|
12
|
+
pollIntervalMs?: number;
|
|
13
|
+
/**
|
|
14
|
+
* The maximum number of polling attempts before timing out.
|
|
15
|
+
*/
|
|
16
|
+
maxAttempts?: number;
|
|
17
|
+
}
|
|
5
18
|
export declare class CheckoutIntentsResource extends APIResource {
|
|
6
19
|
/**
|
|
7
20
|
* Create a checkout intent with the given request body.
|
|
@@ -73,6 +86,116 @@ export declare class CheckoutIntentsResource extends APIResource {
|
|
|
73
86
|
* ```
|
|
74
87
|
*/
|
|
75
88
|
confirm(id: string, body: CheckoutIntentConfirmParams, options?: RequestOptions): APIPromise<CheckoutIntent>;
|
|
89
|
+
/**
|
|
90
|
+
* A helper to poll a checkout intent until a specific condition is met.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* // Poll until completed or failed
|
|
95
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntil(
|
|
96
|
+
* 'id',
|
|
97
|
+
* (intent) => intent.state === 'completed' || intent.state === 'failed'
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
private pollUntil;
|
|
102
|
+
/**
|
|
103
|
+
* A helper to poll a checkout intent until it reaches a completed state
|
|
104
|
+
* (completed or failed).
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntilCompleted('id');
|
|
109
|
+
* if (checkoutIntent.state === 'completed') {
|
|
110
|
+
* console.log('Order placed successfully!');
|
|
111
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
112
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
pollUntilCompleted(id: string, options?: RequestOptions & PollOptions): Promise<CheckoutIntent.CompletedCheckoutIntent | CheckoutIntent.FailedCheckoutIntent>;
|
|
117
|
+
/**
|
|
118
|
+
* A helper to poll a checkout intent until it's ready for confirmation
|
|
119
|
+
* (awaiting_confirmation state) or has failed. This is typically used after
|
|
120
|
+
* creating a checkout intent to wait for the offer to be retrieved from the merchant.
|
|
121
|
+
*
|
|
122
|
+
* The intent can reach awaiting_confirmation (success - ready to confirm) or failed
|
|
123
|
+
* (offer retrieval failed). Always check the state after polling.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const intent = await client.checkoutIntents.pollUntilAwaitingConfirmation('id');
|
|
128
|
+
*
|
|
129
|
+
* if (intent.state === 'awaiting_confirmation') {
|
|
130
|
+
* // Review the offer before confirming
|
|
131
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
132
|
+
* } else if (intent.state === 'failed') {
|
|
133
|
+
* // Handle failure (e.g., offer retrieval failed, product out of stock)
|
|
134
|
+
* console.log('Failed:', intent.failureReason);
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
pollUntilAwaitingConfirmation(id: string, options?: RequestOptions & PollOptions): Promise<CheckoutIntent.AwaitingConfirmationCheckoutIntent | CheckoutIntent.FailedCheckoutIntent>;
|
|
139
|
+
/**
|
|
140
|
+
* A helper to create a checkout intent and poll until it's ready for confirmation.
|
|
141
|
+
* This follows the Rye documented flow: create → poll until awaiting_confirmation.
|
|
142
|
+
*
|
|
143
|
+
* After this method completes, you should review the offer (pricing, shipping, taxes)
|
|
144
|
+
* with the user before calling confirm().
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* // Phase 1: Create and wait for offer
|
|
149
|
+
* const intent = await client.checkoutIntents.createAndPoll({
|
|
150
|
+
* buyer: {
|
|
151
|
+
* address1: '123 Main St',
|
|
152
|
+
* city: 'New York',
|
|
153
|
+
* country: 'United States',
|
|
154
|
+
* email: 'john.doe@example.com',
|
|
155
|
+
* firstName: 'John',
|
|
156
|
+
* lastName: 'Doe',
|
|
157
|
+
* phone: '+1234567890',
|
|
158
|
+
* postalCode: '10001',
|
|
159
|
+
* province: 'NY',
|
|
160
|
+
* },
|
|
161
|
+
* productUrl: 'https://example.com/product',
|
|
162
|
+
* quantity: 1,
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* // Review the offer with the user
|
|
166
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
167
|
+
*
|
|
168
|
+
* // Phase 2: Confirm with payment
|
|
169
|
+
* const completed = await client.checkoutIntents.confirmAndPoll(intent.id, {
|
|
170
|
+
* paymentMethod: { type: 'stripe_token', stripeToken: 'tok_visa' }
|
|
171
|
+
* });
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
createAndPoll(body: CheckoutIntentCreateParams, options?: RequestOptions & PollOptions): Promise<CheckoutIntent.AwaitingConfirmationCheckoutIntent | CheckoutIntent.FailedCheckoutIntent>;
|
|
175
|
+
/**
|
|
176
|
+
* A helper to confirm a checkout intent and poll until it reaches a completed state
|
|
177
|
+
* (completed or failed).
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const checkoutIntent = await client.checkoutIntents.confirmAndPoll(
|
|
182
|
+
* 'id',
|
|
183
|
+
* {
|
|
184
|
+
* paymentMethod: {
|
|
185
|
+
* stripeToken: 'tok_1RkrWWHGDlstla3f1Fc7ZrhH',
|
|
186
|
+
* type: 'stripe_token',
|
|
187
|
+
* },
|
|
188
|
+
* }
|
|
189
|
+
* );
|
|
190
|
+
*
|
|
191
|
+
* if (checkoutIntent.state === 'completed') {
|
|
192
|
+
* console.log('Order placed successfully!');
|
|
193
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
194
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
confirmAndPoll(id: string, body: CheckoutIntentConfirmParams, options?: RequestOptions & PollOptions): Promise<CheckoutIntent.CompletedCheckoutIntent | CheckoutIntent.FailedCheckoutIntent>;
|
|
76
199
|
}
|
|
77
200
|
export interface BaseCheckoutIntent {
|
|
78
201
|
id: string;
|
|
@@ -192,6 +315,6 @@ export interface CheckoutIntentConfirmParams {
|
|
|
192
315
|
paymentMethod: PaymentMethod;
|
|
193
316
|
}
|
|
194
317
|
export declare namespace CheckoutIntentsResource {
|
|
195
|
-
export { type BaseCheckoutIntent as BaseCheckoutIntent, type Buyer as Buyer, type CheckoutIntent as CheckoutIntent, type Money as Money, type Offer as Offer, type PaymentMethod as PaymentMethod, type VariantSelection as VariantSelection, type CheckoutIntentCreateParams as CheckoutIntentCreateParams, type CheckoutIntentAddPaymentParams as CheckoutIntentAddPaymentParams, type CheckoutIntentConfirmParams as CheckoutIntentConfirmParams, };
|
|
318
|
+
export { type BaseCheckoutIntent as BaseCheckoutIntent, type Buyer as Buyer, type CheckoutIntent as CheckoutIntent, type Money as Money, type Offer as Offer, type PaymentMethod as PaymentMethod, type PollOptions as PollOptions, type VariantSelection as VariantSelection, type CheckoutIntentCreateParams as CheckoutIntentCreateParams, type CheckoutIntentAddPaymentParams as CheckoutIntentAddPaymentParams, type CheckoutIntentConfirmParams as CheckoutIntentConfirmParams, };
|
|
196
319
|
}
|
|
197
320
|
//# sourceMappingURL=checkout-intents.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkout-intents.d.ts","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,KAAK,kBAAkB;OACvB,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;
|
|
1
|
+
{"version":3,"file":"checkout-intents.d.ts","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,KAAK,kBAAkB;OACvB,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;AAKzB;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,uBAAwB,SAAQ,WAAW;IACtD;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CAAC,IAAI,EAAE,0BAA0B,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,UAAU,CAAC,cAAc,CAAC;IAI9F;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,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IAAI,cAAc,EACrC,KAAK,KAAK,IAAI,KAAK,EACnB,KAAK,KAAK,IAAI,KAAK,EACnB,KAAK,aAAa,IAAI,aAAa,EACnC,KAAK,WAAW,IAAI,WAAW,EAC/B,KAAK,gBAAgB,IAAI,gBAAgB,EACzC,KAAK,0BAA0B,IAAI,0BAA0B,EAC7D,KAAK,8BAA8B,IAAI,8BAA8B,EACrE,KAAK,2BAA2B,IAAI,2BAA2B,GAChE,CAAC;CACH"}
|
|
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.CheckoutIntentsResource = void 0;
|
|
5
5
|
const resource_1 = require("../core/resource.js");
|
|
6
6
|
const path_1 = require("../internal/utils/path.js");
|
|
7
|
+
const sleep_1 = require("../internal/utils/sleep.js");
|
|
8
|
+
const headers_1 = require("../internal/headers.js");
|
|
7
9
|
class CheckoutIntentsResource extends resource_1.APIResource {
|
|
8
10
|
/**
|
|
9
11
|
* Create a checkout intent with the given request body.
|
|
@@ -83,6 +85,164 @@ class CheckoutIntentsResource extends resource_1.APIResource {
|
|
|
83
85
|
confirm(id, body, options) {
|
|
84
86
|
return this._client.post((0, path_1.path) `/api/v1/checkout-intents/${id}/confirm`, { body, ...options });
|
|
85
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* A helper to poll a checkout intent until a specific condition is met.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* // Poll until completed or failed
|
|
94
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntil(
|
|
95
|
+
* 'id',
|
|
96
|
+
* (intent) => intent.state === 'completed' || intent.state === 'failed'
|
|
97
|
+
* );
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
async pollUntil(id, condition, options) {
|
|
101
|
+
const maxAttempts = options?.maxAttempts ?? 120; // Default: 120 attempts
|
|
102
|
+
const pollIntervalMs = options?.pollIntervalMs ?? 5000; // Default: 5 seconds
|
|
103
|
+
let attempts = 0;
|
|
104
|
+
const headers = (0, headers_1.buildHeaders)([
|
|
105
|
+
options?.headers,
|
|
106
|
+
{
|
|
107
|
+
'X-Stainless-Poll-Helper': 'true',
|
|
108
|
+
'X-Stainless-Custom-Poll-Interval': pollIntervalMs.toString(),
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
while (attempts < maxAttempts) {
|
|
112
|
+
const { data: intent, response } = await this.retrieve(id, {
|
|
113
|
+
...options,
|
|
114
|
+
headers: { ...options?.headers, ...headers.values },
|
|
115
|
+
}).withResponse();
|
|
116
|
+
// Check if condition is met
|
|
117
|
+
if (condition(intent)) {
|
|
118
|
+
return intent;
|
|
119
|
+
}
|
|
120
|
+
attempts++;
|
|
121
|
+
// If we've reached max attempts, throw an error
|
|
122
|
+
if (attempts >= maxAttempts) {
|
|
123
|
+
throw new Error(`Polling timeout: condition not met after ${maxAttempts} attempts (${(maxAttempts * pollIntervalMs) / 1000}s)`);
|
|
124
|
+
}
|
|
125
|
+
// Check if server suggests a polling interval
|
|
126
|
+
let sleepInterval = pollIntervalMs;
|
|
127
|
+
const headerInterval = response.headers.get('retry-after-ms');
|
|
128
|
+
if (headerInterval) {
|
|
129
|
+
const headerIntervalMs = parseInt(headerInterval);
|
|
130
|
+
if (!isNaN(headerIntervalMs)) {
|
|
131
|
+
sleepInterval = headerIntervalMs;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
await (0, sleep_1.sleep)(sleepInterval);
|
|
135
|
+
}
|
|
136
|
+
// This should never be reached due to the throw above, but TypeScript needs it
|
|
137
|
+
throw new Error(`Polling timeout: condition not met after ${maxAttempts} attempts`);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* A helper to poll a checkout intent until it reaches a completed state
|
|
141
|
+
* (completed or failed).
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntilCompleted('id');
|
|
146
|
+
* if (checkoutIntent.state === 'completed') {
|
|
147
|
+
* console.log('Order placed successfully!');
|
|
148
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
149
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
async pollUntilCompleted(id, options) {
|
|
154
|
+
return this.pollUntil(id, (intent) => intent.state === 'completed' || intent.state === 'failed', options);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A helper to poll a checkout intent until it's ready for confirmation
|
|
158
|
+
* (awaiting_confirmation state) or has failed. This is typically used after
|
|
159
|
+
* creating a checkout intent to wait for the offer to be retrieved from the merchant.
|
|
160
|
+
*
|
|
161
|
+
* The intent can reach awaiting_confirmation (success - ready to confirm) or failed
|
|
162
|
+
* (offer retrieval failed). Always check the state after polling.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* const intent = await client.checkoutIntents.pollUntilAwaitingConfirmation('id');
|
|
167
|
+
*
|
|
168
|
+
* if (intent.state === 'awaiting_confirmation') {
|
|
169
|
+
* // Review the offer before confirming
|
|
170
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
171
|
+
* } else if (intent.state === 'failed') {
|
|
172
|
+
* // Handle failure (e.g., offer retrieval failed, product out of stock)
|
|
173
|
+
* console.log('Failed:', intent.failureReason);
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
async pollUntilAwaitingConfirmation(id, options) {
|
|
178
|
+
return this.pollUntil(id, (intent) => intent.state === 'awaiting_confirmation' || intent.state === 'failed', options);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A helper to create a checkout intent and poll until it's ready for confirmation.
|
|
182
|
+
* This follows the Rye documented flow: create → poll until awaiting_confirmation.
|
|
183
|
+
*
|
|
184
|
+
* After this method completes, you should review the offer (pricing, shipping, taxes)
|
|
185
|
+
* with the user before calling confirm().
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* // Phase 1: Create and wait for offer
|
|
190
|
+
* const intent = await client.checkoutIntents.createAndPoll({
|
|
191
|
+
* buyer: {
|
|
192
|
+
* address1: '123 Main St',
|
|
193
|
+
* city: 'New York',
|
|
194
|
+
* country: 'United States',
|
|
195
|
+
* email: 'john.doe@example.com',
|
|
196
|
+
* firstName: 'John',
|
|
197
|
+
* lastName: 'Doe',
|
|
198
|
+
* phone: '+1234567890',
|
|
199
|
+
* postalCode: '10001',
|
|
200
|
+
* province: 'NY',
|
|
201
|
+
* },
|
|
202
|
+
* productUrl: 'https://example.com/product',
|
|
203
|
+
* quantity: 1,
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
* // Review the offer with the user
|
|
207
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
208
|
+
*
|
|
209
|
+
* // Phase 2: Confirm with payment
|
|
210
|
+
* const completed = await client.checkoutIntents.confirmAndPoll(intent.id, {
|
|
211
|
+
* paymentMethod: { type: 'stripe_token', stripeToken: 'tok_visa' }
|
|
212
|
+
* });
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
async createAndPoll(body, options) {
|
|
216
|
+
const intent = await this.create(body, options);
|
|
217
|
+
return this.pollUntilAwaitingConfirmation(intent.id, options);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* A helper to confirm a checkout intent and poll until it reaches a completed state
|
|
221
|
+
* (completed or failed).
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* const checkoutIntent = await client.checkoutIntents.confirmAndPoll(
|
|
226
|
+
* 'id',
|
|
227
|
+
* {
|
|
228
|
+
* paymentMethod: {
|
|
229
|
+
* stripeToken: 'tok_1RkrWWHGDlstla3f1Fc7ZrhH',
|
|
230
|
+
* type: 'stripe_token',
|
|
231
|
+
* },
|
|
232
|
+
* }
|
|
233
|
+
* );
|
|
234
|
+
*
|
|
235
|
+
* if (checkoutIntent.state === 'completed') {
|
|
236
|
+
* console.log('Order placed successfully!');
|
|
237
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
238
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
239
|
+
* }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
async confirmAndPoll(id, body, options) {
|
|
243
|
+
const intent = await this.confirm(id, body, options);
|
|
244
|
+
return this.pollUntilCompleted(intent.id, options);
|
|
245
|
+
}
|
|
86
246
|
}
|
|
87
247
|
exports.CheckoutIntentsResource = CheckoutIntentsResource;
|
|
88
248
|
//# sourceMappingURL=checkout-intents.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkout-intents.js","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;AAI/C,oDAA8C;
|
|
1
|
+
{"version":3,"file":"checkout-intents.js","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;AAI/C,oDAA8C;AAC9C,sDAAgD;AAChD,oDAAmD;AAiBnD,MAAa,uBAAwB,SAAQ,sBAAW;IACtD;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CAAC,IAAgC,EAAE,OAAwB;QAC/D,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,0BAA0B,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAU,EAAE,OAAwB;QAC3C,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAA,WAAI,EAAA,4BAA4B,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;IACzE,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,UAAU,CACR,EAAU,EACV,IAAoC,EACpC,OAAwB;QAExB,OAA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;;;;;;;;;OAkCG;IACH,KAAK,CAAC,aAAa,CACjB,IAAgC,EAChC,OAAsC;QAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC,6BAA6B,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAChE,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,cAAc,CAClB,EAAU,EACV,IAAiC,EACjC,OAAsC;QAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;CACF;AAtSD,0DAsSC"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
import { APIResource } from "../core/resource.mjs";
|
|
3
3
|
import { path } from "../internal/utils/path.mjs";
|
|
4
|
+
import { sleep } from "../internal/utils/sleep.mjs";
|
|
5
|
+
import { buildHeaders } from "../internal/headers.mjs";
|
|
4
6
|
export class CheckoutIntentsResource extends APIResource {
|
|
5
7
|
/**
|
|
6
8
|
* Create a checkout intent with the given request body.
|
|
@@ -80,5 +82,163 @@ export class CheckoutIntentsResource extends APIResource {
|
|
|
80
82
|
confirm(id, body, options) {
|
|
81
83
|
return this._client.post(path `/api/v1/checkout-intents/${id}/confirm`, { body, ...options });
|
|
82
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* A helper to poll a checkout intent until a specific condition is met.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // Poll until completed or failed
|
|
91
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntil(
|
|
92
|
+
* 'id',
|
|
93
|
+
* (intent) => intent.state === 'completed' || intent.state === 'failed'
|
|
94
|
+
* );
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
async pollUntil(id, condition, options) {
|
|
98
|
+
const maxAttempts = options?.maxAttempts ?? 120; // Default: 120 attempts
|
|
99
|
+
const pollIntervalMs = options?.pollIntervalMs ?? 5000; // Default: 5 seconds
|
|
100
|
+
let attempts = 0;
|
|
101
|
+
const headers = buildHeaders([
|
|
102
|
+
options?.headers,
|
|
103
|
+
{
|
|
104
|
+
'X-Stainless-Poll-Helper': 'true',
|
|
105
|
+
'X-Stainless-Custom-Poll-Interval': pollIntervalMs.toString(),
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
while (attempts < maxAttempts) {
|
|
109
|
+
const { data: intent, response } = await this.retrieve(id, {
|
|
110
|
+
...options,
|
|
111
|
+
headers: { ...options?.headers, ...headers.values },
|
|
112
|
+
}).withResponse();
|
|
113
|
+
// Check if condition is met
|
|
114
|
+
if (condition(intent)) {
|
|
115
|
+
return intent;
|
|
116
|
+
}
|
|
117
|
+
attempts++;
|
|
118
|
+
// If we've reached max attempts, throw an error
|
|
119
|
+
if (attempts >= maxAttempts) {
|
|
120
|
+
throw new Error(`Polling timeout: condition not met after ${maxAttempts} attempts (${(maxAttempts * pollIntervalMs) / 1000}s)`);
|
|
121
|
+
}
|
|
122
|
+
// Check if server suggests a polling interval
|
|
123
|
+
let sleepInterval = pollIntervalMs;
|
|
124
|
+
const headerInterval = response.headers.get('retry-after-ms');
|
|
125
|
+
if (headerInterval) {
|
|
126
|
+
const headerIntervalMs = parseInt(headerInterval);
|
|
127
|
+
if (!isNaN(headerIntervalMs)) {
|
|
128
|
+
sleepInterval = headerIntervalMs;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
await sleep(sleepInterval);
|
|
132
|
+
}
|
|
133
|
+
// This should never be reached due to the throw above, but TypeScript needs it
|
|
134
|
+
throw new Error(`Polling timeout: condition not met after ${maxAttempts} attempts`);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A helper to poll a checkout intent until it reaches a completed state
|
|
138
|
+
* (completed or failed).
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const checkoutIntent = await client.checkoutIntents.pollUntilCompleted('id');
|
|
143
|
+
* if (checkoutIntent.state === 'completed') {
|
|
144
|
+
* console.log('Order placed successfully!');
|
|
145
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
146
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
async pollUntilCompleted(id, options) {
|
|
151
|
+
return this.pollUntil(id, (intent) => intent.state === 'completed' || intent.state === 'failed', options);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A helper to poll a checkout intent until it's ready for confirmation
|
|
155
|
+
* (awaiting_confirmation state) or has failed. This is typically used after
|
|
156
|
+
* creating a checkout intent to wait for the offer to be retrieved from the merchant.
|
|
157
|
+
*
|
|
158
|
+
* The intent can reach awaiting_confirmation (success - ready to confirm) or failed
|
|
159
|
+
* (offer retrieval failed). Always check the state after polling.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* const intent = await client.checkoutIntents.pollUntilAwaitingConfirmation('id');
|
|
164
|
+
*
|
|
165
|
+
* if (intent.state === 'awaiting_confirmation') {
|
|
166
|
+
* // Review the offer before confirming
|
|
167
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
168
|
+
* } else if (intent.state === 'failed') {
|
|
169
|
+
* // Handle failure (e.g., offer retrieval failed, product out of stock)
|
|
170
|
+
* console.log('Failed:', intent.failureReason);
|
|
171
|
+
* }
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
async pollUntilAwaitingConfirmation(id, options) {
|
|
175
|
+
return this.pollUntil(id, (intent) => intent.state === 'awaiting_confirmation' || intent.state === 'failed', options);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* A helper to create a checkout intent and poll until it's ready for confirmation.
|
|
179
|
+
* This follows the Rye documented flow: create → poll until awaiting_confirmation.
|
|
180
|
+
*
|
|
181
|
+
* After this method completes, you should review the offer (pricing, shipping, taxes)
|
|
182
|
+
* with the user before calling confirm().
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* // Phase 1: Create and wait for offer
|
|
187
|
+
* const intent = await client.checkoutIntents.createAndPoll({
|
|
188
|
+
* buyer: {
|
|
189
|
+
* address1: '123 Main St',
|
|
190
|
+
* city: 'New York',
|
|
191
|
+
* country: 'United States',
|
|
192
|
+
* email: 'john.doe@example.com',
|
|
193
|
+
* firstName: 'John',
|
|
194
|
+
* lastName: 'Doe',
|
|
195
|
+
* phone: '+1234567890',
|
|
196
|
+
* postalCode: '10001',
|
|
197
|
+
* province: 'NY',
|
|
198
|
+
* },
|
|
199
|
+
* productUrl: 'https://example.com/product',
|
|
200
|
+
* quantity: 1,
|
|
201
|
+
* });
|
|
202
|
+
*
|
|
203
|
+
* // Review the offer with the user
|
|
204
|
+
* console.log('Total:', intent.offer.cost.total);
|
|
205
|
+
*
|
|
206
|
+
* // Phase 2: Confirm with payment
|
|
207
|
+
* const completed = await client.checkoutIntents.confirmAndPoll(intent.id, {
|
|
208
|
+
* paymentMethod: { type: 'stripe_token', stripeToken: 'tok_visa' }
|
|
209
|
+
* });
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
async createAndPoll(body, options) {
|
|
213
|
+
const intent = await this.create(body, options);
|
|
214
|
+
return this.pollUntilAwaitingConfirmation(intent.id, options);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* A helper to confirm a checkout intent and poll until it reaches a completed state
|
|
218
|
+
* (completed or failed).
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const checkoutIntent = await client.checkoutIntents.confirmAndPoll(
|
|
223
|
+
* 'id',
|
|
224
|
+
* {
|
|
225
|
+
* paymentMethod: {
|
|
226
|
+
* stripeToken: 'tok_1RkrWWHGDlstla3f1Fc7ZrhH',
|
|
227
|
+
* type: 'stripe_token',
|
|
228
|
+
* },
|
|
229
|
+
* }
|
|
230
|
+
* );
|
|
231
|
+
*
|
|
232
|
+
* if (checkoutIntent.state === 'completed') {
|
|
233
|
+
* console.log('Order placed successfully!');
|
|
234
|
+
* } else if (checkoutIntent.state === 'failed') {
|
|
235
|
+
* console.log('Order failed:', checkoutIntent.failureReason);
|
|
236
|
+
* }
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
async confirmAndPoll(id, body, options) {
|
|
240
|
+
const intent = await this.confirm(id, body, options);
|
|
241
|
+
return this.pollUntilCompleted(intent.id, options);
|
|
242
|
+
}
|
|
83
243
|
}
|
|
84
244
|
//# sourceMappingURL=checkout-intents.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkout-intents.mjs","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;OAIf,EAAE,IAAI,EAAE;
|
|
1
|
+
{"version":3,"file":"checkout-intents.mjs","sourceRoot":"","sources":["../src/resources/checkout-intents.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;OAIf,EAAE,IAAI,EAAE;OACR,EAAE,KAAK,EAAE;OACT,EAAE,YAAY,EAAE;AAiBvB,MAAM,OAAO,uBAAwB,SAAQ,WAAW;IACtD;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CAAC,IAAgC,EAAE,OAAwB;QAC/D,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,0BAA0B,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;OAUG;IACH,QAAQ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AI,CAAC,SAAS,CACnB,EAAE,EACF,CACE,MAAM,EAC6F,EAAE,CACrG,MAAM,CAAC,KAAK,KAAK,uBAAuB,IAAI,MAAM,CAAC,KAAK,KAAK,QAAQ,EACvE,OAAO,CACR,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkCG;IACH,KAAK,CAAC,aAAa,CACjB,IAAgC,EAChC,OAAsC;QAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC,6BAA6B,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAChE,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,cAAc,CAClB,EAAU,EACV,IAAiC,EACjC,OAAsC;QAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;CACF"}
|
package/src/client.ts
CHANGED
|
@@ -49,6 +49,17 @@ const environments = {
|
|
|
49
49
|
};
|
|
50
50
|
type Environment = keyof typeof environments;
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Extracts the environment from a Rye API key.
|
|
54
|
+
* API keys follow the format: RYE/{environment}-{key}
|
|
55
|
+
* @param apiKey - The API key to parse
|
|
56
|
+
* @returns The extracted environment ('staging' or 'production'), or null if the format doesn't match
|
|
57
|
+
*/
|
|
58
|
+
function extractEnvironmentFromApiKey(apiKey: string): Environment | null {
|
|
59
|
+
const match = apiKey.match(/^RYE\/(staging|production)-/);
|
|
60
|
+
return match ? (match[1] as Environment) : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
52
63
|
export interface ClientOptions {
|
|
53
64
|
/**
|
|
54
65
|
* Rye API key. Format: `RYE/{environment}-abcdef`
|
|
@@ -175,11 +186,22 @@ export class CheckoutIntents {
|
|
|
175
186
|
);
|
|
176
187
|
}
|
|
177
188
|
|
|
189
|
+
// Auto-infer environment from API key
|
|
190
|
+
const inferredEnvironment = extractEnvironmentFromApiKey(apiKey);
|
|
191
|
+
|
|
192
|
+
// Validate environment option matches API key (if both provided)
|
|
193
|
+
if (opts.environment && inferredEnvironment && opts.environment !== inferredEnvironment) {
|
|
194
|
+
throw new Errors.CheckoutIntentsError(
|
|
195
|
+
`Environment mismatch: API key is for '${inferredEnvironment}' environment but 'environment' option is set to '${opts.environment}'. Please use an API key that matches your desired environment or omit the 'environment' option to auto-detect from the API key (only auto-detectable with the RYE/{environment}-abcdef api key format).`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
178
199
|
const options: ClientOptions = {
|
|
179
200
|
apiKey,
|
|
180
201
|
...opts,
|
|
181
202
|
baseURL,
|
|
182
|
-
|
|
203
|
+
// Use provided environment, or infer from API key, or default to staging
|
|
204
|
+
environment: opts.environment ?? inferredEnvironment ?? 'staging',
|
|
183
205
|
};
|
|
184
206
|
|
|
185
207
|
if (baseURL && opts.environment) {
|