-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling_server.js
48 lines (35 loc) · 1.1 KB
/
billing_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
Billing.upgradeTo = function(subscription, plan) {
if (!subscription) {
throw new Meteor.Error(400, "Invalid subscription");
}
var subscription = this.findSubscription(subscription);
if (!subscription) {
throw new Meteor.Error(404, "Invalid subscription");
}
var currentPlan = this.findPlan(subscription.plan);
var plan = this.findPlan(plan);
if (currentPlan && currentPlan.weight > plan.weight) {
throw new Meteor.Error(400, "Can't upgrade to this subscription");
}
if (Billing.onBeforeUpgrade && !Billing.onBeforeUpgrade(subscription, plan)) {
return false;
}
Subscription.update({
_id: subscription._id
}, {
$set: {
'plan': plan._id
}
}, {validate: false});
Billing.onAfterUpgrade && Billing.onAfterUpgrade(subscription, plan);
return true;
}
Meteor.methods({
'billingUpgradeTo': function(plan) {
var subscriber = Billing.subscriber();
if(!subscriber) {
throw new Meteor.Error(403, "You need to be logged in to upgrade your account");
}
return Billing.upgradeTo(subscriber.subscription, plan);
}
});