forked from gnikyt/laravel-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthenticateShop.php
111 lines (98 loc) · 3.03 KB
/
AuthenticateShop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Osiset\ShopifyApp\Actions;
use Illuminate\Http\Request;
use Osiset\ShopifyApp\Actions\InstallShop;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
/**
* Authenticates a shop and fires post authentication actions.
*/
class AuthenticateShop
{
/**
* The API helper.
*
* @var IApiHelper
*/
protected $apiHelper;
/**
* The action for installing a shop.
*
* @var InstallShop
*/
protected $installShopAction;
/**
* The action for dispatching scripts.
*
* @var DispatchScripts
*/
protected $dispatchScriptsAction;
/**
* The action for dispatching webhooks.
*
* @var DispatchWebhooks
*/
protected $dispatchWebhooksAction;
/**
* The action for after authorize actions.
*
* @var AfterAuthorize
*/
protected $afterAuthorizeAction;
/**
* Setup.
*
* @param IApiHelper $apiHelper The API helper.
* @param InstallShop $installShopAction The action for installing a shop.
* @param DispatchScripts $dispatchScriptsAction The action for dispatching scripts.
* @param DispatchWebhooks $dispatchWebhooksAction The action for dispatching webhooks.
* @param AfterAuthorize $afterAuthorizeAction The action for after authorize actions.
*
* @return void
*/
public function __construct(
IApiHelper $apiHelper,
InstallShop $installShopAction,
DispatchScripts $dispatchScriptsAction,
DispatchWebhooks $dispatchWebhooksAction,
AfterAuthorize $afterAuthorizeAction
) {
$this->apiHelper = $apiHelper;
$this->installShopAction = $installShopAction;
$this->dispatchScriptsAction = $dispatchScriptsAction;
$this->dispatchWebhooksAction = $dispatchWebhooksAction;
$this->afterAuthorizeAction = $afterAuthorizeAction;
}
/**
* Execution.
*
* @param Request $request The request object.
*
* @return array
*/
public function __invoke(Request $request): array
{
// Run the check
/** @var $result array */
$result = call_user_func(
$this->installShopAction,
ShopDomain::fromNative($request->get('shop')),
$request->query('code')
);
if (! $result['completed']) {
// No code, redirect to auth URL
return [$result, false];
}
// Determine if the HMAC is correct
$this->apiHelper->make();
if (! $this->apiHelper->verifyRequest($request->all())) {
// Throw exception, something is wrong
return [$result, null];
}
// Fire the post processing jobs
call_user_func($this->dispatchScriptsAction, $result['shop_id'], false);
call_user_func($this->dispatchWebhooksAction, $result['shop_id'], false);
call_user_func($this->afterAuthorizeAction, $result['shop_id']);
return [$result, true];
}
}