Skip to content

Commit

Permalink
[PT-1158] Magento2 plugin trying to confirm a declined order (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-pugach-mondu authored Mar 19, 2024
1 parent bf64ca9 commit 9f85dbf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
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;
}
}
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
}
}
}
}

0 comments on commit 9f85dbf

Please sign in to comment.