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
/
index.js
110 lines (78 loc) · 1.95 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
var util = require('util');
var internalClient = require('deployd/lib/internal-client');
var Resource = require('deployd/lib/resource')
var UserCollection = require('deployd/lib/resources/user-collection');
var Pagseguro = require("./pagseguro");
var Settings = require("./settings.js");
function PagSeguroResource(name, options) {
Resource.apply(this, arguments);
}
// Resource Inheritance
util.inherits(PagSeguroResource, Resource);
// Resource settings
PagSeguroResource.events = ["get", "post"];
PagSeguroResource.basicDashboard = Settings.basicDashboard;
// Handle incoming requests
PagSeguroResource.prototype.handle = function (ctx, next) {
var config = Settings.parse(this.config);
var urls = {
redirectUrl: config.redirUrl,
notificationUrl: config.redirectUrl
}
this.pagseguro = new Pagseguro({
email: config.storeEmail,
token: config.storeToken
}, config.currency);
if (ctx.method === "GET") {
ctx.dpd.users.me(function(me, err) {
console.log(me)
if(err) {
return ctx.done(err, null);
}
else if(!me) {
ctx.res.statusCode = 403;
return ctx.done('forbidden');
}
ctx.done(me);
});
}
// Nova transação
else if (ctx.method === "POST" && this.events.post) {
var _post = this.events.post;
// ger user information
ctx.dpd.users.me(function(me, err) {
if(err) {
return ctx.done(err, null);
}
else if(!me) {
ctx.res.statusCode = 403;
return ctx.done('forbidden');
}
var buyer = {
name: me.name,
email: me.email
};
this.pagseguro.order(ctx.body.items, buyer, urls, function(err, res) {
if(err) console.log(err)
var result = {};
var domain = {
request: {
query: ctx.query,
body: ctx.body
},
response: res,
setResult: function(val) {
result = val;
}
};
_post.run(ctx, domain, function(err) {
ctx.done(err, result);
});
})
});
}
else {
next();
}
}
module.exports = PagSeguroResource;