-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PISHPS-328: added ApplePayDirectDomainAllowList (#790)
* PISHPS-328: added ApplePayValidationAllowList, subscriber to list default saleschannel urls, integrated allow list into applepay validate route * PISHPS-303: applied requested todos * PISHPS-328: applied requested todos * PISHPS-328: added test case to ApplePayDirectDomainSanitizerTest for sub domains
- Loading branch information
1 parent
99f0e81
commit 39ebd2e
Showing
16 changed files
with
572 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ponents/ApplePayDirect/Exceptions/ApplePayDirectDomainAllowListCanNotBeEmptyException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Exceptions; | ||
|
||
class ApplePayDirectDomainAllowListCanNotBeEmptyException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('The Apple Pay Direct domain allow list can not be empty. Please check the configuration.'); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Components/ApplePayDirect/Exceptions/ApplePayDirectDomainNotInAllowListException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Exceptions; | ||
|
||
class ApplePayDirectDomainNotInAllowListException extends \Exception | ||
{ | ||
public function __construct(string $url) | ||
{ | ||
parent::__construct(sprintf('The given URL %s is not in the Apple Pay Direct domain allow list.', $url)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGateway.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Gateways; | ||
|
||
use Kiener\MolliePayments\Components\ApplePayDirect\Models\ApplePayDirectDomainAllowListItem; | ||
use Kiener\MolliePayments\Components\ApplePayDirect\Models\Collections\ApplePayDirectDomainAllowList; | ||
use Kiener\MolliePayments\Service\SettingsService; | ||
use Shopware\Core\System\SalesChannel\SalesChannelContext; | ||
|
||
class ApplePayDirectDomainAllowListGateway | ||
{ | ||
/** | ||
* @var SettingsService | ||
*/ | ||
private $settingsService; | ||
|
||
/** | ||
* ApplePayDirectDomainAllowListItem constructor. | ||
* | ||
* @param SettingsService $settingsService | ||
*/ | ||
public function __construct(SettingsService $settingsService) | ||
{ | ||
$this->settingsService = $settingsService; | ||
} | ||
|
||
/** | ||
* Get the ApplePayDirectDomainAllowList | ||
* | ||
* @param SalesChannelContext $context | ||
* @return ApplePayDirectDomainAllowList | ||
*/ | ||
public function getAllowList(SalesChannelContext $context): ApplePayDirectDomainAllowList | ||
{ | ||
$settings = $this->settingsService->getSettings($context->getSalesChannel()->getId()); | ||
$allowList = $settings->getApplePayDirectDomainAllowList(); | ||
|
||
if (empty($allowList)) { | ||
return ApplePayDirectDomainAllowList::create(); | ||
} | ||
|
||
$allowList = trim($allowList); | ||
|
||
$items = explode(',', $allowList); | ||
$items = array_map([ApplePayDirectDomainAllowListItem::class, 'create'], $items); | ||
|
||
return ApplePayDirectDomainAllowList::create(...$items); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Components/ApplePayDirect/Models/ApplePayDirectDomainAllowListItem.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Models; | ||
|
||
use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayDirectDomainSanitizer; | ||
|
||
class ApplePayDirectDomainAllowListItem | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* ApplePayDirectDomainAllowListItem constructor. | ||
* | ||
* @param string $value | ||
*/ | ||
private function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* Create a new ApplePayDirectDomainAllowListItem | ||
* | ||
* @param string $value | ||
* @return self | ||
*/ | ||
public static function create(string $value): self | ||
{ | ||
if (empty($value)) { | ||
throw new \InvalidArgumentException(sprintf('The value of %s must not be empty', self::class)); | ||
} | ||
|
||
$value = (new ApplePayDirectDomainSanitizer())->sanitizeDomain($value); | ||
|
||
return new self($value); | ||
} | ||
|
||
/** | ||
* Compare the value with the given value | ||
* | ||
* @param string $value value that will be compared | ||
* @return bool | ||
*/ | ||
public function equals(string $value): bool | ||
{ | ||
return $this->value === $value; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Components/ApplePayDirect/Models/Collections/ApplePayDirectDomainAllowList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Models\Collections; | ||
|
||
use Countable; | ||
use Kiener\MolliePayments\Components\ApplePayDirect\Models\ApplePayDirectDomainAllowListItem; | ||
|
||
class ApplePayDirectDomainAllowList implements Countable | ||
{ | ||
/** | ||
* @var ApplePayDirectDomainAllowListItem[] | ||
*/ | ||
private $allowList; | ||
|
||
/** | ||
* ApplePayDirectDomainAllowList constructor. | ||
* | ||
* @param ApplePayDirectDomainAllowListItem[] $allowList | ||
*/ | ||
private function __construct(array $allowList = []) | ||
{ | ||
$this->allowList = $allowList; | ||
} | ||
|
||
/** | ||
* Create a new ApplePayDirectDomainAllowList | ||
* | ||
* @param ApplePayDirectDomainAllowListItem ...$items | ||
* @return ApplePayDirectDomainAllowList | ||
*/ | ||
public static function create(ApplePayDirectDomainAllowListItem ...$items): self | ||
{ | ||
return new self($items); | ||
} | ||
|
||
/** | ||
* Check if the given value is in the allow list | ||
* | ||
* @param string $value | ||
* @return bool | ||
*/ | ||
public function contains(string $value): bool | ||
{ | ||
foreach ($this->allowList as $item) { | ||
if ($item->equals($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEmpty(): bool | ||
{ | ||
return count($this) === 0; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function count(): int | ||
{ | ||
return count($this->allowList); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Components/ApplePayDirect/Services/ApplePayDirectDomainSanitizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Services; | ||
|
||
/** | ||
* This class is responsible for sanitizing the given domain | ||
*/ | ||
class ApplePayDirectDomainSanitizer | ||
{ | ||
/** | ||
* Sanitize the given domain | ||
* | ||
* @param string $domain | ||
* @return string | ||
*/ | ||
public function sanitizeDomain(string $domain): string | ||
{ | ||
# we need to have a protocol before the parse url command | ||
# in order to have it work correctly | ||
if (strpos($domain, 'http') !== 0) { | ||
$domain = 'https://' . $domain; | ||
} | ||
|
||
# now extract the raw domain without protocol | ||
# and without any sub shop urls | ||
return (string)parse_url($domain, PHP_URL_HOST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.