-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
130 lines (113 loc) · 3.1 KB
/
Copy pathserver.js
File metadata and controls
130 lines (113 loc) · 3.1 KB
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
require("dotenv").config();
require("cross-fetch/polyfill");
const path = require("path");
const express = require("express");
const { gretch } = require("gretchen");
const app = express();
const API_BASE_URL = "https://api.truework-sandbox.com";
const { TW_SANDBOX_API_TOKEN, SECURITY_TOKENS } = process.env;
if (!process.env.SECURITY_TOKENS) {
throw Error("Env 'SECURITY_TOKENS' must be defined");
}
const WEBHOOK_TOKENS_ARRAY = SECURITY_TOKENS.split(",");
app.use(express.static(path.join(__dirname, "static")));
app.use(express.json());
app.get("/token", async (req, res) => {
const USER_PAYLOAD = {
targets: [
{
companies: [
{
name: "Acme Corp",
},
],
contact_email: "jane@example.com",
date_of_birth: "2001-08-24",
first_name: "Jane",
last_name: "Doe",
permissible_purpose: "credit-application",
social_security_number: "000-20-0000",
type: "employment-income",
use_case: "mortgage",
},
],
};
const { data, error, status, response } = await gretch(
`${API_BASE_URL}/orders/truework-direct`,
{
method: "POST",
headers: {
Accept: "application/json; version=2023-10-30",
Authorization: `Bearer ${TW_SANDBOX_API_TOKEN}`,
},
json: USER_PAYLOAD,
}
).json();
if (!response.ok) {
console.error(
`${status} error when creating session:\n${JSON.stringify(
error,
null,
2
)}`
);
return res.status(500).json({ message: "Error creating session" });
}
console.info(`Session created:\n${JSON.stringify(data, null, 2)}`);
res
.json({
token: data.truework_direct.targets[0].truework_direct_session_token,
})
.send();
});
app.post("/webhook", async (req, res) => {
const WEBHOOK_TOKEN = req.get("x-truework-token");
/*
* Verify that there's a security token provided so we don't accept or
* interpret out of band requests.
*/
if (!WEBHOOK_TOKENS_ARRAY.includes(WEBHOOK_TOKEN)) {
console.warn(
`Rejected webhook without valid token:\n${JSON.stringify(
req.body,
null,
2
)}`
);
return res.status(403).send();
}
/*
* Otherwise ACK immediately
*/
res.send();
console.info(`Received webhook:\n${JSON.stringify(req.body, null, 2)}`);
/*
* If the verification is completed, get the data from the api
*/
if (req.body.hook.event === "order.completed") {
const id = req.body.data.order_id;
const { status, data, error, response } = await gretch(
`${API_BASE_URL}/orders/${id}`,
{
headers: {
Authorization: `Bearer ${TW_SANDBOX_API_TOKEN}`,
Accept: "application/json; version=2023-10-30",
},
}
).json();
if (!response.ok || !data) {
console.error(
`${status} error when fetching verification:\n${JSON.stringify(
error,
null,
2
)}`
);
} else if (data) {
console.info(
`Received verification data:\n${JSON.stringify(data, null, 2)}`
);
}
}
});
module.exports = app;