-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
71 lines (61 loc) · 1.95 KB
/
proxy.js
File metadata and controls
71 lines (61 loc) · 1.95 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
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
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const port = 3001;
app.use(express.json());
app.use(cors());
let token = '';
const authUrl = 'https://enrollment-api-auth.paymentshub.com/oauth/token';
const payloadUrl = 'https://enrollment-api-sandbox.paymentshub.com/enroll/application';
const data = {
grant_type: 'client_credentials',
scope: 'all',
client_id: 'your_client_id',
client_secret: 'your_client_secret'
};
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const params = new URLSearchParams(data);
app.get('/oauth/token', async (req,res) => {
try {
axios.post(authUrl, params, config)
.then(response => {
console.log('Response:', response.data);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.send(response.data);
token = response.data.access_token;
})
.catch(error => {
console.error('Error:', error);
});
} catch (error) {
res.status(500).send(error.message);
}
});
app.post('/create_application', async (req, res)=>{
axios.post(payloadUrl, req.body,
{headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}})
.then(response => {
console.log('Response:', response.data);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.send(response.data);
})
.catch(error => {
console.error('Error:', error);
res.send(error);
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});