-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
169 lines (141 loc) · 5.36 KB
/
index.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
var request = require("request"),
fs = require("fs"),
hash = require("node_hash"),
events = require("events");
var util = require("util");
var promisify = util.promisify ? util.promisify : function(f) { return f; };
var accountMethods = {
companyInfo: {path: "/acct/detail_company_info", method: "get", pick: "vendor_company_info"},
contactInfo: {path: "/acct/detail_contact_info", method: "get", pick: "vendor_contact_info"},
pendingPayment: {path: "/acct/detail_pending_payment", method: "get", pick: "payment"},
listPayments: {path: "/acct/list_payments", method: "get", pick: "payments"}
};
var salesMethods = {
details: {path: "/sales/detail_sale", method: "get", pick: "sale"},
list: {path: "/sales/list_sales", method: "get", pick: ["page_info", "sale_summary"]},
refundInvoice: {path: "/sales/refund_invoice", method: "post"},
refundLineitem: {path: "/sales/refund_lineitem", method: "post"},
stopLineitemRecurring: {path: "/sales/stop_lineitem_recurring", method: "post"},
markShipped: {path: "/sales/mark_shipped", method: "post"},
createComment: {path: "/sales/create_comment", method: "post"}
};
var productMethods = {
details: {path: "/products/detail_product", method: "get", pick: "product"},
list: {path: "/products/list_products", method: "get", pick: ["page_info", "products"]},
create: {path: "/products/create_product", method: "post"},
update: {path: "/products/update_product", method: "post"},
del: {path: "/products/delete_product", method: "post"}
};
var productOptionsMethods = {
details: {path: "/products/detail_option", method: "get", pick: "option"},
list: {path: "/products/list_option", method: "get", pick: ["page_info", "options"]},
create: {path: "/products/create_option", method: "post"},
update: {path: "/products/update_option", method: "post"},
del: {path: "/products/delete_option", method: "post"}
};
var productCouponMethods = {
details: {path: "/products/detail_coupon", method: "get", pick: "coupon"},
list: {path: "/products/list_coupons", method: "get", pick: "coupon"},
create: {path: "/products/create_coupon", method: "post"},
update: {path: "/products/update_coupon", method: "post"},
del: {path: "/products/delete_coupon", method: "post"}
};
function createMethod(methodDetails, options2co) {
return promisify(function(options, done) {
if(typeof options !== "object") {
done = options;
options = {};
}
done = done || function(){};
var reqOptions = {
url: "https://www.2checkout.com/api" + methodDetails.path,
method: methodDetails.method,
qs: (methodDetails.method=="get"?options:undefined),
form: (methodDetails.method=="post"?options:undefined),
headers: {Accept: "application/json"},
auth: {username: options2co.username, password: options2co.password}
};
console.log(
"[2co]",
"[" + new Date() + "]",
"[" + options2co.username + "]",
reqOptions.method.toUpperCase(),
reqOptions.url,
reqOptions.qs || reqOptions.form || null
);
if(options2co.logFile) {
fs.appendFile(
options2co.logFile,
"[" + new Date() + "] [api-request] [" + options2co.username + "] " + reqOptions.method.toUpperCase() + " " + reqOptions.url + " " + JSON.stringify(reqOptions.qs || reqOptions.form || "", null, 2) + "\n",
function() {}
);
}
request(reqOptions, function(err, res) {
if(err) return done(err);
try {
if(options2co.logFile) {
fs.appendFile(
options2co.logFile,
"[" + new Date() + "] [api-response] [" + options2co.username + "] " + res.body + "\n",
function() {}
);
}
var response = JSON.parse(res.body);
if(response.errors) return done(response.errors);
var returnData = [null];
if(methodDetails.pick) {
((methodDetails.pick instanceof Array)?methodDetails.pick:[methodDetails.pick]).forEach(function(field) {
returnData.push(response[field]);
});
}
returnData.push(response);
done.apply(null, returnData);
} catch(e) {
done(e);
}
});
});
}
function buildObject(methodsHash, options) {
return Object.keys(methodsHash).reduce(function(obj, methodName) {
obj[methodName] = createMethod(methodsHash[methodName], options);
return obj;
}, {});
}
module.exports = function(options) {
var client = {
account: buildObject(accountMethods, options),
sales: buildObject(salesMethods, options),
products: buildObject(productMethods, options)
};
client.products.options = buildObject(productOptionsMethods, options);
client.products.coupons = buildObject(productCouponMethods, options);
client.canTrustINS = function(data) {
if(options.logFile) {
fs.appendFile(
options.logFile,
"[" + new Date() + "] [INS] " + JSON.stringify(data, null, 2) + "\n",
function() {}
);
}
return hash.md5(
data.sale_id + "" + data.vendor_id + data.invoice_id + options.secret
).toUpperCase() == data.md5_hash;
};
client.canTrustReturnData = function(data) {
if(options.logFile) {
fs.appendFile(
options.logFile,
"[" + new Date() + "] [return] " + JSON.stringify(data, null, 2) + "\n",
function() {}
);
}
if(options.test) return true;
// Consider using data.demo above, to eliminate the need for options.test. However, what happens if
// the MitM sets data.demo to truthy in production requests? They would be treated as valid requests!
return hash.md5(
options.secret + data.sid + data.order_number + data.total
).toUpperCase() == data.key;
}
return client;
}