-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
92 lines (79 loc) · 2.15 KB
/
index.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
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
'use strict';
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Payment = require('./models/payment');
const axios = require('axios');
const app = express();
const port = process.env.PORT || 3000;
var path = require('path');
app.get('*', function(req, res) {
res.sendfile(path.resolve('views/index.html'));
});
require('dotenv').config();
// database
mongoose.connect('mongodb://localhost:27017/checkathon', { useNewUrlParser: true });
//config
app.use(express.static('public'));
app.use(express.urlencoded())
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//routing
const makePayment = async(payload) => {
try {
const temporaryPayload = {
amount: {
value: payload.value,
currency: payload.currency
},
paymentMethod: {
type: 'scheme',
number: payload.cardNumber,
expiryMonth: payload.expiryMonth,
expiryYear: payload.expiryYear,
holderName: payload.holderName,
cvc: payload.cvc
}
};
const request = {
amount: {
currency: 'EUR',
value: 5000
},
reference: 'Your order number',
merchantAccount: 'TeamFour',
...temporaryPayload
};
return await axios({
method: 'post',
url: 'https://checkout-test.adyen.com/v41/payments',
headers: {
'Content-Type': 'application/json',
'x-API-key': process.env.API_KEY
},
data: request})
} catch (error) {
console.log(error);
}
}
app.post('/payments', function(req, res) {
makePayment(req.body)
.then(result => {
const newPayment = {
pspReference: result.data.pspReference,
cardHolderName: result.data.additionalData.cardHolderName
};
Payment.create(newPayment)
.then(payment => {
console.log('saved')
res.send({ message: 'Payment authorised' });
})
.catch(error => {
res.status(500).send({ error: 'upsss' });
next(error)
})
})
});
app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
})