diff --git a/src/Components/ApplePayDirect/ApplePayDirect.php b/src/Components/ApplePayDirect/ApplePayDirect.php index e1ade5feb..6247d3e35 100644 --- a/src/Components/ApplePayDirect/ApplePayDirect.php +++ b/src/Components/ApplePayDirect/ApplePayDirect.php @@ -6,10 +6,10 @@ use Kiener\MolliePayments\Components\ApplePayDirect\Exceptions\ApplePayDirectDomainNotInAllowListException; use Kiener\MolliePayments\Components\ApplePayDirect\Gateways\ApplePayDirectDomainAllowListGateway; use Kiener\MolliePayments\Components\ApplePayDirect\Models\ApplePayCart; +use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayDirectDomainSanitizer; use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayDomainVerificationService; use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayFormatter; use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayShippingBuilder; -use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayDirectDomainSanitizer; use Kiener\MolliePayments\Facade\MolliePaymentDoPay; use Kiener\MolliePayments\Factory\MollieApiFactory; use Kiener\MolliePayments\Handler\Method\ApplePayPayment; @@ -281,10 +281,10 @@ public function setShippingMethod(string $shippingMethodID, SalesChannelContext * @param string $validationURL * @param string $domain * @param SalesChannelContext $context - * @return string * @throws ApiException * @throws ApplePayDirectDomainAllowListCanNotBeEmptyException * @throws ApplePayDirectDomainNotInAllowListException + * @return string */ public function createPaymentSession(string $validationURL, string $domain, SalesChannelContext $context): string { @@ -529,9 +529,9 @@ private function buildApplePayCart(Cart $cart): ApplePayCart * * @param SalesChannelContext $context * @param string $domain - * @return string * @throws ApplePayDirectDomainAllowListCanNotBeEmptyException * @throws ApplePayDirectDomainNotInAllowListException + * @return string */ private function getValidDomain(string $domain, SalesChannelContext $context): string { diff --git a/src/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGateway.php b/src/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGateway.php index 99ba3c1d0..5d8c0f136 100644 --- a/src/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGateway.php +++ b/src/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGateway.php @@ -34,9 +34,9 @@ public function __construct(SettingsService $settingsService) public function getAllowList(SalesChannelContext $context): ApplePayDirectDomainAllowList { $settings = $this->settingsService->getSettings($context->getSalesChannel()->getId()); - $allowList = $settings->applePayDirectDomainAllowList ?? ''; + $allowList = $settings->getApplePayDirectDomainAllowList(); - if (empty($allowList) || !is_string($allowList)) { + if (empty($allowList)) { return ApplePayDirectDomainAllowList::create(); } diff --git a/src/Components/ApplePayDirect/Models/ApplePayDirectDomainAllowListItem.php b/src/Components/ApplePayDirect/Models/ApplePayDirectDomainAllowListItem.php index d3ecc6539..d86860abd 100644 --- a/src/Components/ApplePayDirect/Models/ApplePayDirectDomainAllowListItem.php +++ b/src/Components/ApplePayDirect/Models/ApplePayDirectDomainAllowListItem.php @@ -3,6 +3,8 @@ namespace Kiener\MolliePayments\Components\ApplePayDirect\Models; +use Kiener\MolliePayments\Components\ApplePayDirect\Services\ApplePayDirectDomainSanitizer; + class ApplePayDirectDomainAllowListItem { /** @@ -32,15 +34,7 @@ public static function create(string $value): self throw new \InvalidArgumentException(sprintf('The value of %s must not be empty', self::class)); } - # we need to have a protocol before the parse url command - # in order to have it work correctly - if (strpos($value, 'http') !== 0) { - $value = 'https://' . $value; - } - - # now extract the raw domain without protocol - # and without any sub shop urls - $value = (string)parse_url($value, PHP_URL_HOST); + $value = (new ApplePayDirectDomainSanitizer())->sanitizeDomain($value); return new self($value); } diff --git a/src/Components/ApplePayDirect/Services/ApplePayDirectDomainSanitizer.php b/src/Components/ApplePayDirect/Services/ApplePayDirectDomainSanitizer.php index b4cbe3f88..f76aa4338 100644 --- a/src/Components/ApplePayDirect/Services/ApplePayDirectDomainSanitizer.php +++ b/src/Components/ApplePayDirect/Services/ApplePayDirectDomainSanitizer.php @@ -11,19 +11,19 @@ class ApplePayDirectDomainSanitizer /** * Sanitize the given domain * - * @param string $url + * @param string $domain * @return string */ - public function sanitizeDomain(string $url): 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($url, 'http') !== 0) { - $url = 'https://' . $url; + 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($url, PHP_URL_HOST); + return (string)parse_url($domain, PHP_URL_HOST); } } diff --git a/src/Service/SettingsService.php b/src/Service/SettingsService.php index 6a067c9b9..040972781 100644 --- a/src/Service/SettingsService.php +++ b/src/Service/SettingsService.php @@ -18,7 +18,6 @@ class SettingsService implements PluginSettingsServiceInterface const TEST_API_KEY = 'testApiKey'; const LIVE_PROFILE_ID = 'liveProfileId'; const TEST_PROFILE_ID = 'testProfileId'; - const APPLE_PAY_DIRECT_DOMAIN_ALLOW_LIST = 'applePayDirectDomainAllowList'; /** * @var SystemConfigService diff --git a/src/Setting/MollieSettingStruct.php b/src/Setting/MollieSettingStruct.php index 583f2e594..97c7078d7 100644 --- a/src/Setting/MollieSettingStruct.php +++ b/src/Setting/MollieSettingStruct.php @@ -15,6 +15,8 @@ class MollieSettingStruct extends Struct public const ORDER_EXPIRES_AT_MIN_DAYS = 1; public const ORDER_EXPIRES_AT_MAX_DAYS = 100; + const APPLE_PAY_DIRECT_DOMAIN_ALLOW_LIST = 'applePayDirectDomainAllowList'; + /** * @var string */ @@ -248,6 +250,11 @@ class MollieSettingStruct extends Struct */ protected $phoneNumberFieldRequired = false; + /** + * @var string + */ + protected $applePayDirectDomainAllowList = ''; + /** * @return string */ @@ -965,4 +972,14 @@ public function setPhoneNumberFieldRequired(bool $phoneNumberFieldRequired): voi { $this->phoneNumberFieldRequired = $phoneNumberFieldRequired; } + + public function getApplePayDirectDomainAllowList(): string + { + return $this->applePayDirectDomainAllowList; + } + + public function setApplePayDirectDomainAllowList(string $applePayDirectDomainAllowList): void + { + $this->applePayDirectDomainAllowList = $applePayDirectDomainAllowList; + } } diff --git a/tests/PHPUnit/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGatewayTest.php b/tests/PHPUnit/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGatewayTest.php index 7213daf44..2e98db0a2 100644 --- a/tests/PHPUnit/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGatewayTest.php +++ b/tests/PHPUnit/Components/ApplePayDirect/Gateways/ApplePayDirectDomainAllowListGatewayTest.php @@ -6,6 +6,7 @@ use Kiener\MolliePayments\Components\ApplePayDirect\Gateways\ApplePayDirectDomainAllowListGateway; use Kiener\MolliePayments\Service\SettingsService; +use Kiener\MolliePayments\Setting\MollieSettingStruct; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shopware\Core\Framework\Context; @@ -45,8 +46,7 @@ protected function setUp(): void public function testProvidesEmptyAllowList(): void { - $struct = $this->createMock(\Kiener\MolliePayments\Setting\MollieSettingStruct::class); - $struct->{SettingsService::APPLE_PAY_DIRECT_DOMAIN_ALLOW_LIST} = ''; + $struct = $this->createConfiguredMock(MollieSettingStruct::class, ['getApplePayDirectDomainAllowList' => '']); $this->service->expects($this->once())->method('getSettings')->willReturn($struct); $allowList = $this->gateway->getAllowList($this->scContext); @@ -56,8 +56,7 @@ public function testProvidesEmptyAllowList(): void public function testProvidesAllowList(): void { $allowListString = 'https://example.com,https://example-url.org'; - $struct = $this->createMock(\Kiener\MolliePayments\Setting\MollieSettingStruct::class); - $struct->{SettingsService::APPLE_PAY_DIRECT_DOMAIN_ALLOW_LIST} = $allowListString; + $struct = $this->createConfiguredMock(MollieSettingStruct::class, ['getApplePayDirectDomainAllowList' => $allowListString]); $this->service->expects($this->once())->method('getSettings')->willReturn($struct); $allowList = $this->gateway->getAllowList($this->scContext);