1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Console\Commands;
9
10use App\Models\Store\Order;
11use Illuminate\Console\Command;
12use Shopify\ApiVersion;
13use Shopify\Auth\FileSessionStorage;
14use Shopify\Clients\Storefront;
15use Shopify\Context;
16
17class StoreGetShopifyCheckout extends Command
18{
19 protected $signature = 'store:get-shopify-checkout {orderId}';
20
21 protected $description = 'Gets checkout info from shopify.';
22
23 public function handle()
24 {
25 $order = Order::findOrFail(get_int($this->argument('orderId')));
26 if ($order->provider !== 'shopify') {
27 $this->error('Not a Shopify order');
28 return static::INVALID;
29 }
30
31 $this->comment("Getting details for Order {$order->getKey()}");
32 $this->comment($order->reference);
33
34 Context::initialize(
35 // public unauthenticated Storefront API doesn't need OAuth and we can't use blanks.
36 'unauthenticated_only',
37 'unauthenticated_only',
38 'unauthenticated_read_checkouts',
39 $GLOBALS['cfg']['store']['shopify']['domain'],
40 new FileSessionStorage(),
41 ApiVersion::APRIL_2023,
42 );
43
44 $client = new Storefront(
45 $GLOBALS['cfg']['store']['shopify']['domain'],
46 $GLOBALS['cfg']['store']['shopify']['storefront_token'],
47 );
48
49 $id = '"'.$order->reference.'"';
50 $query = <<<QUERY
51 {
52 node(id: $id) {
53 ... on Checkout {
54 id
55 ready
56 webUrl
57 orderStatusUrl
58 completedAt
59 createdAt
60 updatedAt
61 order {
62 id
63 processedAt
64 orderNumber
65 }
66 }
67 }
68 }
69 QUERY;
70
71 $response = $client->query($query);
72 $body = $response->getDecodedBody() ?? '';
73 $this->line(is_array($body) ? json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : $body);
74 }
75}