Skip to content

Commit

Permalink
Merge pull request #179 from buckaroo-it/BP-3636-Add-option-to-Enable…
Browse files Browse the repository at this point in the history
…-Disable-automatic-refunds-via-the-Buckaroo-Plaza

add config for refund and validate if setting is on or off when doing…
  • Loading branch information
gentiprenaj authored Sep 3, 2024
2 parents dfbaa39 + 1665835 commit dd79430
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 231 deletions.
19 changes: 12 additions & 7 deletions controllers/admin/AdminRefundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Buckaroo\PrestaShop\Src\Refund\Request\Handler as RefundRequestHandler;
use Buckaroo\PrestaShop\Src\Refund\Request\QuantityBasedBuilder;
use Buckaroo\PrestaShop\Src\Refund\Request\Response\Handler as RefundResponseHandler;
use Buckaroo\PrestaShop\Src\Refund\Settings;
use PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -140,14 +141,18 @@ private function sentRefundRequest(\Order $order, \OrderPayment $payment, float
}

$body = $this->refundBuilder->create($order, $payment, $refundAmount);
$this->responseHandler->parse(
$this->refundHandler->refund(


if (\Configuration::get(Settings::LABEL_REFUND_CONF)){
$this->responseHandler->parse(
$this->refundHandler->refund(
$body,
$payment->payment_method
),
$body,
$payment->payment_method
),
$body,
$order->id
);
$order->id
);
}

return $maxRefundAmount;
}
Expand Down
2 changes: 2 additions & 0 deletions dev/lang/php_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
"dashboard.pages.settings.no_im_testing": "Nein, ich teste",
"dashboard.pages.settings.order_number": "Bestellnummer",
"dashboard.pages.settings.order_number2": "Bestellnummer",
"dashboard.pages.settings.refund_conf": "Automatische Rückerstattungen über Buckaroo",
"dashboard.pages.settings.refund_conf_label": "Aktivieren/deaktivieren Sie automatische Rückerstattungen über Buckaroo, wenn eine Rückerstattung in PrestaShop initiiert wird.",
"dashboard.pages.settings.re_stock_products": "Produkte wieder auffüllen",
"dashboard.pages.settings.re_stock_products_label": "Ob erstattete Produkte wieder in den Lagerbestand aufgenommen werden sollen (Erhöhung der Lagermenge).",
"dashboard.pages.settings.refunds": "Rückerstattungen",
Expand Down
2 changes: 2 additions & 0 deletions dev/lang/php_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
"dashboard.pages.settings.no_im_testing": "No, I'm Testing",
"dashboard.pages.settings.order_number": "Order number",
"dashboard.pages.settings.order_number2": "order_number",
"dashboard.pages.settings.refund_conf": "Automatic refunds via Buckaroo",
"dashboard.pages.settings.refund_conf_label": "Enable/disable automatic refunds via Buckaroo when a refund from PrestaShop is inititated.",
"dashboard.pages.settings.re_stock_products": "Re-stock Products",
"dashboard.pages.settings.re_stock_products_label": "Whether refunded products should be returned to inventory (increasing the quantity in stock).",
"dashboard.pages.settings.refunds": "Refunds",
Expand Down
2 changes: 2 additions & 0 deletions dev/lang/php_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
"dashboard.pages.settings.no_im_testing": "Non, je teste",
"dashboard.pages.settings.order_number": "Numéro de commande",
"dashboard.pages.settings.order_number2": "numéro de commande",
"dashboard.pages.settings.refund_conf": "Remboursements automatiques via Buckaroo",
"dashboard.pages.settings.refund_conf_label": "Activer/désactiver les remboursements automatiques via Buckaroo lorsqu'un remboursement est initié depuis PrestaShop.",
"dashboard.pages.settings.re_stock_products": "Réapprovisionner les produits",
"dashboard.pages.settings.re_stock_products_label": "Si les produits remboursés doivent être retournés en stock (augmentant la quantité en stock).",
"dashboard.pages.settings.refunds": "Remboursements",
Expand Down
2 changes: 2 additions & 0 deletions dev/lang/php_nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
"dashboard.pages.settings.no_im_testing": "Nee, ik wil testen",
"dashboard.pages.settings.order_number": "Bestellingsnummer",
"dashboard.pages.settings.order_number2": "bestellingsnummer",
"dashboard.pages.settings.refund_conf": "Automatische terugbetalingen via Buckaroo",
"dashboard.pages.settings.refund_conf_label": "Automatische terugbetalingen via Buckaroo in- of uitschakelen wanneer een terugbetaling vanuit PrestaShop wordt gestart.",
"dashboard.pages.settings.re_stock_products": "De voorraad aanvullen bij retourproducten",
"dashboard.pages.settings.re_stock_products_label": "Of terugbetaalde producten moeten worden teruggeplaatst in de voorraad (waardoor de voorraadhoeveelheid wordt verhoogd).",
"dashboard.pages.settings.refunds": "Terugbetalingen",
Expand Down
8 changes: 8 additions & 0 deletions dev/src/pages/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@
</div>
</div>
</div>
<ToggleField class="p-5 space-y-5" v-model="settings.refundconf">
<div>
<label for="client-side-mode" class="font-semibold text-sm">
{{ $t('dashboard.pages.settings.refund_conf') }}
</label>
<div class="text-gray-400 text-xs">{{ $t('dashboard.pages.settings.refund_conf_label') }}</div>
</div>
</ToggleField>
<ToggleField class="p-5 space-y-5" v-model="settings.restock">
<div>
<label for="client-side-mode" class="font-semibold text-sm">
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Config
public const BUCKAROO_SECRET_KEY = 'BUCKAROO_SECRET_KEY';
public const BUCKAROO_TRANSACTION_LABEL = 'BUCKAROO_TRANSACTION_LABEL';
public const BUCKAROO_TRANSACTION_FEE = 'BUCKAROO_TRANSACTION_FEE';

public const LABEL_REFUND_CONF = 'BUCKAROO_REFUND_CONF';
public const LABEL_REFUND_RESTOCK = 'BUCKAROO_REFUND_RESTOCK';
public const LABEL_REFUND_CREDIT_SLIP = 'BUCKAROO_REFUND_CREDIT_SLIP';
public const LABEL_REFUND_VOUCHER = 'BUCKAROO_REFUND_VOUCHER';
Expand Down
1 change: 1 addition & 0 deletions src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ protected function initConfig()
\Configuration::updateValue(Config::BUCKAROO_TRANSACTION_LABEL, '');
\Configuration::updateValue(Config::BUCKAROO_TRANSACTION_FEE, '');

\Configuration::updateValue(Config::LABEL_REFUND_CONF, true);
\Configuration::updateValue(Config::LABEL_REFUND_RESTOCK, false);
\Configuration::updateValue(Config::LABEL_REFUND_CREDIT_SLIP, true);
\Configuration::updateValue(Config::LABEL_REFUND_VOUCHER, false);
Expand Down
1 change: 1 addition & 0 deletions src/Install/Uninstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private function deleteConfig()
Config::BUCKAROO_SECRET_KEY,
Config::BUCKAROO_TRANSACTION_LABEL,
Config::BUCKAROO_TRANSACTION_FEE,
Config::LABEL_REFUND_CONF,
Config::LABEL_REFUND_RESTOCK,
Config::LABEL_REFUND_CREDIT_SLIP,
Config::LABEL_REFUND_VOUCHER,
Expand Down
17 changes: 10 additions & 7 deletions src/Refund/Decorators/IssuePartialRefundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Buckaroo\PrestaShop\Src\Refund\Decorators;

use Buckaroo\PrestaShop\Src\Refund\Handler;
use Buckaroo\PrestaShop\Src\Refund\Settings;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\IssuePartialRefundCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\CommandHandler\IssuePartialRefundHandlerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Expand Down Expand Up @@ -59,13 +60,15 @@ public function __construct(
*/
public function handle(IssuePartialRefundCommand $command): void
{
$refundSummary = $this->refundHandler->getRefundSummary($command);
$this->handler->handle($command);
if (
!$this->session->has(self::KEY_SKIP_REFUND_REQUEST)
) {
$this->refundHandler->execute($command, $refundSummary);
$this->session->remove(self::KEY_SKIP_REFUND_REQUEST);
if (\Configuration::get(Settings::LABEL_REFUND_CONF)){
$refundSummary = $this->refundHandler->getRefundSummary($command);
$this->handler->handle($command);
if (
!$this->session->has(self::KEY_SKIP_REFUND_REQUEST)
) {
$this->refundHandler->execute($command, $refundSummary);
$this->session->remove(self::KEY_SKIP_REFUND_REQUEST);
}
}
}
}
35 changes: 18 additions & 17 deletions src/Refund/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,27 @@ public function __construct(

public function refund(\Order $order, float $amount)
{
$refundData = $this->determineRefundData($order, $amount);

$createCreditSlipValue = \Configuration::get(Settings::LABEL_REFUND_CREDIT_SLIP, null, null, null, true);
if (!is_scalar($createCreditSlipValue)) {
$createCreditSlipValue = true;
}
$refundData = $this->determineRefundData($order, $amount);

$command = new IssuePartialRefundCommand(
$order->id,
$refundData['products'],
$refundData['shipping_amount'],
boolval(\Configuration::get(Settings::LABEL_REFUND_RESTOCK)),
(bool) $createCreditSlipValue,
boolval(\Configuration::get(Settings::LABEL_REFUND_VOUCHER)),
VoucherRefundType::PRODUCT_PRICES_EXCLUDING_VOUCHER_REFUND
);
$createCreditSlipValue = \Configuration::get(Settings::LABEL_REFUND_CREDIT_SLIP, null, null, null, true);
if (!is_scalar($createCreditSlipValue)) {
$createCreditSlipValue = true;
}

$command = new IssuePartialRefundCommand(
$order->id,
$refundData['products'],
$refundData['shipping_amount'],
boolval(\Configuration::get(Settings::LABEL_REFUND_RESTOCK)),
(bool) $createCreditSlipValue,
boolval(\Configuration::get(Settings::LABEL_REFUND_VOUCHER)),
VoucherRefundType::PRODUCT_PRICES_EXCLUDING_VOUCHER_REFUND
);

$this->session->set('buckaroo_skip_refund', true);
$this->commandBus->handle($command);
$this->session->remove('buckaroo_skip_refund');
$this->session->set('buckaroo_skip_refund', true);
$this->commandBus->handle($command);
$this->session->remove('buckaroo_skip_refund');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Refund/Request/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Buckaroo\BuckarooClient;
use Buckaroo\PrestaShop\Classes\Config;
use Buckaroo\PrestaShop\Src\Refund\Settings;
use Buckaroo\Transaction\Response\TransactionResponse;

if (!defined('_PS_VERSION_')) {
Expand All @@ -38,7 +39,6 @@ class Handler
public function refund(array $body, string $method): TransactionResponse
{
$buckaroo = $this->getClient($method);

return $buckaroo->method($method)->refund($body);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Refund/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class Settings
{
public const LABEL_REFUND_CONF = 'BUCKAROO_REFUND_CONF';
public const LABEL_REFUND_RESTOCK = 'BUCKAROO_REFUND_RESTOCK';
public const LABEL_REFUND_CREDIT_SLIP = 'BUCKAROO_REFUND_CREDIT_SLIP';
public const LABEL_REFUND_VOUCHER = 'BUCKAROO_REFUND_VOUCHER';
Expand All @@ -40,6 +41,11 @@ public function getFormFields($module): array
'position' => 0.5,
'enabled' => true,
'input' => [
[
'type' => 'bool',
'name' => self::LABEL_REFUND_CONF,
'label' => $module->l('Re-fund products'),
],
[
'type' => 'bool',
'name' => self::LABEL_REFUND_RESTOCK,
Expand Down Expand Up @@ -73,6 +79,7 @@ public function getFormFields($module): array
public function getValues(): array
{
return [
self::LABEL_REFUND_CONF => \Configuration::get(self::LABEL_REFUND_CONF),
self::LABEL_REFUND_RESTOCK => \Configuration::get(self::LABEL_REFUND_RESTOCK),
self::LABEL_REFUND_CREDIT_SLIP => \Configuration::get(self::LABEL_REFUND_CREDIT_SLIP),
self::LABEL_REFUND_VOUCHER => \Configuration::get(self::LABEL_REFUND_VOUCHER),
Expand All @@ -82,6 +89,7 @@ public function getValues(): array

public function install()
{
\Configuration::updateValue(self::LABEL_REFUND_CONF, false);
\Configuration::updateValue(self::LABEL_REFUND_RESTOCK, false);
\Configuration::updateValue(self::LABEL_REFUND_CREDIT_SLIP, true);
\Configuration::updateValue(self::LABEL_REFUND_VOUCHER, false);
Expand All @@ -90,6 +98,7 @@ public function install()

public function uninstall()
{
\Configuration::deleteByName(self::LABEL_REFUND_CONF);
\Configuration::deleteByName(self::LABEL_REFUND_RESTOCK);
\Configuration::deleteByName(self::LABEL_REFUND_CREDIT_SLIP);
\Configuration::deleteByName(self::LABEL_REFUND_VOUCHER);
Expand All @@ -98,6 +107,7 @@ public function uninstall()

public function updateAll()
{
\Configuration::updateValue(self::LABEL_REFUND_CONF, \Tools::getValue(self::LABEL_REFUND_CONF));
\Configuration::updateValue(self::LABEL_REFUND_RESTOCK, \Tools::getValue(self::LABEL_REFUND_RESTOCK));
\Configuration::updateValue(self::LABEL_REFUND_CREDIT_SLIP, \Tools::getValue(self::LABEL_REFUND_CREDIT_SLIP));
\Configuration::updateValue(self::LABEL_REFUND_VOUCHER, \Tools::getValue(self::LABEL_REFUND_VOUCHER));
Expand Down
4 changes: 3 additions & 1 deletion src/Service/BuckarooSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getSettings()
'website_key' => \Configuration::get(Config::BUCKAROO_MERCHANT_KEY),
'secret_key' => \Configuration::get(Config::BUCKAROO_SECRET_KEY),
'transaction_description' => \Configuration::get(Config::BUCKAROO_TRANSACTION_LABEL),
'refundconf' => (bool) \Configuration::get(Config::LABEL_REFUND_CONF),
'restock' => (bool) \Configuration::get(Config::LABEL_REFUND_RESTOCK),
'creditSlip' => (bool) \Configuration::get(Config::LABEL_REFUND_CREDIT_SLIP),
'voucher' => (bool) \Configuration::get(Config::LABEL_REFUND_VOUCHER),
Expand All @@ -47,7 +48,7 @@ public function isValidData($data)
$requiredKeys = [
'website_key', 'secret_key',
'transaction_description', 'is_live',
'restock', 'creditSlip',
'refundconf', 'restock', 'creditSlip',
'voucher', 'negativePayment',
'payment_fee_mode', 'payment_fee_frontend_label'
];
Expand All @@ -66,6 +67,7 @@ public function updateSettings($data)
\Configuration::updateValue(Config::BUCKAROO_SECRET_KEY, $data['secret_key']);
\Configuration::updateValue(Config::BUCKAROO_TRANSACTION_LABEL, $data['transaction_description']);
\Configuration::updateValue(Config::BUCKAROO_TEST, $data['is_live']);
\Configuration::updateValue(Config::LABEL_REFUND_CONF, $data['refundconf']);
\Configuration::updateValue(Config::LABEL_REFUND_RESTOCK, $data['restock']);
\Configuration::updateValue(Config::LABEL_REFUND_CREDIT_SLIP, $data['creditSlip']);
\Configuration::updateValue(Config::LABEL_REFUND_VOUCHER, $data['voucher']);
Expand Down
Loading

0 comments on commit dd79430

Please sign in to comment.