generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
288 lines (257 loc) · 9.75 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
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
const { logger } = require('firebase-functions')
const fbBizSdk = require('facebook-nodejs-business-sdk')
const {
createContent,
createCustonData,
createServeEvent,
createUserData
} = require('../../lib/fb-api/create-objects')
// read configured E-Com Plus app data
const getAppData = require('./../../lib/store-api/get-app-data')
const SKIP_TRIGGER_NAME = 'SkipTrigger'
const ECHO_SUCCESS = 'SUCCESS'
const ECHO_SKIP = 'SKIP'
const ECHO_API_ERROR = 'STORE_API_ERR'
exports.post = async ({ appSdk }, req, res) => {
// receiving notification from Store API
const { storeId } = req
/**
* Treat E-Com Plus trigger body here
* Ref.: https://developers.e-com.plus/docs/api/#/store/triggers/
*/
const trigger = req.body
try {
const appData = await getAppData({ appSdk, storeId })
if (appData) {
// get app configured options
if (
Array.isArray(appData.ignore_triggers) &&
appData.ignore_triggers.indexOf(trigger.resource) > -1
) {
// ignore current trigger
const err = new Error()
err.name = SKIP_TRIGGER_NAME
throw err
}
let fbPixelId = appData.fb_pixel_id
let fbGraphToken = appData.fb_graph_token
if (fbPixelId && fbGraphToken) {
const EventRequest = fbBizSdk.EventRequest
if (trigger.resource === 'orders' && trigger.action === 'create') {
const orderId = trigger.inserted_id
let order = trigger.body
if (order.status === 'cancelled') {
res.sendStatus(204)
return
}
const { domain } = order
if (domain) {
const domainSpecificPixel = appData.pixels_by_domain?.find((pixelByDomain) => {
return pixelByDomain.domain === domain
})
if (domainSpecificPixel?.fb_pixel_id && domainSpecificPixel.fb_graph_token) {
fbPixelId = domainSpecificPixel.fb_pixel_id
fbGraphToken = domainSpecificPixel.fb_graph_token
}
}
fbBizSdk.FacebookAdsApi.init(fbGraphToken)
const buyer = order.buyers && order.buyers[0]
const clientIp = order.browser_ip
if (orderId && buyer && clientIp) {
/* DO YOUR CUSTOM STUFF HERE */
let clientUserAgent, eventID
const tryFetchOrder = async (isRetry = false) => {
try {
const { response } = await appSdk.apiRequest(storeId, `orders/${orderId}.json`)
order = response.data
if (order.metafields && order.status !== 'cancelled') {
const metafield = order.metafields.find(({ namespace, field }) => {
return namespace === 'fb' && field === 'pixel'
})
if (metafield) {
const value = JSON.parse(metafield.value)
eventID = value.eventID
clientUserAgent = value.userAgent
} else if (order.client_user_agent) {
clientUserAgent = order.client_user_agent
} else if (!isRetry) {
await new Promise((resolve) => {
setTimeout(() => {
logger.log('Retry fetch order')
tryFetchOrder(true).then(resolve)
}, 20000)
})
}
}
} catch (err) {
logger.error(err)
}
return true
}
await tryFetchOrder()
if (order.status === 'cancelled') {
res.sendStatus(204)
return
}
// https://developers.facebook.com/docs/marketing-api/conversions-api/using-the-api#send
const userData = createUserData(buyer, clientIp)
if (clientUserAgent) {
userData.setClientUserAgent(clientUserAgent)
}
const shippingLine = order.shipping_lines && order.shipping_lines[0]
if (shippingLine && shippingLine.to.zip) {
userData.setZip(shippingLine.to.zip.replace(/\D/g, ''))
if (shippingLine.to.province_code) {
userData.setState(shippingLine.to.province_code.toLowerCase())
userData.setCountry((shippingLine.to.country_code || 'BR').toLowerCase())
}
}
const contents = []
const { items } = order
if (items && items.length) {
items.forEach(item => {
if (item.quantity > 0) {
const content = createContent(item)
contents.push(content)
}
})
}
const customData = createCustonData(contents, order.amount.total, order.currency_id)
const eventMs = Math.min(new Date(order.created_at || trigger.datetime).getTime(), Date.now() - 3000)
console.log(`#${storeId} order ${orderId} (${eventID}) at ${eventMs}ms`)
let eventSourceUrl
if (order.checkout_link) {
eventSourceUrl = order.checkout_link
} else if (domain) {
eventSourceUrl = `https://${domain}`
}
const serverEvent = createServeEvent(
'Purchase',
eventMs,
userData,
customData,
eventID || orderId,
eventSourceUrl
)
const eventsData = [serverEvent]
const eventRequest = (new EventRequest(fbGraphToken, fbPixelId))
.setEvents(eventsData)
return eventRequest.execute().then(
response => {
// console.log('>> ', response)
logger.info(response)
// all done
res.status(201).send(ECHO_SUCCESS)
},
err => {
console.error(`Facebook event request error: ${err.message}`, err, JSON.stringify(trigger))
res.sendStatus(202)
}
)
}
} else if (trigger.resource === 'carts' && trigger.action === 'create' && !appData.fb_disable_cart) {
fbBizSdk.FacebookAdsApi.init(fbGraphToken)
// https://developers.facebook.com/docs/meta-pixel/reference#standard-events
// Event name: InitiateCheckout
// custom_event_type: INITIATE_CHECKOUT
// Properties
// content_category, content_ids, contents, currency, num_items and value
// console.log('>>> ', trigger, ' <<<')
const cartId = trigger.inserted_id
const cart = trigger.body
if (cart.completed) {
res.sendStatus(204)
return
}
const eventMs = Math.min(new Date(cart.created_at || trigger.datetime).getTime(), Date.now() - 3000)
console.log(`#${storeId} cart ${cartId} at ${eventMs}ms`)
const tryFetchCustomer = async (customerId) => {
const { response } = await appSdk.apiRequest(storeId, `customers/${customerId}.json`)
return response.data
}
let customer
if (cart.customers && cart.customers.length > 0) {
customer = await tryFetchCustomer(cart.customers[0])
}
let userData
let address
if (customer) {
userData = createUserData(customer)
if (customer.addresses && customer.addresses.length > 0) {
address = customer.addresses[0]
}
}
if (address && address.zip) {
userData.setZip(address.zip.replace(/\D/g, ''))
if (address.province_code) {
userData.setState(address.province_code.toLowerCase())
userData.setCountry((address.country_code || 'BR').toLowerCase())
}
}
const contents = []
const { items } = cart
if (items && items.length) {
items.forEach(item => {
if (item.quantity > 0) {
const content = createContent(item)
contents.push(content)
}
})
}
const customData = createCustonData(contents, cart.subtotal)
let eventSourceUrl
if (cart.permalink) {
eventSourceUrl = cart.permalink
}
const serverEvent = createServeEvent(
'InitiateCheckout',
eventMs,
userData,
customData,
cartId,
eventSourceUrl
)
const eventsData = [serverEvent]
const eventRequest = (new EventRequest(fbGraphToken, fbPixelId))
.setEvents(eventsData)
return eventRequest.execute().then(
response => {
// console.log('>> ', response)
logger.info(response)
// all done
res.status(201).send(ECHO_SUCCESS)
},
err => {
console.error(`Facebook event request error: ${err.message}`, err, JSON.stringify(trigger))
res.sendStatus(202)
}
)
}
}
res.sendStatus(412)
} else {
res.sendStatus(401)
}
} catch (err) {
if (err.name === SKIP_TRIGGER_NAME) {
// trigger ignored by app configuration
res.send(ECHO_SKIP)
} else if (err.appWithoutAuth === true) {
const msg = `Webhook for ${storeId} unhandled with no authentication found`
const error = new Error(msg)
error.trigger = JSON.stringify(trigger)
console.error(error)
res.status(412).send(msg)
} else {
console.error(err)
// request to Store API with error response
// return error status code
res.status(500)
const { message } = err
res.send({
error: ECHO_API_ERROR,
message
})
}
}
}