fix event hash for subscription

This commit is contained in:
Rimian Perkins 2020-01-25 15:06:05 +11:00
parent 3ec1907c1c
commit ebe891c09b
3 changed files with 59 additions and 27 deletions

View File

@ -46,10 +46,14 @@ When you get a moment, take a look at Stripe's documentation. But for now, you c
### Enable Webhooks in your Stripe account
You'll need to tell Stripe where your end points are. You can enter this in your Stripe dashboard.
Also: Add the webhook secret in settings (above).
The address for webhooks is: `[your server address]/s/hooks`
Also: Add the webhook secret in settings (above).
Discourse Subscriptions responds to the following events:
* `customer.subscription.deleted`
* `customer.subscription.updated`
### Set up your User Groups in Discourse

View File

@ -22,18 +22,19 @@ module DiscourseSubscriptions
end
case event[:type]
when 'customer.subscription.updated'
when 'customer.subscription.deleted'
customer = Customer.find_by(
customer_id: event[:customer],
product_id: event[:plan][:product]
customer_id: event[:data][:object][:customer],
product_id: event[:data][:object][:plan][:product]
)
if customer
customer.delete
user = ::User.find(customer.user_id)
group = plan_group(event[:plan])
group = plan_group(event[:data][:object][:plan])
group.remove(user) if group
end
end

View File

@ -22,39 +22,66 @@ module DiscourseSubscriptions
expect(response.status).to eq 200
end
describe "canceling a subscription" do
describe "event types" do
let(:user) { Fabricate(:user) }
let(:group) { Fabricate(:group, name: 'subscribers-group') }
let(:customer) { Fabricate(:customer, customer_id: 'c_575768', product_id: 'p_8654', user_id: user.id) }
before do
event = {
type: 'customer.subscription.deleted',
customer: customer.customer_id,
plan: { product: customer.product_id, metadata: { group_name: group.name } }
}
describe "customer.subscription.updated" do
before do
event = {
type: 'customer.subscription.updated',
data: {
object: {}
}
}
::Stripe::Webhook
.stubs(:construct_event)
.returns(event)
::Stripe::Webhook
.stubs(:construct_event)
.returns(event)
end
group.add(user)
it 'is successfull' do
post "/s/hooks.json"
expect(response.status).to eq 200
end
end
it "deletes the customer" do
expect {
post "/s/hooks.json"
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
describe "customer.subscription.deleted" do
let(:group) { Fabricate(:group, name: 'subscribers-group') }
expect(response.status).to eq 200
end
before do
event = {
type: 'customer.subscription.deleted',
data: {
object: {
customer: customer.customer_id,
plan: { product: customer.product_id, metadata: { group_name: group.name } }
}
}
}
it "removes the user from the group" do
expect {
post "/s/hooks.json"
}.to change { user.groups.count }.by(-1)
::Stripe::Webhook
.stubs(:construct_event)
.returns(event)
expect(response.status).to eq 200
group.add(user)
end
it "deletes the customer" do
expect {
post "/s/hooks.json"
}.to change { DiscourseSubscriptions::Customer.count }.by(-1)
expect(response.status).to eq 200
end
it "removes the user from the group" do
expect {
post "/s/hooks.json"
}.to change { user.groups.count }.by(-1)
expect(response.status).to eq 200
end
end
end
end