Skip to content

Commit d34b3d6

Browse files
committed
fix(firestore-stripe-payments): fixed bug when creating a checkout session in setup mode
1 parent 3b55561 commit d34b3d6

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

firestore-stripe-payments/functions/__tests__/tests/checkoutsessions/createCheckoutSession.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,52 @@ describe('createCheckoutSession', () => {
6666
expect(success_url).toBe('http://test.com/success');
6767
});
6868

69+
test('should setup a future payment with mode:setup', async () => {
70+
const collection = firestore.collection('customers');
71+
72+
const customer: DocumentData = await waitForDocumentToExistInCollection(
73+
collection,
74+
'email',
75+
user.email
76+
);
77+
78+
/** Define params */
79+
const client = 'web';
80+
const mode = 'setup';
81+
const success_url = 'http://test.com/success';
82+
const cancel_url = 'http://test.com/cancel';
83+
const payment_method_types = ['card'];
84+
85+
const checkoutSessionCollection = collection
86+
.doc(customer.doc.id)
87+
.collection('checkout_sessions');
88+
89+
const checkoutSessionDocument: DocumentReference =
90+
await checkoutSessionCollection.add({
91+
client: 'web',
92+
mode: 'setup',
93+
success_url: 'http://test.com/success',
94+
cancel_url: 'http://test.com/cancel',
95+
payment_method_types,
96+
});
97+
98+
const customerDoc = await waitForDocumentToExistWithField(
99+
checkoutSessionDocument,
100+
'created'
101+
);
102+
103+
const result = customerDoc.data();
104+
105+
expect(result.client).toBe(client);
106+
expect(result.mode).toBe(mode);
107+
expect(result.success_url).toBe(success_url);
108+
expect(result.cancel_url).toBe(cancel_url);
109+
expect(result.payment_method_types).toEqual(payment_method_types);
110+
expect(result.sessionId).toBeDefined();
111+
expect(url).toBeDefined();
112+
expect(result.created).toBeDefined();
113+
});
114+
69115
test.skip('throws an error when success_url has not been provided', async () => {});
70116

71117
test.skip('throws an error when cancel_url has not been provided', async () => {});

firestore-stripe-payments/functions/src/index.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ exports.createCustomer = functions.auth
104104
});
105105
});
106106

107+
function generateLineItems(
108+
line_items: Stripe.Checkout.SessionCreateParams.LineItem[],
109+
price: string,
110+
quantity: number
111+
): Stripe.Checkout.SessionCreateParams.LineItem[] {
112+
if (line_items) {
113+
return line_items;
114+
}
115+
116+
if (price && quantity)
117+
return [
118+
{
119+
price,
120+
quantity,
121+
},
122+
];
123+
124+
return [];
125+
}
126+
107127
/**
108128
* Create a CheckoutSession or PaymentIntent based on which client is being used.
109129
*/
@@ -185,14 +205,7 @@ exports.createCheckoutSession = functions
185205
shipping_rates,
186206
customer,
187207
customer_update,
188-
line_items: line_items
189-
? line_items
190-
: [
191-
{
192-
price,
193-
quantity,
194-
},
195-
],
208+
line_items: generateLineItems(line_items, price, quantity),
196209
mode,
197210
success_url,
198211
cancel_url,
@@ -205,6 +218,7 @@ exports.createCheckoutSession = functions
205218
if (payment_method_types) {
206219
sessionCreateParams.payment_method_types = payment_method_types;
207220
}
221+
208222
if (mode === 'subscription') {
209223
sessionCreateParams.payment_method_collection =
210224
payment_method_collection;
@@ -244,15 +258,17 @@ exports.createCheckoutSession = functions
244258
}
245259
if (promotion_code) {
246260
sessionCreateParams.discounts = [{ promotion_code }];
247-
} else {
261+
} else if (mode !== 'setup') {
248262
sessionCreateParams.allow_promotion_codes = allow_promotion_codes;
249263
}
250264
if (client_reference_id)
251265
sessionCreateParams.client_reference_id = client_reference_id;
266+
252267
const session = await stripe.checkout.sessions.create(
253268
sessionCreateParams,
254269
{ idempotencyKey: context.params.id }
255270
);
271+
256272
await snap.ref.set(
257273
{
258274
client,

0 commit comments

Comments
 (0)