-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
165 lines (149 loc) · 4.36 KB
/
Copy pathserver.js
File metadata and controls
165 lines (149 loc) · 4.36 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express')
const app = express()
const bodyparser = require('body-parser')
const cors = require('cors')
const dotenv = require('dotenv').config()
const fetch = require('node-fetch')
const shortid = require('short-id')
const path = require('path')
const port = process.env.PORT || 8000
app.use(express.static(path.join(__dirname, 'client/build')))
// App Configuration
app.use(bodyparser.json())
app.use(cors())
app.get('/', (req, res) => {
res.send('Fake Cakes')
})
// Create a transfer recipient
app.post('/api/create_recipient', (req, res) => {
const body = { ...req.body, type: 'nuban', currency: 'NGN' }
fetch('https://api.paystack.co/transferrecipient', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
}
})
.then(res => res.json())
.then(newRecipient => res.send(newRecipient))
.catch(e => res.send(e))
})
// List transfer recipients
app.get('/api/list_recipients', (req, res) => {
fetch('https://api.paystack.co/transferrecipient', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
}
})
.then(res => res.json())
.then(response => res.send(response))
.catch(e => res.send(e))
})
// Update transfer recipient
app.put('/api/update_recipient/:id', (req, res) => {
const recipient_id = req.params.id
const body = { ...req.body }
console.log(body)
fetch(`https://api.paystack.co/transferrecipient/${recipient_id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
},
body: JSON.stringify(body)
})
.then(res => res.json())
.then(result => res.send(result))
.catch(e => res.send(e))
})
// Delete transfer recipient
app.delete('/api/delete_recipient/:id', (req, res) => {
const recipient_id = req.params.id
fetch(`https://api.paystack.co/transferrecipient/${recipient_id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
}
})
.then(res => res.json())
.then(result => res.send(result))
.catch(e => res.send(e))
})
// Initiate a transfer
app.post('/api/initiate_transfer', (req, res) => {
const reference = shortid.generate()
const body = { ...req.body, source: 'balance', currency: 'NGN', reference }
fetch('https://api.paystack.co/transfer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
},
body: JSON.stringify(body)
})
.then(res => res.json())
.then(result => res.send(result))
.catch(e => res.send(e))
})
// Finalize transfer
app.post('/api/finalize_transfer', (req, res) => {
const body = { ...req.body }
fetch('https://api.paystack.co/transfer/finalize_transfer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
},
body: JSON.stringify(body)
})
.then(res => res.json())
.then(result => res.send(result))
.catch(e => res.send(e))
})
// Bulk transfer
app.post('/api/bulk_transfer', (req, res) => {
const body = req.body.recipients
fetch('https://api.paystack.co/transfer/bulk', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
},
body: JSON.stringify(body)
})
.then(res => res.json())
.then(result => res.send(result))
.catch(e => res.send(e))
})
// List transfers
app.get('/api/list_transfers', (req, res) => {
fetch('https://api.paystack.co/transfer', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
}
})
.then(res => res.json())
.then(transfers => res.send(transfers))
.catch(e => res.send(e))
})
// Check balance
app.get('/api/check_balance', (req, res) => {
fetch('https://api.paystack.co/balance', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.bearer}`
}
})
.then(res => res.json())
.then(balance => res.send(balance))
.catch(e => res.send(e))
})
// Start server
app.listen(port, () => console.log(`Server running on port ${port}`))