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

SUPESC-755 Fixed Unzer notifications processing order. #35

10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
jobs:
setup:
name: Setup Database MariaDB
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
Expand All @@ -44,14 +44,14 @@ jobs:
ci:
name: Unzer (PHP ${{ matrix.php-versions }})
needs: setup
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

strategy:
fail-fast: false
matrix:
php-versions:
- '8.0'
- '8.1'
- '8.2'

services:
mariadb:
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:

lowest:
name: Unzer Prefer Lowest (PHP ${{ matrix.php-versions }})
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

steps:
- name: Checkout
Expand All @@ -117,7 +117,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
extensions: mbstring, intl, bcmath, pdo_mysql

- name: Composer Install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![CI](https://github.com/spryker-eco/unzer/actions/workflows/ci.yml/badge.svg)](https://github.com/spryker-eco/unzer/actions/workflows/ci.yml)
[![Latest Stable Version](https://poser.pugx.org/spryker-eco/unzer/v/stable.svg)](https://packagist.org/packages/spryker-eco/unzer)
[![License](https://img.shields.io/github/license/spryker-eco/unzer.svg?b=master)](https://github.com/spryker-eco/unzer)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg)](https://php.net/)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)

Unzer module provides main business logic for Unzer regular and marketplace payment system integrations.

Expand Down
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Unzer module",
"license": "MIT",
"require": {
"php": ">=8.0",
"php": ">=8.1",
"spryker-eco/unzer-api": "^1.1.0",
"spryker-shop/shop-ui": "^1.56.0",
"spryker/calculation": "^4.0.0",
Expand All @@ -31,8 +31,8 @@
"spryker/zed-request": "^3.0.0"
},
"require-dev": {
"codeception/module-asserts": "^1.3.0",
"phpstan/phpstan": "^1.2.0",
"codeception/module-asserts": "*",
"phpstan/phpstan": "*",
"spryker/application": "*",
"spryker/code-sniffer": "*",
"spryker/customer": "*",
Expand All @@ -44,7 +44,7 @@
"spryker/queue": "*",
"spryker/router": "*",
"spryker/state-machine": "*",
"spryker/testify": "^3.43.0"
"spryker/testify": "*"
},
"suggest": {
"spryker/checkout": "Use this module if you want to use Checkout plugins.",
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,105 @@
<?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\Business\Exception\UnzerException;
use SprykerEco\Zed\Unzer\UnzerConfig;
use SprykerEco\Zed\Unzer\UnzerConstants;

class UnzerOmsStateResolver implements UnzerOmsStateResolverInterface
{
/**
* @var \SprykerEco\Zed\Unzer\UnzerConfig
*/
protected UnzerConfig $unzerConfig;
DmitryLymarenko marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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_AUTHORIZE) {
$authorizeUnzerTransactionTransfer = $unzerTransactionTransfer;
}
}

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

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

return $this->mapUnzerPaymentStateToOmsStatus($unzerPaymentTransfer->getStateIdOrFail());
}

/**
* @param array<string, string> $unzerPaymentStatusToOmsStatusMap
* @param string $unzerPaymentStatus
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return string
*/
protected function mapUnzerPaymentTransactionStatusToOmsStatus(array $unzerPaymentStatusToOmsStatusMap, string $unzerPaymentStatus): string
{
if (!isset($unzerPaymentStatusToOmsStatusMap[$unzerPaymentStatus])) {
$message = sprintf('Undefined payment transaction status provided by Unzer: %s', $unzerPaymentStatus);

throw new UnzerException($message);
}

return $unzerPaymentStatusToOmsStatusMap[$unzerPaymentStatus];
}

/**
* @param int $unzerPaymentStateId
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return string
*/
protected function mapUnzerPaymentStateToOmsStatus(int $unzerPaymentStateId): string
{
$paymentStateToOmsStatusMap = $this->unzerConfig->getUnzerPaymentStateToOmsStatusMap();
if (!isset($paymentStateToOmsStatusMap[$unzerPaymentStateId])) {
$message = sprintf('Undefined payment state provided by Unzer: %s', $unzerPaymentStateId);

throw new UnzerException($message);
}

return $paymentStateToOmsStatusMap[$unzerPaymentStateId];
}
}
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;
}
13 changes: 13 additions & 0 deletions src/SprykerEco/Zed/Unzer/Business/UnzerBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
use SprykerEco\Zed\Unzer\Business\Payment\Filter\UnzerPaymentMethodFilterInterface;
use SprykerEco\Zed\Unzer\Business\Payment\Mapper\UnzerPaymentMapper;
use SprykerEco\Zed\Unzer\Business\Payment\Mapper\UnzerPaymentMapperInterface;
use SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolver;
use SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolverInterface;
use SprykerEco\Zed\Unzer\Business\Payment\Processor\Charge\UnzerChargeProcessorInterface;
use SprykerEco\Zed\Unzer\Business\Payment\Processor\Charge\UnzerCreditCardChargeProcessor;
use SprykerEco\Zed\Unzer\Business\Payment\Processor\Charge\UnzerMarketplaceCreditCardChargeProcessor;
Expand Down Expand Up @@ -403,6 +405,17 @@ public function createUnzerNotificationProcessor(): UnzerNotificationProcessorIn
$this->createUnzerPaymentMapper(),
$this->createUnzerPaymentUpdater(),
$this->createUnzerCredentialsResolver(),
$this->createUnzerOmsStateResolver(),
);
}

/**
* @return \SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolverInterface
*/
public function createUnzerOmsStateResolver(): UnzerOmsStateResolverInterface
{
return new UnzerOmsStateResolver(
$this->getConfig(),
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/SprykerEco/Zed/Unzer/Business/UnzerFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer
*
* @param \Generated\Shared\Transfer\UnzerNotificationTransfer $notificationTransfer
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return \Generated\Shared\Transfer\UnzerNotificationTransfer
*/
public function processNotification(UnzerNotificationTransfer $notificationTransfer): UnzerNotificationTransfer
Expand Down
3 changes: 3 additions & 0 deletions src/SprykerEco/Zed/Unzer/Business/UnzerFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer
* - Checks if provided Unzer notification is enabled.
* - Processes Unzer notification.
* - Updates payment details in DB.
* - Throws `UnzerException` if payment status cannot be mapped to OMS status correctly.
*
* @api
*
* @param \Generated\Shared\Transfer\UnzerNotificationTransfer $notificationTransfer
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return \Generated\Shared\Transfer\UnzerNotificationTransfer
*/
public function processNotification(UnzerNotificationTransfer $notificationTransfer): UnzerNotificationTransfer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class UnzerMapper
{
/**
* @param \Propel\Runtime\Collection\ObjectCollection $paymentUnzerOrderItemEntities
* @param \Propel\Runtime\Collection\ObjectCollection<\Orm\Zed\Unzer\Persistence\SpyPaymentUnzerOrderItem> $paymentUnzerOrderItemEntities
* @param \Generated\Shared\Transfer\PaymentUnzerOrderItemCollectionTransfer $paymentUnzerOrderItemCollectionTransfer
*
* @return \Generated\Shared\Transfer\PaymentUnzerOrderItemCollectionTransfer
Expand Down Expand Up @@ -210,7 +210,7 @@ public function mapUnzerCredentialsEntityToUnzerCredentialsTransfer(
}

/**
* @param \Propel\Runtime\Collection\ObjectCollection $unzerCredentialsStoreEntities
* @param \Propel\Runtime\Collection\ObjectCollection<\Orm\Zed\Unzer\Persistence\SpyUnzerCredentialsStore> $unzerCredentialsStoreEntities
* @param \Generated\Shared\Transfer\StoreRelationTransfer $storeRelationTransfer
*
* @return \Generated\Shared\Transfer\StoreRelationTransfer
Expand Down Expand Up @@ -239,7 +239,7 @@ protected function mapStoreEntityToStoreTransfer(SpyStore $storeEntity, StoreTra
}

/**
* @param \Propel\Runtime\Collection\ObjectCollection $unzerCredentialsEntities
* @param \Propel\Runtime\Collection\ObjectCollection<\Orm\Zed\Unzer\Persistence\SpyUnzerCredentials> $unzerCredentialsEntities
* @param \Generated\Shared\Transfer\UnzerCredentialsCollectionTransfer $unzerCredentialsCollectionTransfer
*
* @return \Generated\Shared\Transfer\UnzerCredentialsCollectionTransfer
Expand Down
Loading
Loading