forked from macdonaldr93/shopify-auto-tag-customers-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (42 loc) · 1.57 KB
/
app.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
var express = require('express');
var compression = require('compression');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var config = require('./config/env.js'); // eslint-disable-line node/no-unpublished-require
var shopify = require('./lib/shopify.js');
if (!config.apiKey) {
throw new Error('Missing API key. Check your config/env.');
} else if (!config.password) {
throw new Error('Missing password. Check your config/env.');
} else if (!config.store) {
throw new Error('Missing store. Check your config/env.');
} else if (!config.sharedSecret) {
throw new Error('Missing shared secret. Check your config/env.');
}
// build authenticated URL based on info provided in config and env
var storeAuth = 'https://' + config.apiKey + ':' + config.password + '@' + config.store + '.myshopify.com';
// setup express server
var app = express();
app.use(morgan('dev'));
app.use(compression());
// verify webhook's origin
app.use(bodyParser.json({
verify: shopify.validateSignature
}));
app.use(bodyParser.urlencoded({
verify: shopify.validateSignature,
extended: true
}));
// block invalid webhook requests
app.use(shopify.requireValidSignature);
// endpoint for Shopify to post order/create event to
app.post('/order_create', function(req, res) {
// verify conditions to take action
if (req.body.customer.id && req.body.shipping_address.country_code === 'CA') {
// update customer
shopify.updateCustomerTags(storeAuth, req.body.customer.id, ['Canada', 'North America']);
}
// return OK to Shopify
res.sendStatus(200);
});
module.exports = app;