Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fullpage_redirect_to" is not working #1287

Closed
udaantech opened this issue Jun 21, 2021 · 11 comments
Closed

"fullpage_redirect_to" is not working #1287

udaantech opened this issue Jun 21, 2021 · 11 comments
Labels

Comments

@udaantech
Copy link

udaantech commented Jun 21, 2021

Description

I have added this gem (version 18.0) and using turbolinks to make it multipage app with session token. I have done it successfully.
Now, i am facing issue when add recurring application charge. I have created charge successfully and getting response.
In response, I am getting confirmation url and i am trying to redirect it using "fullpage_redirect_to" but it is not redirecting to cofirmation url.
Please help me.
Thanks in advance.

@udaantech udaantech added the bug label Jun 21, 2021
@asecondwill
Copy link

asecondwill commented Jun 21, 2021

I'm doing this in the Pay Action
Controller.
@url = charge.confirmation_url

View
<%= javascript_tag do %>
window.top.location.href = "<%= @url %>";
<% end %>

@udaantech
Copy link
Author

udaantech commented Jun 21, 2021

<%= javascript_tag do %>
window.top.location.href = "<%= @url %>";
<% end %>

I am trying your solution but now seeing app install screen in iframe.

@asecondwill
Copy link

Can you show me the value of @url or how you created the charge?

@khoimm92
Copy link

Are you include ShopifyApp::ShopAccessScopesVerification in the controller? If so, I think I got the cause. It will not redirect to confirmation page because the shop param is not present. Check this function in shopify_app-17.2.1/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb

def current_shopify_domain
    return if params[:shop].blank?
    ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
end

So my advice, move all the charge function to another controller, that not include ShopAccessScopesVerification

@yourivdlans
Copy link

What I think that is happening here (correct me if I'm wrong) is that the fullpage_redirect_to method renders the view shopify_app/shared/redirect which includes redirect.js. But due to the way Turbolinks works this is not picked up.

When inspecting my network calls I can see the request returning the proper output but this is not used. It seems this method is not compatible with turbolinks.

I tried to get the shopify_app/shared/redirect view working with something like the turbolinks_render gem but hit a dead end because redirect.js was not loaded which would ensure the redirect would be performed.

So in the end I wrote a new redirect method which uses some js code from redirect.js.

Added this to my AuthenticatedController

def top_redirect_to(url)
  script = <<-SCRIPT
    (function(){
      normalizedLink = document.createElement('a');
      normalizedLink.href = "#{url}";

      data = JSON.stringify({
        message: 'Shopify.API.remoteRedirect',
        data: {location: normalizedLink.href}
      });
      window.parent.postMessage(data, "https://#{current_shopify_domain}");
    })();
  SCRIPT
  self.status = 200
  self.response_body = script
  response.content_type = "text/javascript"
end

Now when creating a charge I can redirect the user like so:

class SubscriptionsController < AuthenticatedController
  def update
    ...
    top_redirect_to result.data.app_subscription_create.confirmation_url
  end
end

@forsbergplustwo
Copy link

forsbergplustwo commented Nov 12, 2021

@yourivdlans I was running into this same issue as you on this, and (using your code as inspiration) came up with a simpler solution:

      charge = ShopifyAPI::ApplicationCharge.create(charge_details)
      fullpage_redirect_to charge.confirmation_url
      response.status = 303 # Makes Turbo render html response

So we can keep using the built in fullpage_redirect_to method, while setting the status code and forcing Turbo to render the html response.

@yourivdlans
Copy link

@forsbergplustwo Nice one! Thanks for sharing :)

@jackculpan
Copy link

jackculpan commented Mar 14, 2022

This has saved me after many hours of searching. Thank you so much! @forsbergplustwo

@meshane
Copy link

meshane commented Mar 17, 2022

Me too. Thanks @forsbergplustwo

@khoimm92
Copy link

If anyone faced the related issue with version 18.1.2 and Application Charge URL, let's check for this issue #1387

@sagar-ranglani
Copy link

@yourivdlans I was running into this same issue as you on this, and (using your code as inspiration) came up with a simpler solution:

      charge = ShopifyAPI::ApplicationCharge.create(charge_details)
      fullpage_redirect_to charge.confirmation_url
      response.status = 303 # Makes Turbo render html response

So we can keep using the built in fullpage_redirect_to method, while setting the status code and forcing Turbo to render the html response.

To me it says, "undefined method `fullpage_redirect_to' for #<HomeController..."

I generated the app using Shopify CLI 3.0, here is the glimpse of the Home Controller

class HomeController < ApplicationController
  include ShopifyApp::EmbeddedApp
  include ShopifyApp::EnsureInstalled
  include ShopifyApp::ShopAccessScopesVerification
  include ShopifyApp::EnsureBilling
...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants