Skip to content

Commit

Permalink
[ECP-8712] Fix broken partial captures and implement adyen_authorized…
Browse files Browse the repository at this point in the history
… status (#2316)

* Revert ECP-8673

* [ECP-8712] Revert #2263 and implement new order status

* [ECP-8712] Set state to payment_review while waiting for CAPTURE webhook

* [ECP-8712] Set default configuration values

* [ECP-8712] Revert unit test

* [ECP-8712] Change the order status after invoicing

* [ECP-8712] Write unit test for data patch

* [ECP-8712] Write unit test for DataPatch helper class

* [ECP-8712] Fix Sonar warnings

* [ECP-8712] Replace the imported class
  • Loading branch information
candemiralp authored Nov 14, 2023
1 parent becd00f commit 0e97203
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 154 deletions.
45 changes: 45 additions & 0 deletions Helper/DataPatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Payment\Helper;

use Magento\Framework\Setup\ModuleDataSetupInterface;

class DataPatch
{
const CONFIG_TABLE = 'core_config_data';

public function findConfig(
ModuleDataSetupInterface $setup,
string $path,
?string $value
): ?array {
$config = null;

$configDataTable = $setup->getTable(self::CONFIG_TABLE);
$connection = $setup->getConnection();

$select = $connection->select()->from($configDataTable)->where('path = ?', $path);
$matchingConfigs = $connection->fetchAll($select);

if (!empty($matchingConfigs) && is_null($value)) {
$config = reset($matchingConfigs);
} else {
foreach ($matchingConfigs as $matchingConfig) {
if ($matchingConfig['value'] === $value) {
$config = $matchingConfig;
}
}
}

return $config;
}
}
2 changes: 1 addition & 1 deletion Helper/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Webhook
* Indicative matrix for possible states to enter after given event
*/
const STATE_TRANSITION_MATRIX = [
'payment_pre_authorized' => [Order::STATE_NEW, Order::STATE_PROCESSING],
'payment_pre_authorized' => [Order::STATE_NEW],
'payment_authorized' => [Order::STATE_PROCESSING]
];

Expand Down
3 changes: 1 addition & 2 deletions Helper/Webhook/AuthorisationWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ private function handleSuccessfulAuthorisation(Order $order, Notification $notif
$additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize($notification->getAdditionalData()) : [];
$requireFraudManualReview = $this->caseManagementHelper->requiresManualReview($additionalData);

$this->invoiceHelper->createInvoice($order, $notification, $isAutoCapture);

if ($isAutoCapture) {
$order = $this->handleAutoCapture($order, $notification, $requireFraudManualReview);
} else {
Expand Down Expand Up @@ -219,6 +217,7 @@ private function handleFailedAuthorisation(Order $order, Notification $notificat
*/
private function handleAutoCapture(Order $order, Notification $notification, bool $requireFraudManualReview): Order
{
$this->invoiceHelper->createInvoice($order, $notification, true);
if ($requireFraudManualReview) {
$order = $this->caseManagementHelper->markCaseAsPendingReview($order, $notification->getPspreference(), true);
} else {
Expand Down
5 changes: 3 additions & 2 deletions Model/Config/Source/PreAuthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Adyen\Payment\Model\Config\Source;

use Magento\Sales\Model\Order;

/**
* Order Statuses source model
*/
Expand All @@ -20,7 +22,6 @@ class PreAuthorized extends \Magento\Sales\Model\Config\Source\Order\Status
* @var string[]
*/
protected $_stateStatuses = [
\Magento\Sales\Model\Order::STATE_PROCESSING,
\Magento\Sales\Model\Order::STATE_NEW
Order::STATE_NEW
];
}
15 changes: 2 additions & 13 deletions Observer/InvoiceObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public function execute(Observer $observer)
return;
}


$this->logger->addAdyenDebug(
'Event sales_order_invoice_save_after for invoice {invoiceId} will be handled',
array_merge($this->logger->getInvoiceContext($invoice), $this->logger->getOrderContext($order))
Expand All @@ -119,18 +118,8 @@ public function execute(Observer $observer)
$this->adyenOrderPaymentHelper->updatePaymentTotalCaptured($adyenOrderPaymentObject, $linkedAmount);
}

$status = $this->configHelper->getConfigData(
'payment_pre_authorized',
Config::XML_ADYEN_ABSTRACT_PREFIX,
$order->getStoreId()
);

if (empty($status)) {
$status = $this->statusResolver->getOrderStatusByState($order, Order::STATE_PENDING_PAYMENT);
}

// Set order to PROCESSING to allow further invoices to be generated
$order->setState(Order::STATE_PENDING_PAYMENT);
$status = $this->statusResolver->getOrderStatusByState($order, Order::STATE_PAYMENT_REVIEW);
$order->setState(Order::STATE_PAYMENT_REVIEW);
$order->setStatus($status);

$this->logger->addAdyenDebug(
Expand Down
107 changes: 107 additions & 0 deletions Setup/Patch/Data/CreateStatusAuthorized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/
declare(strict_types=1);

namespace Adyen\Payment\Setup\Patch\Data;

use Adyen\Payment\Helper\DataPatch;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Sales\Model\Order;

class CreateStatusAuthorized implements DataPatchInterface, PatchVersionInterface
{
private ModuleDataSetupInterface $moduleDataSetup;
private WriterInterface $configWriter;
private ReinitableConfigInterface $reinitableConfig;
private DataPatch $dataPatchHelper;

const ADYEN_AUTHORIZED_STATUS = 'adyen_authorized';
const ADYEN_AUTHORIZED_STATUS_LABEL = 'Authorized';

public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
WriterInterface $configWriter,
ReinitableConfigInterface $reinitableConfig,
DataPatch $dataPatchHelper
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->configWriter = $configWriter;
$this->reinitableConfig = $reinitableConfig;
$this->dataPatchHelper = $dataPatchHelper;
}

public function apply()
{
$setup = $this->moduleDataSetup;

$orderStateAssignmentTable = $this->moduleDataSetup->getTable('sales_order_status_state');
$orderStatusTable = $this->moduleDataSetup->getTable('sales_order_status');

$status = [
[
'status' => self::ADYEN_AUTHORIZED_STATUS,
'label' => self::ADYEN_AUTHORIZED_STATUS_LABEL
]
];

$this->moduleDataSetup->getConnection()->insertOnDuplicate($orderStatusTable, $status);

$stateAssignment = [
[
'status' => self::ADYEN_AUTHORIZED_STATUS,
'state' => Order::STATE_NEW,
'is_default' => 0,
'visible_on_front' => 1
]
];

$this->moduleDataSetup->getConnection()->insertOnDuplicate($orderStateAssignmentTable, $stateAssignment);

$path = 'payment/adyen_abstract/payment_pre_authorized';

$config = $this->dataPatchHelper->findConfig($setup, $path, Order::STATE_PROCESSING);
if (isset($config)) {
$this->configWriter->save(
$path,
self::ADYEN_AUTHORIZED_STATUS,
$config['scope'],
$config['scope_id']
);
}

// re-initialize otherwise it will cause errors
$this->reinitableConfig->reinit();
}

/**
* @inheritdoc
*/
public function getAliases(): array
{
return [];
}

/**
* @inheritdoc
*/
public static function getDependencies(): array
{
return [];
}

public static function getVersion(): string
{
return '9.0.3';
}
}
119 changes: 0 additions & 119 deletions Setup/Patch/Data/PreAuthorizedSettingsMigration.php

This file was deleted.

Loading

0 comments on commit 0e97203

Please sign in to comment.