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

[PT-1158] Magento2 plugin trying to confirm a declined order #102

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 22 additions & 15 deletions Helpers/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
namespace Mondu\Mondu\Helpers;

use Magento\Framework\Api\SearchCriteriaBuilder;
use \Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use \Mondu\Mondu\Model\LogFactory;
use Mondu\Mondu\Model\LogFactory;
use Mondu\Mondu\Model\Request\Factory;
use Mondu\Mondu\Model\Ui\ConfigProvider;

class Log extends AbstractHelper
{
const MONDU_STATE_CONFIRMED = 'confirmed';
const MONDU_STATE_PARTIALLY_SHIPPED = 'partially_shipped';
const MONDU_STATE_PARTIALLY_COMPLETE = 'partially_complete';
const MONDU_STATE_SHIPPED = 'shipped';
const MONDU_STATE_COMPLETE = 'complete';

/**
* @var LogFactory
*/
Expand Down Expand Up @@ -81,9 +86,7 @@ public function getLogCollection($orderUid)
->addFieldToFilter('reference_id', ['eq' => $orderUid])
->load();

$log = $logCollection->getFirstItem();

return $log;
return $logCollection->getFirstItem();
}

/**
Expand All @@ -103,15 +106,14 @@ public function logTransaction($order, $response, $addons = null, $paymentMethod
'store_id' => $order->getStoreId(),
'order_id' => $order->getId() ? $order->getId() : $order->getEntityId(),
'reference_id' => $order->getMonduReferenceId(),
// 'transaction_tstamp' => date('Y-m-d H:i:s',time()),
'created_at' => $order->getCreatedAt(),
'customer_id' => $order->getCustomerId(),
'mondu_state' => $response['state'] ?? null,
// 'mode' => $this->helper->getMode() ? 'sandbox' : 'live',
'mode' => $this->_configProvider->getMode(),
'addons' => json_encode($addons),
'payment_method' => $paymentMethod,
'authorized_net_term' => $response['authorized_net_term'],
'is_confirmed' => 1,
'invoice_iban' => $response['merchant']['viban'] ?? null
];
$monduLogger->addData($logData);
Expand Down Expand Up @@ -233,6 +235,10 @@ public function updateLogMonduData(
$data = [];
if ($monduState) {
$data['mondu_state'] = $monduState;

if ($monduState == self::MONDU_STATE_CONFIRMED) {
$data['is_confirmed'] = 1;
}
}

if ($viban) {
Expand All @@ -253,6 +259,7 @@ public function updateLogMonduData(

$log->addData($data);
$log->save();

return $log->getId();
}

Expand All @@ -273,9 +280,9 @@ public function canShipOrder($orderUid)
$log = $logCollection->getFirstItem()->getData();

if (isset($log['mondu_state']) && (
$log['mondu_state'] === 'confirmed' ||
$log['mondu_state'] === 'partially_shipped' ||
$log['mondu_state'] === 'partially_complete'
$log['mondu_state'] === self::MONDU_STATE_CONFIRMED ||
$log['mondu_state'] === self::MONDU_STATE_PARTIALLY_SHIPPED ||
$log['mondu_state'] === self::MONDU_STATE_PARTIALLY_COMPLETE
)) {
return true;
}
Expand All @@ -300,10 +307,10 @@ public function canCreditMemo($orderUid)
$log = $logCollection->getFirstItem()->getData();

if (isset($log['mondu_state']) && (
$log['mondu_state'] === 'partially_shipped' ||
$log['mondu_state'] === 'shipped' ||
$log['mondu_state'] === 'partially_complete' ||
$log['mondu_state'] === 'complete'
$log['mondu_state'] === self::MONDU_STATE_PARTIALLY_SHIPPED ||
$log['mondu_state'] === self::MONDU_STATE_SHIPPED ||
$log['mondu_state'] === self::MONDU_STATE_PARTIALLY_COMPLETE ||
$log['mondu_state'] === self::MONDU_STATE_COMPLETE
)
) {
return true;
Expand Down
33 changes: 32 additions & 1 deletion Model/Request/ConfirmOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\Client\Curl;
use Mondu\Mondu\Model\Ui\ConfigProvider;
use Mondu\Mondu\Model\LogFactory;

class ConfirmOrder extends CommonRequest implements RequestInterface
{
Expand All @@ -18,23 +19,37 @@ class ConfirmOrder extends CommonRequest implements RequestInterface
*/
protected $configProvider;

/**
* @var LogFactory
*/
private $logger;

/**
* @param Curl $curl
* @param ConfigProvider $configProvider
* @param LogFactory $monduLogger
*/
public function __construct(
Curl $curl,
ConfigProvider $configProvider
ConfigProvider $configProvider,
LogFactory $monduLogger
) {
$this->curl = $curl;
$this->configProvider = $configProvider;
$this->logger = $monduLogger;
}

/**
* @inheritDoc
*/
protected function request($params)
{
$log = $this->getLogData($params['orderUid']);

if ($log && $log['is_confirmed']) {
return true;
}

$url = $this->configProvider->getApiUrl('orders') . '/' . $params['orderUid'] . '/confirm';

$resultJson = $this->sendRequestWithParams(
Expand All @@ -55,4 +70,20 @@ protected function request($params)

return $result;
}

/**
* @param $orderUuid
*
* @return mixed
*/
private function getLogData($orderUuid)
{
$monduLogger = $this->logger->create();

$logCollection = $monduLogger->getCollection()
->addFieldToFilter('reference_id', ['eq' => $orderUuid])
->load();

return $logCollection && $logCollection->getFirstItem()->getData() ? $logCollection->getFirstItem()->getData() : false;
}
}
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.3.4-RC2",
"version": "2.3.5",
tikohov20 marked this conversation as resolved.
Show resolved Hide resolved
"license": [
"MIT"
],
Expand Down
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<column xsi:type="text" name="addons" comment="Order addons"/>
<column xsi:type="varchar" name="invoice_iban" comment="Iban"/>
<column xsi:type="tinyint" name="skip_ship_observer" padding="2" comment="Skip ship observer"/>
<column xsi:type="smallint" name="is_confirmed" unsigned="true" nullable="false" default="0" comment="Is order was confirmed"/>
<column xsi:type="varchar" name="payment_method" comment="Mondu payment method"/>
<column xsi:type="int" name="authorized_net_term" unsigned="true" nullable="true" comment="Mondu authorized net term"/>

Expand Down
3 changes: 2 additions & 1 deletion etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"addons": true,
"invoice_iban": true,
"skip_ship_observer": true,
"is_confirmed": true,
"payment_method": true,
"authorized_net_term": true
},
Expand Down Expand Up @@ -39,4 +40,4 @@
"mondu_reference_id": true
}
}
}
}
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.3.4">
<module name="Mondu_Mondu" setup_version="2.3.5">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down