Skip to content

Commit

Permalink
phpStan & CodeStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
mariolorenz committed Apr 4, 2024
1 parent 06979f3 commit 6cafb57
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
42 changes: 25 additions & 17 deletions src/Controller/DispatcherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ public function updatePaymentTransStatus(): void
}
} else {
$tmpOrder = oxNew(TmpOrder::class);
$tmpData = $tmpOrder->getTmpOrderByUnzerId($unzerPayment->getBasket()->getOrderId());
if ($tmpOrder->load($tmpData['OXID'])) {
if ($this->hasExceededTimeLimit($tmpOrder)) {
$bError = !($unzerPayment->getState() === PaymentState::STATE_COMPLETED ||
$unzerPayment->getState() === PaymentState::STATE_CANCELED ||
$unzerPayment->getState() === PaymentState::STATE_PENDING);
$this->handleTmpOrder($unzerPayment, $tmpOrder, $tmpData, $bError);
}
$iOrderId = $unzerPayment->getBasket() ? (int) $unzerPayment->getBasket()->getOrderId() : 0;
$tmpData = $tmpOrder->getTmpOrderByUnzerId($iOrderId);
if (
isset($tmpData['OXID']) &&
$tmpOrder->load($tmpData['OXID']) &&
$this->hasExceededTimeLimit($tmpOrder)
) {
$bError = !($unzerPayment->getState() === PaymentState::STATE_COMPLETED ||
$unzerPayment->getState() === PaymentState::STATE_CANCELED ||
$unzerPayment->getState() === PaymentState::STATE_PENDING);
$this->handleTmpOrder($unzerPayment, $tmpOrder, $tmpData, $bError);
}
}
}
Expand All @@ -152,21 +155,26 @@ public function updatePaymentTransStatus(): void
* @param Payment $unzerPayment
* @param TmpOrder $tmpOrder
* @param array $tmpData
* @param bool $error
* @return void
* @throws Exception
*/
protected function handleTmpOrder(Payment $unzerPayment, TmpOrder $tmpOrder, array $tmpData, $error = false): void
protected function handleTmpOrder(Payment $unzerPayment, TmpOrder $tmpOrder, array $tmpData, bool $error = false): void
{
$translator = $this->getServiceFromContainer(Translator::class);
$result = $translator->translate('oscunzer_ERROR_HANDLE_TMP_ORDER');
if ($tmpOrder->load($tmpData['OXID'])) {
$aOrderData = unserialize(base64_decode($tmpData['TMPORDER']), ['allowed_classes' => [Order::class]]);
/** @var \OxidSolutionCatalysts\Unzer\Model\Order $oOrder */
$oOrder = $aOrderData['order'];
$oOrder->finalizeTmpOrder($unzerPayment, $error);
$tmpOrder->assign(['STATUS' => 'FINISHED']);
$tmpOrder->save();
Registry::getUtils()->setHeader('HTTP/1.1 200 OK');
Registry::getUtils()->showMessageAndExit('200');
$oOrder = is_array($aOrderData) && isset($aOrderData['order']) ? $aOrderData['order'] : null;
if ($oOrder) {
$oOrder->finalizeTmpOrder($unzerPayment, $error);
$tmpOrder->assign(['STATUS' => 'FINISHED']);
$tmpOrder->save();
$result = $translator->translate('oscunzer_SUCCESS_HANDLE_TMP_ORDER');
}
}
Registry::getUtils()->showMessageAndExit($result);
}

/**
Expand All @@ -175,9 +183,9 @@ protected function handleTmpOrder(Payment $unzerPayment, TmpOrder $tmpOrder, arr
*/
protected function hasExceededTimeLimit(TmpOrder $tmpOrder): bool
{
$UnzerWebhookTimeDifference = (int) Registry::getConfig()->getConfigParam('UnzerWebhookTimeDifference', 5);
$UnzerWebhookTimeDifference = Registry::getConfig()->getConfigParam('UnzerWebhookTimeDifference', 5);
$TimeDifferenceSeconds = $UnzerWebhookTimeDifference * 60;
$tmpOrderTime = $tmpOrder->getFieldData('TIMESTAMP');
$tmpOrderTime = is_string($tmpOrder->getFieldData('TIMESTAMP')) ? $tmpOrder->getFieldData('TIMESTAMP') : '';
$tmpOrderTimeUnix = strtotime($tmpOrderTime);
$nowTimeUnix = time();
$difference = $nowTimeUnix - $tmpOrderTimeUnix;
Expand Down
14 changes: 8 additions & 6 deletions src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ protected function checkForUnzerPaymentErrors(): void
$payment->load($actualPaymentId) &&
$payment->isUnzerPayment()
) {
/** @var string $orderId */
if ($orderId = $session->getVariable('sess_challenge')) {
$orderId = is_string($session->getVariable('sess_challenge')) ? $session->getVariable('sess_challenge') : '';
if ($orderId) {
$order = oxNew(Order::class);
$order->delete($orderId);
$session->deleteVariable('sess_challenge');
Expand All @@ -116,12 +116,14 @@ protected function checkForUnzerPaymentErrors(): void
* @param $session Session
* @return void
*/
protected function checkForDuplicateOrderAttempt($session)
protected function checkForDuplicateOrderAttempt(Session $session)
{
$unzerSDK = $this->getServiceFromContainer(UnzerSDKLoader::class);
$unzerSDK = $unzerSDK->getUnzerSDK();
if ($session->getVariable('settedOrder')) {
if ($paymentId = $session->getVariable('paymentid')) {
$oxOrderIdOfTmpOrder = $session->getVariable('oxOrderIdOfTmpOrder');
$paymentId = is_string($session->getVariable('paymentid')) ? $session->getVariable('paymentid') : '';
if ($oxOrderIdOfTmpOrder) {
if ($paymentId) {
try {
$unzerPayment = $unzerSDK->fetchPayment($paymentId);
$unzerOrderId = $unzerPayment->getOrderId();
Expand All @@ -141,7 +143,7 @@ protected function checkForDuplicateOrderAttempt($session)
}
}
$session->deleteVariable('sess_challenge');
$session->deleteVariable('settedOrder');
$session->deleteVariable('oxOrderIdOfTmpOrder');
}
}
}
9 changes: 8 additions & 1 deletion src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public function finalizeUnzerOrderAfterRedirect(

return $iRet;
}

/**
* @param Basket $oBasket
* @param User $oUser
* @return int|bool
* @throws Exception
*/
public function createTmpOrder(
Basket $oBasket,
User $oUser
Expand Down Expand Up @@ -177,7 +184,7 @@ public function createTmpOrder(
$tmpOrder = oxNew(TmpOrder::class);
$tmpOrder->prepareOrderForJson($this);
$tmpOrder->save();
Registry::getSession()->setVariable('settedOrder', $this->getId());
Registry::getSession()->setVariable('oxOrderIdOfTmpOrder', $this->getId());

return $iRet;
}
Expand Down
27 changes: 13 additions & 14 deletions src/Model/TmpOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Driver\Result;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use OxidEsales\Eshop\Application\Model\Order as CoreOrderModel;

class TmpOrder extends BaseModel
{
Expand All @@ -36,14 +37,15 @@ public function save()
return parent::save();
}

public function prepareOrderForJson($oOrder)
public function prepareOrderForJson(CoreOrderModel $oOrder): void
{
$oConfig = $this->getConfig();
$oConfig = Registry::getConfig();
$oOrderArticles = $oOrder->getOrderArticles();
$completeOrder['order'] = $oOrder;
$completeOrder['orderArticles'] = $oOrderArticles->getArray();
$serializedOrder = serialize($completeOrder);
$base64Order = base64_encode($serializedOrder);
/** @var Order $oOrder */
$this->assign(
[
'OXSHOPID' => $oConfig->getShopId(),
Expand All @@ -57,13 +59,12 @@ public function prepareOrderForJson($oOrder)
}

/**
* @param $unzerOrderNr
* @return false|mixed
* @param int $unzerOrderNr
* @return array
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \Doctrine\DBAL\Driver\Exception
*/
public function getTmpOrderByUnzerId($unzerOrderNr)
public function getTmpOrderByUnzerId(int $unzerOrderNr): array
{
$queryBuilderFactory = $this->getServiceFromContainer(QueryBuilderFactoryInterface::class);
/** @var QueryBuilder $queryBuilder */
Expand All @@ -77,11 +78,9 @@ public function getTmpOrderByUnzerId($unzerOrderNr)
->setParameters(
['OXUNZERORDERNR' => $unzerOrderNr]
);
/** @var Result $blocksData */
$blocksData = $queryBuilder->execute();
$result = $blocksData->fetch();
if ($result) {
return $result;
}
return false;
$result = is_a($blocksData, Result::class) ? $blocksData->fetchAssociative() : false;
return is_array($result) ? $result : [];
}
}
1 change: 1 addition & 0 deletions src/Service/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function executeUnzerPayment(PaymentModel $paymentModel): bool
$currency = $basket->getBasketCurrency()->name;

$oOrder = oxNew(Order::class);
/** @var \OxidSolutionCatalysts\Unzer\Model\Order $oOrder */
$oOrder->createTmpOrder($basket, $user);

$paymentExtension = $this->paymentExtLoader->getPaymentExtensionByCustomerTypeAndCurrency(
Expand Down
4 changes: 2 additions & 2 deletions src/Service/StaticContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function ensureUnzerPaymentMethods(): void

protected function checkAndDeactivatePaymentMethod(array $paymentDefinition, EshopModelPayment $paymentMethod): void
{
if (!$paymentDefinition['active'] && (string) $paymentMethod->getFieldData('oxactive') === '1') {
$paymentMethod->oxpayments__oxactive = new Field('0', Field::T_RAW);
if (!$paymentDefinition['active'] && $paymentMethod->getFieldData('oxactive')) {
$paymentMethod->assign(['oxpayments__oxactive' => '0']);
$paymentMethod->save();
}
}
Expand Down
4 changes: 3 additions & 1 deletion translations/de/oscunzer_errors_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,5 +639,7 @@
'oscunzer_TRANSACTION_CHANGE' => 'Status %s für Payment %s wurde in die DB geschrieben',
'oscunzer_TRANSACTION_NOTHINGTODO' => 'Kein Update erforderlich. Es gab keinen neuen Zahlungsstatus: ',
'oscunzer_APPLEPAY_ERROR' => 'Zahlung mit Apple Pay konnte nicht ausgeführt werden.',
'oscunzer_ERROR_CHANGE_BASKET' => 'Leider hat sich der Warenkorb während des Checkouts noch einmal verändert. Bitte prüfen Sie den Warenkorb und wiederholen den Vorgang.'
'oscunzer_ERROR_CHANGE_BASKET' => 'Leider hat sich der Warenkorb während des Checkouts noch einmal verändert. Bitte prüfen Sie den Warenkorb und wiederholen den Vorgang.',
'oscunzer_ERROR_HANDLE_TMP_ORDER' => 'Behandlung der temporären Bestellung nicht erfolgreich.',
'oscunzer_SUCCESS_HANDLE_TMP_ORDER' => 'Behandlung der temporären Bestellung erfolgreich.',
];
4 changes: 3 additions & 1 deletion translations/en/oscunzer_errors_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,5 +638,7 @@
'oscunzer_TRANSACTION_CHANGE' => 'State %s was written to database for payment %s',
'oscunzer_TRANSACTION_NOTHINGTODO' => 'No update needed. There was no new state for payment: ',
'oscunzer_APPLEPAY_ERROR' => 'Failed to complete payment with Apple Pay.',
'oscunzer_ERROR_CHANGE_BASKET' => 'Unfortunately the shopping cart changed again during checkout. Please check the shopping cart and repeat the process.'
'oscunzer_ERROR_CHANGE_BASKET' => 'Unfortunately the shopping cart changed again during checkout. Please check the shopping cart and repeat the process.',
'oscunzer_ERROR_HANDLE_TMP_ORDER' => 'The temporary order was not handled successfully.',
'oscunzer_SUCCESS_HANDLE_TMP_ORDER' => 'The temporary order was handled successfully.',
];

0 comments on commit 6cafb57

Please sign in to comment.