Skip to content

Commit e4b8686

Browse files
committed
Fixed #204 PHP error saving access token
1 parent d69dff2 commit e4b8686

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Shopify
22

3+
## Unreleased
4+
5+
- Fixed a PHP error that could occur when authorizing the Shopify app. ([#204](https://github.com/craftcms/shopify/issues/204))
6+
37
## 7.0.1 - 2026-03-20
48

59
- Fixed a PHP error that could occur when retrieving products. ([#202](https://github.com/craftcms/shopify/issues/202))

src/models/Settings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ public function getAccessToken(bool $parse = true): string
262262

263263
$accessToken = $accessTokenRecord->accessToken;
264264

265-
// If an actual access token, and not a env var, has been stored we need to unencrypt it
265+
// If an actual access token, and not an env var, has been stored we need to decrypt it
266266
if (!str_starts_with($accessToken, '$')) {
267-
$accessToken = Craft::$app->getSecurity()->decryptByKey($accessToken);
267+
$accessToken = StringHelper::decdec($accessToken);
268268
}
269269

270270
$this->setAccessToken($accessToken);

src/services/Api.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use craft\base\Component;
1212
use craft\helpers\ArrayHelper;
1313
use craft\helpers\Json;
14+
use craft\helpers\StringHelper;
1415
use craft\log\MonologTarget;
1516
use craft\shopify\events\DefineGqlFieldsEvent;
1617
use craft\shopify\events\DefineGqlQueryArgumentsEvent;
@@ -658,14 +659,23 @@ public function getAccessToken(?string $code = null, ?string $shop = null, bool
658659
/** @var AccessToken $record */
659660
$record = AccessToken::find()->one() ?? new AccessToken();
660661

661-
$success = true;
662-
try {
663-
$configService->setDotEnvVar(self::API_ACCESS_TOKEN_ENV_VAR, $body['access_token']);
664-
} catch (\Throwable $e) {
665-
$success = false;
666-
Craft::error('Couldn\'t save the Shopify Access Token in the .env file. ' . $e->getMessage(), __METHOD__);
662+
// If there isn't a `.env` file, let's not try and save it there in case that is by design
663+
$dotEnvPath = $configService->getDotEnvPath();
664+
$hasDotEnv = $dotEnvPath !== '' && file_exists($dotEnvPath);
665+
666+
$isSavedToFile = true;
667+
if (!$hasDotEnv) {
668+
$isSavedToFile = false;
669+
} else {
670+
try {
671+
$configService->setDotEnvVar(self::API_ACCESS_TOKEN_ENV_VAR, $body['access_token']);
672+
} catch (\Throwable $e) {
673+
$isSavedToFile = false;
674+
Craft::error('Couldn\'t save the Shopify Access Token in the .env file. ' . $e->getMessage(), __METHOD__);
675+
}
667676
}
668-
$record->accessToken = $success ? '$' . self::API_ACCESS_TOKEN_ENV_VAR : Craft::$app->getSecurity()->encryptByKey($body['access_token']);
677+
678+
$record->accessToken = $isSavedToFile ? '$' . self::API_ACCESS_TOKEN_ENV_VAR : StringHelper::encenc($body['access_token']);
669679

670680
if (!$record->save()) {
671681
// Get the first error message from the record, if available:

0 commit comments

Comments
 (0)