This repository has been archived by the owner on Oct 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpaypal.js
88 lines (85 loc) · 2.94 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
Meteor.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);
},
//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 first_name = '', last_name = '';
if (data.name){
first_name = data.name.split(' ')[0];
last_name = data.name.split(' ')[1]
}
return {
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
}};
},
//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(transaction_type, cardData, paymentData){
paypal_sdk.configure(Meteor.Paypal.account_options);
var payment_json = Meteor.Paypal.payment_json();
payment_json.intent = transaction_type;
if(cardData == null) {
payment_json.payer = {
payment_method: 'paypal'
};
payment_json.redirect_urls = Meteor.Paypal.account_options.redirect_urls;
} else {
payment_json.payer.funding_instruments.push(Meteor.Paypal.parseCardData(cardData));
}
payment_json.transactions.push(Meteor.Paypal.parsePaymentData(paymentData));
var fut = new Future();
this.unblock();
paypal_sdk.payment.create(payment_json, Meteor.bindEnvironment(function(err, payment){
if (err){
fut.return({saved: false, error: err});
} else {
fut.return({saved: true, payment: payment});
}
},
function(e){
console.error(e);
}));
return fut.wait();
}});
// this is not a method because it should really only be
// called by server-side code
Meteor.Paypal.execute = function execute(payment_id, payer_id, callback) {
paypal_sdk.payment.execute(payment_id, {payer_id: payer_id}, Meteor.Paypal.account_options, callback);
};
});
}