Skip to content

Commit

Permalink
EnsureBilling handles error by redirecting to loginpage without shop …
Browse files Browse the repository at this point in the history
…param
  • Loading branch information
zzooeeyy committed Apr 25, 2024
1 parent 470164a commit 841857f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/shopify_app/controller_concerns/ensure_billing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ def check_billing(session = current_shopify_session)

unless has_payment
if request.xhr?
ShopifyApp::Logger.debug("Responding with 401 unauthorized")
RedirectForEmbedded.add_app_bridge_redirect_url_header(confirmation_url, response)
ShopifyApp::Logger.debug("Responding with 401 unauthorized")
head(:unauthorized)
elsif ShopifyApp.configuration.embedded_app?
render(
"shopify_app/shared/redirect",
layout: false,
locals: { url: confirmation_url, current_shopify_domain: session&.shop },
)
fullpage_redirect_to(confirmation_url)
else
redirect_to(confirmation_url, allow_other_host: true)
end
Expand All @@ -49,8 +45,16 @@ def billing_required?
end

def handle_billing_error(error)
logger.info("#{error.message}: #{error.errors}")
redirect_to_login
ShopifyApp::Logger.warn("Encountered billing error - #{error.message}: #{error.errors}\n" \
"Redirecting to login page")

login_url = ShopifyApp.configuration.login_url
if request.xhr?
RedirectForEmbedded.add_app_bridge_redirect_url_header(login_url, response)
head(:unauthorized)
else
fullpage_redirect_to(login_url)
end
end

def has_active_payment?(session)
Expand Down
34 changes: 34 additions & 0 deletions test/controllers/concerns/ensure_billing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,40 @@ def index
refute response.headers["X-Shopify-API-Request-Failure-Reauthorize-Url"].present?
end

test "Add app bridge redirect headers when handling billing error for XHR requests" do
ShopifyApp.configuration.billing = ShopifyApp::BillingConfiguration.new(
charge_name: TEST_CHARGE_NAME,
amount: 5,
interval: ShopifyApp::BillingConfiguration::INTERVAL_ONE_TIME,
)
@controller.stubs(:run_query).raises(ShopifyApp::BillingError.new("Billing error", { errors: "not good" }))

ShopifyApp::Logger.expects(:warn).with("Encountered billing error - Billing error: {:errors=>\"not good\"}"\
"\nRedirecting to login page")

get :index, xhr: true

assert_response :unauthorized
assert_match "1", response.headers["X-Shopify-API-Request-Failure-Reauthorize"]
assert_match(ShopifyApp.configuration.login_url, response.headers["X-Shopify-API-Request-Failure-Reauthorize-Url"])
end

test "Redirect to login when handling billing errors" do
ShopifyApp.configuration.billing = ShopifyApp::BillingConfiguration.new(
charge_name: TEST_CHARGE_NAME,
amount: 5,
interval: ShopifyApp::BillingConfiguration::INTERVAL_ONE_TIME,
)

@controller.stubs(:run_query).raises(ShopifyApp::BillingError.new("Billing error", { errors: "not good" }))

@controller.expects(:fullpage_redirect_to).with(ShopifyApp.configuration.login_url)
ShopifyApp::Logger.expects(:warn).with("Encountered billing error - Billing error: {:errors=>\"not good\"}"\
"\nRedirecting to login page")

get :index
end

private

def assert_client_side_redirection(url)
Expand Down

0 comments on commit 841857f

Please sign in to comment.