-
Notifications
You must be signed in to change notification settings - Fork 12
/
routes.js
50 lines (41 loc) · 1.37 KB
/
routes.js
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
/**
* routes.js
* Xendit Checkout Demo
* This file defines all the endpoints for this demo (on the api/backend side).
*/
'use strict';
const express = require('express');
const router = express.Router();
// load the controller
const InvoiceController = require('./controller');
const invoiceController = new InvoiceController();
// load the configuration file
const config = require('./config');
/**
* Xendit integration to create invoice
* 1. GET /api/healthcheck/readiness to make sure the server is up and running
* 2. POST /api/invoice to create invoice (proxy from this backend to Xendit API Gateway)
*/
router.get('/api/healthcheck/readiness', (req, res) => {
res.json({
status: 'ok'
});
});
router.post('/api/invoice', async (req, res) => {
try {
// you can change the config with your business details
const data = {
...config.invoiceData,
external_id: `checkout-demo-${+new Date()}`,
currency: req.body.currency,
amount: req.body.amount,
failure_redirect_url: req.body.redirect_url,
success_redirect_url: req.body.redirect_url
};
const invoice = await invoiceController.create(data);
return res.status(200).send(invoice.data);
} catch (e) {
return res.status(e.response.status).send(e.response.data);
}
});
module.exports = router;