Skip to content

Commit

Permalink
NTR: PISHPS-388: Fix webhook (#917)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 16, 2024
1 parent d3fcc72 commit 42a6ce0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions src/Controller/Storefront/Webhook/NotificationFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kiener\MolliePayments\Controller\Storefront\Webhook;

use DateTimeZone;
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderDispatcherAdapterInterface;
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory;
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderFactory;
Expand Down Expand Up @@ -140,32 +141,30 @@ public function onNotify(string $swTransactionId, Context $context): void
# LOAD TRANSACTION
$swTransaction = $this->getTransaction($swTransactionId, $context);

if (!$swTransaction instanceof OrderTransactionEntity) {
if (! $swTransaction instanceof OrderTransactionEntity) {
throw new \Exception('Transaction ' . $swTransactionId . ' not found in Shopware');
}

# -----------------------------------------------------------------------------------------------------

$swOrder = $swTransaction->getOrder();

if (!$swOrder instanceof OrderEntity) {
if (! $swOrder instanceof OrderEntity) {
throw new OrderNotFoundException('Shopware Order not found for transaction: ' . $swTransactionId);
}

$now = new \DateTime();
$now->modify("+2 minutes");

$changedDate = $swOrder->getUpdatedAt();
if ($changedDate === null) {
$changedDate = $swOrder->getCreatedAt();
}

if ($changedDate !== null && $now < $changedDate) {
throw new WebhookIsTooEarlyException((string)$swOrder->getOrderNumber(), $now, $changedDate);
$now = new \DateTime('now', new DateTimeZone('UTC'));
/** @var ?\DateTimeImmutable $orderCreatedAt */
$orderCreatedAt = $swOrder->getCreatedAt();
if ($orderCreatedAt !== null) {
$createdAt = \DateTime::createFromImmutable($orderCreatedAt);
$createdAt->modify('+2 minutes');
if ($now < $orderCreatedAt) {
throw new WebhookIsTooEarlyException((string)$swOrder->getOrderNumber(), $now, $orderCreatedAt);
}
}



# --------------------------------------------------------------------------------------------

# now get the correct settings from the sales channel of that order.
Expand All @@ -188,13 +187,13 @@ public function onNotify(string $swTransactionId, Context $context): void
# verify if the customer really paid with Mollie in the end
$paymentMethod = $swTransaction->getPaymentMethod();

if (!$paymentMethod instanceof PaymentMethodEntity) {
if (! $paymentMethod instanceof PaymentMethodEntity) {
throw new \Exception('Transaction ' . $swTransactionId . ' has no payment method!');
}

$paymentMethodAttributes = new PaymentMethodAttributes($paymentMethod);

if (!$paymentMethodAttributes->isMolliePayment()) {
if (! $paymentMethodAttributes->isMolliePayment()) {
# just skip it if it has been paid
# with another payment provider
# do NOT throw an error
Expand All @@ -214,7 +213,7 @@ public function onNotify(string $swTransactionId, Context $context): void
$molliePayment = null;
$mollieOrder = null;

if (!empty($orderAttributes->getMollieOrderId())) {
if (! empty($orderAttributes->getMollieOrderId())) {
# fetch the order of our mollie ID
# from our sales channel mollie profile
$mollieOrder = $this->gatewayMollie->getOrder($mollieOrderId);
Expand All @@ -232,7 +231,7 @@ public function onNotify(string $swTransactionId, Context $context): void
# --------------------------------------------------------------------------------------------


$logId = (!empty($mollieOrderId)) ? $mollieOrderId : $molliePaymentId;
$logId = (! empty($mollieOrderId)) ? $mollieOrderId : $molliePaymentId;
$this->logger->info('Webhook for order ' . $swOrder->getOrderNumber() . ' and Mollie ID: ' . $logId . ' has been received with Status: ' . $status);


Expand Down
2 changes: 1 addition & 1 deletion src/Exception/WebhookIsTooEarlyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WebhookIsTooEarlyException extends ShopwareHttpException
public const MOLLIE_PAYMENTS__WEBHOOK_TOO_EARLY = 'MOLLIE_PAYMENTS__WEBHOOK_TOO_EARLY';
public function __construct(string $oderNumber, \DateTimeInterface $now, \DateTimeInterface $updatedTime)
{
$message = 'Webhook is too early for order: {{orderNumber}}. The last updateTime of the order is {{lastUpdateTime}}. It should be higher than: {{now}}';
$message = 'Webhook is too early for order: {{orderNumber}}. Request will be accepted after: {{lastUpdateTime}}';
$parameters =[
'orderNumber' => $oderNumber,
'lastUpdateTime' => $updatedTime->format('Y-m-d H:i:s'),
Expand Down

0 comments on commit 42a6ce0

Please sign in to comment.