-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (29 loc) · 1.37 KB
/
index.js
File metadata and controls
38 lines (29 loc) · 1.37 KB
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
const express = require('express');
const axios = require('axios');
// Config Set Up
const targetEnv = 'https://sandbox.dev.clover.com'; // Pointing to Sandbox Environment
// const targetEnv = 'https://www.clover.com'; // Pointing to Prod Environment
const appID = ''; // Input your app ID here
const appSecret = ''; // Input your app secret here
// Initialize Express
const app = express();
// Root Route
app.get('/', (req, res) => authenticate(req, res));
// Steps 1 & 2 - Request merchant authorization to receive authorization code
const authenticate = async (req, res) => {
const url = `${targetEnv}/oauth/authorize?client_id=${appID}`;
/* If there is no code parameter in the query string of the current url
redirect user for authentication. If there isn't then request API token */
!req.query.code ? await res.redirect(url) : await requestAPIToken(res, req.query);
}
// Steps 3 & 4 - Request and serve up API token using the received authorization code
const requestAPIToken = async (res, query) => {
const url = `${targetEnv}/oauth/token?client_id=${appID}&client_secret=${appSecret}&code=${query.code}`;
// Request
await axios.get(url)
.then(({ data }) => res.send(data))
.catch(err => res.send(err.message));
}
// Dynamic Port Binding
const port = process.env.port || 5000
app.listen(port, () => console.log(`🍀 Run http://localhost:${port} in your browser`));