Pecans
Pecans

Reputation: 307

Shopify Webhooks work in testing but not in practice

I'm having some problems with my Shopify 'orders/paid' webhook that I install onto the users store via the Shopify SDK (using a PHP wrapper: https://github.com/phpclassic/php-shopify). Anyways, I create the webhook like so:

$shop = $this->request->getVar('shop');  // Get Shopify store URL

// Get access token for store
$config = array('ShopUrl' => $shop, 'ApiKey' => 'xxx', 'SharedSecret' => 'xxx');
\PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::getAccessToken();

// Keep track of access_token for SDK calls and authenticate to store for webhook creation
$addToken = $this->Stores->addAccessToken($shop, $accessToken);
$config = array('AccessToken' => $accessToken, 'ShopUrl' => $shop);
$shopify = new \PHPShopify\ShopifySDK($config);

// Create orders/paid webhook
$orderPaidHook = array('topic' => 'orders/paid', 'address' => 'https://api.autoloapp.com/webhook/create', 'format' => 'json');

try {
    $webhooks = $shopify->Webhook->post($orderPaidHook);
 }

This code as far as I can tell works, using print_r($webhooks) gives me a valid Webhook that has (in theory) been installed on to the store. However, when an order is actually paid for on the store I DO NOT get a message.

Using the "Test Webhook" settings under "Notification" in the Shopify Admin my webhook works perfectly. Using PostMan to simulate a webhook call works perfectly.

The code to process the webhook looks like this:

$headers = getallheaders();
$topic = $headers['X-Shopify-Topic'];
$storeName = $headers['X-Shopify-Shop-Domain'];

if ($topic == 'orders/paid') {
     // Do some stuff, insert row into database
}

Again, when I use PostMan or the built-in "Test Webhook" setting for Shopify the row in my database gets created just fine. When I do it with a real webhook nothing happens. I'm not sure why this is happening, as far as I know all the headers should be the same. Do I need to verify the webhook in some way?

For reference I'm using CodeIgniter 4.0, PHP 7.0, and the newest version of the Shopify SDK.

I found this note on Shopify's Docs: "Note that if you are using a Rack based framework such as Ruby on Rails or Sinatra the header you are looking for is HTTP_X_SHOPIFY_HMAC_SHA256". With my current set-up I should still be using X-Shopify-Shop-Domain instead of HTTP_X_Shopify_Shop_Domain correct? Any help greatly appreciated!

Upvotes: 0

Views: 960

Answers (1)

Pecans
Pecans

Reputation: 307

So I finally figured out that for some reason the 'orders/paid' webhook wouldn't work for me in "production environments" despite working fine in my test environments. Simply changing the topic to 'orders/create' resolved my problem.

Upvotes: 1

Related Questions