From 925f6de56ef43013583a48dbc49a350ee84cdb3e Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sat, 5 Aug 2023 21:37:27 +0100 Subject: Defines createPaypalOrder endpoint --- api/createPaypalOrder.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 api/createPaypalOrder.ts (limited to 'api') diff --git a/api/createPaypalOrder.ts b/api/createPaypalOrder.ts new file mode 100644 index 0000000..1a6e810 --- /dev/null +++ b/api/createPaypalOrder.ts @@ -0,0 +1,28 @@ +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 = request.body; + + if (request.method !== 'POST') { + response.status(400); + 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 { + const paypalResponse = await paypalCreateOrder({ productDescription, shortDescription, totalPrice }); + + if (paypalResponse.ok) { + response.status(200).json({ + orderId: paypalResponse.id, + }); + } + } +} -- cgit v1.2.3