Last active
July 8, 2023 17:31
-
-
Save jsbeaudry/5f6664fe9d2a05f08d9d7a4d47c25d7b to your computer and use it in GitHub Desktop.
A Checkout Session represents your customer's session as they pay for one-time purchases or subscriptions through Checkout or Payment Links. We recommend creating a new Session each time your customer attempts to pay. Once payment is successful, the Checkout Session will contain a reference to the Customer, and either the successful PaymentInten…
This file contains 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
const Stripe = require("stripe"); | |
const { STRIPE_SECRET_KEY } = process.env; | |
const stripe = Stripe(STRIPE_SECRET_KEY); | |
const [planA, planB, planC] = [ | |
"price_1Mj4hPI4ImfNjweeB0fXE9xA", | |
"price_1Mj4hPI4ImfNjweehhxUwzUh", | |
"price_1Mj4hQI4ImfNjweeP0q6iEuK", | |
]; | |
const stripeSession = async (plan, userId) => { | |
try { | |
const session = await stripe.checkout.sessions.create({ | |
mode: "subscription", | |
payment_method_types: ["card"], | |
line_items: [ | |
{ | |
price: plan, | |
quantity: 1, | |
}, | |
], | |
success_url: "http://localhost:3000/success.html", | |
cancel_url: "http://localhost:3000/cancel.html", | |
client_reference_id: userId, | |
metadata: { userId: userId }, | |
}); | |
return session; | |
} catch (e) { | |
return e; | |
} | |
}; | |
const getSession = async (sessionId) => { | |
try { | |
const session = await stripe.checkout.sessions.retrieve(sessionId); | |
return session; | |
} catch (e) { | |
return e; | |
} | |
}; | |
const checkoutsession = async (plan, userId) => { | |
if (!plan || !userId) return; | |
try { | |
let planId = null; | |
switch (plan) { | |
case 5: | |
planId = planA; | |
case 10: | |
planId = planB; | |
case 20: | |
planId = planC; | |
default: | |
break; | |
} | |
const response = await stripeSession(planId, userId); | |
const { id, url } = response; | |
return { id, url }; | |
} catch (error) { | |
return error; | |
} | |
}; | |
module.exports = { checkoutsession, getSession }; |
This file contains 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
const { checkoutsession, getSession } = require("./subscription"); | |
const testPay = async (plan, userId) => { | |
const response = await checkoutsession(plan, userId); | |
const { sessionId, url } = response; | |
console.log(sessionId, url); | |
}; | |
const testSession = async (sessionId) => { | |
const sessionStripe = await getSession(sessionId); | |
const { status, payment_status, amount_total } = sessionStripe; | |
console.log(status, payment_status, amount_total); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment