Skip to content

Commit

Permalink
Merge pull request #456 from Adyen/develop
Browse files Browse the repository at this point in the history
Release 3.14.4
  • Loading branch information
candemiralp authored Feb 8, 2024
2 parents dacfc11 + d9b26c6 commit a288925
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"description": "Official Shopware 6 Plugin to connect to Payment Service Provider Adyen",
"version": "3.14.3",
"version": "3.14.4",
"type": "shopware-platform-plugin",
"license": "MIT",
"require": {
Expand Down
21 changes: 21 additions & 0 deletions src/Entity/PaymentResponse/PaymentResponseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class PaymentResponseEntity extends Entity
*/
protected $createdAt;

/**
* @var string
*/
protected $pspreference;

/**
* @return string
*/
Expand Down Expand Up @@ -161,4 +166,20 @@ public function setResponse(string $response): void
{
$this->response = $response;
}

/**
* @return string|null
*/
public function getPspreference(): ?string
{
return $this->pspreference;
}

/**
* @param string|null $pspreference
*/
public function setPspreference(?string $pspreference): void
{
$this->pspreference = $pspreference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\UpdatedAtField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
Expand Down Expand Up @@ -67,6 +65,7 @@ protected function defineFields(): FieldCollection
OrderTransactionDefinition::class
))->addFlags(new Required()),
new StringField('result_code', 'resultCode'),
new StringField('pspreference', 'pspreference'),
new LongTextField('response', 'response'),
new CreatedAtField(),
new UpdatedAtField(),
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/AbstractPaymentMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ protected function preparePaymentsRequest(
$imageUrl = null;
}

if (isset($product) && !is_null($product->getCategories())) {
if (isset($product) && !is_null($product->getCategories()) && $product->getCategories()->count() > 0) {
$productCategory = $product->getCategories()->first()->getName();
} else {
$productCategory = null;
Expand Down
37 changes: 37 additions & 0 deletions src/Migration/Migration1705588547AlterAdyenPaymentResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Adyen\Shopware\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1705588547AlterAdyenPaymentResponse extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1705588547;
}

public function update(Connection $connection): void
{
$connection->executeStatement(<<<SQL
ALTER TABLE `adyen_payment_response` ADD COLUMN `pspreference` varchar(255);
SQL);

$connection->executeStatement(<<<SQL
ALTER TABLE `adyen_payment_response`
ADD CONSTRAINT `UQ_ADYEN_PAYMENT_RESPONSE_PSPREFERENCE`
UNIQUE (`pspreference`);
SQL);

$connection->executeStatement(<<<SQL
CREATE INDEX `ADYEN_PAYMENT_RESPONSE_PSPREFERENCE`
ON `adyen_payment_response` (`pspreference`);
SQL);
}

public function updateDestructive(Connection $connection): void
{
// implement update destructive
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/notification-processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<argument type="service" id="Adyen\Shopware\Service\AdyenPaymentService"/>
<argument type="service" id="Adyen\Shopware\Service\CaptureService"/>
<argument type="service" id="Adyen\Shopware\ScheduledTask\Webhook\WebhookHandlerFactory"/>
<argument type="service" id="Adyen\Shopware\Service\PaymentResponseService"/>
<tag name="messenger.message_handler"/>
<call method="setLogger">
<argument key="$logger" type="service" id="monolog.logger.adyen_notification"/>
Expand Down
6 changes: 3 additions & 3 deletions src/ScheduledTask/FetchPaymentMethodLogosHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FetchPaymentMethodLogosHandler extends ScheduledTaskHandler
*/
private $paymentMethodRepository;
/**
* @var EntityRepository
* @var EntityRepository|\Shopware\Core\Content\Media\DataAbstractionLayer\MediaRepositoryDecorator
*/
private $mediaRepository;
/**
Expand All @@ -62,8 +62,8 @@ class FetchPaymentMethodLogosHandler extends ScheduledTaskHandler
public function __construct(
EntityRepository $scheduledTaskRepository,
MediaService $mediaService,
EntityRepository $paymentMethodRepository,
EntityRepository $mediaRepository,
$paymentMethodRepository,
$mediaRepository,
bool $enableUrlUploadFeature = true
) {
parent::__construct($scheduledTaskRepository);
Expand Down
34 changes: 28 additions & 6 deletions src/ScheduledTask/ProcessNotificationsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Adyen\Shopware\Service\AdyenPaymentService;
use Adyen\Shopware\Service\CaptureService;
use Adyen\Shopware\Service\NotificationService;
use Adyen\Shopware\Service\PaymentResponseService;
use Adyen\Shopware\Service\Repository\OrderRepository;
use Adyen\Shopware\Service\Repository\OrderTransactionRepository;
use Adyen\Webhook\Exception\InvalidDataException;
Expand Down Expand Up @@ -102,6 +103,11 @@ class ProcessNotificationsHandler extends ScheduledTaskHandler
*/
private static WebhookHandlerFactory $webhookHandlerFactory;

/**
* @var PaymentResponseService $paymentResponseService
*/
private PaymentResponseService $paymentResponseService;

/**
* @var array Map Shopware transaction states to payment states in the webhook module.
*/
Expand All @@ -124,16 +130,18 @@ class ProcessNotificationsHandler extends ScheduledTaskHandler
* @param AdyenPaymentService $adyenPaymentService
* @param CaptureService $captureService
* @param WebhookHandlerFactory $webhookHandlerFactory
* @param PaymentResponseService $paymentResponseService
*/
public function __construct(
EntityRepository $scheduledTaskRepository,
NotificationService $notificationService,
OrderRepository $orderRepository,
EntityRepository $paymentMethodRepository,
$paymentMethodRepository,
OrderTransactionRepository $orderTransactionRepository,
AdyenPaymentService $adyenPaymentService,
CaptureService $captureService,
WebhookHandlerFactory $webhookHandlerFactory
WebhookHandlerFactory $webhookHandlerFactory,
PaymentResponseService $paymentResponseService
) {
parent::__construct($scheduledTaskRepository);
$this->notificationService = $notificationService;
Expand All @@ -142,6 +150,7 @@ public function __construct(
$this->orderTransactionRepository = $orderTransactionRepository;
$this->adyenPaymentService = $adyenPaymentService;
$this->captureService = $captureService;
$this->paymentResponseService = $paymentResponseService;
self::$webhookHandlerFactory = $webhookHandlerFactory;
}

Expand Down Expand Up @@ -320,10 +329,23 @@ private function getOrderTransaction(
NotificationEntity $notification,
array $logContext
): ?OrderTransactionEntity {
$orderTransaction = $this->orderTransactionRepository->getFirstAdyenOrderTransactionByStates(
$order->getId(),
self::WEBHOOK_TRANSACTION_STATES
);
$orderTransaction = null;

/* Fetch the related order_transaction entity with the given pspreference.
* There might be several order transactions if there have been multiple payment attempts. */
$adyenPaymentResponse = $this->paymentResponseService->getWithPspreference($notification->getPspreference());
if (isset($adyenPaymentResponse)) {
$orderTransaction = $this->orderTransactionRepository->getWithId(
$adyenPaymentResponse->getOrderTransactionId()
);
}

if (is_null($orderTransaction)) {
$orderTransaction = $this->orderTransactionRepository->getFirstAdyenOrderTransactionByStates(
$order->getId(),
self::WEBHOOK_TRANSACTION_STATES
);
}

// Skip if orderTransaction not found (non-Adyen)
if (is_null($orderTransaction)) {
Expand Down
11 changes: 11 additions & 0 deletions src/Service/PaymentResponseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public function getWithOrderId(string $orderId): ?PaymentResponseEntity
return $this->getWithOrderTransaction($orderTransaction);
}

public function getWithPspreference(string $pspreference): ?PaymentResponseEntity
{
$criteria = new Criteria();
$criteria->addFilter((new EqualsFilter('pspreference', $pspreference)));

return $this->repository
->search($criteria, Context::createDefaultContext())
->first();
}

public function getWithOrderTransaction(OrderTransactionEntity $orderTransaction): ?PaymentResponseEntity
{
return $this->repository
Expand All @@ -102,6 +112,7 @@ public function insertPaymentResponse(
$fields['orderTransactionId'] = $orderTransaction->getId();
$fields['resultCode'] = $paymentResponse["resultCode"];
$fields['response'] = json_encode($paymentResponse);
$fields['pspreference'] = $paymentResponse["pspReference"] ?? null;

$this->repository->upsert(
[$fields],
Expand Down
16 changes: 16 additions & 0 deletions src/Service/Repository/OrderTransactionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,20 @@ public function getFirstAdyenOrderTransaction(string $orderId): ?OrderTransactio

return $this->repository->search($criteria, Context::createDefaultContext())->first();
}

/**
* @param string $orderTransactionId
* @return OrderTransactionEntity|null
*/
public function getWithId(string $orderTransactionId): ?OrderTransactionEntity
{
$criteria = new Criteria();
$criteria->addAssociation('order');
$criteria->addAssociation('order.currency');
$criteria->addAssociation('paymentMethod');
$criteria->addAssociation('paymentMethod.plugin');
$criteria->addFilter(new EqualsFilter('id', $orderTransactionId));

return $this->repository->search($criteria, Context::createDefaultContext())->first();
}
}

0 comments on commit a288925

Please sign in to comment.