Skip to content

Commit

Permalink
MOL-199 Partial Refunds (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
KienerNL authored Jul 15, 2021
1 parent 592bcde commit 04f1b02
Show file tree
Hide file tree
Showing 30 changed files with 1,925 additions and 637 deletions.
498 changes: 201 additions & 297 deletions src/Controller/Api/RefundController.php

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/Exception/CouldNotCancelMollieRefundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Kiener\MolliePayments\Exception;

class CouldNotCancelMollieRefundException extends \RuntimeException
{
public function __construct(
string $mollieOrderId,
string $orderNumber,
string $refundId,
?\Throwable $previous = null
)
{
$message = sprintf("Could not cancel the refund with id %s for order %s (Order number %s)",
$refundId,
$mollieOrderId,
$orderNumber
);

parent::__construct($message, 0, $previous);
}
}
20 changes: 20 additions & 0 deletions src/Exception/CouldNotCreateMollieRefundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Kiener\MolliePayments\Exception;

class CouldNotCreateMollieRefundException extends \RuntimeException
{
public function __construct(
string $mollieOrderId,
string $orderNumber,
?\Throwable $previous = null
)
{
$message = sprintf("Could not create a refund for order %s (Order number %s)",
$mollieOrderId,
$orderNumber
);

parent::__construct($message, 0, $previous);
}
}
15 changes: 15 additions & 0 deletions src/Exception/CouldNotExtractMollieOrderIdException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kiener\MolliePayments\Exception;

class CouldNotExtractMollieOrderIdException extends \RuntimeException
{
public function __construct(
string $orderNumber,
?\Throwable $previous = null
)
{
$message = sprintf('Could not extract the Mollie Order ID for order with number %s', $orderNumber);
parent::__construct($message, 0, $previous);
}
}
15 changes: 15 additions & 0 deletions src/Exception/CouldNotFetchMollieOrderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kiener\MolliePayments\Exception;

class CouldNotFetchMollieOrderException extends \RuntimeException
{
public function __construct(
string $mollieOrderId,
?\Throwable $previous = null
)
{
$message = sprintf("Could not fetch the Mollie Order for ID %s", $mollieOrderId);
parent::__construct($message, 0, $previous);
}
}
20 changes: 20 additions & 0 deletions src/Exception/CouldNotFetchMollieRefundsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Kiener\MolliePayments\Exception;

class CouldNotFetchMollieRefundsException extends \RuntimeException
{
public function __construct(
string $mollieOrderId,
string $orderNumber,
?\Throwable $previous = null
)
{
$message = sprintf(
"Could not fetch refunds for the Mollie Order with ID %s (Order number %s)",
$mollieOrderId,
$orderNumber
);
parent::__construct($message, 0, $previous);
}
}
1 change: 0 additions & 1 deletion src/Exception/CouldNotSetRefundAtMollieException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Kiener\MolliePayments\Exception;


class CouldNotSetRefundAtMollieException extends \Exception
{

Expand Down
15 changes: 15 additions & 0 deletions src/Exception/PaymentNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kiener\MolliePayments\Exception;

class PaymentNotFoundException extends \RuntimeException
{
public function __construct(
string $mollieOrderId,
?\Throwable $previous = null
)
{
$message = sprintf('A payment for the Mollie order %s could not be found', $mollieOrderId);
parent::__construct($message, 0, $previous);
}
}
66 changes: 19 additions & 47 deletions src/Facade/SetMollieOrderRefunded.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,28 @@
namespace Kiener\MolliePayments\Facade;

use Kiener\MolliePayments\Exception\CouldNotSetRefundAtMollieException;
use Kiener\MolliePayments\Exception\MissingSalesChannelInOrder;
use Kiener\MolliePayments\Factory\MollieApiFactory;
use Kiener\MolliePayments\Service\MollieApi\Order;
use Kiener\MolliePayments\Service\RefundService;
use Kiener\MolliePayments\Service\TransactionService;
use Mollie\Api\Exceptions\ApiException;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;

class SetMollieOrderRefunded
{
/**
* @var Order
* @var RefundService
*/
private $mollieOrderService;
/**
* @var MollieApiFactory
*/
private $apiFactory;
private $refundService;

/**
* @var TransactionService
*/
private $transactionService;

public function __construct(TransactionService $transactionService, Order $mollieOrderService, MollieApiFactory $apiFactory)
public function __construct(TransactionService $transactionService, RefundService $refundService)
{
$this->transactionService = $transactionService;
$this->mollieOrderService = $mollieOrderService;
$this->apiFactory = $apiFactory;
$this->refundService = $refundService;
}

/**
Expand All @@ -59,41 +51,21 @@ public function setRefunded(string $orderTransactionId, Context $context): void
);
}

$customFields = $order->getCustomFields() ?? [];

$mollieOrderId = $customFields['mollie_payments']['order_id'] ?? '';
$refunded = $this->refundService->getRefundedAmount($order, $context);
$toRefund = $order->getAmountTotal() - $refunded;

if (empty($mollieOrderId)) {
throw new CouldNotSetRefundAtMollieException(
sprintf('Could not find a mollie order id in order %s for transaction %s ',
$order->getOrderNumber(),
$transaction->getId()
)
);
if ($toRefund <= 0.0) {
return;
}

$salesChannel = $order->getSalesChannel();

if (!$salesChannel instanceof SalesChannelEntity) {
throw new MissingSalesChannelInOrder($order->getOrderNumber() ?? $order->getId());
}

$apiClient = $this->apiFactory->getClient($salesChannel->getId(), $context);

try {
$mollieOrder = $apiClient->orders->get($mollieOrderId);
$mollieOrder->refundAll();
} catch (ApiException $e) {
throw new CouldNotSetRefundAtMollieException(
sprintf('Could not refund at mollie for transaction %s with mollieOrderId %s',
$orderTransactionId,
$mollieOrderId
),
0,
$e
);
}
$this->refundService->refund(
$order,
$toRefund,
sprintf(
"Refunded entire order through Shopware Administration. Order number %s",
$order->getOrderNumber()
),
$context
);
}


}
47 changes: 47 additions & 0 deletions src/Hydrator/RefundHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace Kiener\MolliePayments\Hydrator;

use Mollie\Api\Resources\Refund;

class RefundHydrator
{
/**
* @param Refund $refund
* @return array<string, mixed>
*/
public function hydrate(Refund $refund): array
{
$amount = null;
if ($refund->amount instanceof \stdClass) {
$amount = [
'value' => $refund->amount->value,
'currency' => $refund->amount->currency,
];
}

$settlementAmount = null;
if ($refund->settlementAmount instanceof \stdClass) {
$settlementAmount = [
'value' => $refund->settlementAmount->value,
'currency' => $refund->settlementAmount->currency,
];
}

return [
'id' => $refund->id,
'orderId' => $refund->orderId,
'paymentId' => $refund->paymentId,
'amount' => $amount,
'settlementAmount' => $settlementAmount,
'description' => $refund->description,
'createdAt' => $refund->createdAt,
'status' => $refund->status,
'isFailed' => $refund->isFailed(),
'isPending' => $refund->isPending(),
'isProcessing' => $refund->isProcessing(),
'isQueued' => $refund->isQueued(),
'isTransferred' => $refund->isTransferred(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@ class MolliePaymentsRefundService extends ApiService {
super(httpClient, loginService, apiEndpoint);
}

refund(data = {itemId: null, versionId: null, quantity: null, createCredit: null}) {
const headers = this.getBasicHeaders();

__post(endpoint = '', data = {}, headers = {}) {
return this.httpClient
.post(
`_action/${this.getApiBasePath()}/refund`,
`_action/${this.getApiBasePath()}/refund${endpoint}`,
JSON.stringify(data),
{
headers: headers
headers: this.getBasicHeaders(headers)
}
)
.then((response) => {
return ApiService.handleResponse(response);
});
}

total(data = {orderId: null}) {
const headers = this.getBasicHeaders();
refund(data = {orderId: null, amount: null}) {
return this.__post('', data);
}

return this.httpClient
.post(
`_action/${this.getApiBasePath()}/refund/total`,
JSON.stringify(data),
{
headers: headers
}
)
.then((response) => {
return ApiService.handleResponse(response);
});
cancel(data = {orderId: null, refundId: null}) {
return this.__post('/cancel', data);
}

list(data = {orderId: null}) {
return this.__post('/list', data);
}

total(data = {orderId: null}) {
return this.__post('/total', data);
}
}

export default MolliePaymentsRefundService;
export default MolliePaymentsRefundService;
Loading

0 comments on commit 04f1b02

Please sign in to comment.