joshrps / laravel-shopify-API-wrapper

Interface designed for Shopify apps created with Laravel
MIT License
93 stars 48 forks source link

ErrorException in api.php #17

Open vramiguez opened 8 years ago

vramiguez commented 8 years ago

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 commented 8 years ago

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

buicongdang commented 7 years ago

@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 commented 6 years ago

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.