diff --git a/app/controllers/team/manage.js b/app/controllers/team/manage.js index 452cd33..e7f88fc 100644 --- a/app/controllers/team/manage.js +++ b/app/controllers/team/manage.js @@ -1,11 +1,24 @@ import Ember from 'ember'; import extractError from 'teamplaybook-ember/lib/extract-error'; +import ajax from 'ic-ajax'; +import ENV from 'teamplaybook-ember/config/environment'; export default Ember.Controller.extend({ showError: false, errorMessage: null, + cardToken: null, + currentPlan: Ember.computed('model.planSlug', function (){ + var plans = this.store.all('plan'); + return plans.findBy('slug', this.get('model.planSlug')); + }), + + currentPlanIsPaid: Ember.computed.alias('currentPlan.isPaid'), + + plans: Ember.computed(function(){ + return this.store.find('plan'); + }), actions: { delete: function() { @@ -26,6 +39,52 @@ export default Ember.Controller.extend({ }); }); } + }, + changePlan: function(){ + var controller = this; + if(this.get('currentPlanIsPaid')){ + this.createStripeToken().then(function(){ + controller.requestPlanChange(); + }); + }else{ + this.requestPlanChange(); + } } + }, + + _buildURL: function(path) { + var apiUrl = this.get('urlInfo.apiUrl'); + return apiUrl + '/' + path; + }, + + requestPlanChange: function(){ + var team = this.get('model'); + + ajax({ + type: 'POST', + url: this._buildURL('team/change_plan'), + data: { + plan_slug: team.get('planSlug'), + card_token: this.get('cardToken') + } + }).then(function(){ + alert("You have changed your plan"); + }, function(response){ + alert(extractError(response)); + }); + }, + + createStripeToken: function(){ + var Stripe = window.Stripe; + Stripe.setPublishableKey(ENV.STRIPE_PUBLIC_KEY); + var controller = this; + var $form = Ember.$('#payment-form'); + + return new Ember.RSVP.Promise(function(resolve) { + Stripe.card.createToken($form, function(status, response) { + controller.set('cardToken', response.id); + resolve(); + }); + }); } }); \ No newline at end of file diff --git a/app/models/plan.js b/app/models/plan.js new file mode 100644 index 0000000..1593c01 --- /dev/null +++ b/app/models/plan.js @@ -0,0 +1,10 @@ +import DS from 'ember-data'; +import Ember from 'ember'; + +export default DS.Model.extend({ + slug: DS.attr('string'), + name: DS.attr('string'), + trialPeriodDays: DS.attr('number'), + amount: DS.attr('number'), + isPaid: Ember.computed.gt('amount', 0) +}); \ No newline at end of file diff --git a/app/models/team.js b/app/models/team.js index 78783ff..7446e4a 100644 --- a/app/models/team.js +++ b/app/models/team.js @@ -2,8 +2,10 @@ import DS from 'ember-data'; export default DS.Model.extend({ name: DS.attr('string'), + planName: DS.attr('string'), + planSlug: DS.attr('string'), subdomain: DS.attr('string'), owner: DS.belongsTo('user', { async: true }), teamMemberships: DS.hasMany('team-membership', { async: true }), members: DS.hasMany('user') -}); +}); \ No newline at end of file diff --git a/app/templates/account-nav.hbs b/app/templates/account-nav.hbs index ddffae5..08d2a38 100644 --- a/app/templates/account-nav.hbs +++ b/app/templates/account-nav.hbs @@ -12,4 +12,4 @@ {{/if}} - + \ No newline at end of file diff --git a/app/templates/team/manage.hbs b/app/templates/team/manage.hbs index 584006a..fd113b1 100644 --- a/app/templates/team/manage.hbs +++ b/app/templates/team/manage.hbs @@ -1,3 +1,4 @@ +