Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/controllers/team/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Ember from 'ember';
import ajax from 'ic-ajax';
import ENV from 'teamplaybook-ember/config/environment';
import extractError from 'teamplaybook-ember/lib/extract-error';

var $ = window.$;

export default Ember.Controller.extend({
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:{
changePlan: function(){
if(this.get('currentPlanIsPaid')){
this.createStripeToken(this.requestPlanChange.bind(this));
}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(callback){
var Stripe = window.Stripe;
Stripe.setPublishableKey(ENV.STRIPE_PUBLIC_KEY);
var controller = this;
var $form = $('#payment-form');

var stripeCallback = function(status, response){
controller.set('cardToken', response.id);
callback();
};
Stripe.card.createToken($form, stripeCallback);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work here?

return new Ember.RSVP.Promise(function(resolve) {
  Stripe.card.createToken($form, function(status, response) {
    controller.set('cardToken', response.id);
    resolve();
  });
});

Then in the createPlan action, you could have

 this.createStripeToken().then(this.requestPlanChange)

or

var controller = this;
controller.createStripeToken().then(function() {
  this.requestPlanChange
});

, whichever works.

Also, if you move the form and related logic to a component, I think it would be more nicely abstracted, though I think something like that is big enough for a refactor task of it's own.

});
10 changes: 10 additions & 0 deletions app/models/plan.js
Original file line number Diff line number Diff line change
@@ -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'),
trial_period_days: DS.attr('number'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't camelized. Any reason, or did it just slip through? I ask because I had issues with current user's authentication_token normalization (it wouldn't properly map to authenticationToken on the client side), but ended up changing the code so it didn't factor in.

amount: DS.attr('number'),
isPaid: Ember.computed.gt('amount', 0)
});
4 changes: 3 additions & 1 deletion app/models/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});
});
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default Router.map(function() {
this.route('team-members', { path: 'members' }, function() {
this.route('index', { path: '/' });
});
this.route('settings');
this.route('manage');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a refactor task to merge these two routes when this is merged? I think it makes sense to have these two things on one route.

});
} else if (shouldMapGeneralRoutes) {
Expand Down
8 changes: 8 additions & 0 deletions app/routes/team/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function() {
return this.store.find('team', 'current');
}
});
2 changes: 1 addition & 1 deletion app/templates/account-nav.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
{{/if}}
</ul>
</section>
</nav>
</nav>
1 change: 1 addition & 0 deletions app/templates/team-nav.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ul class="side-nav">
<li>{{#link-to 'team.index'}}Home{{/link-to}}</li>
<li>{{#link-to 'team.team-members'}}Members{{/link-to}}</li>
<li>{{#link-to 'team.settings'}}Settings{{/link-to}}</li>
{{#if currentUserIsTeamOwner}}<li>{{#link-to 'team.manage'}}Manage team{{/link-to}}</li>{{/if}}
</ul>
</nav>
34 changes: 34 additions & 0 deletions app/templates/team/settings.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<h1> Setting for {{model.name}}</h1>
<h4> Your team is currently subscribed to the {{model.planName}}</h4>

<form>
{{#each plan in plans}}
{{radio-button
value=plan.slug
groupValue=model.planSlug
required=true
name="plan"}}
{{plan.name}}
{{/each}}
</form>

{{#if currentPlanIsPaid}}

<h3>This is a paid plan, add your credit card information</h3>

<form action="" method="POST" id="payment-form">
<!-- Add a section to display errors if you want -->
<span class='payment-errors'></span>
<label>Card Number</label>
<input data-stripe="number"/>
<label>CVC</label>
<input data-stripe="cvc"/>
<label>Card Expiration Month</label>
<input data-stripe="exp-month"/>
<label>Card Expiration Year</label>
<input data-stripe="exp-year"/>
</form>
{{/if}}
<button {{action 'changePlan'}}>Change plan</button>
8 changes: 5 additions & 3 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ module.exports = function(environment) {
/*jshint quotmark: false*/
contentSecurityPolicy: {
'default-src': "'none'",
'script-src': "'self'",
'script-src': "'self' https://js.stripe.com",
'font-src': "'self'",
'connect-src': "'self'",
'img-src': "'self'",
'style-src': "'self'",
'media-src': "'self'"
'media-src': "'self' https://js.stripe.com",
'frame-src': "'self' https://js.stripe.com"
},
/*jshint quotmark: true*/
subdomainMapping: {
Expand All @@ -36,7 +37,8 @@ module.exports = function(environment) {
routeAfterAuthentication: '/',
authorizer: 'authorizer:custom',
crossOriginWhitelist: ['*']
}
},
STRIPE_PUBLIC_KEY: 'pk_test_2YDSiQNDW9IlNzdADzleLTvQ'
};

if (environment === 'development') {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-export-application-global": "^1.0.2",
"ember-json-api": "git://github.com/eneuhauser/ember-json-api.git"
"ember-json-api": "git://github.com/eneuhauser/ember-json-api.git",
"ember-radio-button": "1.0.5"
}
}