Skip to content

Commit

Permalink
PAYOSWXP-105: Add order action log and webhook log
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Müller committed Nov 14, 2023
1 parent e38e3aa commit 015a9c0
Show file tree
Hide file tree
Showing 57 changed files with 1,361 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\OrderActionLog;

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
) {
}

/**
* @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(),
];

$this->orderActionLogRepository->create([$orderActionLog], $context);
}
}
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;
}
38 changes: 38 additions & 0 deletions src/Components/DataHandler/WebhookLog/WebhookLogDataHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\DataHandler\WebhookLog;

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
) {
}

/**
* @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(),
];

$this->webhookLogRepository->create([$webhookLog], $context);
}
}
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(
$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

0 comments on commit 015a9c0

Please sign in to comment.