summaryrefslogtreecommitdiff
path: root/api-src/createPaypalOrder.ts
blob: 5d30e1fb900b6bb59bf6a55f3034dd0ba76beaa5 (plain)
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
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;
  console.info('api/createPaypalOrder: Received body: ');
  console.info('productDescription: ' + productDescription);
  console.info('shortDescription: ' + shortDescription);
  console.info('totalPrice: ' + totalPrice);

  if (request.method !== 'POST') {
    console.warn('Method was not POST. Method was ' + request.method);
    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}`);
    }
  }
}