summaryrefslogtreecommitdiff
path: root/api-src/createPaypalOrder.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api-src/createPaypalOrder.ts')
-rw-r--r--api-src/createPaypalOrder.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/api-src/createPaypalOrder.ts b/api-src/createPaypalOrder.ts
new file mode 100644
index 0000000..c0053d4
--- /dev/null
+++ b/api-src/createPaypalOrder.ts
@@ -0,0 +1,34 @@
+import type { VercelRequest, VercelResponse } from '@vercel/node';
+import type Order from './Order';
+import { paypalCreateOrder } from './_paypal';
+
+export default async function handler(request: VercelRequest, response: VercelResponse) {
+ const { productDescription, shortDescription, totalPrice }: Partial<Order> = request.body;
+
+ if (request.method !== 'POST') {
+ response.status(404);
+ return;
+ }
+
+ if (!productDescription) {
+ response.status(400).send('Expected body to contain productDescription, but none provided.');
+ } else if (!shortDescription) {
+ response.status(400).send('Expected body to contain shortDescription, but none provided.');
+ } else if (!totalPrice) {
+ response.status(400).send('Expected body to contain totalPrice, but none provided.');
+ } else {
+ try {
+ const paypalResponse = await paypalCreateOrder({ productDescription, shortDescription, totalPrice });
+
+ if (paypalResponse.ok) {
+ response.status(200).json({
+ orderId: paypalResponse.id,
+ });
+ } else {
+ throw new Error(`PayPal returned ${paypalResponse.statusCode} ${paypalResponse.statusMessage}`);
+ }
+ } catch (err) {
+ response.status(500).send(`Internal server error. ${err}`);
+ }
+ }
+}