-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstripe_easy_server.js
126 lines (107 loc) · 3.63 KB
/
stripe_easy_server.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
// if there is not a secret key set
if(!Meteor.settings || !Meteor.settings.Stripe || !Meteor.settings.Stripe.secretKey){
console.warn("Stripe secret key is not set in Meteor.settings");
} else {
var Stripe = Npm.require('stripe')(Meteor.settings.Stripe.secretKey);
}
// TEST WHETHER STRIPE IS DEFINED OR NOT
if(typeof Stripe === "undefined"){
console.warn("Stripe is not defined");
}
Meteor.methods({
stripeEasySubscribe: function(token, plan_id){
if(!this.userId){
throw new Meteor.Error(401, "No userId found by stripeEasySubscribe method");
}
var Future = Npm.require("fibers/future");
var future = new Future();
var user = Meteor.users.findOne({_id: this.userId});
var services = user.services;
var email = null;
if(user && services){
if(services.facebook && services.facebook.email){
email = services.facebook.email;
} else if(services.google && services.google.email){
email = services.google.email;
} else if(user.emails && user.emails[0] && user.emails[0].address){
email = user.emails[0].address;
} else {
throw new Meteor.Error(400, "stripeEasySubscribe Method was unable to find an email address for the signed in user");
}
} else {
throw new Meteor.Error(400, "No services found on user object");
}
var bound = Meteor.bindEnvironment(function(err, customer){
if(err) {
console.warn(err);
future.throw(new Meteor.Error(400, err.message));
}
else {
// console.log(customer);
// console.log(customer.id);
// console.log("subscription data!!!!!");
// console.log(customer.subscriptions.data[0]);
// update the user object
Meteor.users.update({_id: user._id}, {$set: {"stripe.customerId": customer.id, "stripe.subscription": customer.subscriptions.data[0]}});
future.return(customer);
}
});
Stripe.customers.create({
card: token,
plan: plan_id,
email: email
}, bound);
return future.wait();
}, // stripeEasySubscribe
stripeEasyUpdate: function(plan_id){
if(!this.userId){
throw new Meteor.Error(401, "Not an authorized user.");
}
var Future = Npm.require("fibers/future");
var future = new Future();
var user = Meteor.users.findOne({_id: this.userId});
var bound = Meteor.bindEnvironment(function(err, subscription){
if(err) {
console.warn(err);
future.throw(new Meteor.Error(400, err.message));
}
else {
// console.log(subscription);
Meteor.users.update({_id: user._id}, {$set: {"stripe.subscription": subscription}});
future.return(subscription);
}
});
Stripe.customers.updateSubscription(
user.stripe.customerId,
user.stripe.subscription.id,
{ plan: plan_id },
bound
);
return future.wait();
},
stripeEasyCancel: function(){
if(!this.userId){
throw new Meteor.Error(401, "Not an authorized user.");
}
var Future = Npm.require("fibers/future");
var future = new Future();
var user = Meteor.users.findOne({_id: this.userId});
var bound = Meteor.bindEnvironment(function(err, subscription){
if(err) {
console.warn(err);
future.throw(new Meteor.Error(400, err.message));
}
else {
// console.log(subscription);
Meteor.users.update({_id: user._id}, {$set: {"stripe.subscription": subscription}});
future.return(subscription);
}
});
Stripe.customers.cancelSubscription(
user.stripe.customerId,
user.stripe.subscription.id,
bound
);
return future.wait();
}
});