add subscription to group
This commit is contained in:
parent
490c424bc0
commit
a80e9d9bc2
|
@ -8,20 +8,20 @@ module DiscoursePatrons
|
||||||
|
|
||||||
def create
|
def create
|
||||||
begin
|
begin
|
||||||
subscription = ::Stripe::Subscription.create(
|
plan = ::Stripe::Plan.retrieve(params[:plan])
|
||||||
|
|
||||||
|
@subscription = ::Stripe::Subscription.create(
|
||||||
customer: params[:customer],
|
customer: params[:customer],
|
||||||
items: [
|
items: [ { plan: params[:plan] } ]
|
||||||
{ plan: params[:plan] },
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if subscription_ok(subscription)
|
group = plan_group(plan)
|
||||||
# TODO: check group credentials
|
|
||||||
group = Group.find_by_name('group-123')
|
if subscription_ok && group
|
||||||
group.add(current_user)
|
group.add(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
render_json_dump subscription
|
render_json_dump @subscription
|
||||||
|
|
||||||
rescue ::Stripe::InvalidRequestError => e
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
return render_json_error e.message
|
return render_json_error e.message
|
||||||
|
@ -30,8 +30,12 @@ module DiscoursePatrons
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def subscription_ok(subscription)
|
def plan_group(plan)
|
||||||
['active', 'trialing'].include?(subscription[:status])
|
Group.find_by_name(plan[:metadata][:group_name])
|
||||||
|
end
|
||||||
|
|
||||||
|
def subscription_ok
|
||||||
|
['active', 'trialing'].include?(@subscription[:status])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,7 +56,7 @@ module DiscoursePatrons
|
||||||
describe 'create' do
|
describe 'create' do
|
||||||
it 'is of product type service' do
|
it 'is of product type service' do
|
||||||
::Stripe::Product.expects(:create).with(has_entry(:type, 'service'))
|
::Stripe::Product.expects(:create).with(has_entry(:type, 'service'))
|
||||||
post "/patrons/admin/products.json", params: { }
|
post "/patrons/admin/products.json", params: {}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'has a name' do
|
it 'has a name' do
|
||||||
|
|
|
@ -12,42 +12,48 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "create" do
|
describe "create" do
|
||||||
it "creates a subscription with a customer" do
|
it "creates a subscription" do
|
||||||
::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234'))
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'awesome' })
|
||||||
post "/patrons/subscriptions.json", params: { customer: 'cus_1234' }
|
::Stripe::Subscription.expects(:create).with(
|
||||||
end
|
customer: 'cus_1234',
|
||||||
|
items: [ plan: 'plan_1234' ]
|
||||||
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', customer: 'cus_1234' }
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "user groups" do
|
describe "user groups" do
|
||||||
let(:group) { Fabricate(:group, name: 'group-123') }
|
let(:group_name) { 'group-123' }
|
||||||
|
let(:group) { Fabricate(:group, name: group_name) }
|
||||||
|
|
||||||
it "does not add the user to the group" do
|
context "plan has group in metadata" do
|
||||||
::Stripe::Subscription.expects(:create).returns(status: 'failed')
|
before do
|
||||||
|
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: group_name })
|
||||||
|
end
|
||||||
|
|
||||||
expect {
|
it "does not add the user to the group" do
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
|
::Stripe::Subscription.expects(:create).returns(status: 'failed')
|
||||||
}.not_to change { group.users.count }
|
|
||||||
end
|
|
||||||
|
|
||||||
it "adds the user to the group when the subscription is active" do
|
expect {
|
||||||
::Stripe::Subscription.expects(:create).returns(status: 'active')
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
||||||
|
}.not_to change { group.users.count }
|
||||||
|
end
|
||||||
|
|
||||||
expect {
|
it "adds the user to the group when the subscription is active" do
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
|
::Stripe::Subscription.expects(:create).returns(status: 'active')
|
||||||
}.to change { group.users.count }
|
|
||||||
end
|
|
||||||
|
|
||||||
it "adds the user to the group when the subscription is trialing" do
|
expect {
|
||||||
::Stripe::Subscription.expects(:create).returns(status: 'trialing')
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
||||||
|
}.to change { group.users.count }
|
||||||
|
end
|
||||||
|
|
||||||
expect {
|
it "adds the user to the group when the subscription is trialing" do
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234' }
|
::Stripe::Subscription.expects(:create).returns(status: 'trialing')
|
||||||
}.to change { group.users.count }
|
|
||||||
|
expect {
|
||||||
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
||||||
|
}.to change { group.users.count }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue