-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayper.js
More file actions
147 lines (129 loc) · 4.32 KB
/
Copy pathpayper.js
File metadata and controls
147 lines (129 loc) · 4.32 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
// Generated by CoffeeScript 1.6.2
/*
Node Examples for the Paypal REST API with SuperAgent AJAX
See https://github.com/apelade/payper/README.md for details
*/
var CREDIT_CARD, PAYMENT, REFUND, SALE, ajaxGet, ajaxPost, exports, printResults, superagent;
superagent = require('superagent');
printResults = function(err, res) {
if (err != null) {
console.log(err);
}
if ((res != null ? res.body : void 0) != null) {
return console.log(res.body);
}
};
PAYMENT = 'https://api.sandbox.paypal.com/v1/payments/payment/';
SALE = 'https://api.paypal.com/v1/payments/sale/';
REFUND = 'https://api.paypal.com/v1/refund/';
CREDIT_CARD = 'https://api.paypal.com/v1/vault/credit-card/';
/*
Three wrapper functions for each main ajax type we need. No beforeSend func!
*/
ajaxGet = function(extraPath, token, callback) {
return superagent.agent().get(PAYMENT + extraPath).set('Content-Type', 'application/json').set('Authorization', 'Bearer ' + token).end(callback);
};
ajaxPost = function(path, data, token, callback) {
return superagent.agent().post(PAYMENT + path).type('application/x-www-form-urlencoded').send(data).set('Content-Type', 'application/json').set('Authorization', 'Bearer ' + token).end(callback);
};
/*
These functions could be called from Express server route files, for example
Params username and password are paypal sandbox credentials.
Get your own for interaction with live paypal objects.
*/
exports = module.exports = {
getToken: function(username, password, callback) {
var basicAuthCred, client_id, client_secret, data, url;
if (callback == null) {
callback = printResults;
}
client_id = username;
client_secret = password;
basicAuthCred = client_id + ":" + client_secret + "@";
url = "https://" + basicAuthCred + "api.sandbox.paypal.com/v1/oauth2/token";
data = {
grant_type: 'client_credentials'
};
return superagent.agent().post(url).type('application/x-www-form-urlencoded').send(data).set('Accept', 'application/json').set('Accept-Language', 'en_US').end(callback);
},
getAllPayments: function(token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet('', token, callback);
},
getApprovedPayments: function(token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet('?state=approved', token, callback);
},
getPaymentsPaged: function(token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet('?count=10', token, callback);
},
getPaymentById: function(id, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet(id, token, callback);
},
createPayment: function(payment, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxPost('', JSON.stringify(payment), token, callback);
},
executePayment: function(id, payer, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxPost(id + '/execute/', JSON.stringify(payer), token, callback);
},
getSaleById: function(id, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet(SALE + id, token, callback);
},
refundSale: function(id, token, callback) {
var empty_payload;
if (callback == null) {
callback = printResults;
}
empty_payload = {};
return ajaxPost(SALE + id + '/refund', empty_payload, token, callback);
},
getRefundById: function(id, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajaxGet(REFUND + id, token, callback);
},
storeCreditCard: function(type, num, exp_mo, exp_yr, cvv2, first_name, last_name, billing_addr, token, callback) {
var card;
if (callback == null) {
callback = printResults;
}
card = {
type: type,
number: num,
expire_month: exp_mo,
expire_year: exp_yr,
cvv2: cvv2,
first_name: first_name,
last_name: last_name,
billing_addr: billing_addr
};
return ajaxPost(CREDIT_CARD, JSON.stringify(card), token, callback);
},
getCreditCardById: function(id, token, callback) {
if (callback == null) {
callback = printResults;
}
return ajax.get(CREDIT_CARD + id, token, callback);
}
};
return module.exports;