Skip payment process if item is free to borrow

This commit is contained in:
jackiettran
2025-09-22 22:02:08 -04:00
parent 3e76769a3e
commit 67cc997ddc
7 changed files with 245 additions and 68 deletions

View File

@@ -62,7 +62,7 @@ const Rental = sequelize.define("Rental", {
defaultValue: "pending",
},
paymentStatus: {
type: DataTypes.ENUM("pending", "paid", "refunded"),
type: DataTypes.ENUM("pending", "paid", "refunded", "not_required"),
defaultValue: "pending",
},
payoutStatus: {

View File

@@ -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 });

View File

@@ -92,8 +92,8 @@ class RefundService {
};
}
// Check payment status
if (rental.paymentStatus !== "paid") {
// Check payment status - allow cancellation for both paid and free rentals
if (rental.paymentStatus !== "paid" && rental.paymentStatus !== "not_required") {
return {
canCancel: false,
reason: "Cannot cancel rental that hasn't been paid",

View File

@@ -323,7 +323,7 @@ describe('Rentals Routes', () => {
expect(response.body).toEqual({ error: 'Item is already booked for these dates' });
});
it('should return 400 when payment method is missing', async () => {
it('should return 400 when payment method is missing for paid rentals', async () => {
const dataWithoutPayment = { ...rentalData };
delete dataWithoutPayment.stripePaymentMethodId;
@@ -332,7 +332,49 @@ describe('Rentals Routes', () => {
.send(dataWithoutPayment);
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'Payment method is required' });
expect(response.body).toEqual({ error: 'Payment method is required for paid rentals' });
});
it('should create a free rental without payment method', async () => {
// Set up a free item (both prices are 0)
Item.findByPk.mockResolvedValue({
id: 1,
ownerId: 2,
availability: true,
pricePerHour: 0,
pricePerDay: 0
});
const freeRentalData = { ...rentalData };
delete freeRentalData.stripePaymentMethodId;
const createdRental = {
id: 1,
...freeRentalData,
renterId: 1,
ownerId: 2,
totalAmount: 0,
platformFee: 0,
payoutAmount: 0,
paymentStatus: 'not_required',
status: 'pending'
};
Rental.create.mockResolvedValue(createdRental);
Rental.findByPk.mockResolvedValue({
...createdRental,
item: { id: 1, name: 'Free Item' },
owner: { id: 2, username: 'owner1', firstName: 'John', lastName: 'Doe' },
renter: { id: 1, username: 'renter1', firstName: 'Jane', lastName: 'Smith' }
});
const response = await request(app)
.post('/rentals')
.send(freeRentalData);
expect(response.status).toBe(201);
expect(response.body.paymentStatus).toBe('not_required');
expect(response.body.totalAmount).toBe(0);
});
it('should handle database errors during creation', async () => {
@@ -422,6 +464,34 @@ describe('Rentals Routes', () => {
});
});
it('should approve free rental without payment processing', async () => {
const freeRental = {
...mockRental,
totalAmount: 0,
paymentStatus: 'not_required',
stripePaymentMethodId: null,
update: jest.fn().mockResolvedValue(true)
};
mockRentalFindByPk.mockResolvedValueOnce(freeRental);
const updatedFreeRental = {
...freeRental,
status: 'confirmed'
};
mockRentalFindByPk.mockResolvedValueOnce(updatedFreeRental);
const response = await request(app)
.put('/rentals/1/status')
.send({ status: 'confirmed' });
expect(response.status).toBe(200);
expect(StripeService.chargePaymentMethod).not.toHaveBeenCalled();
expect(freeRental.update).toHaveBeenCalledWith({
status: 'confirmed'
});
});
it('should return 400 when renter has no Stripe customer ID', async () => {
const rentalWithoutStripeCustomer = {
...mockRental,

View File

@@ -295,6 +295,17 @@ describe('RefundService', () => {
cancelledBy: null
});
});
it('should allow cancellation for free rental with not_required payment status', () => {
const rental = { ...baseRental, paymentStatus: 'not_required' };
const result = RefundService.validateCancellationEligibility(rental, 100);
expect(result).toEqual({
canCancel: true,
reason: 'Cancellation allowed',
cancelledBy: 'renter'
});
});
});
describe('Edge cases', () => {