-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-token.js
51 lines (48 loc) · 1.12 KB
/
get-token.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
require("dotenv").config();
const axios = require("axios");
const URL = "https://gateway.paymongo.com/auth";
const { PAYMONGO_EMAIL, PAYMONGO_PASS } = process.env;
exports.handler = function (event, context, callback) {
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: "Method Not Allowed",
headers: { Allow: "POST" },
};
}
let payload = {
data: {
attributes: {
email: PAYMONGO_EMAIL,
password: PAYMONGO_PASS,
},
},
};
// send response
const send = (body) => {
let response = {};
response.token = body.data.id;
let date = new Date();
response.expired_at = date.setTime(
date.getTime() + body.data.attributes.expiry_timer * 1000
);
callback(null, {
statusCode: 200,
body: JSON.stringify(response),
});
};
// perform api call
const getApiToken = () => {
axios({
method: "post",
url: URL,
data: payload,
})
.then((res) => send(res.data))
.catch((err) => send(err));
};
// Makesure method is GET
if (event.httpMethod == "POST") {
getApiToken();
}
};