Sindbad~EG File Manager
const stripe = require("stripe")(process.env.STRIPE_SECRETE_KEY);
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}`
);
}
let subscriptionData = {
auctioneer_id: null,
user_id: null,
};
const paymentController = async (req, res) => {
// return res.json({ message: "Success" });
const { auctioneer_id, user_id, email, amount, name } = req.body;
if (!auctioneer_id && !user_id) {
return res.json({
status: 401,
message: "Bad Request,autioneer_id and user_id must required",
});
}
const amountInDollars = parseInt(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: 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: email,
name: 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"],
});
subscriptionData.auctioneer_id = auctioneer_id;
subscriptionData.user_id = user_id;
console.log(subscriptionData);
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" });
}
};
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 { email, name, pamentMethodId } = req.body;
let customerId;
if (!req.body.email || req.body.email === "") {
return res.json({ status: 401, message: "please provide email" });
}
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: email,
name: name,
});
if (customer) {
customerId = customer.id;
} else {
return res.json({ status: 402, message: "Something went wrong" });
}
}
const paymentMethod = await stripe.paymentMethods.attach(pamentMethodId, {
customer: customerId,
});
res.status(200).send({ PaymentMethod: paymentMethod });
} catch (error) {
res.status(400).send({ success: false, msg: error.message });
}
};
const detachPaymentMethod = async (req, res) => {
try {
const { paymentMethodId } = req.body;
if (!paymentMethodId) {
return res.json({
status: 200,
message: "Please provide PaymentMethodId",
});
}
const detachedPaymentMethod = await stripe.paymentMethods.detach(
`${paymentMethodId}`
);
console.log(detachedPaymentMethod);
return res.json({
success: true,
message: "Payment method detached successfully.",
data: detachedPaymentMethod,
});
} 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 PaymentMethods = async (req, res) => {
try {
let customerId;
if (!req.body.email || req.body.email === "") {
return res.json({ status: 401, message: "please provide email" });
}
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) {
return res.json({ status: 400, message: "Customer Not Found" });
}
customerId = customerList.data[0].id;
const paymentMethods = await stripe.customers.listPaymentMethods(
`${customerId}`,
{ type: "card" }
);
// const paymentMethod = await stripe.paymentMethods.attach(
// 'pm_1OISNoJARDo7ZutI7gwh8k6K',
// {customer: 'cus_P2dJoUnwk0l4Xf'}
// );
// console.log(paymentMethods);
const cardsList = paymentMethods.data.map((item) => {
return {
payemen_method_id: item.id,
customer_id: item.customer,
card: item.card,
};
});
// res.json({ data: paymentMethods.data });
res.json({ status: 200, data: cardsList });
} catch (error) {
return res.status(500).json({ success: false, error: error.message });
}
};
module.exports = {
paymentController,
createCustomer,
addNewCard,
createCharges,
PaymentTesting,
PaymentMethods,
subscriptionData,
detachPaymentMethod,
};
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists