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 ### 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. 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` 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 ### Set up your User Groups in Discourse

View File

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

View File

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