Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added possibility to have a proxied webhook url #38

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/Resources/config/packages/shopware.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ shopware:
SWAG_PAYPAL__API_CANNOT_BE_ZERO_OR_NEGATIVE: notice
SWAG_PAYPAL__API_PAYMENT_SOURCE_INFO_CANNOT_BE_VERIFIED: notice
SWAG_PAYPAL__API_PAYMENT_SOURCE_DECLINED_BY_PROCESSOR: notice
paypal:
webhook_proxy_url: null
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you do one more layer? so

paypal:
    webhook:
        proxy_url: null

Background: we will in a future iteration, add more config parameters to the yaml files, therefore we would like to have some sort of namespacing


1 change: 1 addition & 0 deletions src/Resources/config/services/webhook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<argument type="service" id="Swag\PayPal\Webhook\WebhookRegistry"/>
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
<argument type="service" id="router"/>
<argument>%shopware.paypal.webhook_proxy_url%</argument>
</service>

<!-- handler registry -->
Expand Down
42 changes: 29 additions & 13 deletions src/Webhook/WebhookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ class WebhookService implements WebhookServiceInterface

private SystemConfigService $systemConfigService;

private ?string $proxyWebhookUrl;

/**
* @internal
*/
public function __construct(
WebhookResource $webhookResource,
WebhookRegistry $webhookRegistry,
SystemConfigService $systemConfigService,
RouterInterface $router
RouterInterface $router,
?string $proxyWebhookUrl
) {
$this->webhookResource = $webhookResource;
$this->webhookRegistry = $webhookRegistry;
$this->router = $router;
$this->systemConfigService = $systemConfigService;
$this->proxyWebhookUrl = $proxyWebhookUrl;
}

public function getStatus(?string $salesChannelId): string
Expand All @@ -73,12 +77,18 @@ public function getStatus(?string $salesChannelId): string

$webhookExecuteToken = $this->systemConfigService->getString(Settings::WEBHOOK_EXECUTE_TOKEN, $salesChannelId);

$this->router->getContext()->setScheme('https');
$webhookUrl = $this->router->generate(
'api.action.paypal.webhook.execute',
[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken],
UrlGeneratorInterface::ABSOLUTE_URL
);
if ($this->proxyWebhookUrl !== null) {
$webhookUrl = $this->proxyWebhookUrl . '?' . http_build_query(
[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken]
);
} else {
$this->router->getContext()->setScheme('https');
$webhookUrl = $this->router->generate(
'api.action.paypal.webhook.execute',
[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken],
UrlGeneratorInterface::ABSOLUTE_URL
);
}

return $registeredWebhookUrl === $webhookUrl ? self::STATUS_WEBHOOK_VALID : self::STATUS_WEBHOOK_INVALID;
}
Expand All @@ -96,12 +106,18 @@ public function registerWebhook(?string $salesChannelId): string
$webhookExecuteToken = Random::getAlphanumericString(self::PAYPAL_WEBHOOK_TOKEN_LENGTH);
}

$this->router->getContext()->setScheme('https');
$webhookUrl = $this->router->generate(
'api.action.paypal.webhook.execute',
[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken],
UrlGeneratorInterface::ABSOLUTE_URL
);
if ($this->proxyWebhookUrl !== null) {
$webhookUrl = $this->proxyWebhookUrl . '?' . http_build_query(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the proxy already contains GET parameters? Maybe do a str_contains('?') ? '&' : '?' (overly simplified)

[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken]
);
} else {
$this->router->getContext()->setScheme('https');
$webhookUrl = $this->router->generate(
'api.action.paypal.webhook.execute',
[self::PAYPAL_WEBHOOK_TOKEN_NAME => $webhookExecuteToken],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
Comment on lines +109 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please outsource this into an extra private method? So we don't have the duplicate code?


$webhookId = $this->systemConfigService->getString(Settings::WEBHOOK_ID, $salesChannelId);

Expand Down