Created
January 13, 2023 04:55
-
-
Save dipeshhkc/ef1e02273783424416523f5a8634b84a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Stripe from 'stripe'; | |
import { UserWithSubscription } from './user.server'; | |
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { | |
apiVersion: '2022-11-15', | |
}); | |
export const stripeCheckout = async ({ | |
priceId, | |
tier, | |
user, | |
}: { | |
priceId: string; | |
tier: string; | |
user: UserWithSubscription; | |
}): Promise<string> => { | |
const lineItems = [ | |
{ | |
price: priceId, | |
quantity: 1, | |
}, | |
]; | |
const customerData = user?.subscription?.stripe_customer_id | |
? { customer: user?.subscription?.stripe_customer_id } | |
: { customer_email: user!.email }; | |
const session = await stripe.checkout.sessions.create({ | |
...customerData, | |
mode: 'subscription', | |
client_reference_id: String(user!.id), | |
payment_method_types: ['card', 'us_bank_account'], | |
line_items: lineItems, | |
success_url: `${process.env.ORIGIN}/dashboard`, | |
cancel_url: `${process.env.ORIGIN}/subscriptions`, | |
allow_promotion_codes: true, | |
metadata: { tier }, | |
}); | |
return session.url!; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment