-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.js
125 lines (93 loc) · 2.91 KB
/
order.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
var express = require('express');
var bodyParser = require('body-parser');
var pizzapi = require('dominos');
var app = express();
app.use(bodyParser.json());
app.get('/findStores/:zipCode', function(req, res) {
var zipCode = req.params.zipCode;
pizzapi.Util.findNearbyStores(
zipCode,
'Delivery',
function(storeData) {
var prettyStoreData = JSON.stringify(storeData, null, 4);
res.send(prettyStoreData);
}
);
});
app.get('/getMenu/:storeId', function(req, res) {
console.log('getting store menu...');
storeId = parseInt(req.params.storeId);
var myStore = new pizzapi.Store({
ID: storeId
});
myStore.getFriendlyNames(
function(storeData) {
var prettyStoreData = JSON.stringify(storeData, null, 4);
// console.log(prettyStoreData);
res.send(prettyStoreData);
}
);
});
app.post('/validateAndPrice', function(req, res) {
console.log('BODY');
console.log(req.body);
var order = createOrder(req.body);
order.validate(
function(validateResult) {
// console.log("Validate! " + JSON.stringify(validateResult, null, 4));
order.price(
function(priceResult) {
// console.log("Price! " + JSON.stringify(priceResult, null, 4));
console.log(JSON.stringify(order, null, 4));
res.send(JSON.stringify(priceResult, null, 4));
}
);
}
);
});
app.post('/order', function(req, res) {
console.log('Ordering...');
var order = createOrder(req.body);
var cardNumber = process.env.PIZZAPI_CARD_NUMBER || '2222333344445555';
var expiration = process.env.PIZZAPI_CARD_EXPIRATION || '0115';
var securityCode = process.env.PIZZAPI_CARD_CVV || '007';
var postalCode = process.env.PIZZAPI_CARD_ZIP || '90210'; // Billing Zipcode
var cardInfo = new order.PaymentObject();
cardInfo.Amount = order.Amounts.Customer;
cardInfo.Number = cardNumber;
cardInfo.CardType = order.validateCC(cardNumber);
cardInfo.Expiration = expiration; // 01/15 just the numbers "01/15".replace(/\D/g,'');
cardInfo.SecurityCode = securityCode;
cardInfo.PostalCode = postalCode; // Billing Zipcode
order.Payments.push(cardInfo);
order.place(
function(result) {
console.log("Order placed!");
res.send(JSON.stringify(result, null, 4));
}
);
});
function createOrder(requestBody) {
console.log('Creating Order...');
var customer = new pizzapi.Customer(requestBody.customer);
var order = new pizzapi.Order({
customer: customer,
//optional set the store ID right away
storeID: requestBody.storeID,
deliveryMethod: 'Delivery' //Carryout
});
var foodItems = requestBody.items;
foodItems.forEach(function(code) {
var item = {
code: code,
options: [],
quantity: 1
};
order.addItem(new pizzapi.Item(item));
});
order.Phone = requestBody.customer.phone;
return order;
}
app.listen(3000, function() {
console.log('Listening on port 3000!');
});