plans and products

This commit is contained in:
Rimian Perkins 2019-10-17 12:07:06 +11:00
parent 01b78b31df
commit e2b915b905
13 changed files with 107 additions and 40 deletions

View File

@ -20,13 +20,11 @@ module DiscoursePatrons
def create def create
begin begin
plan = ::Stripe::Plan.create( plan = ::Stripe::Plan.create(
amount: params[:amount], amount: params[:amount],
interval: params[:interval], interval: params[:interval],
product: { name: params[:name] }, product: product,
currency: SiteSetting.discourse_patrons_currency, currency: SiteSetting.discourse_patrons_currency,
id: plan_id,
) )
render_json_dump plan render_json_dump plan
@ -49,8 +47,8 @@ module DiscoursePatrons
private private
def plan_id def product
params[:name].parameterize.dasherize if params[:name] params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product]
end end
end end
end end

View File

@ -47,11 +47,10 @@ module DiscoursePatrons
def update def update
begin begin
product = ::Stripe::Product.update( product = ::Stripe::Product.update(
params[:id], { params[:id],
name: params[:name], name: params[:name],
active: params[:active], active: params[:active],
metadata: metadata metadata: metadata
}
) )
render_json_dump product render_json_dump product

View File

@ -7,8 +7,14 @@ module DiscoursePatrons
before_action :set_api_key before_action :set_api_key
def index def index
begin
plans = ::Stripe::Plan.list plans = ::Stripe::Plan.list
render json: plans.data
render_json_dump plans.data
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end end
end end
end end

View File

@ -7,6 +7,7 @@ module DiscoursePatrons
before_action :set_api_key before_action :set_api_key
def create def create
begin
subscription = ::Stripe::Subscription.create( subscription = ::Stripe::Subscription.create(
customer: params[:customer], customer: params[:customer],
items: [ items: [
@ -14,7 +15,23 @@ module DiscoursePatrons
] ]
) )
if subscription_ok(subscription)
# TODO: check group credentials
group = Group.find_by_name('group-123')
group.add(current_user)
end
render_json_dump subscription render_json_dump subscription
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
private
def subscription_ok(subscription)
['active', 'trialing'].include?(subscription[:status])
end end
end end
end end

View File

@ -3,6 +3,19 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
export default Ember.Controller.extend({ export default Ember.Controller.extend({
actions: { actions: {
createPlan() { createPlan() {
let product;
if(this.get("model.plan.product_id")) {
product = this.get("model.products")
.filterBy('id', this.get("model.plan.product_id"))
.get("firstObject");
}
else {
product = this.get("model.products").get("firstObject");
}
this.set("model.plan.product", product);
this.get("model.plan") this.get("model.plan")
.save() .save()
.then(() => { .then(() => {

View File

@ -14,9 +14,15 @@ const AdminPlan = Discourse.Model.extend({
const data = { const data = {
interval: this.interval, interval: this.interval,
amount: this.amount, amount: this.amount,
name: this.name name: this.name,
product: {
id: this.product.id,
// name: this.product.name
}
}; };
console.log(12, data);
return ajax("/patrons/admin/plans", { method: "post", data }); return ajax("/patrons/admin/plans", { method: "post", data });
} }
}); });

View File

@ -2,6 +2,7 @@ import { ajax } from "discourse/lib/ajax";
const AdminProduct = Discourse.Model.extend({ const AdminProduct = Discourse.Model.extend({
isNew: false, isNew: false,
metadata: {},
destroy() { destroy() {
return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" }); return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" });

View File

@ -3,7 +3,7 @@ import { ajax } from "discourse/lib/ajax";
const Plan = Discourse.Model.extend({}); const Plan = Discourse.Model.extend({});
Plan.reopenClass({ Plan.reopenClass({
find() { findAll() {
return ajax("/patrons/plans", { method: "get" }).then(result => return ajax("/patrons/plans", { method: "get" }).then(result =>
result.plans.map(plan => Plan.create(plan)) result.plans.map(plan => Plan.create(plan))
); );

View File

@ -4,7 +4,8 @@ import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan";
export default Discourse.Route.extend({ 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.findAll().then(results => results.map(p => p.id));
return Ember.RSVP.hash({ group, plans }); return Ember.RSVP.hash({ group, plans });
} }
}); });

View File

@ -12,7 +12,7 @@
</p> </p>
<p> <p>
<label for="product">{{i18n 'discourse_patrons.admin.plans.plan.product'}}</label> <label for="product">{{i18n 'discourse_patrons.admin.plans.plan.product'}}</label>
{{combo-box valueAttribute="value" content=model.products value=defaultProduct}} {{combo-box valueAttribute="id" content=model.products value=model.plan.product_id}}
</p> </p>
<p> <p>
<label for="interval">{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</label> <label for="interval">{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</label>

View File

@ -76,14 +76,10 @@ module DiscoursePatrons
post "/patrons/admin/plans.json", params: { amount: '102' } post "/patrons/admin/plans.json", params: { amount: '102' }
end end
it "creates a plan with a title" do it "creates a plan with a product" do
::Stripe::Plan.expects(:create).with(has_entry(:product, name: 'Rick Astley')) product = { id: 'prod_ww', name: 'Walter White' }
post "/patrons/admin/plans.json", params: { name: 'Rick Astley' } ::Stripe::Plan.expects(:create).with(has_entry(product: product))
end post "/patrons/admin/plans.json", params: { product: product }
it "creates a plan with an id" do
::Stripe::Plan.expects(:create).with(has_entry(id: 'rick-astley'))
post "/patrons/admin/plans.json", params: { name: 'Rick Astley' }
end end
end end

View File

@ -4,13 +4,14 @@ require 'rails_helper'
module DiscoursePatrons module DiscoursePatrons
RSpec.describe SubscriptionsController do RSpec.describe SubscriptionsController do
describe "create" do context "authenticated" do
let(:user) { Fabricate(:user, email: 'hello.2@example.com') } let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
before do before do
sign_in(user) sign_in(user)
end end
describe "create" do
it "creates a subscription with a customer" do it "creates a subscription with a customer" do
::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' }
@ -21,5 +22,34 @@ module DiscoursePatrons
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
end end
end end
describe "user groups" do
let(:group) { Fabricate(:group, name: 'group-123') }
it "does not add the user to the group" do
::Stripe::Subscription.expects(:create).returns(status: 'failed')
expect {
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
}.not_to change { group.users.count }
end
it "adds the user to the group when the subscription is active" do
::Stripe::Subscription.expects(:create).returns(status: 'active')
expect {
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
}.to change { group.users.count }
end
it "adds the user to the group when the subscription is trialing" do
::Stripe::Subscription.expects(:create).returns(status: 'trialing')
expect {
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
}.to change { group.users.count }
end
end
end
end end
end end