forked from danibrear/meteor-paypal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaypal.js
executable file
·267 lines (239 loc) · 8.58 KB
/
paypal.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
Paypal = {
account_options: {},
// authorize submits a payment authorization to Paypal
authorize: function(card_info, payment_info, callback){
Meteor.call('paypal_submit', 'authorize', card_info, payment_info, callback);
},
purchase: function(card_info, payment_info, callback){
Meteor.call('paypal_submit', 'sale', card_info, payment_info, callback);
},
vaultCreate: function(cardInfo, callback){
Meteor.call('paypal_vault', 'create', cardInfo, callback);
},
vaultGet: function(cardRef, callback){
Meteor.call('paypal_vault', 'get', null, cardRef, callback);
},
vaultDelete: function(cardRef, callback){
Meteor.call('paypal_vault', 'delete', null, cardRef, callback);
},
vaultList: function(cardFilter, callback){
Meteor.call('paypal_vault', 'list', cardFilter, callback);
},
saleLookup: function(saleId, callback) {
Meteor.call('paypal_sale', 'get', saleId, callback);
},
saleRefund: function(saleId, refund_data, callback) {
Meteor.call('paypal_sale', 'refund', saleId, refund_data, callback);
},
//config is for the paypal configuration settings.
config: function(options){
this.account_options = options;
},
payment_json: function(){
return {
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": []},
"transactions": []
};
},
//parseCardData splits up the card data and puts it into a paypal friendly format.
parseCardData: function(data){
var cardData;
var first_name = '', last_name = '';
if (data.name){
first_name = data.name.split(' ')[0];
last_name = data.name.split(' ')[1];
}
else {
first_name = data.first_name;
last_name = data.last_name;
}
//check if we are passing in a token or credit card information
if (data.credit_card_id) {
cardData = {
credit_card_token: {
credit_card_id: data.credit_card_id
}
};
}
else { //CC info
cardData = {
credit_card: {
type: data.type,
number: data.number,
first_name: first_name,
last_name: last_name,
cvv2: data.cvv2,
expire_month: data.expire_month,
expire_year: data.expire_year
}
};
}
return cardData;
},
parsePayerInfo: function(data) { // future use to pass in a payer's information
//Derive payer info to pass through to PP
var first_name = '', last_name = '';
if (data.name){
first_name = data.name.split(' ')[0];
last_name = data.name.split(' ')[1];
}
else {
first_name = data.first_name;
last_name = data.last_name;
}
var payerInfo = {
first_name: first_name,
last_name: last_name,
email: data.email,
salutation: data.salutation
};
return payerInfo;
},
//parsePaymentData splits up the card data and gets it into a paypal friendly format.
parsePaymentData: function(data){
return {amount: {total: data.total, currency: data.currency}};
}
};
if(Meteor.isServer){
Meteor.startup(function(){
var paypal_sdk = Npm.require('paypal-rest-sdk');
var Fiber = Npm.require('fibers');
var Future = Npm.require('fibers/future');
Meteor.methods({
paypal_submit: function(txType, cardData, paymentData, payerInfo) {
//setup app variables from paypal_config.js as defined in the docs
paypal_sdk.configure(Paypal.account_options);
// setup the future
var fut = new Future();
this.unblock();
//setup payment json
var payment_json = Paypal.payment_json();
//specify transaction type
payment_json.intent = txType;
// either set paypal payment type of CC data
if(cardData === null) {
payment_json.payer = {
payment_method: 'paypal'
};
payment_json.redirect_urls = Paypal.account_options.redirect_urls;
} else {
payment_json.payer.funding_instruments.push(Paypal.parseCardData(cardData));
}
// format payment data
payment_json.transactions.push(Paypal.parsePaymentData(paymentData));
// create the payment
paypal_sdk.payment.create(payment_json, Meteor.bindEnvironment(function(err, payment){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, payment: payment});
}
},
function(e){
console.error(e);
}));
return fut.wait();
},
// Method to enable vault actions: store, get, delete, list
paypal_vault: function(txType, cardData, cardRef) {
//setup app variables from paypal_config.js as defined in the docs
paypal_sdk.configure(Paypal.account_options);
// setup the future
var fut = new Future();
this.unblock();
// check which type of vault action we're dealing with
switch (txType) {
case 'create': // store new credit card
paypal_sdk.credit_card.create(cardData, Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
case 'get': // lookup a stored credit card
paypal_sdk.credit_card.get(cardRef, Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
case 'delete': // delete stored credit card
paypal_sdk.credit_card.delete(cardRef, Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
default: //list cards based on a filter
paypal_sdk.credit_card.get(cardData, Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
}
},
paypal_sale: function(txType, saleId, refund_data) {
//setup app variables from paypal_config.js as defined in the docs
paypal_sdk.configure(Paypal.account_options);
// setup the future
var fut = new Future();
this.unblock();
// check what type of sale transaction we're dealing with
switch (txType) {
case 'refund': // partial/full refund
paypal_sdk.sale.refund(saleId, Paypal.parsePaymentData(refund_data), Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
default: // lookup a sale
paypal_sdk.sale.get(saleId, Meteor.bindEnvironment(function(err, result){
if (err){
fut.return({success: false, error: err});
} else {
fut.return({success: true, result: result});
}
},
function(e){
console.error(e);
}));
return fut.wait();
}
}
}); // Meteor.methods
// execute the payment
Paypal.execute = function execute(payment_id, payer_id, callback) {
paypal_sdk.payment.execute(payment_id, {payer_id: payer_id}, Paypal.account_options, callback);
};
}); // Meteor.isServer
}