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(
customer: params[:customer],
items: [
{ plan: 'plan_CBXbz9i7AIOTzr' },
{ plan: params[:plan] },
]
)

View File

@ -21,14 +21,27 @@ export default Ember.Controller.extend({
// var errorElement = document.getElementById('card-errors');
// errorElement.textContent = result.error.message;
} else {
const data = {
const customerData = {
source: result.token.id
};
return ajax("/patrons/customers", { method: "post", data }).then(
result => {
console.log(4, result, this.get('model'));
// do plan
return ajax("/patrons/customers", { method: "post", data: customerData }).then(
customer => {
// TODO move default plan into settings
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() {
const group = Group.find();
const plans = Plan.find().then(results => results.map(p => p.id));
return Ember.RSVP.hash({ group, plans });
}
});

View File

@ -11,7 +11,7 @@
</p>
</div>
<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
action="stripePaymentHandler"

View File

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

View File

@ -15,6 +15,11 @@ module DiscoursePatrons
::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234'))
post "/patrons/subscriptions.json", params: { customer: 'cus_1234' }
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