I want to create a price rule for my Shopify store using Admin API. I created the app on my shopify store and got the credentials: Shopify app for StoreName name: discount-coupons Admin API access token: shpat_fc24422545********* API key: 288622ebab75e5************ API secret key: 219a2339b773a65d1ae*********** as well as needed admin access scopes with the latest api version 2024-07. On my laravel app I want to create a service which will be called to create a new PriceRule The issue seems to be while creating a new session once i have initialized the app…
// Initialize Shopify API context
Context::initialize(
apiKey: env('SHOPIFY_API_KEY'),
apiSecretKey: env('SHOPIFY_API_SECRET'),
scopes: env('SHOPIFY_APP_SCOPES'),
hostName: env('SHOPIFY_APP_HOST_NAME'),
sessionStorage: new FileSessionStorage(storage_path('/tmp/php_sessions')),
apiVersion: '2024-07',
isPrivateApp: false
);
I think there is some issue regarding cookies or request headers i tried using the documentation but couldn’t find anything.
`$accessToken = env('SHOPIFY_ACCESS_TOKEN');
if (empty($accessToken)) {
Log::error('Shopify Access Token is missing');
return;
}
// Setup headers for the session
$requestHeaders = [
'Authorization' => 'Bearer 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',
'Content-Type' => 'application/json',
];
$cookies = [];
// Initialize session
$session = Utils::loadCurrentSession(
$requestHeaders,
$cookies,
false // This is the default value anyhow
);`
This is my whole code snippet:
<?php
namespace App\Services;
use Illuminate\Support\Facades\Log;
use Shopify\Auth\FileSessionStorage;
use Shopify\Context;
use Shopify\Rest\Admin2024_07\PriceRule;
use Shopify\Utils;
class ShopifyService
{
public function createPriceRule()
{
// Initialize Shopify API context
Context::initialize(
apiKey: env('SHOPIFY_API_KEY'),
apiSecretKey: env('SHOPIFY_API_SECRET'),
scopes: env('SHOPIFY_APP_SCOPES'),
hostName: env('SHOPIFY_APP_HOST_NAME'),
sessionStorage: new FileSessionStorage(storage_path('/tmp/php_sessions')),
apiVersion: '2024-07',
isPrivateApp: false
);
// Fetch the access token from the environment
$accessToken = env('SHOPIFY_ACCESS_TOKEN');
if (empty($accessToken)) {
Log::error('Shopify Access Token is missing');
return;
}
// Setup headers for the session
$requestHeaders = [
'Authorization' => 'Bearer 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',
'Content-Type' => 'application/json',
];
$cookies = [];
// Initialize session
$session = Utils::loadCurrentSession(
$requestHeaders,
$cookies,
false // This is the default value anyhow
);
// Create a new PriceRule instance
$price_rule = new PriceRule($session);
$price_rule->title = "Buy2iPodsGetiPodTouchForFree";
$price_rule->value_type = "percentage";
$price_rule->value = "-100.0";
$price_rule->customer_selection = "all";
$price_rule->target_type = "line_item";
$price_rule->target_selection = "entitled";
$price_rule->allocation_method = "each";
$price_rule->starts_at = "2024-03-22T00:00:00-00:00"; // Update to a valid future date
$price_rule->prerequisite_collection_ids = [
841564295
];
$price_rule->entitled_product_ids = [
921728736
];
$price_rule->prerequisite_to_entitlement_quantity_ratio = [
"prerequisite_quantity" => 2,
"entitled_quantity" => 1
];
$price_rule->allocation_limit = 3;
// Save the price rule
try {
$price_rule->save();
Log::info('Price rule created successfully');
} catch (\Exception $e) {
Log::error('Error creating price rule: ' . $e->getMessage());
}
}
}
my configuration file seems to be alright and I did put debug=false in .env file . this is the error that is comming from the logger file:
[2024-08-15 13:56:17] local.ERROR: Wrong number of segments {"exception":"[object] (UnexpectedValueException(code: 0): Wrong number of segments at C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\firebase\\php-jwt\\src\\JWT.php:109)
[stacktrace]
#0 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\shopify\\shopify-api\\src\\Utils.php(217): Firebase\\JWT\\JWT::decode('shpat_fc2516654...', Object(Firebase\\JWT\\Key))
#1 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\shopify\\shopify-api\\src\\Auth\\OAuth.php(220): Shopify\\Utils::decodeSessionToken('shpat_fc2516654...')
#2 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\shopify\\shopify-api\\src\\Utils.php(202): Shopify\\Auth\\OAuth::getCurrentSessionId(Array, Array, false)
#3 C:\\xampp\\htdocs\\vodafone-coupon\\app\\Services\\ShopifyService.php(45): Shopify\\Utils::loadCurrentSession(Array, Array, false)
#4 C:\\xampp\\htdocs\\vodafone-coupon\\app\\Http\\Controllers\\ReceiveCouponController.php(34): App\\Services\\ShopifyService->createPriceRule()
#5 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(46): App\\Http\\Controllers\\ReceiveCouponController->receiveInput(Object(Illuminate\\Http\\Request))
#6 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\ReceiveCouponController), 'receiveInput')
#7 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(206): Illuminate\\Routing\\Route->runController()
#8 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(808): Illuminate\\Routing\\Route->run()
#9 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#10 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(51): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#12 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(91): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#14 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#16 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#17 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest(Object(Illuminate\\Http\\Request), Object(Illuminate\\Session\\Store), Object(Closure))
#18 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#19 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#20 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(75): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(807): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#25 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(786): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#26 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(750): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#27 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(739): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#28 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(201): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#29 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#33 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#34 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TrimStrings.php(51): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#35 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php(110): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#40 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\HandleCors.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#41 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#42 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#43 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#44 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#45 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(176): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#46 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(145): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#47 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(1188): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#48 C:\\xampp\\htdocs\\vodafone-coupon\\public\\index.php(17): Illuminate\\Foundation\\Application->handleRequest(Object(Illuminate\\Http\\Request))
#49 C:\\xampp\\htdocs\\vodafone-coupon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\resources\\server.php(23): require_once('C:\\\\xampp\\\\htdocs...')
#50 {main}
"}