174 lines
4.8 KiB
JavaScript
174 lines
4.8 KiB
JavaScript
const express = require("express");
|
|
const { authenticateToken } = require("../middleware/auth");
|
|
const { User, Item } = require("../models");
|
|
const StripeService = require("../services/stripeService");
|
|
const router = express.Router();
|
|
|
|
// Get checkout session status
|
|
router.get("/checkout-session/:sessionId", async (req, res) => {
|
|
try {
|
|
const { sessionId } = req.params;
|
|
|
|
const session = await StripeService.getCheckoutSession(sessionId);
|
|
|
|
res.json({
|
|
status: session.status,
|
|
payment_status: session.payment_status,
|
|
customer_email: session.customer_details?.email,
|
|
setup_intent: session.setup_intent,
|
|
metadata: session.metadata,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error retrieving checkout session:", error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Create connected account
|
|
router.post("/accounts", authenticateToken, async (req, res) => {
|
|
try {
|
|
const user = await User.findByPk(req.user.id);
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ error: "User not found" });
|
|
}
|
|
|
|
// Check if user already has a connected account
|
|
if (user.stripeConnectedAccountId) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "User already has a connected account" });
|
|
}
|
|
|
|
// Create connected account
|
|
const account = await StripeService.createConnectedAccount({
|
|
email: user.email,
|
|
country: "US", // You may want to make this configurable
|
|
});
|
|
|
|
// Update user with account ID
|
|
await user.update({
|
|
stripeConnectedAccountId: account.id,
|
|
});
|
|
|
|
res.json({
|
|
stripeConnectedAccountId: account.id,
|
|
success: true,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating connected account:", error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Generate onboarding link
|
|
router.post("/account-links", authenticateToken, async (req, res) => {
|
|
try {
|
|
const user = await User.findByPk(req.user.id);
|
|
|
|
if (!user || !user.stripeConnectedAccountId) {
|
|
return res.status(400).json({ error: "No connected account found" });
|
|
}
|
|
|
|
const { refreshUrl, returnUrl } = req.body;
|
|
|
|
if (!refreshUrl || !returnUrl) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "refreshUrl and returnUrl are required" });
|
|
}
|
|
|
|
const accountLink = await StripeService.createAccountLink(
|
|
user.stripeConnectedAccountId,
|
|
refreshUrl,
|
|
returnUrl
|
|
);
|
|
|
|
res.json({
|
|
url: accountLink.url,
|
|
expiresAt: accountLink.expires_at,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating account link:", error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Get account status
|
|
router.get("/account-status", authenticateToken, async (req, res) => {
|
|
try {
|
|
const user = await User.findByPk(req.user.id);
|
|
|
|
if (!user || !user.stripeConnectedAccountId) {
|
|
return res.status(400).json({ error: "No connected account found" });
|
|
}
|
|
|
|
const accountStatus = await StripeService.getAccountStatus(
|
|
user.stripeConnectedAccountId
|
|
);
|
|
|
|
res.json({
|
|
accountId: accountStatus.id,
|
|
detailsSubmitted: accountStatus.details_submitted,
|
|
payoutsEnabled: accountStatus.payouts_enabled,
|
|
capabilities: accountStatus.capabilities,
|
|
requirements: accountStatus.requirements,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error getting account status:", error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Create embedded setup checkout session for collecting payment method
|
|
router.post("/create-setup-checkout-session", authenticateToken, async (req, res) => {
|
|
try {
|
|
const { rentalData } = req.body;
|
|
|
|
const user = await User.findByPk(req.user.id);
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ error: "User not found" });
|
|
}
|
|
|
|
// Create or get Stripe customer
|
|
let stripeCustomerId = user.stripeCustomerId;
|
|
|
|
if (!stripeCustomerId) {
|
|
// Create new Stripe customer
|
|
const customer = await StripeService.createCustomer({
|
|
email: user.email,
|
|
name: `${user.firstName} ${user.lastName}`,
|
|
metadata: {
|
|
userId: user.id.toString()
|
|
}
|
|
});
|
|
|
|
stripeCustomerId = customer.id;
|
|
|
|
// Save customer ID to user record
|
|
await user.update({ stripeCustomerId });
|
|
}
|
|
|
|
// Add rental data to metadata if provided
|
|
const metadata = rentalData ? {
|
|
rentalData: JSON.stringify(rentalData)
|
|
} : {};
|
|
|
|
const session = await StripeService.createSetupCheckoutSession({
|
|
customerId: stripeCustomerId,
|
|
metadata
|
|
});
|
|
|
|
res.json({
|
|
clientSecret: session.client_secret,
|
|
sessionId: session.id
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating setup checkout session:", error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|