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

MOL-733: Invalid order ID: '' #424

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
20 changes: 19 additions & 1 deletion src/Controller/Api/Webhook/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,26 @@ public function __construct(NotificationFacade $notificationFacade, Subscription
*/
public function webhookAction(string $swTransactionId, Request $request, Context $context): JsonResponse
{
$actionId='';
$requestContent = $request->getContent(false);
if (is_string($requestContent)) {
$explodedString = explode('=', $requestContent);
if (isset($explodedString[1])) {
$actionId = $explodedString[1];
}
}

if (empty($actionId)) {
$this->logger->error(
'Error in Mollie Webhook for Transaction no valid transaction id found' . $swTransactionId,
[
'transactionId' => $swTransactionId
]
);
}

try {
$this->notificationFacade->onNotify($swTransactionId, $context);
$this->notificationFacade->onNotify($swTransactionId, $context, $actionId);

return new JsonResponse([
'success' => true
Expand Down
40 changes: 37 additions & 3 deletions src/Controller/Storefront/Webhook/NotificationFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory;
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderFactory;
use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
use Kiener\MolliePayments\Event\MollieOrderBuildEvent;
use Kiener\MolliePayments\Exception\CustomerCouldNotBeFoundException;
use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
use Kiener\MolliePayments\Handler\Method\ApplePayPayment;
Expand All @@ -17,6 +18,7 @@
use Kiener\MolliePayments\Service\SettingsService;
use Kiener\MolliePayments\Struct\Order\OrderAttributes;
use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
use Kiener\MolliePayments\Subscriber\MollieOrderBuildSubscriber;
use Mollie\Api\Resources\Order;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
Expand Down Expand Up @@ -132,10 +134,12 @@ public function __construct(MollieGatewayInterface $gatewayMollie, OrderStatusCo
/**
* @param string $swTransactionId
* @param Context $context
* @throws CustomerCouldNotBeFoundException
* @param string $actionId
* @return void
* @throws CustomerCouldNotBeFoundException
* @throws \Exception
*/
public function onNotify(string $swTransactionId, Context $context): void
public function onNotify(string $swTransactionId, Context $context, string $actionId): void
{
# -----------------------------------------------------------------------------------------------------
# LOAD TRANSACTION
Expand Down Expand Up @@ -196,7 +200,37 @@ public function onNotify(string $swTransactionId, Context $context): void
$molliePayment = null;
$mollieOrder = null;

if (!empty($orderAttributes->getMollieOrderId())) {
if (empty($mollieOrderId)) {
if (str_starts_with($actionId, 'ord')) {
$mollieOrder = $this->gatewayMollie->getOrder($actionId);
$molliePayment = $this->statusConverter->getLatestPayment($mollieOrder);
} elseif (str_starts_with($actionId, 'tr')) {
$molliePayment = $this->gatewayMollie->getPayment($actionId);
if ($molliePayment->orderId != null) {
$mollieOrder = $this->gatewayMollie->getOrder($molliePayment->orderId);
}
}

if ($mollieOrder == null) {
throw new \Exception('No valid order has been found: ' . $swOrder->getOrderNumber());
}

$metadata = json_decode($mollieOrder->metadata, true);

if ($metadata == null) {
throw new \Exception('Order has no metadata: ' . $swOrder->getOrderNumber());
}

$metaShortId = $metadata[MollieOrderBuildEvent::METADATA_SHORT_TRANSACTION_ID_KEY];
$transShortId = substr($swTransactionId, 0, 8);

if ($metaShortId == $transShortId) {
$mollieOrderId = $mollieOrder->id;
}
}


if (!empty($mollieOrderId)) {

# fetch the order of our mollie ID
# from our sales channel mollie profile
Expand Down
25 changes: 22 additions & 3 deletions src/Controller/Storefront/Webhook/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,31 @@ public function __construct(NotificationFacade $notificationFacade, Subscription
*
* @param SalesChannelContext $context
* @param string $swTransactionId
* @param Request $request
* @return JsonResponse
*/
public function onWebhookReceived(SalesChannelContext $context, string $swTransactionId): JsonResponse
public function onWebhookReceived(SalesChannelContext $context, string $swTransactionId, Request $request): JsonResponse
{
$actionId='';
$requestContent = $request->getContent(false);
if (is_string($requestContent)) {
$explodedString = explode('=', $requestContent);
if (isset($explodedString[1])) {
$actionId = $explodedString[1];
}
}

if (empty($actionId)) {
$this->logger->error(
'Error in Mollie Webhook for Transaction no valid transaction id found' . $swTransactionId,
[
'transactionId' => $swTransactionId
]
);
}

try {
$this->notificationFacade->onNotify($swTransactionId, $context->getContext());
$this->notificationFacade->onNotify($swTransactionId, $context->getContext(), $actionId);

return new JsonResponse(['success' => true]);
} catch (\Throwable $ex) {
Expand Down Expand Up @@ -142,7 +161,7 @@ public function webhookSubscriptionRenew(string $swSubscriptionId, Request $requ
# now simply redirect to the official webhook
# that handles the full order, validates the payment and
# starts to trigger things.
return $this->onWebhookReceived($context, $latestTransaction->getId());
return $this->onWebhookReceived($context, $latestTransaction->getId(), $request);
} catch (SubscriptionSkippedException $ex) {
# if we skip a new subscription, then we need to respond with
# 200 OK so that Mollie will not try it again.
Expand Down
5 changes: 3 additions & 2 deletions src/Event/MollieOrderBuildEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MollieOrderBuildEvent
{

public const METADATA_SHORT_TRANSACTION_ID_KEY = 'tid';
/**
* @var array<mixed>
*/
Expand Down Expand Up @@ -47,7 +47,8 @@ public function __construct(array $orderData, OrderEntity $order, string $transa
$this->transactionId = $transactionId;
$this->salesChannelContext = $salesChannelContext;

$this->metadata = [];
$shortId = substr($transactionId, 0, 8);
$this->metadata = [self::METADATA_SHORT_TRANSACTION_ID_KEY =>$shortId];
}

/**
Expand Down