Skip to content

Commit

Permalink
UNZER-523 Unzer order model inheritance chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Nov 27, 2024
1 parent a0d76c2 commit ef6b2fb
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 115 deletions.
23 changes: 3 additions & 20 deletions src/Controller/Admin/ModuleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ModuleConfiguration extends ModuleConfiguration_parent
{
Expand Down Expand Up @@ -193,10 +194,6 @@ public function transferApplePayPaymentProcessingData(): void
}
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
public function getApplePayPaymentProcessingKeyExists(): bool
{
$keyId = $this->moduleSettings->getApplePayPaymentKeyId();
Expand All @@ -215,10 +212,6 @@ public function getApplePayPaymentProcessingKeyExists(): bool
return false;
}

/**
* @throws GuzzleException
* @throws FileException
*/
public function getApplePayPaymentProcessingCertExists(): bool
{
$certId = $this->moduleSettings->getApplePayPaymentCertificateId();
Expand All @@ -242,11 +235,10 @@ public function getApplePayPaymentProcessingCertExists(): bool
}

/**
* @return void
* @throws FileException
* @SuppressWarnings(PHPMD.StaticAccess)
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
public function saveConfVars()
public function saveConfVars(): void
{
$moduleId = $this->getUnzerStringRequestEscapedParameter('oxid');
if ($moduleId === Module::MODULE_ID) {
Expand All @@ -269,9 +261,6 @@ public function saveConfVars()
parent::saveConfVars();
}

/**
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
private function saveMerchantKey(string $systemMode): void
{
$errorIds = [
Expand Down Expand Up @@ -318,9 +307,6 @@ private function saveMerchantCert(string $systemMode): void
}
}

/**
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
private function savePaymentKey(string $systemMode): void
{
$errorIds = [
Expand All @@ -343,9 +329,6 @@ private function savePaymentKey(string $systemMode): void
}
}

/**
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
private function savePaymentCert(string $systemMode): void
{
$errorIds = [
Expand Down
4 changes: 0 additions & 4 deletions src/Controller/Admin/OrderMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ protected function onOrderSend()
}
}

/**
* @param Order $oOrder
* @return void
*/
public function sendShipmentNotification(Order $oOrder): void
{
$paymentService = $this->getServiceFromContainer(PaymentService::class);
Expand Down
31 changes: 26 additions & 5 deletions src/Controller/DispatcherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,33 @@ private function updateOrder(Order $order, Payment $unzerPayment, string $paymen
return $this->translator->translate('oscunzer_TRANSACTION_NOTHINGTODO') . $paymentId;
}

/**
* @throws \Exception
*/
public function finalizeTmpOrder(
Payment $unzerPayment,
TmpOrder $tmpOrder,
array $tmpData,
bool $bError
): string {
$return = $this->returnError();
if ($tmpOrder->load($tmpData['OXID'])) {
if ($tmpOrder->load($tmpData['oxid'])) {
$flexibleSerializer = $this->getFlexibleSerializer();
$oOrder = $flexibleSerializer->safeUnserialize($tmpData['tmporder'], [Order::class]);

/** @var Order $oOrder */
$oOrder = $flexibleSerializer->safeUnserialize(
$tmpData['tmporder'],
[
Order::class
]
);

if ($oOrder instanceof \stdClass || $oOrder === false) {
$oOrder = $flexibleSerializer->restoreOrderFromStrClass($tmpData['tmporder']);
}

if ($oOrder instanceof Order) {
if (!is_null($oOrder) && method_exists($oOrder, 'finalizeTmpOrder')) {
/** @var \OxidSolutionCatalysts\Unzer\Model\Order $oOrder */
$oOrder->finalizeTmpOrder($unzerPayment, $bError);
$tmpOrder->assign(['status' => 'FINISHED']);
$tmpOrder->save();
Expand Down Expand Up @@ -229,15 +240,25 @@ protected function getTransactionService(): Transaction
return $this->getServiceFromContainer(Transaction::class);
}

protected function returnError(): string
{
return $this->translator->translate('oscunzer_ERROR_HANDLE_TMP_ORDER');
}

protected function returnSuccess(): string
{
return $this->translator->translate('oscunzer_SUCCESS_HANDLE_TMP_ORDER');
}

private function handleTmpOrder(Payment $unzerPayment): string
{
$tmpOrder = oxNew(TmpOrder::class);
$orderId = $unzerPayment->getBasket() ? $unzerPayment->getBasket()->getOrderId() : '';
$tmpData = $tmpOrder->getTmpOrderByUnzerId($orderId);

if (
isset($tmpData['OXID']) &&
$tmpOrder->load($tmpData['OXID']) &&
isset($tmpData['oxid']) &&
$tmpOrder->load($tmpData['oxid']) &&
$this->hasExceededTimeLimit($tmpOrder)
) {
$bError = !(
Expand Down
29 changes: 10 additions & 19 deletions src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,30 +272,26 @@ public function setUnzerTransId(string $sTransId): void
}

/**
* @param \UnzerSDK\Resources\Payment|null $unzerPayment
* @return bool
* @throws UnzerApiException
* @throws Exception
*/
public function initWriteTransactionToDB($unzerPayment = null): bool
public function initWriteTransactionToDB(\UnzerSDK\Resources\Payment|null $unzerPayment = null): void
{
/** @var string $oxpaymenttype */
$oxpaymenttype = $this->getFieldData('oxpaymenttype');
if (strpos($oxpaymenttype, "oscunzer") !== false) {
if ($oxpaymenttype !== null && str_contains($oxpaymenttype, "oscunzer")) {
$transactionService = $this->getServiceFromContainer(TransactionService::class);

$unzerPayment = $unzerPayment instanceof \UnzerSDK\Resources\Payment ?
$unzerPayment :
$this->getServiceFromContainer(PaymentService::class)->getSessionUnzerPayment(true);

return $transactionService->writeTransactionToDB(
$transactionService->writeTransactionToDB(
$this->getId(),
$this->getOrderUser()->getId() ?: '',
$unzerPayment
);
}

return false;
}

/**
Expand All @@ -304,9 +300,9 @@ public function initWriteTransactionToDB($unzerPayment = null): bool
public function getUnzerInvoiceNr()
{
/** @var int $number */
$number = $this->getFieldData('OXINVOICENR') !== 0 ?
$this->getFieldData('OXINVOICENR') :
$this->getFieldData('OXORDERNR');
$number = $this->getFieldData('oxinvoicenr') !== 0 ?
$this->getFieldData('oxinvoicenr') :
$this->getFieldData('oxordernr');
return $number;
}

Expand Down Expand Up @@ -334,22 +330,17 @@ public function delete($sOxId = null)
return parent::delete($sOxId);
}

/**
* @param string $fieldName
* @param string $value
* @param int $dataType
* @return false|void
*/
public function setFieldData($fieldName, $value, $dataType = Field::T_TEXT)

public function setFieldData($fieldName, $value, $dataType = Field::T_TEXT): void
{
return parent::setFieldData($fieldName, $value, $dataType);
parent::setFieldData($fieldName, $value, $dataType);
}

private function setTmpOrderStatus(string $unzerOrderId, string $status): void
{
$tmpOrder = oxNew(TmpOrder::class);
$tmpData = $tmpOrder->getTmpOrderByUnzerId($unzerOrderId);
if ($tmpOrder->load($tmpData['OXID'])) {
if ($tmpOrder->load($tmpData['oxid'])) {
$tmpOrder->assign(['status' => $status]);
$tmpOrder->save();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/TmpOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function getTmpOrderByOxOrderId(string $oxSessionOrderId): ?CoreOrderMode
$rawRes = $queryBuilder->execute();
$result = $rawRes->fetchAssociative();

if (is_array($result) && isset($result['TMPORDER']) && is_string($result['TMPORDER'])) {
$tmpOrder = $result['TMPORDER'];
if (is_array($result) && isset($result['tmporder']) && is_string($result['tmporder'])) {
$tmpOrder = $result['tmporder'];
$result = unserialize(base64_decode($tmpOrder));
if (is_array($result) && isset($result['order']) && is_object($result['order'])) {
/** @var CoreOrderModel $order */
Expand Down
40 changes: 20 additions & 20 deletions src/Service/FlexibleSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace OxidSolutionCatalysts\Unzer\Service;

use Exception;
use OxidEsales\Eshop\Application\Model\Order;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use stdClass;

class FlexibleSerializer
Expand All @@ -34,7 +33,7 @@ public function safeSerialize($object): string
* @param array $allowedClasses An array of fully qualified class names that are allowed to be unserialized.
* @return mixed The unserialized data.
*/
public function safeUnserialize($serialized, array $allowedClasses = [])
public function safeUnserialize(string $serialized, array $allowedClasses = [])
{
$unserializedData = unserialize(
$serialized,
Expand All @@ -54,16 +53,27 @@ public function restoreOrderFromStrClass(string $serialized): ?Order
$unserializedData = unserialize(
$serialized,
[
'allowed_classes' => [stdClass::class, Order::class]
'allowed_classes' => [stdClass::class, Order ::class]
]
);

/** @var \OxidSolutionCatalysts\Unzer\Model\Order $order */
$order = $this->getOrderModel();

/**
* @var string $value
*/
foreach (get_object_vars($unserializedData) as $property => $value) {
if (property_exists($order, $property) || method_exists($order, 'setFieldData')) {
if (
property_exists($order, $property)
|| method_exists($order, 'setFieldData')
) {
$property = str_replace("\0", '', $property);
if (method_exists($order, 'setFieldData')) {
$order->setFieldData($property, $value);
$reflectionMethod = new ReflectionMethod($order, 'setFieldData');
if ($reflectionMethod->isPublic()) {
$order->setFieldData($property, $value);
}
}
$order->$property = $value;
}
Expand Down Expand Up @@ -106,11 +116,6 @@ private function makeSerializable($data)
}

/**
* Restore unserializable data, including objects of allowed classes.
*
* @param mixed $data The data to be restored.
* @param array $allowedClasses An array of fully qualified class names that are allowed to be restored.
* @return mixed The restored data.
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ElseExpression)
*/
Expand All @@ -125,17 +130,11 @@ private function restoreUnserializable(mixed $data, array $allowedClasses): mixe
if (is_object($data) && isset($data->__class)) {
$className = $data->__class;
if ($this->isAllowedClass($className, $allowedClasses)) {
$reflection = new ReflectionClass($className);
$restored = $reflection->newInstanceWithoutConstructor();
$restored = new $className();
foreach (get_object_vars($data) as $key => $value) {
if ($key !== '__class') {
if ($reflection->hasProperty($key)) {
$property = $reflection->getProperty($key);
$property->setAccessible(true);
$property->setValue($restored, $this->restoreUnserializable($value, $allowedClasses));
} else {
$restored->$key = $this->restoreUnserializable($value, $allowedClasses);
}
$key = str_replace("\0", '', $key);
$restored->$key = $this->restoreUnserializable($value, $allowedClasses);
}
}
return $restored;
Expand All @@ -146,6 +145,7 @@ private function restoreUnserializable(mixed $data, array $allowedClasses): mixe
$restored = new stdClass();
foreach (get_object_vars($data) as $key => $value) {
if ($key !== '__class') {
$key = str_replace("\0", '', $key);
$restored->$key = $this->restoreUnserializable($value, $allowedClasses);
}
}
Expand Down
32 changes: 10 additions & 22 deletions src/Service/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,11 @@ public function doUnzerCollect(
return true;
}

/**
* @param UnzerOrderModel|null $oOrder
* @param string $unzerid
* @param float $amount
* @return UnzerApiException|bool
*/
public function doUnzerAuthorizationCancel($oOrder, $unzerid, $amount)
{
public function doUnzerAuthorizationCancel(
UnzerOrderModel|null $oOrder,
string $unzerid,
float $amount
): UnzerApiException|bool {
if (!($oOrder instanceof Order)) {
return false;
}
Expand All @@ -475,14 +472,12 @@ public function doUnzerAuthorizationCancel($oOrder, $unzerid, $amount)
}

/**
* @param UnzerOrderModel|null $oOrder
* @param string $sPaymentId
* @return UnzerApiException|bool
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function sendShipmentNotification($oOrder, $sPaymentId = null)
{
public function sendShipmentNotification(
?Order $oOrder,
string $sPaymentId = null
): Exception|bool|UnzerApiException {
if (!($oOrder instanceof Order)) {
return false;
}
Expand Down Expand Up @@ -545,11 +540,7 @@ public function sendShipmentNotification($oOrder, $sPaymentId = null)
return $blSuccess;
}

/**
* @param \UnzerSDK\Resources\Payment $unzerPayment
* @return BasePaymentType|AbstractUnzerResource The updated PaymentType object.
*/
public function setInstallmentDueDate($unzerPayment)
public function setInstallmentDueDate(UnzerPayment $unzerPayment): BasePaymentType|AbstractUnzerResource
{
/** @var InstallmentSecured $installment */
$installment = $unzerPayment->getPaymentType();
Expand All @@ -573,9 +564,6 @@ public function getTomorrowsTimestamp(): string
return date('Y-m-d', strtotime("+1 days"));
}

/**
* @return bool
*/
public function isPdfSession(): bool
{
return (bool)Registry::getRequest()->getRequestParameter('pdfConfirm', '0');
Expand Down
Loading

0 comments on commit ef6b2fb

Please sign in to comment.