gnikyt / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
1.24k stars 374 forks source link

File downloaded instead of redirecting to App proxy #673

Closed sandeeppangeni closed 3 years ago

sandeeppangeni commented 3 years ago

@osiset @Jamesking56 Need help

For bug reporting only! If you're posting a feature request or discussion, please ignore.

Expected Behavior

I have setup the app proxy as

image

And given the routes as

Route::get('/cartmade_payment', function () { return response('Hello, world!')->withHeaders(['Content-Type' => 'application/liquid']); })->middleware('auth.proxy');

But it is downloading the file instead of redirecting

.htaccess file `

Options -MultiViews -Indexes
RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

php -- BEGIN cPanel-generated handler, do not edit

Set the “ea-php74” package as the default “PHP” programming language.

AddHandler application/x-httpd-ea-php74 .php .php7 .phtml

php -- END cPanel-generated handler, do not edit

`

Steps to Reproduce

when I change the .htaccess file as below, I got 500 internal server.

Redirect Trailing Slashes If Not A Folder...

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

Changes to

Redirect Trailing Slashes If Not A Folder...

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule !^/?cartmade_payment*$ ^ %1 [L,R=301]

Failure Information

image

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

"php": "^7.3|^7.4|^8.0", "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "laravel/framework": "^8.12", "laravel/tinker": "^2.5", "osiset/basic-shopify-api": "^10.0", "osiset/laravel-shopify": "^15.1"

gnikyt commented 3 years ago

This is a server issue if it's downloading a file, not a package issue.

sandeeppangeni commented 3 years ago

@osiset Can you please help me out how can I resolve the server Issue and what may be the issue ?

gnikyt commented 3 years ago

I don't provide that sort of support sorry. Try posting in discussions and someone may have some info.

sandeeppangeni commented 3 years ago

@osiset We need installation guide video for app development with App proxy that will be very helpful. :)

ihateonions-uk commented 3 years ago

I had a similar issue previously and as mentioned it wasn't anything todo with the package. Try and pass the access control allow origin with the header and return a 200 response as documented.

return response('Hello, world!',200)->withHeaders(['Content-Type' => 'application/liquid','Access-Control-Allow-Origin' => '*']);

Also in terms of the htaccess file, try something like...

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)/$ /$1 [L,R=301]

Note also that if you're not requesting a file (ex: test.php in my case), but using a directory (ex: http://yourdomain.com/test) that the request to the server includes the trailing slash (meaning that Shopify's proxy requests http://yourdomain.com/test/). The htaccess file issues a 301 redirect, which is forcing Shopify to download the file instead of interpret it as liquid. To fix that issue, you might add another rewrite condition that excludes those domains from being redirect to their "non-trailing-slash" counterparts. (As described in this post, community.shopify.com/

sandeeppangeni commented 3 years ago

@ihateonions-uk I have hosted the laravel in subdomain. In the routes, I have have the following code

Route::get('/cartmade_payment', [AppProxy::class,'index'])->middleware('auth.proxy');

And in AppProxy controller

`class AppProxy extends Controller { public function index(){ $shopdomain = Auth::user()->name; $accesstoken = Auth::user()->password; // Create options for the API $options = new Options(); $options->setVersion('2020-01');

    // Create the client and session
    $api = new BasicShopifyAPI($options);
    $api->setSession(new Session($shopdomain, $accesstoken));

    // Now run your requests...
    $result = $api->rest('GET', '/admin/shop.json');
    $shop_data = $result['body']->container;

    return view('customcratmadepayment')->with(compact('shop_data'))->withHeaders(['Content-Type' => 'application/liquid','Access-Control-Allow-Origin' => '*']]);
}

}`

On view, I have 'customcratmadepayment.blade.php'. but it is downloading the file. When I change something it will show 500 Internal server error.

Can you please help me to figure out error in .htaccess file ?

`

Options -MultiViews -Indexes
RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

`

sandeeppangeni commented 3 years ago

@osiset @ihateonions-uk

I have setup the app proxy as below

image

But In shop storefront, when the shop redirect to prefix (apps/cartmadepayment) then it is proxied to the provided proxy url as

https://cartmadepaymentv1.cartbrain.net/cartmade_payment?nshop=cartmadeteststore.myshopify.com&path_prefix=%2Fapps%2Fcartmadepayment&timestamp=1611804352&signature=948dc9d29ced74bdee89d922537f4244c1c0f9be00fc73b3e6a2d9d9527f66f4

But App proxy must redirect within shop domain itself as (cartmadeteststore.myshopify.com/apps/cartmadepayment) we need shop domain to get cart data.

gnikyt commented 3 years ago

Auth::user won't work as there's no user session. You need to grab the domain of the shop from the request and manually log the user in or access their records. Without logs on the 500 error you won't be able to debug, that's probably where your issue is. In the end though, this is a code and server thing.

sandeshpaudyal commented 3 years ago

@osiset I tried returning normal view from the proxy controller. The Domain get changed to the proxy URL but I think the domain must be same as the shop domain ?

Here is the code for the controller public function index(){ return view('testproxyview')->withHeaders(['Content-Type' => 'application/liquid']); }

gnikyt commented 3 years ago

The domain shouldn't matter. Shopify fetches the URL and processes the liquid on server side, then renders it onto the page.

gnikyt commented 3 years ago

Your QA is open, will close this.

Eduard9969 commented 3 years ago

Hi, @sandeeppangeni . I’m sure I’m not on time. But I faced a similar problem.

In my case, the proxy application worked on Apache 2.2 and stopped working on Apache 2.4. The solution is pretty simple. Edit your .htaccess file as:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

to

# Redirect Trailing Slashes If Not A Folder... AND not an cartmade_payment endpoint call
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(?!\/cartmade_payment\/)(.+)\/$
RewriteRule ^ %1 [L,R=301]

Hope this helps you :)