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

PAYOSWXP-105: Add order action log and webhook log #282

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\OrderActionLog;

use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;

class OrderActionLogDataHandler implements OrderActionLogDataHandlerInterface
{
public function __construct(
protected readonly EntityRepository $orderActionLogRepository,
protected readonly LoggerInterface $logger
) {
}

/**
* @param array<string, mixed> $request
* @param array<string, mixed> $response
*/
public function createOrderActionLog(
OrderEntity $order,
array $request,
array $response,
Context $context
): void {
$orderActionLog = [
'orderId' => $order->getId(),
'transactionId' => $response['txid'],
'referenceNumber' => $order->getOrderNumber(),
'request' => $request['request'],
'response' => $response['status'],
'amount' => $request['amount'],
'mode' => $request['mode'],
'merchantId' => $request['mid'],
'portalId' => $request['portalid'],
'requestDetails' => $request,
'responseDetails' => $response,
'requestDateTime' => new \DateTime(),
];

try {
$this->orderActionLogRepository->create([$orderActionLog], $context);
} catch (\Exception $exception) {
$this->logger->error('Failed to create order action log', [
'message' => $exception->getMessage(),
'data' => $orderActionLog,
'trace' => $exception->getTraceAsString(),
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\OrderActionLog;

use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;

interface OrderActionLogDataHandlerInterface
{
/**
* @param array<string, mixed> $request
* @param array<string, mixed> $response
*/
public function createOrderActionLog(
OrderEntity $order,
array $request,
array $response,
Context $context
): void;
}
48 changes: 48 additions & 0 deletions src/Components/DataHandler/WebhookLog/WebhookLogDataHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\WebhookLog;

use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;

class WebhookLogDataHandler implements WebhookLogDataHandlerInterface
{
public function __construct(
protected readonly EntityRepository $webhookLogRepository,
protected readonly LoggerInterface $logger
) {
}

/**
* @param array<string, mixed> $webhookData
*/
public function createWebhookLog(
OrderEntity $order,
array $webhookData,
Context $context
): void {
$webhookLog = [
'orderId' => $order->getId(),
'transactionId' => $webhookData['txid'],
'transactionState' => $webhookData['txaction'],
'sequenceNumber' => (int) $webhookData['sequencenumber'],
'clearingType' => $webhookData['clearingtype'],
'webhookDetails' => $webhookData,
'webhookDateTime' => new \DateTime(),
];

try {
$this->webhookLogRepository->create([$webhookLog], $context);
} catch (\Exception $exception) {
$this->logger->error('Failed to create webhook log', [
'message' => $exception->getMessage(),
'data' => $webhookLog,
'trace' => $exception->getTraceAsString(),
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\WebhookLog;

use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;

interface WebhookLogDataHandlerInterface
{
/**
* @param array<string, mixed> $webhookData
*/
public function createWebhookLog(
OrderEntity $order,
array $webhookData,
Context $context
): void;
}
18 changes: 14 additions & 4 deletions src/Components/TransactionHandler/AbstractTransactionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\Components\TransactionHandler;

use PayonePayment\Components\Currency\CurrencyPrecisionInterface;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Payone\Client\Exception\PayoneRequestException;
use PayonePayment\Payone\Client\PayoneClientInterface;
Expand All @@ -28,7 +29,7 @@ abstract class AbstractTransactionHandler

protected PayoneClientInterface $client;

protected TransactionDataHandlerInterface $dataHandler;
protected TransactionDataHandlerInterface $transactionDataHandler;

protected EntityRepository $transactionRepository;

Expand All @@ -38,6 +39,8 @@ abstract class AbstractTransactionHandler

protected CurrencyPrecisionInterface $currencyPrecision;

protected OrderActionLogDataHandlerInterface $orderActionLogDataHandler;

public function handleRequest(ParameterBag $parameterBag, string $action, Context $context): array
{
$this->context = $context;
Expand Down Expand Up @@ -93,11 +96,18 @@ protected function executeRequest(array $request): array
try {
$response = $this->client->request($request);

$this->dataHandler->logResponse($this->paymentTransaction, $this->context, [
$this->transactionDataHandler->logResponse($this->paymentTransaction, $this->context, [
'request' => $request,
'response' => $response,
]);

$this->orderActionLogDataHandler->createOrderActionLog(
rommelfreddy marked this conversation as resolved.
Show resolved Hide resolved
$this->paymentTransaction->getOrder(),
$request,
$response,
$this->context
);

return [
new JsonResponse(['status' => true]),
$response,
Expand Down Expand Up @@ -141,8 +151,8 @@ protected function updateTransactionData(ParameterBag $parameterBag, float $capt
}
}

$this->dataHandler->incrementSequenceNumber($this->paymentTransaction, $transactionData);
$this->dataHandler->saveTransactionData($this->paymentTransaction, $this->context, $transactionData);
$this->transactionDataHandler->incrementSequenceNumber($this->paymentTransaction, $transactionData);
$this->transactionDataHandler->saveTransactionData($this->paymentTransaction, $this->context, $transactionData);
}

protected function saveOrderLineItemData(array $orderLines, Context $context): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\Components\TransactionHandler\Capture;

use PayonePayment\Components\Currency\CurrencyPrecisionInterface;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\TransactionHandler\AbstractTransactionHandler;
use PayonePayment\Components\TransactionStatus\TransactionStatusServiceInterface;
Expand All @@ -26,15 +27,17 @@ class CaptureTransactionHandler extends AbstractTransactionHandler implements Ca
public function __construct(
RequestParameterFactory $requestFactory,
PayoneClientInterface $client,
TransactionDataHandlerInterface $dataHandler,
TransactionDataHandlerInterface $transactionDataHandler,
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
private readonly TransactionStatusServiceInterface $transactionStatusService,
EntityRepository $transactionRepository,
EntityRepository $lineItemRepository,
CurrencyPrecisionInterface $currencyPrecision
) {
$this->requestFactory = $requestFactory;
$this->client = $client;
$this->dataHandler = $dataHandler;
$this->transactionDataHandler = $transactionDataHandler;
$this->orderActionLogDataHandler = $orderActionLogDataHandler;
$this->transactionRepository = $transactionRepository;
$this->lineItemRepository = $lineItemRepository;
$this->currencyPrecision = $currencyPrecision;
Expand Down Expand Up @@ -123,7 +126,7 @@ private function updateClearingBankAccountData(array $payoneResponse): void
$newClearingBankAccountData = $payoneResponse['clearing']['BankAccount'] ?? null;

if (!empty($newClearingBankAccountData)) {
$this->dataHandler->saveTransactionData(
$this->transactionDataHandler->saveTransactionData(
$this->paymentTransaction,
$this->context,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\Components\TransactionHandler\Refund;

use PayonePayment\Components\Currency\CurrencyPrecisionInterface;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\TransactionHandler\AbstractTransactionHandler;
use PayonePayment\Components\TransactionStatus\TransactionStatusServiceInterface;
Expand All @@ -26,15 +27,17 @@ class RefundTransactionHandler extends AbstractTransactionHandler implements Ref
public function __construct(
RequestParameterFactory $requestFactory,
PayoneClientInterface $client,
TransactionDataHandlerInterface $dataHandler,
TransactionDataHandlerInterface $transactionDataHandler,
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
private readonly TransactionStatusServiceInterface $transactionStatusService,
EntityRepository $transactionRepository,
EntityRepository $lineItemRepository,
CurrencyPrecisionInterface $currencyPrecision
) {
$this->requestFactory = $requestFactory;
$this->client = $client;
$this->dataHandler = $dataHandler;
$this->transactionDataHandler = $transactionDataHandler;
$this->orderActionLogDataHandler = $orderActionLogDataHandler;
$this->transactionRepository = $transactionRepository;
$this->lineItemRepository = $lineItemRepository;
$this->currencyPrecision = $currencyPrecision;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PayonePayment\DataAbstractionLayer\Entity\OrderActionLog;

use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;

/**
* @extends EntityCollection<PayonePaymentOrderActionLogEntity>
*/
class PayonePaymentOrderActionLogCollection extends EntityCollection
{
protected function getExpectedClass(): string
{
return PayonePaymentOrderActionLogEntity::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace PayonePayment\DataAbstractionLayer\Entity\OrderActionLog;

use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\CreatedAtField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\UpdatedAtField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class PayonePaymentOrderActionLogDefinition extends EntityDefinition
{
final public const ENTITY_NAME = 'payone_payment_order_action_log';

public function getEntityName(): string
{
return self::ENTITY_NAME;
}

public function getCollectionClass(): string
{
return PayonePaymentOrderActionLogCollection::class;
}

public function getEntityClass(): string
{
return PayonePaymentOrderActionLogEntity::class;
}

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->setFlags(new PrimaryKey(), new Required()),

(new FkField('order_id', 'orderId', OrderDefinition::class))->addFlags(new Required()),
(new ReferenceVersionField(OrderDefinition::class, 'order_version_id'))->addFlags(new Required()),
new ManyToOneAssociationField('order', 'order_id', OrderDefinition::class, 'id', false),

(new StringField('transaction_id', 'transactionId'))->setFlags(new Required()),
(new StringField('reference_number', 'referenceNumber'))->setFlags(new Required()),
(new StringField('request', 'request'))->setFlags(new Required()),
(new StringField('response', 'response'))->setFlags(new Required()),
(new IntField('amount', 'amount'))->setFlags(new Required()),
(new StringField('mode', 'mode'))->setFlags(new Required()),
(new StringField('merchant_id', 'merchantId'))->setFlags(new Required()),
(new StringField('portal_id', 'portalId'))->setFlags(new Required()),
(new JsonField('request_details', 'requestDetails'))->setFlags(new Required()),
(new JsonField('response_details', 'responseDetails'))->setFlags(new Required()),
(new DateTimeField('request_date_time', 'requestDateTime'))->setFlags(new Required()),

new CreatedAtField(),
new UpdatedAtField(),
]);
}
}
Loading
Loading