-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwebhook.js
41 lines (36 loc) · 1 KB
/
webhook.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
const FoxyWebhook = require("../../foxy/FoxyWebhook.js");
const { DataStore } = require("./DataStore.js");
/**
* Process a transaction/created Foxy Webhook.
*
* @param {Object} body the parsed body of the Foxy request.
* @returns {Response} the respose to be sent
*/
async function transactionCreated(body) {
try {
const datastore = await getDataStore();
const result = await datastore.shipment(body);
if (result.success === 'true' || result.success === true) {
return response();
}
} catch (e) {
console.error(e.code, e.message);
}
return response('Internal Server Error', 500);
}
/**
* @returns {Object} the ShipTheory client.
*/
async function getDataStore() {
const shiptheoryDS = new DataStore();
const authenticated = await shiptheoryDS.authenticate();
if (!authenticated) {
throw new Error("Could not authenticate against Shiptheory.");
}
return shiptheoryDS;
}
const response = FoxyWebhook.response;
module.exports = {
response,
transactionCreated,
}