-
Notifications
You must be signed in to change notification settings - Fork 6
/
shopify.js
104 lines (85 loc) · 2.53 KB
/
shopify.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
let debug = require('debug')('shopify');
let requestPromise = require('request-promise');
let qs = require('qs');
let crypto = require('crypto');
async function getAccessToken(shop, code, apiKey, apiSecret) {
let url = `https://${shop}/admin/oauth/access_token`;
let payload = {
client_id: apiKey,
client_secret: apiSecret,
code
};
let options = {
method: 'POST',
uri: url,
body: payload,
json: true // Automatically stringifies the body to JSON
};
try {
let response = await requestPromise(options);
debug('Got access token: ', response);
if (response.access_token)
return response.access_token;
else
return '';
} catch (error) {
console.log(error);
return '';
}
}
async function requestShopInfo(shop, accessToken) {
let url = `https://${shop}/admin/shop.json`;
let headers = {
'X-Shopify-Access-Token': accessToken,
};
let options = {
method: 'GET',
uri: url,
headers: headers,
json: true // Automatically stringifies the body to JSON
};
try {
debug('Requesting shop information...');
let info = await requestPromise(options);
return info;
} catch (error) {
console.log(error);
return {};
}
}
function isHMACValid(secret, { hmac, shop, code, timestamp }) {
let input = qs.stringify({ code, shop, timestamp });
let generatedHash = crypto.createHmac('sha256', secret).update(input).digest('hex');
return generatedHash !== hmac;
}
// eslint-disable-next-line no-unused-vars
function handleUninstalledEvent(request, response, next) {
let topic = request.get('X-Shopify-Topic');
let hmac = request.get('X-Shopify-Hmac-Sha256');
let shop = request.get('X-Shopify-Shop-Domain');
// Quickly acknowledge received event first and defer all further processing.
setTimeout(processUninstalledEvent, 0, topic, hmac, shop, request.body);
response.sendStatus(200);
}
function processUninstalledEvent(topic, hmac, shop, body) {
debug(`Received ${topic} event from ${shop}`);
if (!topic ||!hmac || !shop) {
debug('Missing required header information');
return false;
}
if (hmac !== getWebhookHMAC(process.env.SHOPIFY_API_SECRET, body)) {
debug('HMAC is invalid');
return false;
}
debug(`Hello Shopify has been removed from ${shop}`);
}
function getWebhookHMAC(secret, body) {
let generatedHash = crypto.createHmac('sha256', secret).update(Buffer.from(body)).digest('base64');
return generatedHash;
}
module.exports = {
getAccessToken,
requestShopInfo,
isHMACValid,
handleUninstalledEvent
}