aboutsummaryrefslogtreecommitdiff
path: root/server/controllers/transaction.js
blob: b8c2bf4c158add43c2aa71ca394ee8cc922f4eaa (plain) (blame)
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
const paypal = require('@paypal/checkout-server-sdk');
const paypalClient = require('../config/paypal');
const {siteUrl} = require('../config/env');
const Product = require('../models/Product');
const Order = require('../models/Order');

module.exports = {

  async setup(req, res) {
    let newOrderObj = {
      status: 'ordered',
      paypalOrderId: null,
      userId: req.user._id,
      items: []
    };
    let transactionSetupData = {
      intent: 'CAPTURE',
      application_context: {
        brand_name: "MEVN Store"
      },
      purchase_units: [{
        amount: {
          currency_code: 'USD',
          value: null,
          breakdown: {
            item_total: {
              currency_code: "USD",
              value: null
            }
          }
        },
        items: []
      }]
    };

    if (!req.body.items)
      return res.status(400).json({status: "No items given."});

    let totalToPay = 0;
    for (let i = 0; i < req.body.items.length; i++) {
      const itemId = req.body.items[i].id;

      const itemQuantity = req.body.items[i].quantity;
      if (!itemQuantity)
        return res.status(400).json({status: "No quantity given."});

      let item;
      try {
        item = await Product.findOne({_id: itemId});
      }
      catch {
        return res.status(400).json({status: "Couldn't find one of the given items. CB"});
      }
      if (!item)
        return res.status(400).json({status: "Couldn't find one of the given items."});

      transactionSetupData.purchase_units[0].items.push({
        name: item.name,
        unit_amount: {
          currency_code: "USD",
          value: item.price
        },
        quantity: itemQuantity
      });

      totalToPay += item.price * itemQuantity;

      newOrderObj.items.push({
        productId: itemId,
        name: item.name,
        quantity: itemQuantity
      });
    }
    transactionSetupData.purchase_units[0].amount.value = totalToPay;
    transactionSetupData.purchase_units[0].amount.breakdown.item_total.value = totalToPay;

    const request = new paypal.orders.OrdersCreateRequest();
    request.prefer("return=representation");
    request.requestBody(transactionSetupData);

    let order;
    try {
      order = await paypalClient.execute(request);
    } catch (err) {
      console.error(err);
      return res.sendStatus(500);
    }

    newOrderObj.paypalOrderId = order.result.id;

    const dbOrder = new Order(newOrderObj);
    dbOrder.save()
      .then(() => res.status(200).json({orderId: order.result.id}))
      .catch(err => {
        res.status(500).json({status: "Couldn't save new order to database!"});
        console.error(err);
      });
  },

  async capture(req, res) {
    let dbOrder;
    try {
      dbOrder = await Order.findOne({paypalOrderId: req.body.orderId});
    }
    catch {
      return res.status(400).json({status: "Couldn't find given order in database! CB"});
    }
    if (!dbOrder)
      return res.status(400).json({status: "Couldn't find given order in database!"});

    if (!dbOrder.userId.equals(req.user._id))
      return res.sendStatus(403);

    const request = new paypal.orders.OrdersCaptureRequest(req.body.orderId);
    request.requestBody({});

    let capture;
    try {
      capture = await paypalClient.execute(request);
    } catch (err) {
      console.error(err);
      return res.sendStatus(500);
    }

    dbOrder.status = 'paid';
    dbOrder.shipping = capture.result.purchase_units[0].shipping;

    dbOrder.save()
      .then(() => res.sendStatus(200))
      .catch(err => {
        res.status(500).json({status: "Couldn't update order in database!"})
        console.error(err);
      });
  },

  async showPaid(req, res) {
    const orders = await Order.find({status: 'paid'});
    res.json(orders);
  },

  async showPaidByUser(req, res) {
    const orders = await Order.find({status: 'paid', userId: req.user._id});
    res.json(orders);
  }

};