-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshopify.js
457 lines (433 loc) · 15.7 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
var express = require('express')
, EventEmitter = require('events').EventEmitter
, emitter = new EventEmitter()
, url = require("url")
, request = require('request')
, qs = require('qs')
, cradle = require('cradle')
, ar = require('couch-ar')
, ccdb = require('connect-couchdb')(express)
, app = express.createServer()
, shop
;
// the module is bootstrapped by calling it with an configuration object. The
// module fires an 'init' event when all the startup processing is done, the
// server may started by the calling module when this event is fired.
// e.g.
// require('manage.js')(config).on('init', function(server) {
// server.listen(port);
// });
var clientName = ''
, webhookDomain = '';
module.exports = function(config) {
clientName = config.name;
webhookDomain = config.smtp.webhookDomain;
configureServer(config);
return emitter;
}
// TODO: consider consolidating all this configure stuff into a common set of methods
// shared with 'app'
// bootstrapping begins. Connect to the database and check for the following,
// creating them if necessary:
//
// - 'admin' user
// - client configuration document
//
// TODO: change admin to admin@mapsherpa.com and choose a better password :)
function configureServer(config) {
ar.init({
host: 'http://' + config.db.host,
port: config.db.port,
dbName: config.db.name,
connectionOptions: {
auth: {
username: config.db.user,
password: config.db.pass
}
},
root: __dirname + '/models'
}, function(cradleDb) {
// globalize database connection so we can use cradle functions for attachments etc
db = cradleDb;
// check for session db existance
var sessionDB = new (cradle.Connection)(config.db.host,config.db.port, {auth: {username: config.db.user, password: config.db.pass}}).database(config.db.session);
sessionDB.exists(function(err, exists) {
if (err) {
console.log('error initializing session db', err);
} else if (!exists) {
sessionDB.create(function(){});
}
// synchronous, configure the express server
configureShop(config, function(){
configureApp(config);
emitter.emit('init', app);
});
});
});
}
function configureShop(config, next) {
ar.Shop.findByDomain(config.shopify.shopDomain, function(aShop){
if (aShop) {
if (aShop.access_token) {
shop = aShop;
console.log('shop record authorized in the DB');
} else {
console.log('Shop record without token found');
// TODO: can we reauthorize?
process.exit();
}
} else {
console.log('no shop record.');
}
next();
});
}
function validShop(req, res, next) {
if (shop && shop.access_token) {
return next();
} else {
return next(new Error(401));
}
}
function configureApp(config) {
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(express.logger({
format: ':method :url'
}));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.get('/', function(req, res, next) {
res.render('index.hbs', {});
});
var returnUrl;
app.get('/authorize', function(req, res){
returnUrl = decodeURIComponent(req.query.returnUrl);
if (shop) {
res.render('return', {
title: 'Already Authorized',
status: 'warn',
message: 'MapSherpa already appears to be authorized with your Shopify account. If things are not working correctly then please <a href="mailto:support@mapsherpa.com">Contact MapSherpa Support</a>.',
returnUrl: returnUrl
});
} else if (!config.shopify ||
!config.shopify.shopDomain ||
!config.shopify.apiKey ||
!config.shopify.sharedSecret ||
!config.shopify.scope) {
res.render('return', {
title: 'Missing Shopify Configuration Information',
status: 'error',
message: 'Your MapSherpa account is missing required Shopify configuration information. Please <a href="mailto:support@mapsherpa.com">Contact MapSherpa Support</a>.',
returnUrl: returnUrl
});
} else {
var authUrl = 'https://'+config.shopify.shopDomain + '/admin/oauth/authorize';
var params = {
client_id: config.shopify.apiKey,
scope: config.shopify.scope,
};
authUrl += '?' + qs.stringify(params);
console.log('redirecting to '+authUrl);
res.redirect(authUrl);
}
});
app.get('/callback', function(req, res, next) {
var accessToken;
var authUrl = 'https://'+req.query.shop + '/admin/oauth/access_token';
var params = {
client_id: config.shopify.apiKey,
client_secret: config.shopify.sharedSecret,
code: req.query.code
};
console.log('issuing access token request to '+authUrl);
console.log('with params '+ JSON.stringify(params));
request.post({url: authUrl, json: params}, function (err, req2, body) {
if (err) {
console.log('Error requesting Shopify access token:'+authUrl);
res.render('return', {
title: 'Authorization Request Error',
status: 'error',
message: 'An error occurred when requesting authorization. If this is a temporary network error, you can return to MapSherpa and try to authorize again. If you see this message again, please <a href="mailto:support@mapsherpa.com">Contact MapSherpa Support</a>.',
returnUrl: returnUrl
});
} else {
if (body.err) {
console.log("access token error:"+body.err);
res.render('return', {
title: 'Authorization Error',
status: 'error',
message: 'The following error occurred when requesting authorization: ' + err + '</p><p>Please <a href="mailto:support@mapsherpa.com">Contact MapSherpa Support</a>.',
returnUrl: returnUrl
});
} else {
if (req2.statusCode > 400) {
console.log("access token 400 error:"+body);
res.render('return', {
title: 'Authorization Error',
status: 'error',
message: 'The authorization request returned status code ' + req2.statusCode + '.</p><p>Please <a href="mailto:support@mapsherpa.com">Contact MapSherpa Support</a>.',
returnUrl: returnUrl
});
} else {
console.log('shopify token success:'+body.access_token);
accessToken = body.access_token;
//store access_token in the DB
//get the shop info and store in DB
var storeInfoUrl = 'https://'+req.query.shop + '/admin/shop.json';
request.get({
url: storeInfoUrl,
headers: {
'X-Shopify-Access-Token': accessToken
}
}, function(err, req3, body2) {
if (req3.statusCode < 400) {
console.log('shop info:'+body2);
var shopInfo = JSON.parse(body2).shop;
shopInfo.shopifyId = shopInfo.id; //ID is special for ar
delete shopInfo.id; //rename it to shopifyId
shopInfo.access_token = accessToken;
console.log('set shop owner to:'+clientName);
shopInfo.owner = clientName;
var shopifyStore = ar.Shop.create(shopInfo);
shopifyStore.save(function(err,obj) {
if (err) {
console.log('error creating shopify record', err);
res.json({success:false}, 500);
} else {
console.log('created shopify record');
var webhook = {
"webhook": {
"topic": "orders/paid",
"address": webhookDomain + '/fullfill',
"format": "json"
}
};
var webhookUrl = 'https://'+req.query.shop + '/admin/webhooks.json';
request.post({
url: webhookUrl,
json: webhook,
headers: {
'X-Shopify-Access-Token': accessToken
}
}, function(err, req4, body4) {
if (req4.statusCode < 400) {
console.log('webhook info:'+body4);
shop = shopifyStore;
res.redirect(returnUrl);
} else {
console.log('error creating shopify webhook', JSON.stringify(req4));
res.json({success:false}, 500);
}
});
}
});
} else {
console.log('error getting store info');
res.json({success:false}, req3.statusCode);
}
});
}
}
}
});
});
app.post('/fullfill', validShop, function(req, res, next) {
console.log('fullfilling:'+JSON.stringify(req.body));
res.json({success:true}, 200);
});
//Product routes
//get all products
app.get('/products', function(req, res, next) {
var url = 'https://'+shop.domain + '/admin/products.json';
console.log('access token:'+shop.access_token);
request.get({
url: url,
headers: {
'X-Shopify-Access-Token': shop.access_token
}
}, function (err, req2, body) {
if (err) {
console.log('error reading shopify product'+ err);
res.json({success:false}, 500);
} else {
var response;
if (typeof(body) == 'string') {
response = JSON.parse(body);
} else {
response = body;
}
if (response.errors) {
console.log('error reading products:'+response.errors);
res.json({success:false, message:response.errors}, 500);
} else {
var products = response.products;
res.json({success: true, products: products}, 200);
}
}
});
});
//get a single product
app.get('/products/:id', validShop, function(req, res, next) {
var url;
if (req.params.id) {
url = 'https://'+shop.domain + '/admin/products/'+req.params.id+'.json';
} else {
console.log('product id missing');
res.json({success:false, message:'product id missing'}, 500);
return;
}
console.log('access token:'+shop.access_token);
console.log('accessing url:'+url);
request.get({
url: url,
headers: {
'X-Shopify-Access-Token': shop.access_token
}
}, function (err, req2, body) {
if (err) {
console.log('error reading shopify product'+ err);
res.json({success:false}, 500);
} else {
var response;
if (typeof(body) == 'string') {
console.log(body);
response = JSON.parse(body);
} else {
response = body;
}
if (response.errors) {
console.log('error reading products:'+response.errors);
res.json({success:false, message:response.errors}, 500);
} else {
var products = response.product;
res.json({success: true, products: products}, 200);
}
}
});
});
app.post('/products', validShop, function(req, res, next) {
var url = 'https://'+shop.domain + '/admin/products.json';
var params = JSON.parse(JSON.stringify(req.body));
console.log('response headers:'+shop.access_token);
//params required by shopify
var options = {
"product": {
"title": params.name,
"vendor": 'mapsherpa',
"product_type": 'online-map',
"variants": [
{
"option1": params.tileset,
"price": params.price,
"sku": params.sku
}
]
}
}
console.log('params:'+JSON.stringify(options));
request.post({
url: url,
json: options,
headers: {
'X-Shopify-Access-Token': shop.access_token
}
}, function (err, req2, body) {
console.log('creating product status:'+req2.statusCode);
if (err) {
console.log('error creating shopify product'+ err);
res.json({success:false}, 500);
} else {
var response;
if (typeof(body) == 'string') {
console.log('response body:'+body);
response = JSON.parse(body);
} else {
console.log('response body:'+JSON.stringify(body));
response = body;
}
if (response.errors) {
console.log('error creating product:'+response.errors);
res.json({success:false, message:response.errors}, 500);
} else {
var products = response.product;
console.log('product created');
res.json({success: true, products: products}, 200);
}
}
});
});
app.put('/products/:id', validShop, function(req, res, next) {
var url = 'https://'+shop.domain + '/admin/products/'+req.params.id+'.json';
var params = JSON.parse(JSON.stringify(req.body));
var options = {
"product": {
"title": params.name,
"vendor": 'mapsherpa',
"product_type": 'online-map',
"variants": [
{
"option1": params.tileset,
"price": params.price,
"sku": params.sku
}
]
}
}
request.put({
url: url,
json: options,
headers: {
'X-Shopify-Access-Token': shop.access_token
}
}, function (err, req2, body) {
if (err) {
console.log('error updating shopify product'+ err);
res.json({success:false}, 500);
} else {
var response;
if (typeof(body) == 'string') {
console.log('response body:'+body);
response = JSON.parse(body);
} else {
console.log('response body:'+JSON.stringify(body));
response = body;
}
if (response.errors) {
console.log('error updating product:'+response.errors);
res.json({success:false, message:response.errors}, 500);
} else {
var products = response.product;
console.log('product updated');
res.json({success: true, products: products}, 200);
}
}
});
});
app['delete']('/products/:id', validShop, function(req, res, next) {
var url = 'https://'+shop.domain + '/admin/products/'+req.params.id+'.json';
request.del({
url: url,
headers: {
'X-Shopify-Access-Token': shop.access_token
}
}, function (err, req2, body) {
if (req2.statusCode >= 400) {
console.log('error deleting shopify product'+ err);
res.json({success:false}, 500);
} else {
console.log('product deleted');
res.json({success: true}, 200);
}
});
});
app.error(function(err, req, res) {
console.log(err);
});
}