This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlob.js
334 lines (291 loc) · 9.88 KB
/
lob.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
const express = require('express')
const axios = require('axios')
const Lob = require('lob')
const router = express.Router()
const ALLOWED_ADDRESS_FIELDS = ['line1', 'line2', 'city', 'state', 'zip']
const VALID_US_ZIP_CODE_MATCH = /^(?:\d{1,4}|\d{5}(?:[+-]?\d{4})?)$/
const DELIVERABILITY_WARNINGS = {
undeliverable: 'Address is not deliverable',
deliverable_incorrect_unit:
'Address may be deliverable but contains a suite number that does not exist',
deliverable_missing_unit:
'Address may be deliverable but is missing a suite number',
deliverable_unnecessary_unit:
'Address may be deliverable but contains an unnecessary suite number'
}
router.post('/createAddress', async (req, res) => {
// Get description, to, and template_id from request body
const address = req.body || {}
const lobApiKey = getLobApiKey()
const lob = new Lob({ apiKey: lobApiKey })
try {
const response = await lob.usVerifications.verify({
primary_line: address.line1,
secondary_line: address.line2,
city: address.city,
state: address.state,
zip_code: address.zipCode
})
const {
deliverability,
components: {
state: revisedState,
address_type: addressType,
record_type: recordType
}
} = response
const isUndeliverable =
!deliverability || deliverability === 'undeliverable'
const isResidential = addressType === 'residential'
const isPostOfficeBox = recordType === 'po_box'
const isPuertoRico = revisedState === 'PR'
const deliverable =
!isUndeliverable && isResidential && !isPostOfficeBox && !isPuertoRico
if (!deliverable) {
let errorMessage = 'Address is undeliverable'
if (!isUndeliverable) {
if (!isResidential) {
errorMessage = 'Non-residential addresses are not currently supported'
} else if (isPostOfficeBox) {
errorMessage = 'Post office boxes are not currently supported'
} else if (isPuertoRico) {
errorMessage = 'Puerto Rico addresses are not currently supported'
}
}
return res.status(400).send({ error: errorMessage })
}
// Create Lob address using variables passed into route via post body
const addressResponse = await lob.addresses.create({
description: address.description,
name: address.name,
address_line1: address.line1,
address_line2: address.line2,
address_city: address.city,
address_state: address.state,
address_zip: address.zip,
address_country: 'US'
})
res.status(200).send({ address_id: addressResponse.id })
} catch (error) {
res.status(500).send({ error: 'Something failed!' })
}
})
router.post('/createLetter', async (req, res) => {
// Get description, to, and template_id from request body
const { description, to, from, template_id, charge } = req.body || {}
const lobApiKey = getLobApiKey()
const lob = new Lob({ apiKey: lobApiKey })
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)
try {
// Create Lob address using variables passed into route via post body
const letter = await lob.letters.create({
description: description,
to: {
name: to.name,
address_line1: to.line1,
address_line2: to.line2,
address_city: to.city,
address_state: to.state,
address_zip: to.zip
},
from: from.address_id,
file: template_id,
color: false
})
res
.status(200)
.send({ expected_delivery_date: letter.expected_delivery_date })
} catch (error) {
// We'll need a stripe test env key to test this in our integration tests
const refund = await stripe.refunds.create({
charge: charge
})
// TODO handle error for refund error. Not doing this currently because chance of
// user making it this far in the process and both LOB API and Stripe failing is very small.
res.status(500).send({
error: `Something failed! A refund of ${refund.amount} ${refund.currency} has been issued`
})
}
})
router.get('/templates/:templateId', async (req, res) => {
const { templateId } = req.params
var templateInfo = {}
try {
// We must use `axios` here as the `lob` package does not yet support
// the [beta] Templates API.
const response = await axios.get(
`https://api.lob.com/v1/templates/${templateId}`,
{
auth: { username: getLobApiKey() }
}
)
templateInfo = response.data
return res.status(200).send(templateInfo)
} catch (error) {
return handleLobError(error, res)
}
})
router.post('/addressVerification', async (req, res) => {
const address = req.body
// Very rough schema validation
try {
const keys = Object.keys(address || {}).sort()
if (!address || keys.length === 0) {
throw new Error('Address object cannot be empty')
}
const disallowedKeys = keys.reduce((badKeys, key) => {
if (!ALLOWED_ADDRESS_FIELDS.includes(key)) {
badKeys.push(key)
}
return badKeys
}, [])
if (disallowedKeys.length > 0) {
throw new Error(
`Address object contained unexpected keys: ${JSON.stringify(
disallowedKeys
)}`
)
}
if (!(address.line1 || '').trim()) {
throw new Error('Address object must contain a primary line (line1)')
}
const { zip } = address
if (zip != null && typeof zip !== 'string') {
throw new Error('Address object must contain a string-based ZIP code')
}
let zipCode = (zip || '').trim()
if (zipCode) {
if (!VALID_US_ZIP_CODE_MATCH.test(zipCode)) {
throw new Error(
`Address object contained an invalid ZIP code: ${zipCode}`
)
}
} else if (!((address.city || '').trim() && (address.state || '').trim())) {
throw new Error(
'Address object must include both city and state, or a ZIP code'
)
}
} catch (validationError) {
return res.status(400).send({ error: validationError.message })
}
const { line1, line2, city, state, zip } = address
// Ensure the ZIP code is at least 5 digits
const zipCode = zip ? zip.padStart(5, '0') : null
try {
const lob = new Lob({ apiKey: getLobApiKey() })
const response = await lob.usVerifications.verify({
primary_line: line1,
secondary_line: line2,
city,
state,
zip_code: zipCode
})
const {
deliverability,
primary_line: revisedLine1,
secondary_line: revisedLine2,
components: {
city: revisedCity,
state: revisedState,
zip_code: revisedZip,
zip_code_plus_4: revisedZipPlus4,
address_type: addressType,
record_type: recordType
}
} = response
const isUndeliverable =
!deliverability || deliverability === 'undeliverable'
const isResidential = addressType === 'residential'
const isPostOfficeBox = recordType === 'po_box'
const isPuertoRico = revisedState === 'PR'
const deliverable =
!isUndeliverable && isResidential && !isPostOfficeBox && !isPuertoRico
const warning = DELIVERABILITY_WARNINGS[deliverability] || null
if (!deliverable) {
let errorMessage = 'Address is undeliverable'
if (!isUndeliverable) {
if (!isResidential) {
errorMessage = 'Non-residential addresses are not currently supported'
} else if (isPostOfficeBox) {
errorMessage = 'Post office boxes are not currently supported'
} else if (isPuertoRico) {
errorMessage = 'Puerto Rico addresses are not currently supported'
}
}
return res.status(400).send({ error: errorMessage })
}
return res.status(200).send({
deliverable,
warning,
revisedAddress: {
line1: revisedLine1,
line2: revisedLine2 || null,
city: revisedCity,
state: revisedState,
zip: revisedZip + (revisedZipPlus4 ? '-' + revisedZipPlus4 : '')
}
})
} catch (error) {
// This endpoint should not return anything other than `200` status
// codes, even for undeliverable addresses
return handleLobError(error, res)
}
})
module.exports = router
// Temporary implementation for fallback with deprecation warnings
function getLobApiKey() {
const { LOB_API_KEY, LiveLob } = process.env
const lobApiKey = LOB_API_KEY || LiveLob
if (LiveLob) {
if (LOB_API_KEY) {
console.warn('Using "LOB_API_KEY" environment variable.')
console.warn(
'Please remove your deprecated "LiveLob" environment variable!'
)
} else {
console.warn('Expected "LOB_API_KEY" environment variable was not found.')
console.warn(
'Falling back to deprecated "LiveLob" environment variable....'
)
console.warn('Please update your environment to use the expected key!')
}
}
return lobApiKey
}
function handleLobError(error, res) {
let status = 500
let errorMessage = 'Whoops'
if (error) {
// error.response is from the `axios` package
// error._response is from the `lob` package
if (error.response || error._response) {
status = 502
let lobStatus = null
let lobApiError = {}
// Handle Lob API errors from `axios` requests
if (error.response) {
lobStatus = error.response.status
lobApiError = error.response.data.error
}
// Handle Lob API errors from `lob` requests
else if (error._response) {
lobStatus = error._response.statusCode
lobApiError = error._response.body.error
}
if (process.env.NODE_ENV !== 'test') {
console.error(
`Lob API error (${lobStatus}): ${JSON.stringify(lobApiError)}`
)
}
// If the error is being blamed on the request...
// See: https://docs.lob.com/#errors
if ([400, 404, 422].includes(lobStatus)) {
status = 400
errorMessage = lobApiError.message
}
} else {
console.error(error)
}
}
return res.status(status).send({ error: errorMessage })
}