forked from lucasbrigida/node-moip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoip.js
More file actions
232 lines (184 loc) · 4.75 KB
/
moip.js
File metadata and controls
232 lines (184 loc) · 4.75 KB
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
/*
* Module to Moip API
*
* @author Lucas Pereira Brigida <a href="http://github.com/lucasbrigida"></a>
*
* @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>
*
*
*/
/*
* Dependencies
*/
var xml2js = require('xml2js');
var data2xml_convert = require('data2xml')();
function Moip(){
var self = this;
this.environment= "test";
this.send = function(payment, callback){
//if(payment.environment) return(new Payment(environment, payment).validate().send()); validate(); --> Not Implemented
if(payment.environment){
if(callback) return(new Payment(payment).send(callback));
else return(new Payment(payment));
}
else{
payment.environment = self.environment;
if(callback) return(new Payment(payment).send(callback));
else return(new Payment(payment));
}
}
return(this);
}
function Payment(payment){
var self = this;
var payment = payment;
this.XML = null;
this.environment = payment.environment;
(function createPayment(){
if(payment.xml){
self.XML = payment.xml;
return;
}
if(payment.data){
//Generate XML (Basic Payment)
self.moip_payment = data2xml_convert("EnviarInstrucao", payment.data);
self.XML = self.moip_payment;
}
else throw('"data" variable undefined. Use data = json');
})();
this.validate = function(){
switch(payment.mode){
case 'identification': {
if(payment.xml) var xml = payment.xml;
else var xml = self.moip_payment;
// Validar XML gerado em xml(variável)
self.XML = xml;
var validation = true;
return(validation); // Retornar true ou false
} break;
default: {
if(payment.xml) var xml = payment.xml;
else var xml = self.moip_payment;
// Validar XML gerado em xml(variável)
self.XML = xml;
var validation = true;
return(validation); // Retornar true ou false
} break;
}
}
this.send = function(callback){
if(callback){
if(typeof(callback) === 'function'){
return(new Sender().send({
environment: self.environment,
xml: self.XML,
token: payment.token,
appkey: payment.appkey
}, callback));
}
else{
return(new Sender().send({
environment: self.environment,
xml: self.XML,
token: payment.token,
appkey: payment.appkey
}));
}
}
}
return(this);
}
function Sender(){
var self = this;
var https = require("https");
this.response = null;
this.received = false;
this.send = function(options, callback){
if(callback){
if(typeof(callback) === 'function'){
if(options.environment === 'test'){
var url = 'desenvolvedor.moip.com.br'
, path = "/sandbox/ws/alpha/EnviarInstrucao/Unica";
}else{
var url = 'www.moip.com.br'
, path = "/ws/alpha/EnviarInstrucao/Unica";
}
//Verificar o environment, definir url e realizar os procedimentos de Envio
self.sendRaw({
url: url,
path: path,
xml: options.xml,
appkey: options.appkey,
token: options.token,
callback: callback
});
return;
}
}
else{
//Verificar o environment, definir url e realizar os procedimentos de Envio
if(options.environment === 'test'){
var url = 'desenvolvedor.moip.com.br'
, path = "/sandbox/ws/alpha/EnviarInstrucao/Unica";
}else{
var url = 'www.moip.com.br'
, path = "/ws/alpha/EnviarInstrucao/Unica";
}
self.sendRaw({
url: url,
path: path,
xml: options.xml,
appkey: options.appkey,
token: options.token
});
return;
}
}
this.sendRaw = function(options){
/* @options = {
url: "A Moip url to send payment data",
path: "Path to Moip URL",
xml: "The Payment XML",
appkey: "The App Key provide by Moip",
token: "The Token provide by Moip",
callback: "Is NOT required"
};
*/
//console.log(options.xml);
var https_options = {
host: options.url,
path: options.path,
method: 'POST',
auth: options.token + ':' + options.appkey
};
var req = https.request(https_options, function(res){
res.on('data', function(chunck){
var parser = new xml2js.Parser();
parser.parseString(chunck.toString(), function (err, result) {
if(err){
console.log('[MOIP_MODULE_ERROR]: ',err);
console.log('[MOIP_MODULE_RESULT]:',result);
options.callback(null);
return;
}
var Resposta = result;
if(options.callback){
if(typeof(options.callback) === 'function'){
var _callback = options.callback;
if(Resposta) _callback(Resposta);
else _callback(null);
}
}
self.received = true;
self.response = Resposta;
});
});
});
req.write(options.xml);
req.end();
};
return(this);
}
exports.Moip = Moip;
exports.Payment = Payment;
exports.Sender = Sender;