Allow destruction of StripeAccounts even if deauthorise request fails

Log deauthorise failures to Bugsnag
This commit is contained in:
Rob Harrington
2017-09-22 16:47:22 +10:00
parent dd3d205536
commit db5503dd80
2 changed files with 14 additions and 9 deletions

View File

@@ -7,12 +7,15 @@ class StripeAccount < ActiveRecord::Base
accounts = StripeAccount.where(stripe_user_id: stripe_user_id)
# Only deauthorize the user if it is not linked to multiple accounts
if accounts.count > 1 || Stripe::OAuth.deauthorize(stripe_user_id: stripe_user_id)
destroy
else
false
end
return destroy if accounts.count > 1
destroy && Stripe::OAuth.deauthorize(stripe_user_id: stripe_user_id)
rescue Stripe::OAuth::OAuthError
false
Bugsnag.notify(
RuntimeError.new("StripeDeauthorizeFailure"),
stripe_account: stripe_user_id,
enterprise_id: enterprise_id
)
true
end
end

View File

@@ -21,9 +21,10 @@ describe StripeAccount do
to_return(status: 400, body: JSON.generate(error: 'invalid_grant', error_description: "Some Message"))
end
it "doesn't destroy the record" do
it "destroys the record and notifies Bugsnag" do
expect(Bugsnag).to receive(:notify)
stripe_account.deauthorize_and_destroy
expect(StripeAccount.all).to include(stripe_account)
expect(StripeAccount.all).to_not include(stripe_account)
end
end
@@ -41,11 +42,12 @@ describe StripeAccount do
end
context "if the account is also associated with another Enterprise" do
let!(:another_stripe_account) { create(:stripe_account, enterprise: enterprise2, stripe_user_id: stripe_account.stripe_user_id) }
let!(:another_stripe_account) { create(:stripe_account, enterprise: enterprise2, stripe_user_id: stripe_user_id) }
it "Doesn't make a Stripe API disconnection request " do
expect(Stripe::OAuth).to_not receive(:deauthorize)
stripe_account.deauthorize_and_destroy
expect(StripeAccount.all).not_to include(stripe_account)
end
end
end