Skip to content

Commit

Permalink
Feature/acp 4656/master adjust event status payment history (#27)
Browse files Browse the repository at this point in the history
* ACP-4656 Added Payment Status History.
* ACP-4656 Added paymentStatusHistory to details sent in PaymentCreated and PaymentUpdated messages.
  • Loading branch information
stereomon authored Jan 9, 2025
1 parent 86e33a9 commit 6d081bc
Show file tree
Hide file tree
Showing 35 changed files with 457 additions and 102 deletions.
40 changes: 20 additions & 20 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

use Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector;
use Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector;
use Rector\Config\RectorConfig;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
Expand All @@ -40,27 +37,30 @@
]);

$rectorConfig->skip([
RenameForeachValueVariableToMatchExprVariableRector::class => [
'src/Spryker/Zed/AppPayment/Business/Payment/Writer/PaymentWriter.php',
],
RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class => [
__DIR__ . '/src/Spryker/Glue/AppPaymentBackendApi/Mapper/Payment/GlueResponsePaymentMapper.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Business/MessageBroker/RefundPaymentMessageHandler.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Business/Payment/Transfer/PaymentTransfer.php',
'src/Spryker/Glue/AppPaymentBackendApi/Mapper/Payment/GlueResponsePaymentMapper.php',
'src/Spryker/Zed/AppPayment/Business/MessageBroker/RefundPaymentMessageHandler.php',
'src/Spryker/Zed/AppPayment/Business/Payment/Transfer/PaymentTransfer.php',
],
// Ignore this rule on the AppRouteProviderPlugin as it breaks the code
ClassPropertyAssignToConstructorPromotionRector::class => [
__DIR__ . '/src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToAppPaymentFacadeBridge.php',
__DIR__ . '/src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToTranslatorFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppKernelFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToMessageBrokerFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppWebhookFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Service/AppPaymentToUtilEncodingServiceBridge.php',
'src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToAppPaymentFacadeBridge.php',
'src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToTranslatorFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppKernelFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToMessageBrokerFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppWebhookFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Service/AppPaymentToUtilEncodingServiceBridge.php',
],
AddParamTypeFromPropertyTypeRector::class => [
__DIR__ . '/src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToAppPaymentFacadeBridge.php',
__DIR__ . '/src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToTranslatorFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppKernelFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToMessageBrokerFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppWebhookFacadeBridge.php',
__DIR__ . '/src/Spryker/Zed/AppPayment/Dependency/Service/AppPaymentToUtilEncodingServiceBridge.php',
'src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToAppPaymentFacadeBridge.php',
'src/Spryker/Glue/AppPaymentBackendApi/Dependency/Facade/AppPaymentBackendApiToTranslatorFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppKernelFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToMessageBrokerFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Facade/AppPaymentToAppWebhookFacadeBridge.php',
'src/Spryker/Zed/AppPayment/Dependency/Service/AppPaymentToUtilEncodingServiceBridge.php',
],
]);
};
21 changes: 19 additions & 2 deletions src/Spryker/Shared/AppPayment/Transfer/app_payment.transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@
<property name="redirectCancelUrl" type="string"/>
<property name="additionalPaymentData" type="array" associative="true" singular="additionalPaymentDatum"/>
<property name="details" type="string"/>
<property name="originPayment" type="Payment" description="This is the origin payment right after fetching from DB or after creation. It an be used to compare after it may have been changed."/>
<property name="paymentStatusHistory" type="PaymentStatusHistory" description="The history of all states this payment had."/>
</transfer>

<transfer name="PaymentRefund" strict="true">
Expand Down Expand Up @@ -416,17 +418,32 @@
<transfer name="PaymentCreated" strict="true">
<property name="entityReference" type="string"/>
<property name="paymentReference" type="string"/>
<property name="details" type="string"/>
<property name="details" type="string" description="A JSON representation of payment details such as PSP specific data and payment status history data"/>
<property name="messageAttributes" type="MessageAttributes"/>
</transfer>

<transfer name="PaymentUpdated" strict="true">
<property name="entityReference" type="string"/>
<property name="paymentReference" type="string"/>
<property name="details" type="string"/>
<property name="details" type="string" description="A JSON representation of payment details such as PSP specific data and payment status history data"/>
<property name="messageAttributes" type="MessageAttributes"/>
</transfer>

<transfer name="PaymentStatusHistory" strict="true">
<property name="paymentStates" type="PaymentStatus[]" singular="paymentState"/>
</transfer>

<transfer name="PaymentStatusHistoryCriteria" strict="true">
<property name="tenantIdentifier" type="string"/>
<property name="transactionId" type="string"/>
<property name="orderReference" type="string"/>
</transfer>

<transfer name="PaymentStatus" strict="true">
<property name="status" type="string"/>
<property name="createdAt" type="string"/>
</transfer>

<transfer name="MessageContext" strict="true">
<property name="orderItemsIds" type="array" singular="orderItemId"/>
<property name="amount" type="string"/>
Expand Down
17 changes: 12 additions & 5 deletions src/Spryker/Zed/AppPayment/Business/AppPaymentBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use Spryker\Zed\AppPayment\Business\Payment\Webhook\WebhookHandler;
use Spryker\Zed\AppPayment\Business\Payment\Webhook\WebhookMessageSender;
use Spryker\Zed\AppPayment\Business\Payment\Webhook\WebhookRequestExtender;
use Spryker\Zed\AppPayment\Business\Payment\Writer\PaymentWriter;
use Spryker\Zed\AppPayment\Business\Payment\Writer\PaymentWriterInterface;
use Spryker\Zed\AppPayment\Business\Redirect\Redirect;
use Spryker\Zed\AppPayment\Dependency\Facade\AppPaymentToAppKernelFacadeInterface;
use Spryker\Zed\AppPayment\Dependency\Facade\AppPaymentToAppWebhookFacadeInterface;
Expand Down Expand Up @@ -68,12 +70,12 @@ public function createPayment(): Payment

public function createPaymentInitializer(): PaymentInitializer
{
return new PaymentInitializer($this->getPlatformPlugin(), $this->getEntityManager(), $this->getRepository(), $this->createPaymentMethodNormalizer(), $this->createMessageSender(), $this->getConfig(), $this->createAppConfigLoader());
return new PaymentInitializer($this->getPlatformPlugin(), $this->getEntityManager(), $this->getRepository(), $this->createPaymentWriter(), $this->createPaymentMethodNormalizer(), $this->createMessageSender(), $this->getConfig(), $this->createAppConfigLoader());
}

public function createPaymentPreOrder(): PaymentPreOrder
{
return new PaymentPreOrder($this->getPlatformPlugin(), $this->getRepository(), $this->getEntityManager(), $this->getAppWebhookFacade(), $this->createMessageSender(), $this->getConfig(), $this->createAppConfigLoader());
return new PaymentPreOrder($this->getPlatformPlugin(), $this->getRepository(), $this->createPaymentWriter(), $this->getAppWebhookFacade(), $this->createMessageSender(), $this->getConfig(), $this->createAppConfigLoader());
}

public function createPaymentTransfer(): PaymentTransfer
Expand Down Expand Up @@ -123,7 +125,7 @@ public function createWebhookHandlerSelector(): WebhookHandlerSelector
public function createPaymentWebhookHandler(): PaymentWebhookHandler
{
return new PaymentWebhookHandler(
$this->getEntityManager(),
$this->createPaymentWriter(),
$this->createPaymentStatusTransitionValidator(),
);
}
Expand Down Expand Up @@ -218,7 +220,7 @@ public function createCancelPayment(): CancelPayment
return new CancelPayment(
$this->getPlatformPlugin(),
$this->createPaymentStatusTransitionValidator(),
$this->getEntityManager(),
$this->createPaymentWriter(),
$this->getConfig(),
$this->createAppConfigLoader(),
);
Expand Down Expand Up @@ -270,14 +272,19 @@ public function createTenantIdentifierExtractor(): TenantIdentifierExtractor

public function createPaymentCapturer(): PaymentCapturer
{
return new PaymentCapturer($this->getPlatformPlugin(), $this->getEntityManager(), $this->getConfig(), $this->createAppConfigLoader());
return new PaymentCapturer($this->getPlatformPlugin(), $this->createPaymentWriter(), $this->getConfig(), $this->createAppConfigLoader());
}

public function createRedirect(): Redirect
{
return new Redirect($this->getPlatformPlugin(), $this->getRepository(), $this->getConfig(), $this->createAppConfigLoader());
}

public function createPaymentWriter(): PaymentWriterInterface
{
return new PaymentWriter($this->getEntityManager(), $this->getRepository(), $this->createMessageSender());
}

public function createPaymentMethodReader(): PaymentMethodReader
{
return new PaymentMethodReader($this->getRepository(), $this->createPaymentMethodNormalizer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Spryker\Zed\AppPayment\Business\Payment\AppConfig\AppConfigLoader;
use Spryker\Zed\AppPayment\Business\Payment\Status\PaymentStatus;
use Spryker\Zed\AppPayment\Business\Payment\Status\PaymentStatusTransitionValidator;
use Spryker\Zed\AppPayment\Business\Payment\Writer\PaymentWriterInterface;
use Spryker\Zed\AppPayment\Dependency\Plugin\AppPaymentPlatformPluginInterface;
use Spryker\Zed\AppPayment\Persistence\AppPaymentEntityManagerInterface;
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
use Throwable;

Expand All @@ -28,7 +28,7 @@ class CancelPayment
public function __construct(
protected AppPaymentPlatformPluginInterface $appPaymentPlatformPlugin,
protected PaymentStatusTransitionValidator $paymentStatusTransitionValidator,
protected AppPaymentEntityManagerInterface $appPaymentEntityManager,
protected PaymentWriterInterface $paymentWriter,
protected AppPaymentConfig $appPaymentConfig,
protected AppConfigLoader $appConfigLoader
) {
Expand Down Expand Up @@ -72,6 +72,6 @@ public function cancelPayment(CancelPaymentRequestTransfer $cancelPaymentRequest
protected function savePayment(PaymentTransfer $paymentTransfer, string $status): void
{
$paymentTransfer->setStatus($status);
$this->appPaymentEntityManager->savePayment($paymentTransfer);
$this->paymentWriter->updatePayment($paymentTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Spryker\Zed\AppPayment\AppPaymentConfig;
use Spryker\Zed\AppPayment\Business\Payment\AppConfig\AppConfigLoader;
use Spryker\Zed\AppPayment\Business\Payment\Status\PaymentStatus;
use Spryker\Zed\AppPayment\Business\Payment\Writer\PaymentWriterInterface;
use Spryker\Zed\AppPayment\Dependency\Plugin\AppPaymentPlatformPluginInterface;
use Spryker\Zed\AppPayment\Persistence\AppPaymentEntityManagerInterface;
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
use Throwable;

Expand All @@ -26,7 +26,7 @@ class PaymentCapturer

public function __construct(
protected AppPaymentPlatformPluginInterface $appPaymentPlatformPlugin,
protected AppPaymentEntityManagerInterface $appPaymentEntityManager,
protected PaymentWriterInterface $paymentWriter,
protected AppPaymentConfig $appPaymentConfig,
protected AppConfigLoader $appConfigLoader
) {
Expand Down Expand Up @@ -60,6 +60,6 @@ public function capturePayment(CapturePaymentRequestTransfer $capturePaymentRequ
protected function savePayment(PaymentTransfer $paymentTransfer, string $status): void
{
$paymentTransfer->setStatus($status);
$this->appPaymentEntityManager->savePayment($paymentTransfer);
$this->paymentWriter->updatePayment($paymentTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Spryker\Zed\AppPayment\Business\Payment\Message\MessageSender;
use Spryker\Zed\AppPayment\Business\Payment\Method\Normalizer\PaymentMethodNormalizer;
use Spryker\Zed\AppPayment\Business\Payment\Status\PaymentStatus;
use Spryker\Zed\AppPayment\Business\Payment\Writer\PaymentWriterInterface;
use Spryker\Zed\AppPayment\Dependency\Plugin\AppPaymentPlatformPluginInterface;
use Spryker\Zed\AppPayment\Persistence\AppPaymentEntityManagerInterface;
use Spryker\Zed\AppPayment\Persistence\AppPaymentRepositoryInterface;
Expand All @@ -31,6 +32,7 @@ public function __construct(
protected AppPaymentPlatformPluginInterface $appPaymentPlatformPlugin,
protected AppPaymentEntityManagerInterface $appPaymentEntityManager,
protected AppPaymentRepositoryInterface $appPaymentRepository,
protected PaymentWriterInterface $paymentWriter,
protected PaymentMethodNormalizer $paymentMethodNormalizer,
protected MessageSender $messageSender,
protected AppPaymentConfig $appPaymentConfig,
Expand Down Expand Up @@ -74,9 +76,9 @@ public function initializePayment(InitializePaymentRequestTransfer $initializePa
}

/** @phpstan-var \Generated\Shared\Transfer\InitializePaymentResponseTransfer */
return $this->getTransactionHandler()->handleTransaction(function () use ($initializePaymentRequestTransfer, $initializePaymentResponseTransfer): \Generated\Shared\Transfer\InitializePaymentResponseTransfer {
return $this->getTransactionHandler()->handleTransaction(function () use ($initializePaymentRequestTransfer, $initializePaymentResponseTransfer): InitializePaymentResponseTransfer {
// When we have already persisted a payment in the database and this method is called a second time the payment will be set in the request transfer
// In this case we are in the pre-order payment process and we don't want to save the payment again.
// In this case we are in the pre-order payment process, and we don't want to save the payment again.
// When the grandTotal has changed the response will contain a different transactionId as we have persisted in the database because so we need to update
if ($initializePaymentRequestTransfer->getPayment() instanceof PaymentTransfer) {
// Nothing has changed so we can step out here
Expand All @@ -92,8 +94,7 @@ public function initializePayment(InitializePaymentRequestTransfer $initializePa
return $initializePaymentResponseTransfer;
}

$this->savePayment($initializePaymentRequestTransfer, $initializePaymentResponseTransfer);
$this->messageSender->sendPaymentCreatedMessage($initializePaymentRequestTransfer, $initializePaymentResponseTransfer);
$this->createPayment($initializePaymentRequestTransfer, $initializePaymentResponseTransfer);

return $initializePaymentResponseTransfer;
});
Expand Down Expand Up @@ -121,7 +122,7 @@ protected function addRedirectUrl(
return $initializePaymentResponseTransfer;
}

protected function savePayment(
protected function createPayment(
InitializePaymentRequestTransfer $initializePaymentRequestTransfer,
InitializePaymentResponseTransfer $initializePaymentResponseTransfer
): void {
Expand All @@ -138,7 +139,7 @@ protected function savePayment(
->setRedirectCancelUrl($initializePaymentRequestTransfer->getRedirectCancelUrl())
->setStatus(PaymentStatus::STATUS_NEW);

$this->appPaymentEntityManager->createPayment($paymentTransfer);
$this->paymentWriter->createPayment($paymentTransfer);
}

protected function normalizePaymentMethod(InitializePaymentRequestTransfer $initializePaymentRequestTransfer): InitializePaymentRequestTransfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
namespace Spryker\Zed\AppPayment\Business\Payment\Message;

use ArrayObject;
use Generated\Shared\Transfer\InitializePaymentRequestTransfer;
use Generated\Shared\Transfer\InitializePaymentResponseTransfer;
use Generated\Shared\Transfer\MessageContextTransfer;
use Generated\Shared\Transfer\PaymentAuthorizationFailedTransfer;
use Generated\Shared\Transfer\PaymentAuthorizedTransfer;
Expand Down Expand Up @@ -102,31 +100,28 @@ public function sendPaymentCancellationFailedMessage(PaymentTransfer $paymentTra
}

public function sendPaymentCreatedMessage(
InitializePaymentRequestTransfer $initializePaymentRequestTransfer,
InitializePaymentResponseTransfer $initializePaymentResponseTransfer
PaymentTransfer $paymentTransfer
): void {
$quoteTransfer = $initializePaymentRequestTransfer->getOrderDataOrFail();

// It may be that payment gets created without having either of the following. (PreOrderPayment)
// In this case, we do send a PaymentUpdated message later. E.g. when ConfirmPreOrderPayment is made
if (!$quoteTransfer->getOrderReference() && ($initializePaymentResponseTransfer->getTransactionId() === null || $initializePaymentResponseTransfer->getTransactionId() === '' || $initializePaymentResponseTransfer->getTransactionId() === '0')) {
if (($paymentTransfer->getOrderReference() === null || $paymentTransfer->getOrderReference() === '' || $paymentTransfer->getOrderReference() === '0') && ($paymentTransfer->getTransactionId() === null || $paymentTransfer->getTransactionId() === '' || $paymentTransfer->getTransactionId() === '0')) {
return;
}

$transactionId = $initializePaymentResponseTransfer->getTransactionIdOrFail();
$transactionId = $paymentTransfer->getTransactionIdOrFail();

if (str_starts_with($transactionId, AppPaymentConfig::IGNORE_PAYMENT_CREATED_MESSAGE_SENDING_TRANSACTION_ID_PREFIX)) {
return;
}

$paymentCreatedTransfer = new PaymentCreatedTransfer();
$paymentCreatedTransfer->fromArray($initializePaymentResponseTransfer->toArray(), true);
$paymentCreatedTransfer->fromArray($paymentTransfer->toArray(), true);
$paymentCreatedTransfer
->setEntityReference($quoteTransfer->getOrderReference())
->setPaymentReference($initializePaymentResponseTransfer->getTransactionId());
->setEntityReference($paymentTransfer->getOrderReference())
->setPaymentReference($paymentTransfer->getTransactionIdOrFail());

$paymentCreatedTransfer->setMessageAttributes($this->getMessageAttributes(
$initializePaymentRequestTransfer->getTenantIdentifierOrFail(),
$paymentTransfer->getTenantIdentifierOrFail(),
$paymentCreatedTransfer::class,
));

Expand Down
Loading

0 comments on commit 6d081bc

Please sign in to comment.