Skip to content
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
5 changes: 4 additions & 1 deletion CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.5.4
* Aktualisierung iDEAL Payment Naming

# 6.5.3
* **Wichtiger Hotfix**
* Dieser Hotfix behebt ein Problem, bei dem einige Webhooks in Shopware 6.6 nicht korrekt registriert wurden.
Expand Down Expand Up @@ -69,7 +72,7 @@
* EPS: Entfernung Bank Feld

# 6.2.1
* Cardholder Name in Kreditkarten-Checkout hinzugefügt
* Cardholder Name in Kreditkarten-Checkout hinzugefügt

# 6.2.0
* TWINT als weitere Zahlungsart hinzugefügt
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.5.4
* Updated iDEAL Payment naming

# 6.5.3
* **Important hotfix**
* This hotfix will fix an issue where some webhooks were not correctly registered in Shopware 6.6.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "unzerdev/shopware6",
"description": "Unzer payment integration for Shopware 6",
"version": "6.5.3",
"version": "6.5.4",
"type": "shopware-platform-plugin",
"license": "Apache-2.0",
"minimum-stability": "dev",
Expand Down
11 changes: 6 additions & 5 deletions src/Components/CancelService/CancelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function cancelChargeById(string $orderTransactionId, string $chargeId, f
}

$this->updateOrderStatus($client, $transaction, $context);

return $responseCancellation;
}

Expand Down Expand Up @@ -123,6 +124,11 @@ public function cancelAuthorizationById(string $orderTransactionId, string $paym
$this->updateOrderStatus($client, $transaction, $context);
}

public function isPaylaterPaymentMethod(string $paymentMethodId): bool
{
return \in_array($paymentMethodId, self::PAYLATER_PAYMENT_METHODS, true);
}

protected function getOrderTransaction(string $orderTransactionId, Context $context): ?OrderTransactionEntity
{
$criteria = new Criteria([$orderTransactionId]);
Expand All @@ -141,11 +147,6 @@ protected function getCancelReasonCode(?string $reasonCode): string
return $reasonCode ?? CancelReasonCodes::REASON_CODE_CANCEL;
}

public function isPaylaterPaymentMethod(string $paymentMethodId): bool
{
return \in_array($paymentMethodId, self::PAYLATER_PAYMENT_METHODS, true);
}

private function updateOrderStatus(Unzer $client, OrderTransactionEntity $orderTransaction, Context $context): void
{
try {
Expand Down
124 changes: 57 additions & 67 deletions src/Components/PaymentActions/PaymentActionService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
<?php declare(strict_types=1);

namespace UnzerPayment6\Components\PaymentActions;

use Kiener\MolliePayments\Components\RefundManager\DAL\RefundItem\RefundItemEntity;
use Psr\Log\LoggerInterface;
use Shopware\Commercial\ReturnManagement\Entity\OrderReturn\OrderReturnEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
Expand All @@ -13,7 +12,6 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Throwable;
use UnzerPayment6\Components\CancelService\CancelServiceInterface;
use UnzerPayment6\Components\ClientFactory\ClientFactoryInterface;
use UnzerPayment6\Components\PaymentActions\Struct\RefundItem;
Expand All @@ -24,34 +22,33 @@
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\TransactionTypes\Charge;

class PaymentActionService{
class PaymentActionService
{
public function __construct(
protected EntityRepository $orderTransactionRepository,
protected ClientFactoryInterface $clientFactory,
protected EntityRepository $orderTransactionRepository,
protected ClientFactoryInterface $clientFactory,
protected TransactionStateHandlerInterface $transactionStateHandler,
protected CancelServiceInterface $cancelService,
private EntityRepository $productRepository,
private EntityRepository $orderLineItemRepository,
protected LoggerInterface $logger,
protected UnzerTransactionUtil $unzerTransactionUtil,
protected CancelServiceInterface $cancelService,
private EntityRepository $productRepository,
private EntityRepository $orderLineItemRepository,
protected LoggerInterface $logger,
protected UnzerTransactionUtil $unzerTransactionUtil,
protected ?EntityRepository $orderReturnRepository = null
)
{
) {
}


/**
* @throws \Exception
*/
public function captureOrder(OrderEntity $order, Context $context): bool
{
$this->logger->info('Capturing order', ['order' => $order->getId()]);
$orderTransaction = $this->unzerTransactionUtil->getOrderTransactionFromOrder($order, $context);

if ($orderTransaction === null) {
return false;
}

$this->logger->info('Capturing order', ['order' => $order->getId()]);
$client = $this->clientFactory->createClient(KeyPairContext::createFromOrderTransaction($orderTransaction));
try {
$charge = $client->performChargeOnPayment($orderTransaction->getId(), new Charge($orderTransaction->getAmount()->getTotalPrice()));
Expand All @@ -67,21 +64,18 @@ public function captureOrder(OrderEntity $order, Context $context): bool
return true;
}




/**
* @throws \Exception
*/
public function refundOrder(OrderEntity $order, Context $context): void
{
$this->logger->info('Refunding order', ['order' => $order->getId()]);
$orderTransaction = $this->unzerTransactionUtil->getOrderTransactionFromOrder($order, $context);

if ($orderTransaction === null) {
return;
}

$this->logger->info('Refunding order', ['order' => $order->getId()]);
$client = $this->clientFactory->createClient(KeyPairContext::createFromOrderTransaction($orderTransaction));
try {
$payment = $client->fetchPayment($orderTransaction->getId());
Expand All @@ -98,7 +92,7 @@ public function refundOrder(OrderEntity $order, Context $context): void
null,
$context
);
} catch (Throwable $e) {
} catch (\Throwable $e) {
$this->logger->error('Error while refunding charge', ['charge' => $charge->getId(), 'error' => $e->getMessage()]);
}
}
Expand All @@ -113,7 +107,7 @@ public function refundOrder(OrderEntity $order, Context $context): void
$authorization->getAmount() - $authorization->getCancelledAmount(),
$context
);
} catch (Throwable $e) {
} catch (\Throwable $e) {
$this->logger->error('Error while refunding authorization', ['authorization' => $authorization->getId(), 'error' => $e->getMessage()]);
}
}
Expand All @@ -127,14 +121,11 @@ public function refundOrder(OrderEntity $order, Context $context): void
}
}



public function executeReturnRefunds(OrderEntity $order, Context $context): void
{
$this->logger->info('Refunding order based on returns', ['order' => $order->getId()]);

if($this->orderReturnRepository === null) {
if ($this->orderReturnRepository === null) {
$this->logger->warning('Returns repository does not exist');

return;
}

Expand All @@ -144,9 +135,10 @@ public function executeReturnRefunds(OrderEntity $order, Context $context): void
return;
}

$this->logger->info('Refunding order based on returns', ['order' => $order->getId()]);
$returnsToProcess = $this->getUnprocessedReturns($orderTransaction, $context);

foreach($returnsToProcess as $returnEntity) {
foreach ($returnsToProcess as $returnEntity) {
$items = new RefundItemCollection();
foreach ($returnEntity->getLineItems() as $lineItem) {
$refundItem = new RefundItem(
Expand All @@ -163,8 +155,8 @@ public function executeReturnRefunds(OrderEntity $order, Context $context): void
amount: $returnEntity->getAmountTotal(),
context: $context,
items: $items,
comment: 'SW Auto Refund from order #'.$order->getOrderNumber().' return #'.$returnEntity->getReturnNumber(),
referenceText: $order->getOrderNumber().'/'.$returnEntity->getReturnNumber()
comment: 'SW Auto Refund from order #' . $order->getOrderNumber() . ' return #' . $returnEntity->getReturnNumber(),
referenceText: $order->getOrderNumber() . '/' . $returnEntity->getReturnNumber()
);

$transactionCustomFields = $orderTransaction->getCustomFields() ?? [];
Expand All @@ -191,46 +183,14 @@ public function executeReturnRefunds(OrderEntity $order, Context $context): void
],
], $context);
}

}

/**
* @return OrderReturnEntity[]
*/
protected function getUnprocessedReturns(OrderTransactionEntity $orderTransaction, Context $context): array
{
if ($this->orderReturnRepository === null) {
return [];
}

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderId', $orderTransaction->getOrderId()));
$criteria->addAssociation('lineItems.lineItem.orderLineItem');

$returns = $this->orderReturnRepository->search($criteria, $context);

$transactionCustomFields = $orderTransaction->getCustomFields() ?? [];
$processedReturns = $transactionCustomFields['unzerRefundDetails']['processedReturns'] ?? [];

$unprocessedReturns = [];

foreach ($returns as $return) {
if (isset($processedReturns[$return->getId()])) {
continue;
}

$unprocessedReturns[] = $return;
}

return $unprocessedReturns;
}

public function doUnifiedRefund(OrderTransactionEntity $orderTransaction, float $amount, Context $context, ?RefundItemCollection $items = null, string $comment = '', string $referenceText = ''): string
{
$client = $this->clientFactory->createClient(KeyPairContext::createFromOrderTransaction($orderTransaction));
$payment = UnzerTransactionUtil::fetchPaymentFromOrderTransaction($orderTransaction, $client);
$charges = $payment->getCharges();
$charge = reset($charges);//TODO
$charge = reset($charges); // TODO

$cancellation = $this->cancelService->cancelChargeById($orderTransaction->getId(), $charge->getId(), $amount, null, $context, $referenceText);

Expand All @@ -240,7 +200,7 @@ public function doUnifiedRefund(OrderTransactionEntity $orderTransaction, float
}

$transactionCustomFields['unzerRefundDetails'][$cancellation->getId()] = [
'items' => $items?$items->jsonSerialize():[],
'items' => $items ? $items->jsonSerialize() : [],
'comment' => $comment,
'cancellation' => $cancellation->expose(),
];
Expand All @@ -252,15 +212,45 @@ public function doUnifiedRefund(OrderTransactionEntity $orderTransaction, float

$orderTransaction->setCustomFields($transactionCustomFields);

if($items !== null && $items->count() > 0) {
if ($items !== null && $items->count() > 0) {
$this->processRefundItems($items, $context);
}

return $cancellation->getId();
}

/**
* @return OrderReturnEntity[]
*/
protected function getUnprocessedReturns(OrderTransactionEntity $orderTransaction, Context $context): array
{
if ($this->orderReturnRepository === null) {
return [];
}

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderId', $orderTransaction->getOrderId()));
$criteria->addAssociation('lineItems.lineItem.orderLineItem');

$returns = $this->orderReturnRepository->search($criteria, $context);

$transactionCustomFields = $orderTransaction->getCustomFields() ?? [];
$processedReturns = $transactionCustomFields['unzerRefundDetails']['processedReturns'] ?? [];

$unprocessedReturns = [];

foreach ($returns as $return) {
if (isset($processedReturns[$return->getId()])) {
continue;
}

$unprocessedReturns[] = $return;
}

return $unprocessedReturns;
}

protected function processRefundItems(RefundItemCollection $items, Context $context)
protected function processRefundItems(RefundItemCollection $items, Context $context): void
{
foreach ($items->getElements() as $item) {
$lineItemId = $item->getId();
Expand Down Expand Up @@ -292,7 +282,7 @@ protected function processRefundItems(RefundItemCollection $items, Context $cont
}
if ($quantity > 0) {
$customFields = $lineItem->getCustomFields();
if (!is_array($customFields)) {
if (!\is_array($customFields)) {
$customFields = [];
}

Expand All @@ -306,4 +296,4 @@ protected function processRefundItems(RefundItemCollection $items, Context $cont
}
}
}
}
}
7 changes: 2 additions & 5 deletions src/Components/PaymentActions/Struct/RefundItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

class RefundItem extends Struct
{


public function __construct(
protected string $id,
protected int $quantity = 0,
protected float $amount = 0.0,
protected int $resetStockQuantity = 0,
protected ?string $label = null
)
{
) {
}

public static function fromArray(array $data): self
Expand Down Expand Up @@ -55,4 +52,4 @@ public function getLabel(): ?string
{
return $this->label;
}
}
}
5 changes: 3 additions & 2 deletions src/Components/PaymentActions/Struct/RefundItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ public static function fromArray(array $items): self
return $collection;
}

public function jsonSerialize():array
public function jsonSerialize(): array
{
$return = [];
foreach ($this->getElements() as $item) {
$return[] = $item->jsonSerialize();
}

return $return;
}

protected function getExpectedClass(): ?string
{
return RefundItem::class;
}
}
}
Loading
Loading