Skip to content

Commit

Permalink
Don't throw 404 errors for CI account in webhook (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadaismail-stripe authored Feb 28, 2023
1 parent 8363023 commit 48e880f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions app/controllers/stripe_webhook_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class StripeWebhookController < ApplicationController
skip_forgery_protection

WEBHOOK_SECRET = ENV.fetch("STRIPE_WEBHOOK_SECRET")
STRIPEFORCE_CI_ACCOUNT = "acct_1MHBTOC9fP1FVBtd"

def stripe_webhook
payload = request.body.read.freeze
Expand Down Expand Up @@ -35,17 +36,14 @@ def stripe_webhook
event_id = event.id
event_type = event.type

# TODO this should be pulled out into a helper method
unless stripe_account_id.nil?
user = StripeForce::User.find(stripe_account_id: stripe_account_id, livemode: livemode)

# if a user does not exist for the specified livemode, search a user in a different livemode
# if one exists, return 200 to webhook originator to avoid resending this webhook
if !user
user = StripeForce::User.find(stripe_account_id: stripe_account_id)
end
# ignore webhooks and don't error for StripeForce CI account
if stripe_account_id == STRIPEFORCE_CI_ACCOUNT
render plain: "ignoring webhook for StripeForce CI account"
return
end

user = find_user(stripe_account_id, livemode)

if user.nil?
log.info "no user found for webhook"
render status: :not_found, plain: "SuiteSync: user not found"
Expand Down Expand Up @@ -89,4 +87,19 @@ def stripe_webhook

render plain: "Successfully processed event #{event_id}"
end

def find_user(stripe_account_id, livemode)
user = nil

unless stripe_account_id.nil?
user = StripeForce::User.find(stripe_account_id: stripe_account_id, livemode: livemode)

# if a user does not exist for the specified livemode, search for the user in a different livemode
# if one exists, return 200 to webhook originator to avoid resending this webhook
if !user
user = StripeForce::User.find(stripe_account_id: stripe_account_id)
end
end
user
end
end

0 comments on commit 48e880f

Please sign in to comment.