This repository was archived by the owner on Mar 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathcheckout.ts
203 lines (178 loc) · 5.77 KB
/
checkout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { getConfig, initializeDatabaseConfiguration } from "../config";
import { ReservationRequest } from "../models/reservation-request";
import { getListingById } from "../models/listing";
import {
findReservationsByListingIdAndDateRange,
saveReservation,
updateReservationStatus,
} from "../models/reservation";
import { Listing } from "../models/listing.schema";
import { ReservationStatus } from "../models/reservation-status";
// POST: Checkout
export async function postCheckout(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function postCheckout processed request for url "${request.url}"`);
await initializeDatabaseConfiguration();
const config = await getConfig();
const reservation = (await request.json()) as ReservationRequest;
const guests = Number(reservation.guests) || 0;
const from = new Date(reservation.from);
const to = new Date(reservation.to);
const now = localeDateToUTCDate(new Date());
if (!reservation.userId) {
return {
status: 400,
jsonBody: {
error: "User ID is required",
},
};
}
if (!reservation.listingId) {
return {
status: 400,
jsonBody: {
error: "Listing ID is required",
},
};
}
const listing = await getListingById({ id: reservation.listingId });
if (!listing) {
return {
status: 404,
jsonBody: {
error: "Listing not found for specified id",
},
};
}
if (guests <= 0 || guests > listing.capacity) {
context.error(`Invalid number of guests: ${guests} (listing capacity: ${listing.capacity})`);
return {
status: 400,
jsonBody: {
error: "Invalid number of guests",
},
};
}
if (!reservation.from || from.getTime() < now.getTime()) {
context.error(
`Invalid reservation start date: ${reservation.from} (resolved: ${from.toISOString()}, now: ${now.toISOString()}`,
);
return {
status: 400,
jsonBody: {
error: "Invalid reservation start date",
},
};
}
if (!reservation.to || to.getTime() <= from.getTime()) {
context.error(
`Invalid reservation end date: ${reservation.to} (resolved: ${to.toISOString()}, from: ${from.toISOString()}`,
);
return {
status: 400,
jsonBody: {
error: "Invalid reservation end date",
},
};
}
const overlaps = await findReservationsByListingIdAndDateRange(listing.id, from.toISOString(), to.toISOString());
if (overlaps.length > 0) {
return {
status: 400,
jsonBody: {
error: `Reservation overlaps with existing reservation(s): ${overlaps
.map(o => o.id)
.join(", ")} (from: ${from.toISOString()}, to: ${to.toISOString()})`,
},
};
}
const total = calculateTotal(listing, guests, from, to);
const amount = Math.round(total * 100);
const name = `Booking ${listing.title}`;
let reservationRecord;
try {
const currency = listing.fees[5].split(":")[0];
const pendingReservation = {
userId: reservation.userId,
listingId: reservation.listingId,
title: listing.title,
guests,
from,
to,
status: ReservationStatus.Pending as const,
createdAt: now,
};
reservationRecord = await saveReservation(pendingReservation);
try {
const checkout = {
productName: name,
reservationId: reservationRecord.id,
userId: reservation.userId,
listingId: reservation.listingId,
guests,
from: from.toISOString(),
to: to.toISOString(),
currency: currency.toLowerCase(),
amount,
createdAt: now.toISOString(),
};
const response = await fetch(config.stripeServiceUrl + "/stripe/checkout", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(checkout),
});
if (!response.ok) {
const error = await response.text();
throw new Error(error);
}
const { sessionUrl } = (await response.json()) as any;
return {
jsonBody: {
sessionUrl,
},
};
} catch (error: unknown) {
const err = error as Error;
context.error(`Error creating checkout session: ${err.message}`);
await updateReservationStatus(reservationRecord.id, ReservationStatus.Cancelled);
throw error;
}
} catch (error: unknown) {
const err = error as Error;
context.error(`Error creating checkout session: ${err.message}`);
return {
status: 500,
jsonBody: {
error: "Cannot create checkout session",
},
};
}
function getMonths(from: Date, to: Date) {
if (!from || !to) {
return 0;
}
const days = (to.getTime() - from.getTime()) / 1000 / 60 / 60 / 24;
return +(days / 30).toFixed(2);
}
function calculateTotal(listing: Listing, guests: number, from: Date, to: Date): number {
const months = getMonths(from, to);
const monthlyRentPrice = Number(listing.fees[4]) || 0;
const monthlyRentPriceWithDiscount = Math.max(
0,
monthlyRentPrice * (1 - (parseInt(listing?.fees?.[4], 10) || 0) / 100),
);
const cleaning = Number(listing.fees[0]) || 0;
const service = Number(listing.fees[1]) || 0;
const occupancy = (Number(listing.fees[2]) || 0) * guests;
const total = months * monthlyRentPriceWithDiscount + cleaning + service + occupancy;
return total;
}
// We're only interested in the date day relative to the current timezone,
// so we need to convert the date to UTC to avoid any timezone offset
function localeDateToUTCDate(date: Date) {
const utc = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
return new Date(utc);
}
}