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,17 +22,43 @@ 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) }
describe "customer.subscription.updated" do
before do
event = {
type: 'customer.subscription.updated',
data: {
object: {}
}
}
::Stripe::Webhook
.stubs(:construct_event)
.returns(event)
end
it 'is successfull' do
post "/s/hooks.json"
expect(response.status).to eq 200
end
end
describe "customer.subscription.deleted" do
let(:group) { Fabricate(:group, name: 'subscribers-group') }
before do
event = {
type: 'customer.subscription.deleted',
data: {
object: {
customer: customer.customer_id,
plan: { product: customer.product_id, metadata: { group_name: group.name } }
}
}
}
::Stripe::Webhook
.stubs(:construct_event)
@ -59,3 +85,4 @@ module DiscourseSubscriptions
end
end
end
end