aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/config/paypal.js6
-rw-r--r--server/controllers/transaction.js59
-rw-r--r--server/routes/api.js4
3 files changed, 62 insertions, 7 deletions
diff --git a/server/config/paypal.js b/server/config/paypal.js
index da6c2c3..3b45015 100644
--- a/server/config/paypal.js
+++ b/server/config/paypal.js
@@ -8,8 +8,4 @@ function environment() {
return new paypal.core.SandboxEnvironment(paypalClientId, paypalClientSecret);
}
-function client() {
- return new paypal.core.PayPalHttpClient(environment());
-}
-
-module.exports = client;
+module.exports = new paypal.core.PayPalHttpClient(environment());
diff --git a/server/controllers/transaction.js b/server/controllers/transaction.js
index a2226be..c540a7b 100644
--- a/server/controllers/transaction.js
+++ b/server/controllers/transaction.js
@@ -5,11 +5,70 @@ const {siteUrl} = require('../config/env');
module.exports = {
async setup(req, res) {
+ let transactionData = {
+ intent: 'CAPTURE',
+ purchase_units: [{
+ amount: {
+ currency_code: 'USD',
+ value: '180.00',
+ breakdown: {
+ item_total: {
+ currency_code: "USD",
+ value: "180.00"
+ },
+ }
+ },
+ items: [
+ {
+ name: "T-Shirt",
+ unit_amount: {
+ currency_code: "USD",
+ value: "90.00"
+ },
+ quantity: "1"
+ },
+ {
+ name: "Shoes",
+ unit_amount: {
+ currency_code: "USD",
+ value: "45.00"
+ },
+ quantity: "2"
+ }
+ ]
+ }]
+ };
+ const request = new paypal.orders.OrdersCreateRequest();
+ request.prefer("return=representation");
+ request.requestBody(transactionData);
+
+ let order;
+ try {
+ order = await paypalClient.execute(request);
+ } catch (err) {
+ console.error(err);
+ return res.sendStatus(500);
+ }
+ console.log(order);
+
+ res.status(200).json({orderId: order.result.id});
},
async capture(req, res) {
+ const request = new paypal.orders.OrdersCaptureRequest(req.body.orderId);
+ request.requestBody({});
+
+ try {
+ const capture = await paypalClient.execute(request);
+ console.log(capture);
+ console.log(capture.result);
+ } catch (err) {
+ console.error(err);
+ return res.sendStatus(500);
+ }
+ res.sendStatus(200);
}
};
diff --git a/server/routes/api.js b/server/routes/api.js
index c48e169..feea5dd 100644
--- a/server/routes/api.js
+++ b/server/routes/api.js
@@ -17,7 +17,7 @@ router.post('/products', isAuth, isAdmin, upload.single('image'), productsContro
router.patch('/products/:id', isAuth, isAdmin, upload.single('image'), productsController.update);
router.delete('/products/:id', isAuth, isAdmin, productsController.destroy);
-router.post('/transaction/setup', isAuth, transactionController.setup);
-router.get('/transaction/capture', transactionController.capture);
+router.post('/transaction/setup', transactionController.setup);
+router.post('/transaction/capture', transactionController.capture);
module.exports = router;