/
home
/
infinitibizsol
/
.trash
/
tests.2
/
File Upload :
llllll
Current File: /home/infinitibizsol/.trash/tests.2/stripe-payment.js
// Define routes for payment processing app.post("/process-payment", async (req, res) => { try { const { total_amount, card_number, exp_month, exp_year, cvc } = req.body; const token = await stripe.tokens.create({ card: { number: card_number, exp_month: exp_month, exp_year: exp_year, cvc: cvc, }, }); const charge = await stripe.charges.create({ amount: Math.floor(total_amount * 100), // Convert amount to cents currency: "usd", // source: token.id, source: "tok_visa", description: "Payment Description", }); if (charge.status === "succeeded") { return res.json({ success: true, message: "Payment successful" }); } else { return res.status(400).json({ success: false, error: "Payment failed" }); } } catch (error) { return res.status(500).json({ success: false, error: error.message }); } }); // const stripe = require("stripe")("sk_test_BHlJPzC6PloLo7ELEKksI1uy00LlQbLa2X"); // const stripe = require("stripe")(process.env.STRIPE_SECRETE_KEY); const stripe = require("stripe")(process.env.STRIPE_SECRETE_KEY); const createCustomer = async (req, res) => { try { const customer = await stripe.customers.create({ name: req.body.name, email: req.body.email, }); res.status(200).send(customer); } catch (error) { res.status(400).send({ success: false, msg: error.message }); } }; const addNewCard = async (req, res) => { try { const { customer_id, card_Name, card_ExpYear, card_ExpMonth, card_Number, card_CVC, } = req.body; const card_token = await stripe.tokens.create({ card: { name: card_Name, number: card_Number, exp_year: card_ExpYear, exp_month: card_ExpMonth, cvc: card_CVC, }, }); const card = await stripe.customers.createSource(customer_id, { source: `${card_token.id}`, }); res.status(200).send({ card: card.id }); } catch (error) { res.status(400).send({ success: false, msg: error.message }); } }; const createCharges = async (req, res) => { try { const createCharge = await stripe.charges.create({ receipt_email: "tester@gmail.com", amount: parseInt(req.body.amount) * 100, //amount*100 currency: "usd", card: req.body.card_id, customer: req.body.customer_id, }); res.status(200).send(createCharge); } catch (error) { res.status(400).send({ success: false, msg: error.message }); } }; const PaymentTesting = async (req, res) => { try { const { total_amount, card_number, exp_month, exp_year, cvc } = req.body; const amountInDollars = parseInt(total_amount); const amountInCents = Math.round(amountInDollars * 100); const token = await stripe.tokens.create({ card: { number: card_number, exp_month: exp_month, exp_year: exp_year, cvc: cvc, }, }); const charge = await stripe.charges.create({ amount: amountInCents, // Convert amount to cents currency: "usd", // source: token.id, source: "tok_visa", description: "Payment Description", }); if (charge.status === "succeeded") { return res.json({ success: true, message: "Payment successful" }); } else { return res.status(400).json({ success: false, error: "Payment failed" }); } } catch (error) { return res.status(500).json({ success: false, error: error.message }); } }; const PaymentMethodTesting = async (req, res) => { try { const paymentMethod = await stripe.paymentMethods.create({ type: "card", card: { number: "4242424242424242", exp_month: 12, exp_year: 2034, cvc: "314", }, }); // const paymentMethod = await stripe.paymentMethods.attach( // 'pm_1OISNoJARDo7ZutI7gwh8k6K', // {customer: 'cus_P2dJoUnwk0l4Xf'} // ); res.json({ data: paymentMethod }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); } }; function handlePaymentSuccess(paymentIntent) { // Extract relevant information from paymentIntent const paymentMethod = paymentIntent.payment_method; const amountReceived = paymentIntent.amount_received; console.log( `PaymentIntent succeeded: ${paymentMethod}, Amount: ${amountReceived}` ); } const paymentController = async (req, res) => { // return res.json({ message: "Success" }); const amountInDollars = parseInt(req.body.amount); const amountInCents = Math.round(amountInDollars * 100); try { let customerId; //Gets the customer who's email id matches the one sent by the client const customerList = await stripe.customers.list({ email: req.body.email, limit: 1, }); //Checks the if the customer exists, if not creates a new customer if (customerList.data.length !== 0) { customerId = customerList.data[0].id; } else { const customer = await stripe.customers.create({ email: req.body.email, name: req.body.name, }); customerId = customer.data.id; } //Creates a temporary secret key linked with the customer const ephemeralKey = await stripe.ephemeralKeys.create( { customer: customerId }, { apiVersion: "2020-08-27" } ); const paymentIntent = await stripe.paymentIntents.create({ amount: amountInCents, currency: "usd", customer: customerId, // payment_method_types: ["card"], }); const customer = await stripe.customers.retrieve(customerId); const cards = customer; console.log(cards); console.log("customer details ended"); const paymentMethods = await stripe.customers.listPaymentMethods( `${customerId}`, { type: "card" } ); res.json({ clientSecret: paymentIntent.client_secret, ephemeralKey: ephemeralKey.secret, customer: customerId, success: true, }); // res.json({ "payment-intent": paymentIntent }); } catch (error) { console.error(error); res.status(500).json({ error: "Internal Server Error" }); } }; module.exports = { paymentController, createCustomer, addNewCard, createCharges, PaymentTesting, PaymentMethodTesting, };
Copyright ©2k19 -
Hexid
|
Tex7ure