plans and products
This commit is contained in:
parent
01b78b31df
commit
e2b915b905
|
@ -20,13 +20,11 @@ module DiscoursePatrons
|
|||
|
||||
def create
|
||||
begin
|
||||
|
||||
plan = ::Stripe::Plan.create(
|
||||
amount: params[:amount],
|
||||
interval: params[:interval],
|
||||
product: { name: params[:name] },
|
||||
product: product,
|
||||
currency: SiteSetting.discourse_patrons_currency,
|
||||
id: plan_id,
|
||||
)
|
||||
|
||||
render_json_dump plan
|
||||
|
@ -49,8 +47,8 @@ module DiscoursePatrons
|
|||
|
||||
private
|
||||
|
||||
def plan_id
|
||||
params[:name].parameterize.dasherize if params[:name]
|
||||
def product
|
||||
params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -47,11 +47,10 @@ module DiscoursePatrons
|
|||
def update
|
||||
begin
|
||||
product = ::Stripe::Product.update(
|
||||
params[:id], {
|
||||
name: params[:name],
|
||||
active: params[:active],
|
||||
metadata: metadata
|
||||
}
|
||||
params[:id],
|
||||
name: params[:name],
|
||||
active: params[:active],
|
||||
metadata: metadata
|
||||
)
|
||||
|
||||
render_json_dump product
|
||||
|
|
|
@ -7,8 +7,14 @@ module DiscoursePatrons
|
|||
before_action :set_api_key
|
||||
|
||||
def index
|
||||
plans = ::Stripe::Plan.list
|
||||
render json: plans.data
|
||||
begin
|
||||
plans = ::Stripe::Plan.list
|
||||
|
||||
render_json_dump plans.data
|
||||
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
return render_json_error e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,14 +7,31 @@ module DiscoursePatrons
|
|||
before_action :set_api_key
|
||||
|
||||
def create
|
||||
subscription = ::Stripe::Subscription.create(
|
||||
customer: params[:customer],
|
||||
items: [
|
||||
{ plan: params[:plan] },
|
||||
]
|
||||
)
|
||||
begin
|
||||
subscription = ::Stripe::Subscription.create(
|
||||
customer: params[:customer],
|
||||
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
|
||||
|
|
|
@ -3,6 +3,19 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
|
|||
export default Ember.Controller.extend({
|
||||
actions: {
|
||||
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")
|
||||
.save()
|
||||
.then(() => {
|
||||
|
|
|
@ -14,9 +14,15 @@ const AdminPlan = Discourse.Model.extend({
|
|||
const data = {
|
||||
interval: this.interval,
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
|
|
@ -2,6 +2,7 @@ import { ajax } from "discourse/lib/ajax";
|
|||
|
||||
const AdminProduct = Discourse.Model.extend({
|
||||
isNew: false,
|
||||
metadata: {},
|
||||
|
||||
destroy() {
|
||||
return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" });
|
||||
|
|
|
@ -3,7 +3,7 @@ import { ajax } from "discourse/lib/ajax";
|
|||
const Plan = Discourse.Model.extend({});
|
||||
|
||||
Plan.reopenClass({
|
||||
find() {
|
||||
findAll() {
|
||||
return ajax("/patrons/plans", { method: "get" }).then(result =>
|
||||
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({
|
||||
model() {
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
</p>
|
||||
<p>
|
||||
<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>
|
||||
<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' }
|
||||
end
|
||||
|
||||
it "creates a plan with a title" do
|
||||
::Stripe::Plan.expects(:create).with(has_entry(:product, name: 'Rick Astley'))
|
||||
post "/patrons/admin/plans.json", params: { name: 'Rick Astley' }
|
||||
end
|
||||
|
||||
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' }
|
||||
it "creates a plan with a product" do
|
||||
product = { id: 'prod_ww', name: 'Walter White' }
|
||||
::Stripe::Plan.expects(:create).with(has_entry(product: product))
|
||||
post "/patrons/admin/plans.json", params: { product: product }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ module DiscoursePatrons
|
|||
|
||||
it 'has a metadata' do
|
||||
::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
|
||||
|
||||
|
@ -85,7 +85,7 @@ module DiscoursePatrons
|
|||
describe 'update' do
|
||||
it 'updates the product' do
|
||||
::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
|
||||
|
||||
|
|
|
@ -4,21 +4,51 @@ require 'rails_helper'
|
|||
|
||||
module DiscoursePatrons
|
||||
RSpec.describe SubscriptionsController do
|
||||
describe "create" do
|
||||
context "authenticated" do
|
||||
let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
it "creates a subscription with a customer" do
|
||||
::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234'))
|
||||
post "/patrons/subscriptions.json", params: { customer: 'cus_1234' }
|
||||
describe "create" do
|
||||
it "creates a subscription with a customer" do
|
||||
::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
|
||||
|
||||
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' }
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue