This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagseguro.js
67 lines (50 loc) · 1.45 KB
/
pagseguro.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
var hat = require('hat')
var PagSeguroGateway = require('pagseguro');
var PagSeguro = function(store, currency) {
this.store = store;
this.currency = currency;
};
PagSeguro.prototype.order = function(items, buyer, urls, fn) {
fn = fn || function(){};
this.urls = urls || {};
// Prepare gateway
this.gateway = new PagSeguroGateway(this.store.email, this.store.token);
this.gateway.currency(this.currency || "BRL");
// Order reference
var reference = hat();
this.gateway.reference(this.reference);
// Buyer information
this.gateway.buyer({
name: buyer.name,
email: buyer.email
});
// Populate with items
for(var i = 0; i < items.length; i++) {
var item = items[i];
this.gateway.addItem({
id: item.id,
description: item.description,
amount: item.value.toFixed(2).toString(),
quantity: parseInt(item.quantity)
});
}
// Redir e notification url
if(this.urls.redirectUrl)
this.gateway.setRedirectURL("http://www.lojamodelo.com.br/retorno");
if(this.urls.notificationUrl)
this.gateway.setNotificationURL("http://www.lojamodelo.com.br/notificacao");
// Enviando o xml ao pagseguro
this.gateway.send(function(err, xml) {
if(err) {
fn(err);
}
else {
var parseString = require('xml2js').parseString;
parseString(xml, fn);
}
});
}
PagSeguro.prototype.receipt = function(notificationCode, notificationType, fn) {
// TODO: acessar pagseguro para retornar as informacoes da transacao
}
module.exports = PagSeguro;