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
37 changes: 35 additions & 2 deletions src/Controller/Storefront/Webhook/NotificationFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 +133,12 @@ public function __construct(MollieGatewayInterface $gatewayMollie, OrderStatusCo
/**
* @param string $swTransactionId
* @param Context $context
* @param string $actionId
* @throws \Exception
* @throws CustomerCouldNotBeFoundException
* @return void
*/
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 +199,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[MollieOrderBuildSubscriber::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: 5 additions & 0 deletions src/Resources/config/services/subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
<argument type="service" id="mollie_payments.logger"/>
<tag name="kernel.event_subscriber"/>
</service>

<service id="Kiener\MolliePayments\Subscriber\MollieOrderBuildSubscriber">
<tag name="kernel.event_subscriber"/>
</service>

</services>
</container>
35 changes: 35 additions & 0 deletions src/Subscriber/MollieOrderBuildSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kiener\MolliePayments\Subscriber;

use Kiener\MolliePayments\Event\MollieOrderBuildEvent;

class MollieOrderBuildSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
public const METADATA_SHORT_TRANSACTION_ID_KEY = 'tid';

/**
* @inheritDoc
*/
public static function getSubscribedEvents()
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think we can just add this to the core function? this is more for third party devs :)
also it should then be even visible in the unit tests which doesnt seem to be the case right now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you elaborate what you mean please?

{
return [
MollieOrderBuildEvent::class => 'onMollieOrderBuilt'
];
}

/**
* Adds the first
* @param MollieOrderBuildEvent $event
* @return void
*/
public function onMollieOrderBuilt(MollieOrderBuildEvent $event)
{
$transactionId = $event->getTransactionId();

if (!empty($transactionId)) {
$shortId = substr($transactionId, 0, 8);
$event->setMetadata([self::METADATA_SHORT_TRANSACTION_ID_KEY =>$shortId]);
}
}
}