| 20 | const stripeInstance = new Stripe(API_KEY, { apiVersion: '2022-08-01' }); |
| 21 | |
| 22 | function createSession({ |
| 23 | email, |
| 24 | customerId, |
| 25 | mode, |
| 26 | quantity, |
| 27 | }: { |
| 28 | email: string; |
| 29 | customerId: string; |
| 30 | mode: Stripe.Checkout.SessionCreateParams.Mode; |
| 31 | quantity: number; |
| 32 | }) { |
| 33 | const params: Stripe.Checkout.SessionCreateParams = { |
| 34 | customer_email: customerId ? undefined : email, |
| 35 | customer: customerId, |
| 36 | payment_method_types: ['card'], |
| 37 | mode, |
| 38 | success_url: `${URL_API}/stripe/checkout-completed/{CHECKOUT_SESSION_ID}`, |
| 39 | cancel_url: `${URL_APP}/settings/my-billing?message=You%20did%20not%20complete%20payment%20at%20the%20Stripe%20Checkout%20page%2E`, |
| 40 | metadata: { email }, |
| 41 | }; |
| 42 | |
| 43 | const price_per_seat = dev ? process.env.STRIPE_TEST_PRICE_ID : process.env.STRIPE_LIVE_PRICE_ID; |
| 44 | |
| 45 | if (mode === 'subscription') { |
| 46 | params.line_items = [{ price: price_per_seat, quantity }]; |
| 47 | } else if (mode === 'setup') { |
| 48 | if (!customerId) { |
| 49 | // logger.info('customerId is required'); |
| 50 | throw new Error('customerId is required'); |
| 51 | } |
| 52 | |
| 53 | params.setup_intent_data = { |
| 54 | metadata: { customer_id: customerId }, |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | return stripeInstance.checkout.sessions.create(params); |
| 59 | } |
| 60 | |
| 61 | function retrieveSession({ sessionId }: { sessionId: string }) { |
| 62 | return stripeInstance.checkout.sessions.retrieve(sessionId, { |