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

Tc 9 31 order tab #30

Merged
merged 12 commits into from
Nov 26, 2024
6 changes: 3 additions & 3 deletions menu.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="ISO-8859-15"?>
<OX>
<OXMENU id="NAVIGATION_ESHOPADMIN">
<MAINMENU id="mxuadmin">
<SUBMENU id="mxusers">
<TAB id="tbcluser_greetings" cl="oemt_admin_greeting" />
<MAINMENU id="mxorders">
<SUBMENU id="mxdisplayorders" cl="admin_order" list="order_list">
<TAB id="tbclorder_telecash" cl="OrderTeleCash" />
</SUBMENU>
</MAINMENU>
</OXMENU>
Expand Down
6 changes: 5 additions & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OxidEsales\Eshop\Application\Model\State as oxState;
use OxidEsales\Eshop\Application\Model\User as oxUser;
use OxidSolutionCatalysts\TeleCash\Application\Controller\Admin\AdminTeleCashJsonEndpoint;
use OxidSolutionCatalysts\TeleCash\Application\Controller\Admin\OrderTeleCash;
use OxidSolutionCatalysts\TeleCash\Application\Controller\FrontendTeleCashNotificationEndpoint;
use OxidSolutionCatalysts\TeleCash\Core\Module;
use OxidSolutionCatalysts\TeleCash\Extension\Application\Controller\Admin\ModuleConfiguration;
Expand Down Expand Up @@ -61,7 +62,10 @@
'email' => '',
'controllers' => [
// Admin
'AdminTeleCashJsonEndpoint' => AdminTeleCashJsonEndpoint::class,
'AdminTeleCashJsonEndpoint' => AdminTeleCashJsonEndpoint::class,
'OrderTeleCash' => OrderTeleCash::class,

// Frontend
'FrontendTeleCashNotificationEndpoint' => FrontendTeleCashNotificationEndpoint::class,
],
'events' => [
Expand Down
137 changes: 137 additions & 0 deletions src/Application/Controller/Admin/OrderTeleCash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidSolutionCatalysts\TeleCash\Application\Controller\Admin;

use DateTime;
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidSolutionCatalysts\TeleCash\Application\Model\TeleCashOrder;
use OxidSolutionCatalysts\TeleCash\Application\Model\TeleCashPayment;
use OxidSolutionCatalysts\TeleCash\Core\Module;
use OxidSolutionCatalysts\TeleCash\Core\Service\RegistryService;
use OxidSolutionCatalysts\TeleCash\Exception\TeleCashException;
use OxidSolutionCatalysts\TeleCash\Extension\Application\Model\Order;
use OxidSolutionCatalysts\TeleCash\Extension\Application\Model\Payment;
use OxidSolutionCatalysts\TeleCash\Traits\ModelGetter;
use OxidSolutionCatalysts\TeleCash\Traits\ServiceContainer;

class OrderTeleCash extends AdminController
{
use ModelGetter;
use ServiceContainer;

protected $_sThisTemplate = '@' . Module::MODULE_ID . '/admin/order_telecash';

protected RegistryService $registryService;

/**
* Constructor for Order.
*
* Initializes a new instance of the OrderTeleCash class. This constructor can be used
* in both production and test environments due to its flexible parameter configuration.
*
* @param bool $initParent Whether to initialize the parent BaseModel.
* Set to false in test environment to avoid
* OXID framework dependencies. Default is true.
*/
public function __construct(
bool $initParent = true
) {
if ($initParent) {
parent::__construct();
}

$this->setContainer($this->getContainer());
$this->registryService = $this->getRequiredService(
RegistryService::class,
'RegistryService'
);
}

/** @inheritdoc
* @throws TeleCashException
*/
public function render()
{
parent::render();

$this->addTeleCashToTemplate();

return $this->getTemplateName();
}


/**
* @throws TeleCashException
*/
private function addTeleCashToTemplate(): void
{
$oxId = $this->getEditObjectId();

/** @var Order $order */
$order = $this->getOxidOrderModel();
$order->load($oxId);
$isNettoMode = $order->isNettoMode();

$isTeleCashOrder = $order->isTeleCashOrder();
$teleCashOrder = $order->getTeleCashOrder();

$paymentId = $order->getFieldStringData('oxpaymenttype');
$payment = $this->getOxidPaymentModel();

$payment->load($paymentId);
/** @var Payment $payment */
$teleCashPayment = $payment->getTeleCashPayment();

// these variables are needed in any case
$this->addTplParam('oxid', $oxId);
$this->addTplParam('teleCashModuleId', Module::MODULE_ID);
$this->addTplParam('isTeleCashOrder', $isTeleCashOrder);

if ($isTeleCashOrder && $teleCashOrder) {
$this->addTeleCashOrderToTemplate(
$teleCashOrder,
$isNettoMode,
$teleCashPayment
);
}
}

/**
* @throws TeleCashException
*/
private function addTeleCashOrderToTemplate(
TeleCashOrder $teleCashOrder,
bool $isNettoMode,
?TeleCashPayment $teleCashPayment
): void {
$chargeTotal = $teleCashOrder->getChargeTotal();
$currency = $teleCashOrder->getCurrency();
$priceChargeTotal = $this->getPriceObj($chargeTotal, $isNettoMode);
$currencyObj = $this->registryService->getConfig()->getCurrencyObject($currency);

$dateTime = $teleCashOrder->getTxnDateTime();
$txnDateTime = $dateTime ? $dateTime->format('d.m.Y H:i:s') : '';

$this->addTplParam('priceChargeTotal', $priceChargeTotal);
$this->addTplParam('currencyObj', $currencyObj);
$this->addTplParam('paymentMethod', $teleCashOrder->getPaymentMethod());
$this->addTplParam('status', $teleCashOrder->getStatus());
$this->addTplParam('responseCode', $teleCashOrder->getProcessorResponseCode());
$this->addTplParam('txnDateTime', $txnDateTime);
$this->addTplParam('txnType', $teleCashOrder->getTxnType());
$this->addTplParam('ipgTransactionId', $teleCashOrder->getIpgTransactionId());
$this->addTplParam('endpointTransactionId', $teleCashOrder->getEndpointTransactionId());
$this->addTplParam('terminalId', $teleCashOrder->getTerminalId());

if ($teleCashPayment) {
$this->addTplParam('captureType', $teleCashPayment->getTeleCashCaptureType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OxidSolutionCatalysts\TeleCash\Application\Controller;

use OxidEsales\Eshop\Core\Controller\BaseController;
use OxidSolutionCatalysts\TeleCash\Exception\TeleCashException;
use OxidSolutionCatalysts\TeleCash\Traits\ModelGetter;
use OxidSolutionCatalysts\TeleCash\Traits\RequestGetter;
use OxidSolutionCatalysts\TeleCash\Traits\ServiceContainer;
Expand Down Expand Up @@ -46,11 +47,25 @@ public function __construct(bool $initParent = true)
}

/**
* receive Notifications from TeleCash
* Collect TeleCash-Notification and add this to order
*
* @return void
* @throws TeleCashException
* TODO remove PHPMD.UnusedLocalVariable
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function receiveNotifications(): void
{
$telecashConnect = $this->getTeleCashConnect();
$telecashConnect->setResponseData($_POST);
if (!$telecashConnect->isValidResponse()) {
/** Throw an Error is too hard, but Ok for the moment, Show an Error is better */
throw (new TeleCashException())->noValidTransactionResult();
}

/** TODO follow up the work ...
* We´ve got TeleCashPost-Data
* The notification result contains new informations for the order.
*/
$notificationResult = $telecashConnect->getTransactionResult();
}
}
4 changes: 3 additions & 1 deletion src/Application/Model/Interface/TeleCashOrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace OxidSolutionCatalysts\TeleCash\Application\Model\Interface;

use DateTime;

interface TeleCashOrderInterface
{
/**
Expand All @@ -21,7 +23,7 @@ public function setTransactionResult(array $transactionData): void;
public function getTxnType(): string;

/** get the Txn DateTime */
public function getTxnDateTime(): string;
public function getTxnDateTime(): ?DateTime;

/** get the Oid */
public function getOid(): string;
Expand Down
94 changes: 78 additions & 16 deletions src/Application/Model/TeleCashOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

namespace OxidSolutionCatalysts\TeleCash\Application\Model;

use DateTime;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use InvalidArgumentException;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidSolutionCatalysts\TeleCash\Application\Model\Interface\TeleCashOrderInterface;
use OxidSolutionCatalysts\TeleCash\Core\Module;
use OxidSolutionCatalysts\TeleCash\Exception\TeleCashException;
use OxidSolutionCatalysts\TeleCash\IPG\Model\TransactionResult;
use OxidSolutionCatalysts\TeleCash\IPG\TeleCashCurrency;
use OxidSolutionCatalysts\TeleCash\Traits\DataGetter;
Expand All @@ -25,42 +30,89 @@ class TeleCashOrder extends BaseModel implements TeleCashOrderInterface
use DataGetter;
use Json;

protected $_sClassName = 'OxidSolutionCatalysts\TeleCash\Application\TeleCashOrder';
protected $_sCoreTable = Module::TELECASH_ORDER_EXTENSION_TABLE;
protected string $oxOrderId = '';

protected Connection $connection;
protected TransactionResult $transactionResult;

protected string $oxOrderId = '';

/**
* Helper Class for Currency Handling
*/
/** Helper Class for Currency Handling */
protected TeleCashCurrency $teleCashCurrency;

protected $_sClassName = 'OxidSolutionCatalysts\TeleCash\Application\TeleCashOrder';
protected $_sCoreTable = Module::TELECASH_ORDER_EXTENSION_TABLE;

/**
* Constructor for TeleCashOrder.
*
* Initializes a new instance of the TeleCashPayment class. This constructor can be used
* in both production and test environments due to its flexible parameter configuration.
*
* @param bool $initParent Whether to initialize the parent BaseModel.
* Set to false in test environment to avoid
* OXID framework dependencies. Default is true.
* @param string $oxOrderId orderID from OXID
* @param Connection|null $connection Optional database connection. If null, the connection
* will be retrieved from the service container.
* Primarily used for testing.
* @param bool $initParent Whether to initialize the parent BaseModel.
* Set to false in test environment to avoid
* OXID framework dependencies. Default is true.
*/
public function __construct(
string $oxOrderId,
?Connection $connection = null,
bool $initParent = true
) {
if ($initParent) {
parent::__construct();
}

$this->setContainer($this->getContainer());
$this->init($this->_sCoreTable);

$this->oxOrderId = $oxOrderId;
$this->teleCashCurrency = new TeleCashCurrency();
$this->transactionResult = new TransactionResult([]);

$this->init($this->_sCoreTable);
$this->setContainer($this->getContainer());
if ($connection !== null) {
$this->connection = $connection;
} else {
$connectionProvider = $this->getRequiredService(
ConnectionProviderInterface::class,
'ConnectionProviderInterface'
);
$this->connection = $connectionProvider->get();
}
}

/**
* Loads TeleCash-Order by using orderid instead of oxid.
*
* @param string $orderId content load ID
*
* @return bool
*/
public function loadByOrderId(string $orderId = ''): bool
{
$orderId = $orderId ?: $this->oxOrderId;

//getting at least one field before lazy loading the object
$this->addField('oxid', 0);

$table = $this->getViewName();

$query = $this->buildSelectString([
$table . '.' . Module::TELECASH_ORDER_EXTENSION_TABLE_OXORDERID => $orderId
]);

try {
$result = $this->connection->fetchAssociative($query);
if ($result !== false && is_array($result)) {
$this->assign($result);
$this->_isLoaded = true;
}
} catch (Exception) {
$this->_isLoaded = false;
}

return $this->isLoaded();
}

/**
Expand All @@ -83,10 +135,15 @@ public function getTxnType(): string
return $txnType;
}

/** get the Txn DateTime */
public function getTxnDateTime(): string
/**
* get the Txn DateTime
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function getTxnDateTime(): ?DateTime
{
return (string) $this->transactionResult->getValue('txndatetime');
$txnDateTime = (string) $this->transactionResult->getValue('txndatetime');
$result = DateTime::createFromFormat('Y:m:d-H:i:s', $txnDateTime);
return $result ?: null;
}

/** get the Oid */
Expand Down Expand Up @@ -132,7 +189,9 @@ public function getCurrency(): string
/** get the EndpointTransactionId */
public function getChargeTotal(): float
{
$chargeTotalString = $this->transactionResult->getValue('chargetotal');
$chargeTotalString = (string) $this->transactionResult->getValue('chargetotal');
// bulletproof because is_numeric does not recognize that German commas are numeric
$chargeTotalString = str_replace(',', '.', $chargeTotalString);
return is_numeric($chargeTotalString) ? (float) $chargeTotalString : 0.0;
}

Expand Down Expand Up @@ -187,6 +246,9 @@ public function save()
/** load the transaction */
public function loadTransactionResultFromDb(): void
{
if ($this->oxOrderId && !$this->isLoaded()) {
$this->loadByOrderId();
}
$data = $this->getFieldStringData(Module::TELECASH_ORDER_EXTENSION_TABLE_RESPONSE);

if (!empty($data)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Model/TeleCashPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function loadByPaymentId(string $paymentId = ''): bool
if (isset($result[Module::TELECASH_PAYMENT_EXTENSION_TABLE_IDENT])) {
$ident = $result[Module::TELECASH_PAYMENT_EXTENSION_TABLE_IDENT];
if ($ident === Module::TELECASH_PAYMENT_IDENT_DEFAULT) {
throw (new TeleCashException())->isNotTeleCashPayment();
throw (new TeleCashException())->isNotTeleCash();
}
$this->assign($result);
$this->_isLoaded = true;
Expand Down
Loading
Loading