-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (168 loc) · 5.1 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
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
/**
* Created by Bruno Sampaio Pinho da Silva
* On Feb. 3rd, 2016
*/
"use strict";
var request = require('request');
var parseString = require('xml2js').parseString;
// default mode
var PROD_MODE = true;
var Item = require('./lib/item');
var Buyer = require('./lib/buyer');
var Shipment = require('./lib/shipment');
var URL_API_PAY;
var URL_API_PAY_PROD = 'https://ws.pagseguro.uol.com.br/v2/checkout/';
var URL_API_PAY_SAND = 'https://ws.sandbox.pagseguro.uol.com.br/v2/checkout/';
if (PROD_MODE) { // Production mode
URL_API_PAY = 'https://ws.pagseguro.uol.com.br/v2/checkout/';
}
else { // Sandbox mode
URL_API_PAY = 'https://ws.sandbox.pagseguro.uol.com.br/v2/checkout/';
}
/**
* PagSeguro object interacts with payment API
* @param {string} email - account which makes requests to PagSeguro
* @param {string} token - necessary token to start requests
* @param {string} charset - optional charset
* @returns {object} PagSeguro object
*/
var PagSeguro = function (email, token, charset) {
var self = this;
if (arguments.length === 3) {
self.charset = charset.toString().toUpperCase();
if (self.charset != 'UTF-8' && self.charset != 'ISO-8859-1') {
console.error('Error: correct charsets are UTF-8 or ISO-8859-1');
return false;
}
self.email = email;
self.token = token;
}
else if (arguments.length == 2) {
self.token = token;
self.email = email;
self.charset = 'UTF-8';
}
else {
console.error('Error: correct parameters for PagSeguro -> (email, token [,charset])');
return false;
}
self.currency = "BRL";
self.items = [];
self.buyer;
self.shipment;
self.reference = '';
self.redirectURL = '';
self.notificationURL = '';
self.maxUses = 1000;
self.maxAge = 1000;
var mountQueryString = function () {
var qs = {};
for (var i = 0; i < self.items.length; i++) {
qs = self.items[i].mountQueryString(i + 1, qs);
}
if (self.buyer)
qs = self.buyer.mountQueryString(qs);
if (self.shipment)
qs = self.shipment.mountQueryString(qs);
return qs;
};
return {
setToken : function (newToken) {
self.token = newToken;
},
setEmail : function (email) {
if (email && typeof email === 'string')
self.email = email;
},
setMode : function (mode) {
if (mode && typeof mode === 'string') {
if (mode.toUpperCase() == 'SANDBOX')
PROD_MODE = false;
else if (mode.toUpperCase() == 'PROD')
PROD_MODE = true;
}
else if (typeof mode === 'boolean') {
PROD_MODE = mode;
}
else {
console.log('Invalid mode, default set: PROD');
}
},
setRedirectURL : function (url) {
self.redirectURL = url;
},
setNotificationURL : function (url) {
self.notificationURL = url;
},
setReference : function (ref) {
self.reference = ref;
},
getItemsLength : function () {
return self.items.length;
},
addItem : function (item) {
self.items.push(new Item(item));
},
getItemById : function (id) {
for (var i = 0; i < self.items.length; i++) {
if (self.items[i].getId() === id) return self.items[i];
}
return null;
},
addBuyer : function (buyer) {
self.buyer = new Buyer(buyer);
},
addShipment : function (spm) {
self.shipment = new Shipment(spm);
},
processOrder : function (callback) {
var hasCallback = true;
if (!callback || typeof callback != "function") {
hasCallback = false;
}
var options = {
url: PROD_MODE ? URL_API_PAY_PROD : URL_API_PAY_SAND,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=' + self.charset
}
};
options.qs = mountQueryString();
options.qs.email = self.email;
options.qs.token = self.token;
options.qs.currency = self.currency;
if (self.redirectURL != '') options.qs.redirectURL = self.redirectURL;
if (self.notificationURL != '') options.qs.notificationURL = self.notificationURL;
if (self.reference != '') options.qs.reference = self.reference;
if (!PROD_MODE) console.log(options);
request.post(options, function (error, response, body) {
// parse xml returned from pagSeguro API
parseString(body, function (err, result) {
// if you get an error here, make sure you are
// passing parameters correctly
// error reading xml
if (err) {
console.log(err);
if (hasCallback)
callback(err, response, null);
else
return err;
}
// got good response
if (!error && response.statusCode == 200) {
if (hasCallback)
callback(null, response, result.checkout);
else
return result.checkout;
}
else { // got error
if (hasCallback)
callback(result.errors.error, response, null);
else
return result.errors.error;
}
});
});
}
};
};
module.exports = PagSeguro;