Skip to content

Commit

Permalink
Add column is_development_shop to shopify sessions and fill on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdierich committed Dec 19, 2023
1 parent 964457a commit 5d557aa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('shopify_sessions', function (Blueprint $table) {
$table->boolean('is_development_shop')->default(false);
});
}

public function down(): void
{
Schema::table('shopify_sessions', function (Blueprint $table) {
$table->dropColumn('is_development_shop');
});
}
};
2 changes: 1 addition & 1 deletion src/LaravelShopifyIntegrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function configurePackage(Package $package): void
$package
->name('laravel-shopify-integration')
->hasConfigFile()
->hasMigration('create_shopify_sessions_table')
->hasMigrations(['create_shopify_sessions_table', 'add_is_development_shop_to_shopify_sessions'])
->hasRoutes('web', 'api');

$router = $this->app->make(Router::class);
Expand Down
1 change: 1 addition & 0 deletions src/Lib/DbSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function storeSession(Session $session): bool
'access_token' => $session->getAccessToken(),
'expires_at' => $session->getExpires(),
'scope' => $session->getScope(),
'is_development_shop' => app(ShopifyDevelopmentShopHandler::class)->fetchIsDevelopmentShop($session),

'user_id' => $session->getOnlineAccessInfo()?->getId(),
'user_first_name' => $session->getOnlineAccessInfo()?->getFirstName(),
Expand Down
28 changes: 28 additions & 0 deletions src/Lib/ShopifyDevelopmentShopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Codelayer\LaravelShopifyIntegration\Lib;

use Shopify\Auth\Session;
use Shopify\Clients\Graphql;

class ShopifyDevelopmentShopHandler
{
public const DEVELOPMENT_SHOP_GRAPHQL_QUERY = <<<'QUERY'
{
shop {
plan {
partnerDevelopment
}
}
}
QUERY;

public function fetchIsDevelopmentShop(Session $session): bool
{
$client = new Graphql($session->getShop(), $session->getAccessToken());

$response = $client->query(self::DEVELOPMENT_SHOP_GRAPHQL_QUERY);

return data_get($response->getDecodedBody(), 'data.shop.plan.partnerDevelopment');
}
}

0 comments on commit 5d557aa

Please sign in to comment.