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

Remove inexistent order canceled webhook #93

Merged
merged 6 commits into from
Nov 29, 2023
Merged
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
20 changes: 17 additions & 3 deletions Controller/Webhooks/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function execute()

try {
$content = $this->getRequest()->getContent();

$headers = $this->getRequest()->getHeaders()->toArray();
$signature = hash_hmac('sha256', $content, $this->_monduConfig->getWebhookSecret());
if ($signature !== ($headers['X-Mondu-Signature'] ?? null)) {
Expand All @@ -94,7 +94,6 @@ public function execute()
case 'order/pending':
[$resBody, $resStatus] = $this->handlePending($params);
break;
case 'order/canceled':
case 'order/declined':
[$resBody, $resStatus] = $this->handleDeclinedOrCanceled($params);
break;
Expand Down Expand Up @@ -135,7 +134,12 @@ public function handlePending($params): array
if (empty($order->getData())) {
return [['message' => 'Order does not exist', 'error' => 0], 200];
}

$order->setState(Order::STATE_PAYMENT_REVIEW);
$order->setStatus(Order::STATE_PAYMENT_REVIEW);
$order->addStatusHistoryComment(
__('Mondu: Order Status changed to Payment Review by a webhook')
);
$order->save();
$this->_monduLogger->updateLogMonduData($monduId, $params['order_state']);

return [['message' => 'ok', 'error' => 0], 200];
Expand Down Expand Up @@ -163,6 +167,12 @@ public function handleConfirmed($params): array
return [['message' => 'Order does not exist', 'error' => 0], 200];
}

$order->setState(Order::STATE_PROCESSING);
$order->setStatus(Order::STATE_PROCESSING);
$order->addStatusHistoryComment(
__('Mondu: Order Status changed to Processing by a webhook')
);
$order->save();
$this->_monduLogger->updateLogMonduData($monduId, $params['order_state'], $viban);

return [['message' => 'ok', 'error' => 0], 200];
Expand Down Expand Up @@ -190,6 +200,10 @@ public function handleDeclinedOrCanceled($params): array
return [['message' => 'Order does not exist', 'error' => 0], 200];
}

$order->addStatusHistoryComment(
__('Mondu: Order has been declined')
);

if ($orderState === 'canceled') {
$order->setStatus(Order::STATE_CANCELED)->save();
} elseif ($orderState === 'declined') {
Expand Down
2 changes: 1 addition & 1 deletion Model/Ui/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getApiUrl($path = null): string
return $baseUrl . ($path ? '/'.$path : '');
}

/**
/**
* Returns mondu.js url
*
* @return string
Expand Down
53 changes: 53 additions & 0 deletions Observer/AfterPlaceOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Mondu\Mondu\Observer;

use Magento\Framework\Event\Observer;
use Mondu\Mondu\Helpers\ContextHelper;
use Mondu\Mondu\Helpers\Logger\Logger;
use Mondu\Mondu\Helpers\PaymentMethod;

class AfterPlaceOrder extends MonduObserver
{
/**
* @var \Mondu\Mondu\Helpers\Log
*/
protected $monduLogger;

/**
* @param PaymentMethod $paymentMethodHelper
* @param Logger $monduFileLogger
* @param ContextHelper $contextHelper
* @param \Mondu\Mondu\Helpers\Log $monduLogger
*/
public function __construct(
PaymentMethod $paymentMethodHelper,
Logger $monduFileLogger,
ContextHelper $contextHelper,
\Mondu\Mondu\Helpers\Log $monduLogger
) {
parent::__construct($paymentMethodHelper, $monduFileLogger, $contextHelper);
$this->monduLogger = $monduLogger;
}

/**
* Execute
*
* @param Observer $observer
* @return void
*/
public function _execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$monduUuid = $order->getMonduReferenceId();
$orderData = $this->monduLogger->getTransactionByOrderUid($monduUuid);

if (isset($orderData['mondu_state']) && $orderData['mondu_state'] === 'pending') {
$order->addStatusHistoryComment(
__('Mondu: Order Status changed to Payment Review because it needs manual confirmation')
);
$order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
$order->setStatus(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
$order->save();
}
}
}
8 changes: 1 addition & 7 deletions Observer/Config/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class Save implements ObserverInterface
private $subscriptions = [
'order/confirmed',
'order/declined',
'order/pending',
'order/canceled'
'order/pending'
];

/**
Expand Down Expand Up @@ -94,11 +93,6 @@ public function execute(Observer $observer)
->setTopic('order/declined')
->process();

$this->requestFactory
->create(RequestFactory::WEBHOOKS_REQUEST_METHOD)
->setTopic('order/canceled')
->process();

$this->monduConfig->clearConfigurationCache();
} catch (\Exception $e) {
throw new LocalizedException(__($e->getMessage()));
Expand Down
1 change: 0 additions & 1 deletion Observer/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public function _execute(Observer $observer)

$order->setData('mondu_reference_id', $orderUid);
$order->addStatusHistoryComment(__('Mondu: order id %1', $orderData['uuid']));

$order->save();
$this->monduFileLogger->info('Saved the order in Magento ', ['orderNumber' => $order->getIncrementId()]);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mondu_gmbh/magento2-payment",
"description": "Mondu payment method for magento 2",
"type": "magento2-module",
"version": "2.2.1",
"version": "2.2.2",
"license": [
"MIT"
],
Expand Down
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ services:
ports:
- 80:8080
environment:
- BITNAMI_DEBUG=true
- MAGENTO_HOST=localhost
- MAGENTO_MODE=developer
- MAGENTO_DATABASE_HOST=mariadb
- MAGENTO_DATABASE_PORT_NUMBER=3306
- MAGENTO_DATABASE_USER=bn_magento
- MAGENTO_DATABASE_NAME=bitnami_magento
- MAGENTO_ELASTICSEARCH_HOST=elasticsearch
- MAGENTO_ELASTICSEARCH_PORT_NUMBER=9200
- MAGENTO_USERNAME=mondu
- MAGENTO_PASSWORD=mondu
- MAGENTO_PASSWORD=mondu123
- [email protected]
- ALLOW_EMPTY_PASSWORD=yes
- ELASTICSEARCH_HOST=elasticsearch
- ELASTICSEARCH_PORT_NUMBER=9200
- PHP_MEMORY_LIMIT=5120M
volumes:
- magento_data:/bitnami/magento
Expand Down
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<event name="sales_order_place_before">
<observer name="sales_order_place_before_mondu" instance="Mondu\Mondu\Observer\CreateOrder"/>
</event>
<event name="sales_order_place_after">
<observer name="sales_order_place_after_mondu" instance="Mondu\Mondu\Observer\AfterPlaceOrder"/>
</event>

<event name="order_cancel_after">
<observer name="mondu_order_cancel_after"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mondu_Mondu" setup_version="2.2.1">
<module name="Mondu_Mondu" setup_version="2.2.2">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down