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

ErrorException in api.php #17

Open
vramiguez opened this issue Oct 17, 2016 · 3 comments
Open

ErrorException in api.php #17

vramiguez opened this issue Oct 17, 2016 · 3 comments

Comments

@vramiguez
Copy link

I need some help with the API calls on my first embedded application developed in Laravel. So far, I've been able to install my app and to see some basic (html) code in the backend of my shop.

However, I would like to display some products in my embedded application, and I'm having a hard time to connect with the API:

In the controller of my callback page, I'm have the following:

public function callback(Request $request){

      $shopify = \App::make('ShopifyAPI');

      $code = $request->input('code');  

      $shopify->setup(['API_KEY' => env('SHOPIFY_APP_API_KEY'), 'API_SECRET' => env('SHOPIFY_APP_API_SECRET'), 'SHOP_DOMAIN' => $request->input('shop')]);

      try
      {

        $verify = $shopify->verifyRequest($request->input());
        if ($verify)
        {
          $token = $shopify->getAccessToken($code);
          $shopify->setup(['ACCESS_TOKEN' => $token]);

         // Gets a list of products
          $result = $shopify->call([
            'METHOD'     => 'GET',
            'URL'         => '/admin/products.json?page=1'
          ]);

          $products = $result->products;

          return  view('shopify.callbackView', compact('products'));
        }
        else
        {
            echo 'Issue with data';
        }  

      }
      catch (Exception $e)
      {
          echo '<pre>Error: ' . $e->getMessage() . '</pre>';
      }
    }

However, I have tracked the issue to the callback view ('shopify.callbackView'), in the ShopifyApp.init method to be more specific:

<head>
    <script src="https://cdn.shopify.com/s/assets/external/app.js"></script>
    <script type="text/javascript">
        ShopifyApp.init({
            apiKey: '{{ env('SHOPIFY_APP_API_KEY') }}',
            shopOrigin: 'https://{{$_GET['shop'] }}'
        });
    </script>
</head>
<body>
    @foreach ($products as $product)
        {{ $product->title }} 
    @endforeach
</body>

Apparently it doesn't recognize the code value on the URL (but I see it in the URL) and throws an error: ErrorException in api.php line 64: Undefined index: code

If I remove the javascript code that has the ShopifyApp on it, I get the body displayed, but not embedded in the backend of Shopify.

Not sure where the problem is and if that code is using the best practices. I would appreciate any help/guidance you can provide.

Thanks

@joshrps
Copy link
Owner

joshrps commented Oct 26, 2016

I think you need to replace $verify = $shopify->verifyRequest($request->input()); with $verify = $shopify->verifyRequest($request->all());

@buicongdang
Copy link

@joshrps I have tried the way of you, but it is not working. Please help me.
Thank you!
screen shot 2016-12-13 at 4 07 30 pm

@AjithLalps
Copy link

AjithLalps commented Oct 31, 2018

Hi Joshrps,

I got the same issue ie. undefined index code while verifying the request using the function verifyRequest() in api.php file.

I think there is an error on the function called verifyRequest().

elseif (is_array($data))
{
	$da = $data;
}

The array variable $da having only three array veriables. they are hmac,shop and timestamp.
On line number 64 on api.php file

$queryString = http_build_query(array('code' => $da['code'], 'shop' => $da['shop'], 'timestamp' => $da['timestamp']));

you are setting an addition non array veriable $da['code'] and this returning the above shown error undefined index code. Hope you understand the issue.

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

No branches or pull requests

4 participants