-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-refund.js
389 lines (347 loc) · 11 KB
/
create-refund.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
if (!process.env.NETLIFY) {
require("dotenv").config();
}
const { GoogleSpreadsheet } = require("google-spreadsheet");
const axios = require("axios");
const AWS = require("aws-sdk");
const {
GOOGLE_SERVICE_ACCOUNT_EMAIL,
GOOGLE_PRIVATE_KEY,
GOOGLE_SPREADSHEET_ID_FROM_URL,
PAYMONGO_EMAIL,
PAYMONGO_PASS,
PAYMONGO_LIVEMODE,
PAYMONGO_MERCHANT_NAME,
SES_ACCESS_KEY,
SES_SECRET_KEY,
SES_REGION,
REFUND_POLICY_HRS,
} = process.env;
if (!GOOGLE_SERVICE_ACCOUNT_EMAIL)
throw new Error("No GOOGLE_SERVICE_ACCOUNT_EMAIL env var set");
if (!GOOGLE_PRIVATE_KEY) throw new Error("No GOOGLE_PRIVATE_KEY env var set");
if (!GOOGLE_SPREADSHEET_ID_FROM_URL)
throw new Error("No GOOGLE_SPREADSHEET_ID_FROM_URL env var set");
if (!PAYMONGO_EMAIL) throw new Error("No PAYMONGO_EMAIL env var set");
if (!PAYMONGO_PASS) throw new Error("No PAYMONGO_PASS env var set");
if (!PAYMONGO_LIVEMODE) throw new Error("No PAYMONGO_LIVEMODE env var set");
const URL = "https://gateway.paymongo.com/transactions";
const AUTH_URI = "https://gateway.paymongo.com/auth";
exports.handler = async (event) => {
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: JSON.stringify({ error: "Method Not Allowed!" }),
headers: { Allow: "POST" },
};
}
const getApiToken = async () => {
let credentials = {
data: {
attributes: {
email: PAYMONGO_EMAIL,
password: PAYMONGO_PASS,
},
},
};
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 {
reference_no = null,
message = null,
email = null,
type = "cancellation",
mop = null,
mop_details = null,
} = JSON.parse(event.body);
let validationError = [];
if (!reference_no) {
let error = {
field: "reference_no",
message: "No Reference No Submitted",
};
validationError.push(error);
}
if (!message) {
let error = {
field: "message",
message: "No Message Submitted",
};
validationError.push(error);
}
if (!email) {
let error = {
field: "email",
message: "No Email Submitted",
};
validationError.push(error);
}
if (!mop) {
let error = {
field: "mop",
message: "No Mode Of Payment Submitted",
};
validationError.push(error);
}
if (!mop_details) {
let error = {
field: "mop_details",
message: "No Payout Details Submitted",
};
validationError.push(error);
}
let types = [
"cancellation",
"back_order",
"defective",
"deceptive",
"counterfeit",
"missing",
"expired",
];
if (!types.includes(type)) {
let error = {
field: "type",
message: "Refund Type Submitted is Invalid!",
};
validationError.push(error);
}
if (validationError.length > 0) {
return {
statusCode: 422,
body: JSON.stringify({ errors: validationError }),
};
}
const getAmount = async (reference_no) => {
let endpoint = `${URL}/${reference_no}?livemode=${PAYMONGO_LIVEMODE}`;
const token = await getApiToken();
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
var payment;
try {
payment = await axios.get(endpoint);
} catch (e) {
return {
statusCode: 404,
body: JSON.stringify("Reference No. Not Found!"),
};
}
let attributes = payment.data.data.attributes;
let payments = attributes.payments;
const paymentIndex = payments.findIndex((x) => x.status === "paid");
if (paymentIndex == -1) {
return {
statusCode: 500,
body: JSON.stringify("You Cannot Request Refund On UNPAID Order!"),
};
}
let payment_details = attributes.payments[paymentIndex];
const { id, currency, net_amount, paid_at, billing } = payment_details;
const { name, email, phone } = billing;
return {
payment_id: id,
currency,
amount: `${parseFloat(net_amount) / 100}`,
date_paid: new Date(paid_at * 1000),
name,
email,
phone,
};
};
try {
const doc = new GoogleSpreadsheet(GOOGLE_SPREADSHEET_ID_FROM_URL);
await doc.useServiceAccountAuth({
client_email: GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"),
});
await doc.loadInfo();
var refund_sheet = doc.sheetsById[3];
if (!refund_sheet) {
await doc.addSheet({
headerValues: [
"reference_no",
"payment_id",
"currency",
"amount",
"refundable_until",
"type",
"mail_sent",
"message",
"email",
"phone",
"mop",
"mop_details",
"approved",
"remarks",
],
sheetId: 3,
});
refund_sheet = doc.sheetsById[3];
await refund_sheet.updateProperties({ title: "Refunds" });
}
try {
await refund_sheet.loadHeaderRow();
} catch (e) {
await refund_sheet.setHeaderRow([
"reference_no",
"payment_id",
"currency",
"amount",
"refundable_until",
"type",
"mail_sent",
"message",
"email",
"phone",
"mop",
"mop_details",
"approved",
"remarks",
]);
}
const rows = await refund_sheet.getRows();
const rowIndex = rows.findIndex((x) => x.reference_no == reference_no);
if (rowIndex > -1) {
let error = {
statusCode: 400,
body: JSON.stringify({ error: "Refund Request Already Exist!" }),
};
return error;
}
const {
payment_id,
currency,
amount,
date_paid,
name,
phone,
} = await getAmount(reference_no);
let d = date_paid.getDate();
let m = date_paid.getMonth();
let y = date_paid.getFullYear();
let refundable_until = `${y}-${m}-${d}`;
let expiration_date = date_paid.setHours(
date_paid.getHours() + parseInt(REFUND_POLICY_HRS)
);
// only sent this if we have configure AWS Keys and define a refund policy hrs
if (
SES_ACCESS_KEY &&
SES_SECRET_KEY &&
REFUND_POLICY_HRS &&
new Date() <= expiration_date
) {
AWS.config.update({
accessKeyId: SES_ACCESS_KEY,
secretAccessKey: SES_SECRET_KEY,
region: SES_REGION,
});
let support = "support@paymongo.com";
const ses = new AWS.SES({ apiVersion: "2010-12-01" });
const params = {
Destination: {
ToAddresses: [support],
},
// ConfigurationSetName: <<ConfigurationSetName>>,
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: `<html>
<body>
<p>Refund Request was generated automatically as per our ${REFUND_POLICY_HRS}hrs Refund Policy</p>
<p>We Have Listed Below The Necessary Details You Mandated for Refund Request</p><br />
Client Name: ${name}
<br /><br />
Reference No: ${reference_no} <br /><br />
Payment ID: ${payment_id} <br /><br />
Amount To Be Refunded: ₱${amount} <br /><br />
Type Of Refund: ${type} <br /><br />
Refund Reason Of Our Client:<br />
<p>${message}</p>
</body>
</html>`,
},
Text: {
Charset: "UTF-8",
Data: "",
},
},
Subject: {
Charset: "UTF-8",
Data: `REFUND - ${PAYMONGO_MERCHANT_NAME}`,
},
},
Source: PAYMONGO_EMAIL,
};
return ses
.sendEmail(params)
.promise()
.then((data) => {
refund_sheet.addRow({
reference_no,
refundable_until,
type,
name,
phone,
message,
email,
currency,
amount,
mop,
mail_sent: "YES",
mop_details,
});
console.log("email submitted to SES", data);
return {
statusCode: 200,
body: JSON.stringify({
message:
"We have SENT mail to Paymongo, We Will Update You Once We Have Received Reply From Them",
}),
};
})
.catch((error) => {
console.log(error);
return {
statusCode: 500,
body: JSON.stringify({ error }),
};
});
}
await refund_sheet.addRow({
reference_no,
payment_id,
refundable_until,
type,
name,
phone,
message,
email,
currency,
amount,
mop,
mail_sent: "NO",
mop_details,
});
return {
statusCode: 200,
body: JSON.stringify("Refund Request Successfully Submitted"),
};
} catch (e) {
console.log(e.toString());
return {
statusCode: 500,
body: e.toString(),
};
}
};