plans and products
This commit is contained in:
parent
01b78b31df
commit
e2b915b905
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -7,8 +7,14 @@ module DiscoursePatrons
|
||||||
before_action :set_api_key
|
before_action :set_api_key
|
||||||
|
|
||||||
def index
|
def index
|
||||||
plans = ::Stripe::Plan.list
|
begin
|
||||||
render json: plans.data
|
plans = ::Stripe::Plan.list
|
||||||
|
|
||||||
|
render_json_dump plans.data
|
||||||
|
|
||||||
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
|
return render_json_error e.message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,14 +7,31 @@ module DiscoursePatrons
|
||||||
before_action :set_api_key
|
before_action :set_api_key
|
||||||
|
|
||||||
def create
|
def create
|
||||||
subscription = ::Stripe::Subscription.create(
|
begin
|
||||||
customer: params[:customer],
|
subscription = ::Stripe::Subscription.create(
|
||||||
items: [
|
customer: params[:customer],
|
||||||
{ plan: params[:plan] },
|
items: [
|
||||||
]
|
{ plan: params[:plan] },
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
|
||||||
render_json_dump subscription
|
if subscription_ok(subscription)
|
||||||
|
# TODO: check group credentials
|
||||||
|
group = Group.find_by_name('group-123')
|
||||||
|
group.add(current_user)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
@ -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(() => {
|
||||||
|
|
|
@ -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 });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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" });
|
||||||
|
|
|
@ -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))
|
||||||
);
|
);
|
||||||
|
|
|
@ -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 });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ module DiscoursePatrons
|
||||||
|
|
||||||
it 'has a metadata' do
|
it 'has a metadata' do
|
||||||
::Stripe::Product.expects(:create).with(has_entry(metadata: { group_name: 'discourse-user-group-name' }))
|
::Stripe::Product.expects(:create).with(has_entry(metadata: { group_name: 'discourse-user-group-name' }))
|
||||||
post "/patrons/admin/products.json", params: { metadata: { group_name: 'discourse-user-group-name' }}
|
post "/patrons/admin/products.json", params: { metadata: { group_name: 'discourse-user-group-name' } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ module DiscoursePatrons
|
||||||
describe 'update' do
|
describe 'update' do
|
||||||
it 'updates the product' do
|
it 'updates the product' do
|
||||||
::Stripe::Product.expects(:update)
|
::Stripe::Product.expects(:update)
|
||||||
patch "/patrons/admin/products/prod_walterwhite.json", params: { metadata: { group_name: '' }}
|
patch "/patrons/admin/products/prod_walterwhite.json", params: { metadata: { group_name: '' } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,21 +4,51 @@ 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
|
||||||
|
|
||||||
it "creates a subscription with a customer" do
|
describe "create" do
|
||||||
::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234'))
|
it "creates a subscription with a customer" do
|
||||||
post "/patrons/subscriptions.json", params: { customer: 'cus_1234' }
|
::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
|
||||||
|
|
||||||
it "creates a subscription with a plan" do
|
describe "user groups" do
|
||||||
::Stripe::Subscription.expects(:create).with(has_entry(items: [ plan: 'plan_1234' ]))
|
let(:group) { Fabricate(:group, name: 'group-123') }
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue