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