Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Feb 17, 2025
1 parent b05c61c commit b3611ac
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ private function isPaymentCancelledAfterFirstTransaction(PaymentService $payment
$oTmpOrder = oxNew(TmpOrder::class);
$tmpOrderArray = $oTmpOrder->getTmpOrderByUnzerId($orderId);

if (!empty($tmpOrderArray)) {
return true;
if (count($tmpOrderArray) > 0) {
return false;
}

return true;
Expand Down
52 changes: 23 additions & 29 deletions src/Service/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OxidSolutionCatalysts\Unzer\Exception\RedirectWithMessage;
use OxidSolutionCatalysts\Unzer\PaymentExtensions\UnzerPayment as AbstractUnzerPayment;
use OxidSolutionCatalysts\Unzer\Service\Transaction as TransactionService;
use OxidSolutionCatalysts\Unzer\Traits\Request;
use stdClass;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\Payment as UnzerPayment;
Expand All @@ -42,6 +43,8 @@
*/
class Payment
{
use Request;

public const STATUS_OK = "OK";
public const STATUS_CANCELED = "CANCELED";
public const STATUS_NOT_FINISHED = "NOT_FINISHED";
Expand All @@ -65,28 +68,24 @@ class Payment

protected TransactionService $transactionService;

/**
* @param Session $session
* @param PaymentExtensionLoader $paymentExtLoader
* @param Translator $translator
* @param Unzer $unzerService
* @param UnzerSDKLoader $unzerSDKLoader
* @param TransactionService $transactionService
*/
private TmpOrderServiceInterface $tmpOrderService;

public function __construct(
Session $session,
PaymentExtensionLoader $paymentExtLoader,
Translator $translator,
Unzer $unzerService,
UnzerSDKLoader $unzerSDKLoader,
TransactionService $transactionService
TransactionService $transactionService,
TmpOrderServiceInterface $tmpOrderService
) {
$this->session = $session;
$this->paymentExtLoader = $paymentExtLoader;
$this->translator = $translator;
$this->unzerService = $unzerService;
$this->unzerSDKLoader = $unzerSDKLoader;
$this->transactionService = $transactionService;
$this->tmpOrderService = $tmpOrderService;
}

/**
Expand All @@ -99,8 +98,7 @@ public function __construct(
public function executeUnzerPayment(PaymentModel $paymentModel): bool
{
$paymentExtension = null;
/** @var string $customerType */
$customerType = Registry::getRequest()->getRequestParameter('unzer_customer_type', 'B2C');
$customerType = $this->getUnzerStringRequestParameter('unzer_customer_type');
$user = $this->session->getUser();
$basket = $this->session->getBasket();
$currency = $basket->getBasketCurrency()->name;
Expand All @@ -113,7 +111,7 @@ public function executeUnzerPayment(PaymentModel $paymentModel): bool
);

$oOrder = oxNew(Order::class);
/** @var UnzerOrderModel $oOrder */
/** @var Order $oOrder */
$oOrder->createTmpOrder($basket, $user, $paymentExtension->getUnzerOrderId());

$paymentExtension->execute(
Expand Down Expand Up @@ -141,9 +139,7 @@ public function executeUnzerPayment(PaymentModel $paymentModel): bool
);
} catch (Exception $e) {
throw new RedirectWithMessage(
$this->unzerService->prepareOrderRedirectUrl(
$paymentExtension instanceof AbstractUnzerPayment && $paymentExtension->redirectUrlNeedPending()
),
$this->unzerService->prepareOrderRedirectUrl(false),
$e->getMessage()
);
}
Expand Down Expand Up @@ -228,12 +224,6 @@ public function getUnzerOrderId(): string
return $result;
}

/**
* @param string $paymentId
* @param string $currency
* @param string $customerType
* @return \UnzerSDK\Unzer
*/
protected function getUnzerSDK(
string $paymentId = '',
string $currency = '',
Expand All @@ -246,10 +236,10 @@ protected function getUnzerSDK(
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getSessionUnzerPayment(bool $cache): ?UnzerPayment
public function getSessionUnzerPayment(bool $noCache = false): ?UnzerPayment
{
$result = null;
if ($cache === true) {
if ($noCache === false) {
if ($this->sessionUnzerPayment instanceof UnzerPayment) {
return $this->sessionUnzerPayment;
}
Expand All @@ -265,13 +255,17 @@ public function getSessionUnzerPayment(bool $cache): ?UnzerPayment
return null;
}

/** @var Order $order */
$order = oxNew(Order::class);
$order->load($sessionOrderId);
$order = $this->tmpOrderService->getOrderBySessionOrderId($sessionOrderId);

$paymentType = $this->getPaymentType($sessionOrderId, $order);
$currency = $this->getOrderCurrency($sessionOrderId, $order, $paymentType);
$customerType = $this->getCustomerType($currency, $paymentType);
$paymentType = '';
$currency = '';
$customerType = '';

if ($order !== null) {
$paymentType = $this->tmpOrderService->getPaymentType($sessionOrderId, $order);
$currency = $this->tmpOrderService->getOrderCurrency($sessionOrderId, $order, $paymentType);
$customerType = $this->tmpOrderService->getCustomerType($currency, $paymentType);
}

try {
$result = $this->unzerSDKLoader->getUnzerSDK(
Expand Down
5 changes: 2 additions & 3 deletions src/Service/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ protected function prepareTransactionOxid(array $params): string
{
unset($params['oxactiondate']);
unset($params['serialized_basket']);
unset($params['customertype']);

/** @var string $jsonEncode */
$jsonEncode = json_encode($params);
Expand Down Expand Up @@ -526,7 +525,7 @@ public function getCustomerTypeAndCurrencyByOrderId(string $orderId): array
$transaction->load($transactionId);

return [
'customertype' => $transaction->getFieldData('customertype') ?? '',
'customertype' => $transaction->getFieldData('customertype') ?? 'B2C',
'currency' => $transaction->getFieldData('currency') ?? '',
];
}
Expand Down Expand Up @@ -633,7 +632,7 @@ public function getSavedPaymentsForUser(?User $user, array $ids, bool $cache): a
$user,
$paymentTypeId,
$typeData['currency'] ?: '',
$typeData['customertype'] ?: '',
!empty($typeData['customertype']) ? $typeData['customertype'] : 'B2C',
$paymentTypeId
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Service/UnzerSDKLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function __construct(
public function getUnzerSDK(string $paymentId = '', string $currency = '', string $customerType = ''): Unzer
{
if (UnzerDefinitions::INVOICE_UNZER_PAYMENT_ID === $paymentId) {
if (empty($customerType) || empty($currency)) {
throw new UnzerException('Customer type and currency must be set for invoice payment');
}
$key = $this->moduleSettings->getInvoicePrivateKeyByCustomerTypeAndCurrency(
$customerType,
$currency
Expand All @@ -79,8 +82,7 @@ public function getUnzerSDK(string $paymentId = '', string $currency = '', strin
$sdk = $this->getUnzerSDKbyKey($key);
} catch (UnzerException $e) {
$logEntry = sprintf(
'Try to get the SDK with the Key "%s" defined by paymentId "%s", currency "%s", customerType "%s" '
. $e->getTraceAsString(),
'Try to get the SDK with the Key "%s" defined by paymentId "%s", currency "%s", customerType "%s"',
$key,
$paymentId,
$currency,
Expand Down
1 change: 1 addition & 0 deletions tests/Codeception/Config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'MYSQL_CONFIG_PATH' => getMysqlConfigPath(),
'SELENIUM_SERVER_PORT' => getenv('SELENIUM_SERVER_PORT') ?: '4444',
'SELENIUM_SERVER_IP' => getenv('SELENIUM_SERVER_IP') ?: 'selenium',
'SELENIUM_SERVER_HOST' => getenv('SELENIUM_SERVER_HOST') ?: 'selenium',
'BROWSER_NAME' => getenv('BROWSER_NAME') ?: 'chrome',
'PHP_BIN' => $phpBinEnv,
'SCREEN_SHOT_URL' => $screenShotPathEnv
Expand Down
4 changes: 2 additions & 2 deletions tests/Codeception/acceptance.suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ modules:
populate: true # run populator before all tests
cleanup: false # run populator before each test#
populator: >
./bin/oe-console oe:setup:demodata &&
./bin/oe-console oe:module:activate osc-unzer &&
bin/oe-console oe:setup:demodata &&
bin/oe-console oe:module:activate osc-unzer &&
mysql --defaults-file=$mysql_config --default-character-set=utf8 $dbname < $module_dump
- \OxidEsales\Codeception\Module\Oxideshop:
screen_shot_url: '%SCREEN_SHOT_URL%'
Expand Down

0 comments on commit b3611ac

Please sign in to comment.