Skip to content

Commit

Permalink
SUPESC-755 Implemented OMS state resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkicheglovspryker committed Nov 30, 2023
1 parent ce3129f commit 0113907
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 11 deletions.
1 change: 1 addition & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ paths:
tests: tests
support: .
log: tests/_output
output: tests/_output
data: tests/_data
envs: tests/_envs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SprykerEco\Zed\Unzer\Business\ApiAdapter\UnzerPaymentAdapterInterface;
use SprykerEco\Zed\Unzer\Business\Credentials\UnzerCredentialsResolverInterface;
use SprykerEco\Zed\Unzer\Business\Payment\Mapper\UnzerPaymentMapperInterface;
use SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolverInterface;
use SprykerEco\Zed\Unzer\Business\Payment\Updater\UnzerPaymentUpdaterInterface;
use SprykerEco\Zed\Unzer\Business\Reader\UnzerReaderInterface;
use SprykerEco\Zed\Unzer\UnzerConfig;
Expand All @@ -24,32 +25,37 @@ class UnzerNotificationProcessor implements UnzerNotificationProcessorInterface
/**
* @var \SprykerEco\Zed\Unzer\Business\ApiAdapter\UnzerPaymentAdapterInterface
*/
protected $unzerPaymentAdapter;
protected UnzerPaymentAdapterInterface $unzerPaymentAdapter;

/**
* @var \SprykerEco\Zed\Unzer\UnzerConfig
*/
protected $unzerConfig;
protected UnzerConfig $unzerConfig;

/**
* @var \SprykerEco\Zed\Unzer\Business\Reader\UnzerReaderInterface
*/
protected $unzerReader;
protected UnzerReaderInterface $unzerReader;

/**
* @var \SprykerEco\Zed\Unzer\Business\Payment\Mapper\UnzerPaymentMapperInterface
*/
protected $unzerPaymentMapper;
protected UnzerPaymentMapperInterface $unzerPaymentMapper;

/**
* @var \SprykerEco\Zed\Unzer\Business\Payment\Updater\UnzerPaymentUpdaterInterface
*/
protected $unzerPaymentUpdater;
protected UnzerPaymentUpdaterInterface $unzerPaymentUpdater;

/**
* @var \SprykerEco\Zed\Unzer\Business\Credentials\UnzerCredentialsResolverInterface
*/
protected $unzerCredentialsResolver;
protected UnzerCredentialsResolverInterface $unzerCredentialsResolver;

/**
* @var \SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolverInterface
*/
protected UnzerOmsStateResolverInterface $unzerOmsStateResolver;

/**
* @param \SprykerEco\Zed\Unzer\Business\ApiAdapter\UnzerPaymentAdapterInterface $unzerPaymentAdapter
Expand All @@ -58,21 +64,24 @@ class UnzerNotificationProcessor implements UnzerNotificationProcessorInterface
* @param \SprykerEco\Zed\Unzer\Business\Payment\Mapper\UnzerPaymentMapperInterface $unzerPaymentMapper
* @param \SprykerEco\Zed\Unzer\Business\Payment\Updater\UnzerPaymentUpdaterInterface $unzerPaymentUpdater
* @param \SprykerEco\Zed\Unzer\Business\Credentials\UnzerCredentialsResolverInterface $unzerCredentialsResolver
* @param \SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolverInterface $unzerOmsStateResolver
*/
public function __construct(
UnzerPaymentAdapterInterface $unzerPaymentAdapter,
UnzerConfig $unzerConfig,
UnzerReaderInterface $unzerReader,
UnzerPaymentMapperInterface $unzerPaymentMapper,
UnzerPaymentUpdaterInterface $unzerPaymentUpdater,
UnzerCredentialsResolverInterface $unzerCredentialsResolver
UnzerCredentialsResolverInterface $unzerCredentialsResolver,
UnzerOmsStateResolverInterface $unzerOmsStateResolver
) {
$this->unzerPaymentAdapter = $unzerPaymentAdapter;
$this->unzerConfig = $unzerConfig;
$this->unzerReader = $unzerReader;
$this->unzerPaymentMapper = $unzerPaymentMapper;
$this->unzerPaymentUpdater = $unzerPaymentUpdater;
$this->unzerCredentialsResolver = $unzerCredentialsResolver;
$this->unzerOmsStateResolver = $unzerOmsStateResolver;
}

/**
Expand Down Expand Up @@ -109,9 +118,7 @@ public function processNotification(UnzerNotificationTransfer $unzerNotification
return $unzerNotificationTransfer;
}

$orderItemStatus = $this->unzerConfig->mapUnzerEventToOmsStatus(
$unzerNotificationTransfer->getEventOrFail(),
);
$orderItemStatus = $this->unzerOmsStateResolver->getUnzerPaymentOmsStatus($unzerPaymentTransfer);

$this->unzerPaymentUpdater->updateUnzerPaymentDetails($unzerPaymentTransfer, $orderItemStatus);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver;

use Generated\Shared\Transfer\UnzerPaymentTransfer;
use SprykerEco\Zed\Unzer\UnzerConfig;
use SprykerEco\Zed\Unzer\UnzerConstants;

class UnzerOmsStateResolver implements UnzerOmsStateResolverInterface
{
protected UnzerConfig $unzerConfig;

/**
* @param \SprykerEco\Zed\Unzer\UnzerConfig $unzerConfig
*/
public function __construct(UnzerConfig $unzerConfig)
{
$this->unzerConfig = $unzerConfig;
}

/**
* @param \Generated\Shared\Transfer\UnzerPaymentTransfer $unzerPaymentTransfer
*
* @return string
*/
public function getUnzerPaymentOmsStatus(UnzerPaymentTransfer $unzerPaymentTransfer): string
{
$chargeUnzerTransactionTransfer = null;
$authorizeUnzerTransactionTransfer = null;
foreach ($unzerPaymentTransfer->getTransactions() as $unzerTransactionTransfer) {
if ($unzerTransactionTransfer->getTypeOrFail() === UnzerConstants::TRANSACTION_TYPE_CHARGE) {
$chargeUnzerTransactionTransfer = $unzerTransactionTransfer;

continue;
}

if ($unzerTransactionTransfer->getTypeOrFail() === UnzerConstants::TRANSACTION_TYPE_CHARGE) {
$authorizeUnzerTransactionTransfer = $unzerTransactionTransfer;
}
}

if ($chargeUnzerTransactionTransfer !== null) {
return $this->unzerConfig->mapUnzerChargePaymentStatusToOmsStatus($chargeUnzerTransactionTransfer->getStatusOrFail());
}

if ($authorizeUnzerTransactionTransfer !== null) {
return $this->unzerConfig->mapUnzerAuthorizePaymentStatusToOmsStatus($authorizeUnzerTransactionTransfer->getStatusOrFail());
}

return $this->unzerConfig->mapUnzerPaymentStatusToOmsStatus($unzerPaymentTransfer->getStateIdOrFail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver;

use Generated\Shared\Transfer\UnzerPaymentTransfer;

interface UnzerOmsStateResolverInterface
{
/**
* @param \Generated\Shared\Transfer\UnzerPaymentTransfer $unzerPaymentTransfer
*
* @return string
*/
public function getUnzerPaymentOmsStatus(UnzerPaymentTransfer $unzerPaymentTransfer): string;
}
50 changes: 50 additions & 0 deletions src/SprykerEco/Zed/Unzer/UnzerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ class UnzerConfig extends AbstractBundleConfig
UnzerConstants::UNZER_PAYMENT_STATUS_CHARGE_BACK => UnzerConstants::OMS_STATUS_PAYMENT_CHARGEBACK,
];

/**
* @var array<string, string>
*/
protected const UNZER_CHARGE_STATE_OMS_STATUS_MAP = [
UnzerConstants::TRANSACTION_STATUS_SUCCESS => UnzerConstants::OMS_STATUS_PAYMENT_COMPLETED,
UnzerConstants::TRANSACTION_STATUS_PENDING => UnzerConstants::OMS_STATUS_CHARGE_PENDING,
UnzerConstants::TRANSACTION_STATUS_ERROR => UnzerConstants::OMS_STATUS_CHARGE_FAILED,
];

/**
* @var array<string, string>
*/
protected const UNZER_AUTHORIZE_STATE_OMS_STATUS_MAP = [
UnzerConstants::TRANSACTION_STATUS_SUCCESS => UnzerConstants::OMS_STATUS_AUTHORIZE_SUCCEEDED,
UnzerConstants::TRANSACTION_STATUS_PENDING => UnzerConstants::OMS_STATUS_AUTHORIZE_PENDING,
UnzerConstants::TRANSACTION_STATUS_ERROR => UnzerConstants::OMS_STATUS_AUTHORIZE_FAILED,
];

/**
* @var array<string, string>
*/
Expand Down Expand Up @@ -367,6 +385,8 @@ public function isNotificationTypeEnabled(string $eventType): bool
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param string $unzerEvent
*
* @return string
Expand All @@ -376,6 +396,36 @@ public function mapUnzerEventToOmsStatus(string $unzerEvent): string
return static::UNZER_EVENT_OMS_STATUS_MAP[$unzerEvent];
}

/**
* Specification:
* - Maps Unzer charge transaction status to OMS status.
*
* @api
*
* @param string $transactionStatus
*
* @return string
*/
public function mapUnzerChargePaymentStatusToOmsStatus(string $transactionStatus): string
{
return static::UNZER_CHARGE_STATE_OMS_STATUS_MAP[$transactionStatus];
}

/**
* Specification:
* - Maps Unzer authorize transaction status to OMS status.
*
* @api
*
* @param string $transactionStatus
*
* @return string
*/
public function mapUnzerAuthorizePaymentStatusToOmsStatus(string $transactionStatus): string
{
return static::UNZER_AUTHORIZE_STATE_OMS_STATUS_MAP[$transactionStatus];
}

/**
* Specification:
* - Get OMS status `Authorize succeeded`.
Expand Down
10 changes: 10 additions & 0 deletions src/SprykerEco/Zed/Unzer/UnzerConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ interface UnzerConstants
*/
public const TRANSACTION_STATUS_SUCCESS = 'success';

/**
* @var string
*/
public const TRANSACTION_STATUS_PENDING = 'pending';

/**
* @var string
*/
public const TRANSACTION_STATUS_ERROR = 'error';

/**
* @var int
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEcoTest\Zed\Unzer\Business\Payment;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\UnzerPaymentTransfer;
use Generated\Shared\Transfer\UnzerTransactionTransfer;
use SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolver;
use SprykerEco\Zed\Unzer\UnzerConfig;
use SprykerEco\Zed\Unzer\UnzerConstants;

class UnzerOmsStateResolverTest extends Unit
{
/**
* @dataProvider unzerPaymentStatusDataProvider
*
* @param string $expectedOmsState
* @param \Generated\Shared\Transfer\UnzerPaymentTransfer $paymentTransfer
*
* @return void
*/
public function testShouldReturn(string $expectedOmsState, UnzerPaymentTransfer $paymentTransfer): void
{
// Act
$omsState = (new UnzerOmsStateResolver(new UnzerConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame($expectedOmsState, $omsState);
}

/**
* @return array<string, mixed>
*/
protected function unzerPaymentStatusDataProvider(): array
{
return [
'Payment status authorize pending' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_PENDING,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
),
],
'Payment status authorize success' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_SUCCEEDED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
),
],
'Payment status authorize failed' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_FAILED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
),
],
'Payment status charge failed' => [
UnzerConstants::OMS_STATUS_CHARGE_FAILED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_CANCELED)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_ERROR),
),
],
'Payment status payment completed' => [
UnzerConstants::OMS_STATUS_PAYMENT_COMPLETED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
*
* @SuppressWarnings(PHPMD)
* @SuppressWarnings(\SprykerEcoTest\Zed\Unzer\PHPMD)
*/
class UnzerBusinessTester extends Actor
{
Expand Down

0 comments on commit 0113907

Please sign in to comment.