Skip payment process if item is free to borrow
This commit is contained in:
@@ -233,12 +233,12 @@ router.post("/", authenticateToken, async (req, res) => {
|
||||
// Calculate fees using FeeCalculator
|
||||
const fees = FeeCalculator.calculateRentalFees(totalAmount);
|
||||
|
||||
// Validate that payment method was provided
|
||||
if (!stripePaymentMethodId) {
|
||||
return res.status(400).json({ error: "Payment method is required" });
|
||||
// Validate that payment method was provided for paid rentals
|
||||
if (totalAmount > 0 && !stripePaymentMethodId) {
|
||||
return res.status(400).json({ error: "Payment method is required for paid rentals" });
|
||||
}
|
||||
|
||||
const rental = await Rental.create({
|
||||
const rentalData = {
|
||||
itemId,
|
||||
renterId: req.user.id,
|
||||
ownerId: item.ownerId,
|
||||
@@ -247,13 +247,19 @@ router.post("/", authenticateToken, async (req, res) => {
|
||||
totalAmount: fees.totalChargedAmount,
|
||||
platformFee: fees.platformFee,
|
||||
payoutAmount: fees.payoutAmount,
|
||||
paymentStatus: "pending",
|
||||
paymentStatus: totalAmount > 0 ? "pending" : "not_required",
|
||||
status: "pending",
|
||||
deliveryMethod,
|
||||
deliveryAddress,
|
||||
notes,
|
||||
stripePaymentMethodId,
|
||||
});
|
||||
};
|
||||
|
||||
// Only add stripePaymentMethodId if it's provided (for paid rentals)
|
||||
if (stripePaymentMethodId) {
|
||||
rentalData.stripePaymentMethodId = stripePaymentMethodId;
|
||||
}
|
||||
|
||||
const rental = await Rental.create(rentalData);
|
||||
|
||||
const rentalWithDetails = await Rental.findByPk(rental.id, {
|
||||
include: [
|
||||
@@ -310,17 +316,19 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
|
||||
return res.status(403).json({ error: "Unauthorized to update this rental" });
|
||||
}
|
||||
|
||||
// If owner is approving a pending rental, charge the stored payment method
|
||||
// If owner is approving a pending rental, handle payment for paid rentals
|
||||
if (
|
||||
status === "confirmed" &&
|
||||
rental.status === "pending" &&
|
||||
rental.ownerId === req.user.id
|
||||
) {
|
||||
if (!rental.stripePaymentMethodId) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "No payment method found for this rental" });
|
||||
}
|
||||
// Skip payment processing for free rentals
|
||||
if (rental.totalAmount > 0) {
|
||||
if (!rental.stripePaymentMethodId) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "No payment method found for this rental" });
|
||||
}
|
||||
|
||||
try {
|
||||
// Import StripeService to process the payment
|
||||
@@ -385,6 +393,31 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
|
||||
details: paymentError.message,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// For free rentals, just update status directly
|
||||
await rental.update({
|
||||
status: "confirmed"
|
||||
});
|
||||
|
||||
const updatedRental = await Rental.findByPk(rental.id, {
|
||||
include: [
|
||||
{ model: Item, as: "item" },
|
||||
{
|
||||
model: User,
|
||||
as: "owner",
|
||||
attributes: ["id", "username", "firstName", "lastName"],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: "renter",
|
||||
attributes: ["id", "username", "firstName", "lastName"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
res.json(updatedRental);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await rental.update({ status });
|
||||
|
||||
Reference in New Issue
Block a user