Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Added Event Listener system #1274

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8119832
fixed issue when using custom table for authentication instead of use…
nahid Aug 25, 2022
f73467d
Merge branch 'osiset:master' into master
nahid Aug 28, 2022
e00b49e
fixed charges model saving when using custom shop table
nahid Sep 7, 2022
7ab1e9a
refactor code for reusing shops table name and its foreign id
nahid Sep 8, 2022
94bf062
Merge branch 'osiset:master' into master
nahid Sep 8, 2022
54907b6
refactor getShopsTable() and added new method for getting shops table…
nahid Sep 8, 2022
2aa9a2e
Merge branch 'master' of github.com:nahid/laravel-shopify
nahid Sep 8, 2022
c099191
Remove unwanted new line feom Charge model contractor
nahid Sep 9, 2022
41c8734
auto fixes code style from php-cs-fixer
nahid Sep 9, 2022
624e0ca
Merge branch 'master' into master
nahid Sep 9, 2022
40db940
Merge branch 'osiset:master' into master
nahid Sep 9, 2022
5ddf28b
added name, email and password in create shops table migration if its…
nahid Sep 9, 2022
6ffa402
Merge branch 'master' of github.com:nahid/laravel-shopify
nahid Sep 9, 2022
b05089b
Merge branch 'osiset:master' into master
nahid Sep 9, 2022
79ff4bb
fixed get data from request issue in StoreUsageCharge
nahid Sep 27, 2022
4e510ad
Merge branch 'master' of github.com:nahid/laravel-shopify
nahid Sep 27, 2022
47d23d3
Merge branch 'osiset:master' into master
nahid Nov 28, 2022
663fe85
feat(event, listener): added various events, update Util
nahid Nov 28, 2022
fb007eb
fixed by php-cs-fixer
nahid Nov 28, 2022
94cc3f4
fixed: support for php 7.2
nahid Nov 28, 2022
7b8048a
fixed(ServiceProvider): added laravel version compatibility in servic…
nahid Nov 29, 2022
d72ea66
fixed(Request Validation): update StoreUsageChare request
nahid Nov 29, 2022
de8a415
fix: cs-fix changes
nahid Dec 13, 2022
1f9a9d0
fixed iframe ancestors issue
nahid Dec 13, 2022
c38c3ad
fixed iframe ancestors issue
nahid Dec 13, 2022
13a2446
Merge branch 'gnikyt:master' into master
nahid Dec 13, 2022
ead87de
update verify shop middleware
nahid Dec 15, 2022
118cec9
Merge branch 'master' of github.com:nahid/laravel-shopify
nahid Dec 15, 2022
3686198
feat(shopdomain): validate correct shop domain when authenticate
nahid Feb 16, 2023
b21ae45
change after authenticate job fire logic
nahid Feb 17, 2023
ceffdd3
Update composer.json
nahid Mar 27, 2023
f5090af
update laravel shopify package for laravel 10
nahid Aug 27, 2023
d96986b
Update composer.json
nahid Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
}
],
"require": {
"php": ">=7.2",
"php": "^8.2",
"ext-json": "*",
"funeralzone/valueobjects": "^0.5",
"jenssegers/agent": "^2.6",
"laravel/framework": "^7.0 || ^8.0 || ^9.0",
"laravel/framework": "^10.0 || ^11.0",
"osiset/basic-shopify-api": "^9.0 || <=10.0.5"
},
"require-dev": {
Expand Down
3 changes: 3 additions & 0 deletions src/Actions/ActivatePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId;
use Osiset\ShopifyApp\Contracts\Queries\Plan as IPlanQuery;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent;
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Objects\Enums\PlanType;
Expand Down Expand Up @@ -142,6 +143,8 @@ public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $charge
$charge = $this->chargeCommand->make($transfer);
$this->shopCommand->setToPlan($shopId, $planId);

event(new PlanActivatedEvent($shop, $plan, $charge));

return $charge;
}
}
2 changes: 1 addition & 1 deletion src/Actions/AfterAuthorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __invoke(ShopIdValue $shopId): bool
$job = Arr::get($config, 'job');
if (Arr::get($config, 'inline', false)) {
// Run this job immediately
$job::dispatchNow($shop);
$job::dispatchSync($shop);
} else {
// Run later
$job::dispatch($shop)
Expand Down
5 changes: 5 additions & 0 deletions src/Actions/AuthenticateShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Http\Request;
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
use Osiset\ShopifyApp\Messaging\Events\AppInstalledEvent;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;

/**
* Authenticates a shop and fires post authentication actions.
Expand Down Expand Up @@ -105,6 +107,9 @@ public function __invoke(Request $request): array
call_user_func($this->dispatchWebhooksAction, $result['shop_id'], false);
call_user_func($this->afterAuthorizeAction, $result['shop_id']);

event(new AppInstalledEvent($result['shop_id']));


return [$result, true];
}
}
2 changes: 1 addition & 1 deletion src/Actions/DispatchScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool

// Run the installer job
if ($inline) {
($this->jobClass)::dispatchNow(
($this->jobClass)::dispatchSync(
$shop->getId(),
$scripttags
);
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/DispatchWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool

// Run the installer job
if ($inline) {
($this->jobClass)::dispatchNow(
($this->jobClass)::dispatchSync(
$shop->getId(),
$webhooks
);
Expand Down
17 changes: 17 additions & 0 deletions src/Actions/InstallShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function __construct(
*/
public function __invoke(ShopDomain $shopDomain, ?string $code): array
{

if (!$this->isValidShop($shopDomain)) {
return [
'completed' => false,
'url' => null,
'shop_id' => null,
];
}
// Get the shop
$shop = $this->shopQuery->getByDomain($shopDomain, [], true);
if ($shop === null) {
Expand Down Expand Up @@ -102,4 +110,13 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
];
}
}

public function isValidShop(ShopDomain $shopDomain): bool
{
$regex = '/^[a-zA-Z0-9][a-zA-Z0-9\-]*.myshopify.com/';
$isMatched = preg_match($regex, $shopDomain->toNative(), $matches, PREG_OFFSET_CAPTURE);

return $isMatched === 1;
}

}
10 changes: 9 additions & 1 deletion src/Http/Middleware/IframeProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Cache;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;

/**
* Responsibility for protection against clickjaking
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$ancestors = Util::getShopifyConfig('iframe_ancestors');

$shop = Cache::remember(
'frame-ancestors_'.$request->get('shop'),
Expand All @@ -57,9 +59,15 @@ function () use ($request) {
? $shop->name
: '*.myshopify.com';

$iframeAncestors = "frame-ancestors https://admin.shopify.com https://$domain";

if (!blank($ancestors)) {
$iframeAncestors .= ' ' . $ancestors;
}

$response->headers->set(
'Content-Security-Policy',
"frame-ancestors https://$domain https://admin.shopify.com"
$iframeAncestors
);

return $response;
Expand Down
35 changes: 34 additions & 1 deletion src/Http/Middleware/VerifyShopify.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ public function handle(Request $request, Closure $next)
}

if (!Util::useNativeAppBridge()) {
$storeResult = !$this->isApiRequest($request) && $this->checkPreviousInstallation($request);
$shop = $this->getShopIfAlreadyInstalled($request);
$storeResult = !$this->isApiRequest($request) && $shop;

if ($storeResult) {
$this->loginFromShop($shop);
return $next($request);
}
}
Expand Down Expand Up @@ -511,4 +513,35 @@ protected function checkPreviousInstallation(Request $request): bool

return $shop && $shop->password && ! $shop->trashed();
}

/**
* Get shop model if there is a store record in the database.
*
* @param Request $request The request object.
*
* @return ?ShopModel
*/
protected function getShopIfAlreadyInstalled(Request $request): ?ShopModel
{
$shop = $this->shopQuery->getByDomain(ShopDomain::fromRequest($request), [], true);

return $shop && $shop->password && ! $shop->trashed() ? $shop : null;
}

/**
* Login and validate store
*
* @param ShopModel $shop
* @return void
*/
protected function loginFromShop(ShopModel $shop): void
{
// Override auth guard
if (($guard = Util::getShopifyConfig('shop_auth_guard'))) {
$this->auth->setDefaultDriver($guard);
}

// All is well, login the shop
$this->auth->login($shop);
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/AppInstalledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Objects\Values\ShopId;

/**
* Event fired when this
*/
class AppInstalledEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var ShopId
*/
public $shopId;

/**
* Create a new event instance.
*
* @param ShopId $shop_id
*
* @return void
*/
public function __construct(ShopId $shop_id)
{
$this->shopId = $shop_id;
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/AppUninstalledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;

/**
* Event fired when this
*/
class AppUninstalledEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var IShopModel
*/
public $shop;

/**
* Create a new event instance.
*
* @param IShopModel $shop
*
* @return void
*/
public function __construct(IShopModel $shop)
{
$this->shop = $shop;
}
}
53 changes: 53 additions & 0 deletions src/Messaging/Events/PlanActivatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
use Osiset\ShopifyApp\Objects\Values\ChargeId;

/**
* Event fired when this
*/
class PlanActivatedEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var IShopModel
*/
public $shop;

/**
* Plan's instance.
*
* @var Model
*/
public $plan;

/**
* Charge ID
*
* @var ChargeId
*/
public $chargeId;

/**
* Create a new event instance.
*
* @param IShopModel $shop
*
* @return void
*/
public function __construct(IShopModel $shop, Model $plan, ChargeId $chargeId)
{
$this->shop = $shop;
$this->plan = $plan;
$this->chargeId = $chargeId;
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/ShopAuthenticatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Objects\Values\ShopId;

/**
* Event fired when this
*/
class ShopAuthenticatedEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var ShopId
*/
public $shopId;

/**
* Create a new event instance.
*
* @param ShopId $shop_id
*
* @return void
*/
public function __construct(ShopId $shop_id)
{
$this->shopId = $shop_id;
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/ShopDeletedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;

/**
* Event fired when this
*/
class ShopDeletedEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var IShopModel
*/
public $shop;

/**
* Create a new event instance.
*
* @param IShopModel $shop
*
* @return void
*/
public function __construct(IShopModel $shop)
{
$this->shop = $shop;
}
}
Loading