Skip to content

Commit 270e311

Browse files
authored
Merge pull request #311 from stripe/hkj-session-params
feat: Added more parameters to SessionCreateParams
2 parents 79535be + 1738b61 commit 270e311

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

firestore-stripe-web-sdk/etc/firestore-stripe-payments.api.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ export interface CommonLineItemParams {
1515

1616
// @public
1717
export interface CommonSessionCreateParams {
18+
allow_promotion_codes?: boolean;
19+
automatic_tax?: boolean;
1820
cancel_url?: string;
21+
client_reference_id?: string;
22+
metadata?: {
23+
[key: string]: any;
24+
};
1925
mode?: "subscription" | "payment";
26+
payment_method_types?: PaymentMethodType[];
27+
promotion_code?: string;
2028
success_url?: string;
29+
tax_id_collection?: boolean;
30+
trial_from_plan?: boolean;
2131
}
2232

2333
// @public (undocumented)
@@ -92,6 +102,9 @@ export interface LineItemSessionCreateParams extends CommonSessionCreateParams {
92102
// @public
93103
export function onCurrentUserSubscriptionUpdate(payments: StripePayments, onUpdate: (snapshot: SubscriptionSnapshot) => void, onError?: (error: StripePaymentsError) => void): () => void;
94104

105+
// @public
106+
export type PaymentMethodType = "card" | "acss_debit" | "afterpay_clearpay" | "alipay" | "bacs_debit" | "bancontact" | "boleto" | "eps" | "fpx" | "giropay" | "grabpay" | "ideal" | "klarna" | "oxxo" | "p24" | "sepa_debit" | "sofort" | "wechat_pay";
107+
95108
// @public
96109
export interface Price {
97110
// (undocumented)
@@ -137,14 +150,24 @@ export interface Product {
137150

138151
// @public
139152
export interface Session {
153+
readonly allow_promotion_codes?: boolean;
154+
readonly automatic_tax?: boolean;
140155
readonly cancel_url: string;
156+
readonly client_reference_id?: string;
141157
readonly created_at: string;
142158
readonly id: string;
143159
readonly line_items?: LineItem[];
160+
readonly metadata?: {
161+
[key: string]: any;
162+
};
144163
readonly mode: "subscription" | "payment";
164+
readonly payment_method_types?: PaymentMethodType[];
145165
readonly price?: string;
166+
readonly promotion_code?: string;
146167
readonly quantity?: number;
147168
readonly success_url: string;
169+
readonly tax_id_collection?: boolean;
170+
readonly trial_from_plan?: boolean;
148171
readonly url: string;
149172
}
150173

firestore-stripe-web-sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export {
3131
LineItem,
3232
LineItemParams,
3333
LineItemSessionCreateParams,
34+
PaymentMethodType,
3435
PriceIdLineItemParams,
3536
PriceIdSessionCreateParams,
3637
Session,

firestore-stripe-web-sdk/src/session.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,90 @@ import {
4343
* Parameters common across all session types.
4444
*/
4545
export interface CommonSessionCreateParams {
46+
/**
47+
* Enables user redeemable promotion codes.
48+
*/
49+
allow_promotion_codes?: boolean;
50+
51+
/**
52+
* Set to true to enable automatic taxes. Defaults to false.
53+
*/
54+
automatic_tax?: boolean;
55+
56+
/**
57+
* A unique string to reference the Checkout Session. This can be a customer ID, a cart ID,
58+
* or similar, and can be used to reconcile the session with your internal systems.
59+
*/
60+
client_reference_id?: string;
61+
4662
/**
4763
* The URL the customer will be directed to if they decide to cancel payment and return to
4864
* your website.
4965
*/
5066
cancel_url?: string;
5167

68+
/**
69+
* Set of key-value pairs that you can attach to an object. This can be useful for storing
70+
* additional information about the object in a structured format.
71+
*/
72+
metadata?: { [key: string]: any };
73+
5274
/**
5375
* The mode of the Checkout Session. If not specified defaults to `subscription`.
5476
*/
5577
mode?: "subscription" | "payment";
5678

79+
/**
80+
* A list of the types of payment methods (e.g., `card`) this Checkout Session can accept.
81+
* Defaults to `["card"]`.
82+
*/
83+
payment_method_types?: PaymentMethodType[];
84+
85+
/**
86+
* The promotion code to apply to this Session.
87+
*/
88+
promotion_code?: string;
89+
5790
/**
5891
* The URL to which Stripe should send customers when payment or setup is complete.
5992
*/
6093
success_url?: string;
94+
95+
/**
96+
* Controls tax ID collection settings for the session.
97+
*/
98+
tax_id_collection?: boolean;
99+
100+
/**
101+
* Indicates if a plan’s `trial_period_days` should be applied to the subscription. Defaults
102+
* to `true`.
103+
*/
104+
trial_from_plan?: boolean;
61105
}
62106

107+
/**
108+
* Supported payment methods.
109+
*/
110+
export type PaymentMethodType =
111+
| "card"
112+
| "acss_debit"
113+
| "afterpay_clearpay"
114+
| "alipay"
115+
| "bacs_debit"
116+
| "bancontact"
117+
| "boleto"
118+
| "eps"
119+
| "fpx"
120+
| "giropay"
121+
| "grabpay"
122+
| "ideal"
123+
| "klarna"
124+
| "oxxo"
125+
| "p24"
126+
| "sepa_debit"
127+
| "sofort"
128+
| "wechat_pay";
129+
63130
/**
64131
* Parameters for createing a session with one or more line items.
65132
*/
@@ -160,22 +227,66 @@ export interface Session {
160227
*/
161228
readonly url: string;
162229

230+
/**
231+
* Enables user redeemable promotion codes.
232+
*/
233+
readonly allow_promotion_codes?: boolean;
234+
235+
/**
236+
* Indicates whether automatic tax is enabled for the session
237+
*/
238+
readonly automatic_tax?: boolean;
239+
240+
/**
241+
* A unique string to reference the Checkout Session. This can be a customer ID, a cart ID,
242+
* or similar, and can be used to reconcile the session with your internal systems.
243+
*/
244+
readonly client_reference_id?: string;
245+
163246
/**
164247
* The array of line items purchased with this session. A session is guaranteed to contain either
165248
* {@link Session.line_items} or {@link Session.price}.
166249
*/
167250
readonly line_items?: LineItem[];
168251

252+
/**
253+
* Set of key-value pairs that you can attach to an object. This can be useful for storing
254+
* additional information about the object in a structured format.
255+
*/
256+
readonly metadata?: { [key: string]: any };
257+
258+
/**
259+
* A list of the types of payment methods (e.g., `card`) this Checkout Session can accept.
260+
* Defaults to `["card"]`.
261+
*/
262+
readonly payment_method_types?: PaymentMethodType[];
263+
169264
/**
170265
* The ID of the Stripe price object purchased with this session. A session is guaranteed to
171266
* contain either {@link Session.line_items} or {@link Session.price}.
172267
*/
173268
readonly price?: string;
174269

270+
/**
271+
* The promotion code to apply to this Session.
272+
*/
273+
readonly promotion_code?: string;
274+
175275
/**
176276
* The quantity of item purchased. Defaults to 1.
177277
*/
178278
readonly quantity?: number;
279+
280+
/**
281+
* Controls tax ID collection settings for the session.
282+
*/
283+
readonly tax_id_collection?: boolean;
284+
285+
/**
286+
* Indicates if a plan’s `trial_period_days` should be applied to the subscription. Defaults
287+
* to `true`.
288+
*/
289+
readonly trial_from_plan?: boolean;
179290
}
180291

181292
/**

firestore-stripe-web-sdk/test/emulator.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,59 @@ describe("Emulator tests", () => {
187187
},
188188
];
189189
const session = await createCheckoutSession(payments, {
190+
allow_promotion_codes: true,
191+
automatic_tax: true,
190192
cancel_url: "https://example.com/cancel",
193+
client_reference_id: "example",
191194
line_items: lineItems,
195+
metadata: {
196+
test: true,
197+
},
192198
mode: "subscription",
199+
payment_method_types: ["card"],
200+
promotion_code: "discount",
193201
success_url: "https://example.com/success",
202+
tax_id_collection: true,
203+
trial_from_plan: true,
194204
});
195205

196206
expect(backend.events).to.have.length(1);
197207
const { uid, docId, data, timestamp } = backend.events[0];
198208
expect(session).to.eql({
209+
allow_promotion_codes: true,
210+
automatic_tax: true,
199211
cancel_url: "https://example.com/cancel",
212+
client_reference_id: "example",
200213
created_at: timestamp.toDate().toUTCString(),
201214
id: `test_session_${docId}`,
202215
line_items: lineItems,
216+
metadata: {
217+
test: true,
218+
},
203219
mode: "subscription",
220+
payment_method_types: ["card"],
221+
promotion_code: "discount",
204222
success_url: "https://example.com/success",
223+
tax_id_collection: true,
224+
trial_from_plan: true,
205225
url: `https://example.stripe.com/session/${docId}`,
206226
});
207227
expect(uid).to.equal(currentUser);
208228
expect(data).to.eql({
229+
allow_promotion_codes: true,
230+
automatic_tax: true,
209231
cancel_url: "https://example.com/cancel",
232+
client_reference_id: "example",
210233
line_items: lineItems,
234+
metadata: {
235+
test: true,
236+
},
211237
mode: "subscription",
238+
payment_method_types: ["card"],
239+
promotion_code: "discount",
212240
success_url: "https://example.com/success",
241+
tax_id_collection: true,
242+
trial_from_plan: true,
213243
});
214244
});
215245

@@ -240,32 +270,62 @@ describe("Emulator tests", () => {
240270

241271
it("should create a session when called with all price ID parameters", async () => {
242272
const session = await createCheckoutSession(payments, {
273+
allow_promotion_codes: true,
274+
automatic_tax: true,
243275
cancel_url: "https://example.com/cancel",
276+
client_reference_id: "example",
277+
metadata: {
278+
test: true,
279+
},
244280
mode: "subscription",
281+
payment_method_types: ["card"],
245282
price: "foo",
283+
promotion_code: "discount",
246284
quantity: 5,
247285
success_url: "https://example.com/success",
286+
tax_id_collection: true,
287+
trial_from_plan: true,
248288
});
249289

250290
expect(backend.events).to.have.length(1);
251291
const { uid, docId, data, timestamp } = backend.events[0];
252292
expect(session).to.eql({
293+
allow_promotion_codes: true,
294+
automatic_tax: true,
253295
cancel_url: "https://example.com/cancel",
296+
client_reference_id: "example",
254297
created_at: timestamp.toDate().toUTCString(),
255298
id: `test_session_${docId}`,
299+
metadata: {
300+
test: true,
301+
},
256302
mode: "subscription",
303+
payment_method_types: ["card"],
257304
price: "foo",
305+
promotion_code: "discount",
258306
quantity: 5,
259307
success_url: "https://example.com/success",
308+
tax_id_collection: true,
309+
trial_from_plan: true,
260310
url: `https://example.stripe.com/session/${docId}`,
261311
});
262312
expect(uid).to.equal(currentUser);
263313
expect(data).to.eql({
314+
allow_promotion_codes: true,
315+
automatic_tax: true,
264316
cancel_url: "https://example.com/cancel",
317+
client_reference_id: "example",
318+
metadata: {
319+
test: true,
320+
},
265321
mode: "subscription",
322+
payment_method_types: ["card"],
266323
price: "foo",
324+
promotion_code: "discount",
267325
quantity: 5,
268326
success_url: "https://example.com/success",
327+
tax_id_collection: true,
328+
trial_from_plan: true,
269329
});
270330
});
271331

firestore-stripe-web-sdk/test/session.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ const payments: StripePayments = getStripePayments(app, {
4444
});
4545

4646
const testSession: Session = {
47+
allow_promotion_codes: true,
48+
automatic_tax: true,
4749
cancel_url: "https://example.com/cancel",
50+
client_reference_id: "example",
4851
created_at: new Date().toUTCString(),
4952
id: "test_session_1",
53+
metadata: {
54+
test: true,
55+
},
5056
mode: "subscription",
57+
payment_method_types: ["card"],
5158
price: "price1",
59+
promotion_code: "discount",
5260
success_url: "https://example.com/success",
61+
tax_id_collection: true,
62+
trial_from_plan: true,
5363
url: "https://example.stripe.com/session/test_session_1",
5464
};
5565

@@ -183,11 +193,21 @@ describe("createCheckoutSession()", () => {
183193
const userFake: SinonSpy = sinonFake.returns("alice");
184194
setUserDAO(payments, testUserDAO(userFake));
185195
const params: SessionCreateParams = {
196+
allow_promotion_codes: true,
197+
automatic_tax: true,
198+
client_reference_id: "example",
186199
cancel_url: "https://example.com/cancel",
200+
metadata: {
201+
test: true,
202+
},
187203
mode: "subscription",
204+
payment_method_types: ["card"],
188205
price: "price1",
206+
promotion_code: "discount",
189207
quantity: 5,
190208
success_url: "https://example.com/success",
209+
tax_id_collection: true,
210+
trial_from_plan: true,
191211
};
192212

193213
const session: Session = await createCheckoutSession(payments, params);

0 commit comments

Comments
 (0)