/
index.js
48 lines (41 loc) · 1.07 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
const { json, send } = require('micro')
const cors = require('micro-cors')()
const { ServerClient: PostmarkClient } = require('postmark')
const postmark = new PostmarkClient(process.env.POSTMARK_API_KEY)
module.exports = cors(async (req, res) => {
if (
(await req.headers['x-moltin-secret-key']) !=
process.env.MOLTIN_WEBHOOK_SECRET
)
return send(res, 401)
const data = await json(req)
const resource = JSON.parse(data.resources)
try {
const {
data: {
id,
customer: { email: to, name },
meta: {
display_price: {
with_tax: { formatted: order_total },
},
},
},
included: { items },
} = resource
await postmark.sendEmailWithTemplate({
from: process.env.POSTMARK_FROM_ADDRESS,
to,
templateId: process.env.POSTMARK_CONFIRMATION_TEMPLATE_ID,
templateModel: {
customer_name: name,
order_ref: id,
order_total,
order_items: items,
},
})
send(res, 201)
} catch ({ errors }) {
send(res, 500, errors)
}
})