Skip to content

Commit

Permalink
Add Customer Session to server & playground
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed Oct 4, 2024
1 parent 7987b24 commit 8aca8f7
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 101 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"nodemon": "^2.0.19",
"path": "^0.12.7",
"react-test-renderer": "18.0.0",
"stripe": "^11.0.0",
"stripe": "^14.18.0",
"typescript": "^4.5.5"
}
}
170 changes: 135 additions & 35 deletions example/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ app.post(
const { secret_key } = getKeys(payment_method_types[0]);

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand Down Expand Up @@ -177,7 +177,7 @@ app.post(
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});
const customers = await stripe.customers.list({
Expand Down Expand Up @@ -255,7 +255,7 @@ app.post(
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand Down Expand Up @@ -344,7 +344,7 @@ app.post('/create-setup-intent', async (req, res) => {
const { secret_key } = getKeys(payment_method_types[0]);

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});
const customer = await stripe.customers.create({ email });
Expand Down Expand Up @@ -401,7 +401,7 @@ app.post(
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});
// console.log('webhook!', req);
Expand Down Expand Up @@ -464,7 +464,7 @@ app.post('/charge-card-off-session', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand Down Expand Up @@ -534,11 +534,27 @@ app.post('/charge-card-off-session', async (req, res) => {
// This example sets up an endpoint using the Express framework.
// Watch this video to get started: https://youtu.be/rPR2aJ6XnAc.

app.post('/payment-sheet', async (_, res) => {
app.post('/payment-sheet', async (req, res) => {
const {
customer_key_type,
}: {
customer_key_type?: string;
} = req.body;

if (
customer_key_type !== 'legacy_ephemeral_key' &&
customer_key_type !== 'customer_session'
) {
return res.send({
error:
'`customer_key_type` is not valid! Please pass either "customer_session" or "legacy_ephemeral_key"',
});
}

const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -553,10 +569,6 @@ app.post('/payment-sheet', async (_, res) => {
});
}

const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2022-11-15' }
);
const paymentIntent = await stripe.paymentIntents.create({
amount: 5099,
currency: 'usd',
Expand All @@ -577,18 +589,72 @@ app.post('/payment-sheet', async (_, res) => {
// 'us_bank_account',
],
});
return res.json({
paymentIntent: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
});

if (customer_key_type === 'legacy_ephemeral_key') {
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2023-10-16' }
);

return res.json({
paymentIntent: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
});
} else {
const customerSessionClientSecret = await stripe.customerSessions.create(
{
customer: customer.id,
components: {
// This needs to be ignored because `mobile_payment_element` is not specified as a type in `stripe-node` yet.
// @ts-ignore
mobile_payment_element: {
enabled: true,
features: {
payment_method_save: 'disabled',
payment_method_remove: 'enabled',
payment_method_redisplay: 'enabled',
payment_method_allow_redisplay_filters: [
'unspecified',
'limited',
'always',
],
},
},
},
},
{ apiVersion: '2023-10-16' }
);

return res.json({
paymentIntent: paymentIntent.client_secret,
customerSessionClientSecret: customerSessionClientSecret.client_secret,
customer: customer.id,
});
}
});

app.post('/payment-sheet-subscription', async (_, res) => {
app.post('/payment-sheet-subscription', async (req, res) => {
const {
customer_key_type,
}: {
customer_key_type?: string;
} = req.body;

if (
customer_key_type !== 'legacy_ephemeral_key' &&
customer_key_type !== 'customer_session'
) {
return res.send({
error:
'`customer_key_type` is not valid! Please pass either "customer_session" or "legacy_ephemeral_key"',
});
}

const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -603,10 +669,6 @@ app.post('/payment-sheet-subscription', async (_, res) => {
});
}

const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2022-11-15' }
);
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ price: 'price_1L3hcFLu5o3P18Zp9GDQEnqe' }],
Expand All @@ -618,11 +680,49 @@ app.post('/payment-sheet-subscription', async (_, res) => {
subscription.pending_setup_intent
);

return res.json({
setupIntent: setupIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
});
if (customer_key_type === 'legacy_ephemeral_key') {
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2023-10-16' }
);

return res.json({
setupIntent: setupIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
});
} else {
const customerSessionClientSecret = await stripe.customerSessions.create(
{
customer: customer.id,
components: {
// This needs to be ignored because `mobile_payment_element` is not specified as a type in `stripe-node` yet.
// @ts-ignore
mobile_payment_element: {
enabled: true,
features: {
payment_method_save: true,
payment_method_remove: true,
payment_method_redisplay: true,
payment_method_save_allow_redisplay_override: true,
payment_method_allow_redisplay_filters: [
'unspecified',
'limited',
'always',
],
},
},
},
},
{ apiVersion: '2023-10-16' }
);

return res.json({
setupIntent: setupIntent.client_secret,
customerSessionClientSecret: customerSessionClientSecret.client_secret,
customer: customer.id,
});
}
} else {
throw new Error(
'Expected response type string, but received: ' +
Expand Down Expand Up @@ -655,7 +755,7 @@ app.post('/issuing-card-details', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -678,7 +778,7 @@ app.post('/financial-connections-sheet', async (_, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -704,7 +804,7 @@ app.post('/payment-intent-for-payment-sheet', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -726,7 +826,7 @@ app.post('/customer-sheet', async (_, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand Down Expand Up @@ -754,7 +854,7 @@ app.post('/fetch-payment-methods', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand All @@ -771,7 +871,7 @@ app.post('/attach-payment-method', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});
console.log({ customer: req.body.customerId });
Expand All @@ -789,7 +889,7 @@ app.post('/detach-payment-method', async (req, res) => {
const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2022-11-15',
apiVersion: '2023-10-16',
typescript: true,
});

Expand Down
Loading

0 comments on commit 8aca8f7

Please sign in to comment.