-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-link.js
92 lines (80 loc) · 2.22 KB
/
create-link.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
92
require("dotenv").config();
const axios = require("axios");
const CREATE_URI = "https://gateway.paymongo.com/transactions";
const AUTH_URI = "https://gateway.paymongo.com/auth";
const { PAYMONGO_EMAIL, PAYMONGO_PASS, PAYMONGO_LIVEMODE } = process.env;
exports.handler = async function (event) {
if (event.httpMethod !== "POST") {
let error = {
statusCode: 405,
body: "Method Not Allowed",
headers: { Allow: "POST" },
};
return error;
}
let credentials = {
data: {
attributes: {
email: PAYMONGO_EMAIL,
password: PAYMONGO_PASS,
},
},
};
const getApiToken = async () => {
try {
const res = await axios({
method: "post",
url: AUTH_URI,
data: credentials,
});
const token = await res.data.data.id;
return token;
} catch (error) {
console.log(error);
return error;
}
};
const { amount, description, remarks = "" } = JSON.parse(event.body);
if (!amount || !description) {
let error = {
statusCode: 422,
body: "amount, and description are required.",
};
return error;
}
let livemode = false;
if (PAYMONGO_LIVEMODE === true || PAYMONGO_LIVEMODE == "true") {
livemode = true;
}
let payload = {
data: {
attributes: {
amount,
description,
livemode,
remarks,
},
},
};
const createLink = async (token) => {
axios.defaults.headers.common["Authorization"] = "Bearer " + token;
try {
const res = await axios({
method: "post",
url: CREATE_URI,
data: payload,
});
const data = await res.data;
return data;
} catch (error) {
console.log(error);
return error;
}
};
const token = await getApiToken();
const data = await createLink(token);
return {
statusCode: 200,
body: JSON.stringify(data),
};
};