create the subscription

This commit is contained in:
Rimian Perkins 2019-10-14 11:47:49 +11:00
parent 51a0820397
commit 4e615908f4
6 changed files with 26 additions and 8 deletions

View File

@ -10,7 +10,7 @@ module DiscoursePatrons
subscription = ::Stripe::Subscription.create( subscription = ::Stripe::Subscription.create(
customer: params[:customer], customer: params[:customer],
items: [ items: [
{ plan: 'plan_CBXbz9i7AIOTzr' }, { plan: params[:plan] },
] ]
) )

View File

@ -21,14 +21,27 @@ export default Ember.Controller.extend({
// var errorElement = document.getElementById('card-errors'); // var errorElement = document.getElementById('card-errors');
// errorElement.textContent = result.error.message; // errorElement.textContent = result.error.message;
} else { } else {
const data = { const customerData = {
source: result.token.id source: result.token.id
}; };
return ajax("/patrons/customers", { method: "post", data }).then( return ajax("/patrons/customers", { method: "post", data: customerData }).then(
result => { customer => {
console.log(4, result, this.get('model')); // TODO move default plan into settings
// do plan if(this.get('model.selectedPlan') == undefined) {
this.set('model.selectedPlan', this.get('model.plans.firstObject'));
}
const subscriptionData = {
customer: customer.id,
plan: this.get('model.selectedPlan')
};
return ajax("/patrons/subscriptions", { method: "post", data: subscriptionData }).then(
subscription => {
console.log(3, subscription);
}
);
} }
); );
} }

View File

@ -5,7 +5,6 @@ export default Discourse.Route.extend({
model() { model() {
const group = Group.find(); const group = Group.find();
const plans = Plan.find().then(results => results.map(p => p.id)); const plans = Plan.find().then(results => results.map(p => p.id));
return Ember.RSVP.hash({ group, plans }); return Ember.RSVP.hash({ group, plans });
} }
}); });

View File

@ -11,7 +11,7 @@
</p> </p>
</div> </div>
<div class="section-column"> <div class="section-column">
{{combo-box valueAttribute="id" content=model.plans value=model.plan}} {{combo-box valueAttribute="id" content=model.plans value=model.selectedPlan}}
{{#d-button {{#d-button
action="stripePaymentHandler" action="stripePaymentHandler"

View File

@ -12,6 +12,7 @@ DiscoursePatrons::Engine.routes.draw do
end end
resources :customers, only: [:create] resources :customers, only: [:create]
resources :subscriptions, only: [:create]
resources :plans, only: [:index] resources :plans, only: [:index]
resources :patrons, only: [:index, :create] resources :patrons, only: [:index, :create]

View File

@ -15,6 +15,11 @@ module DiscoursePatrons
::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234')) ::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234'))
post "/patrons/subscriptions.json", params: { customer: 'cus_1234' } post "/patrons/subscriptions.json", params: { customer: 'cus_1234' }
end end
it "creates a subscription with a plan" do
::Stripe::Subscription.expects(:create).with(has_entry(items: [ plan: 'plan_1234' ]))
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
end
end end
end end
end end