Skip to content

Commit

Permalink
Merge pull request #42 from buckaroo-it/develop
Browse files Browse the repository at this point in the history
Update test & release (1.2.0)
  • Loading branch information
vegimcarkaxhija authored Sep 23, 2024
2 parents 250e35a + aeeb060 commit d304882
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 149 deletions.
36 changes: 10 additions & 26 deletions Block/Totals/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ class Fee extends \Magento\Framework\View\Element\Template
{
protected PaymentFee $feeHelper;

protected SessionCheckout $sessionCheckout;
protected SessionCheckout $sessionCheckout;

public function __construct(
Context $context,
array $data,
PaymentFee $feeHelper,
Context $context,
array $data,
PaymentFee $feeHelper,
SessionCheckout $sessionCheckout
) {
)
{
parent::__construct($context, $data);
$this->feeHelper = $feeHelper;
$this->sessionCheckout = $sessionCheckout;
}

/**
* Get title based on payment method config
*
Expand All @@ -33,29 +35,11 @@ public function getTitle(): string
{
try {
$payment = $this->sessionCheckout
->getQuote()
->getPayment();
->getQuote()
->getPayment();
return $this->feeHelper->getBuckarooPaymentFeeLabel($payment->getMethod());
} catch (\Throwable $th) {
return __('Fee');
}
return __('Fee');
}

/**
* Get total from array of data
*
* @return float
*/
public function getTotal(): float
{
$totalData = $this->getSegment();
if (
isset($totalData['extension_attributes']['buckaroo_fee']) &&
is_scalar($totalData['extension_attributes']['buckaroo_fee'])
) {
return floatval($totalData['extension_attributes']['buckaroo_fee']);
return __('Payment Fee');
}
return 0;
}
}
33 changes: 31 additions & 2 deletions Magewire/Payment/Method/Giftcards.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Buckaroo\Magento2\Model\Giftcard\Response\Giftcard as GiftcardResponse;
use Buckaroo\Magento2\Model\Giftcard\Request\GiftcardInterface as GiftcardRequest;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Giftcards as MethodConfigProvider;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;

class Giftcards extends Component\Form implements EvaluationInterface
{
Expand All @@ -34,6 +35,7 @@ class Giftcards extends Component\Form implements EvaluationInterface
protected GiftcardResponse $giftcardResponse;

protected Log $logger;
protected $pricingHelper;

public function __construct(
UrlInterface $urlBuilder,
Expand All @@ -42,7 +44,8 @@ public function __construct(
MethodConfigProvider $methodConfigProvider,
GiftcardRequest $giftcardRequest,
GiftcardResponse $giftcardResponse,
Log $logger
Log $logger,
PricingHelper $pricingHelper
) {
$this->urlBuilder = $urlBuilder;
$this->sessionCheckout = $sessionCheckout;
Expand All @@ -51,6 +54,7 @@ public function __construct(
$this->giftcardRequest = $giftcardRequest;
$this->giftcardResponse = $giftcardResponse;
$this->logger = $logger;
$this->pricingHelper = $pricingHelper;
}

/**
Expand Down Expand Up @@ -150,7 +154,32 @@ public function applyGiftcard(
}
}

/**
* Convert and format price value for current store
*
* @param float $price
* @return float|string
*/
public function getFormattedPrice($price)
{
return $this->pricingHelper->currency($price, true, false);
}

public function getRemainingAmount()
{
$quote = $this->sessionCheckout->getQuote();

$grandTotal = round(floatval($quote->getGrandTotal()), 2);

// Get the amount already paid through group transactions
$alreadyPaid = $this->groupTransaction->getAlreadyPaid($quote->getReservedOrderId());

// Calculate the remaining amount
$remainingAmount = $grandTotal - $alreadyPaid;

// Ensure the remaining amount is never negative
$this->emit("remainingAmount", $this->getFormattedPrice($remainingAmount));
}
protected function getGiftcardResponse(Quote $quote, $response)
{
$this->giftcardResponse->set($response, $quote);
Expand Down Expand Up @@ -183,8 +212,8 @@ protected function getGiftcardResponse(Quote $quote, $response)
$this->giftcardResponse->getCurrency()
);
}

return [
'remainder_amount_currency' => $this->getFormattedPrice($remainingAmount),
'remainder_amount' => $remainingAmount,
'already_paid' => $this->giftcardResponse->getAlreadyPaid($quote),
'remaining_amount_message' => $buttonMessage,
Expand Down
184 changes: 184 additions & 0 deletions Magewire/Payment/Method/PayByBank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

namespace Buckaroo\HyvaCheckout\Magewire\Payment\Method;

use Rakit\Validation\Validator;
use Magewirephp\Magewire\Component;
use Magento\Framework\View\Asset\Repository;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Checkout\Model\Session as SessionCheckout;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Hyva\Checkout\Model\Magewire\Component\EvaluationInterface;
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory;
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultInterface;
use Buckaroo\Magento2\Model\ConfigProvider\Method\PayByBank as MethodPayByBank;

class PayByBank extends Component\Form implements EvaluationInterface
{
public ?string $issuer = null;

protected $loader = [
'issuer' => 'Saving Bank issuer'
];

protected $rules = [
'issuer' => 'required'
];

protected $messages = [
'issuer:required' => 'The bank issuer is required'
];

protected SessionCheckout $sessionCheckout;

protected CartRepositoryInterface $quoteRepository;

protected Repository $assetRepo;

protected ScopeConfigInterface $scopeConfig;


public function __construct(
Validator $validator,
SessionCheckout $sessionCheckout,
CartRepositoryInterface $quoteRepository,
Repository $assetRepo,
ScopeConfigInterface $scopeConfig,

) {
parent::__construct($validator);

$this->sessionCheckout = $sessionCheckout;
$this->quoteRepository = $quoteRepository;
$this->assetRepo = $assetRepo;
$this->scopeConfig = $scopeConfig;

}

/**
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function mount(): void
{
$this->issuer = $this->getLastIssuer();
}

/**
* Listen for bank issuer been updated.
*/
public function updatedIssuer(string $value): ?string
{
$this->validateOnly();
$value = empty($value) ? null : $value;

try {
$quote = $this->sessionCheckout->getQuote();
$quote->getPayment()->setAdditionalInformation('issuer', $value);

$this->quoteRepository->save($quote);
} catch (LocalizedException $exception) {
$this->dispatchErrorMessage($exception->getMessage());
}

return $value;
}

public function evaluateCompletion(EvaluationResultFactory $resultFactory): EvaluationResultInterface
{
if ($this->issuer === null) {
return $resultFactory->createErrorMessageEvent()
->withCustomEvent('payment:method:error')
->withMessage('The bank issuer is required');
}

return $resultFactory->createSuccess();
}

public function getIssuers(): array
{
return [
[
'name' => 'ABN AMRO',
'code' => 'ABNANL2A',
'imgName' => 'abnamro'
],
[
'name' => 'ASN Bank',
'code' => 'ASNBNL21',
'imgName' => 'asnbank'
],
[
'name' => 'ING',
'code' => 'INGBNL2A',
'imgName' => 'ing'
],
[
'name' => 'Knab Bank',
'code' => 'KNABNL2H',
'imgName' => 'knab'
],
[
'name' => 'Rabobank',
'code' => 'RABONL2U',
'imgName' => 'rabobank'
],
[
'name' => 'RegioBank',
'code' => 'RBRBNL21',
'imgName' => 'regiobank'
],
[
'name' => 'SNS Bank',
'code' => 'SNSBNL2A',
'imgName' => 'sns'
],
[
'name' => 'N26',
'code' => 'NTSBDEB1',
'imgName' => 'n26'
]
];
}

public function getLastIssuer()
{

$quote = $this->sessionCheckout->getQuote();

$customerId = $quote->getCustomerId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);

$customer = $customerRepository->getById($customerId);

$customAttributes = $customer->getCustomAttributes();
$issuerAttribute = $customAttributes['buckaroo_last_paybybank_issuer'] ?? null;

if ($issuerAttribute) {
return $issuerAttribute->getValue();
} else {
return $issuerAttribute;
}

}

public function getImageUrl(string $issuerImage): string
{
return $this->assetRepo->getUrl("Buckaroo_Magento2::images/ideal/{$issuerImage}.svg");
}

public function displayAsSelect($storeId = null): bool
{
return $this->scopeConfig->getValue(
MethodPayByBank::XPATH_PAYBYBANK_SELECTION_TYPE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
) === '2';
}
}
39 changes: 0 additions & 39 deletions Model/MethodMetaData.php

This file was deleted.

32 changes: 32 additions & 0 deletions Plugin/IconRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Buckaroo\HyvaCheckout\Plugin;

use Hyva\Checkout\Model\MethodMetaData\IconRenderer as IconRendererHyva;
use Magento\Framework\View\Asset\Repository;

class IconRenderer
{
protected Repository $assetRepository;

public function __construct(
Repository $assetRepository
) {
$this->assetRepository = $assetRepository;
}

public function aroundRender(
IconRendererHyva $subject,
callable $proceed,
array $icon
): string {
// Check if this is a Buckaroo method and handle it
if (isset($icon['svg']) && strpos($icon['svg'], 'Buckaroo_') !== false) {
$logoLink = $this->assetRepository->getUrl($icon['svg']);
return '<img style="height:30px" src="' . $logoLink . '">';
}

// Otherwise, proceed with the default behavior
return $proceed($icon);
}
}
Loading

0 comments on commit d304882

Please sign in to comment.