Skip to content

Commit

Permalink
revert hosted fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vegimcarkaxhija committed Jan 24, 2025
1 parent 7217666 commit d0dadc9
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
114 changes: 114 additions & 0 deletions Magewire/Payment/Method/Creditcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Buckaroo\HyvaCheckout\Magewire\Payment\Method;

use Rakit\Validation\Validator;
use Magewirephp\Magewire\Component;
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\Ideal as MethodIdeal;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Creditcard as MethodConfigProvider;

class Creditcard extends Component\Form implements EvaluationInterface
{
public ?string $cardType = null;

protected $loader = [
'cardType' => 'Saving card type'
];

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

protected $messages = [
'cardType:required' => 'A card type is required'
];

protected SessionCheckout $sessionCheckout;

protected CartRepositoryInterface $quoteRepository;

protected ScopeConfigInterface $scopeConfig;

protected MethodConfigProvider $methodConfigProvider;

public function __construct(
Validator $validator,
SessionCheckout $sessionCheckout,
CartRepositoryInterface $quoteRepository,
ScopeConfigInterface $scopeConfig,
MethodConfigProvider $methodConfigProvider
) {
parent::__construct($validator);

$this->sessionCheckout = $sessionCheckout;
$this->quoteRepository = $quoteRepository;
$this->scopeConfig = $scopeConfig;
$this->methodConfigProvider = $methodConfigProvider;
}

/**
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function mount(): void
{
$this->cardType = $this->sessionCheckout
->getQuote()
->getPayment()
->getAdditionalInformation('card_type');
}

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

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

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

return $value;
}
public function evaluateCompletion(EvaluationResultFactory $resultFactory): EvaluationResultInterface
{
if ($this->cardType === null) {
return $resultFactory->createErrorMessageEvent()
->withCustomEvent('payment:method:error')
->withMessage('A card type is required');
}

return $resultFactory->createSuccess();
}

public function getIssuers(): array
{
return $this->methodConfigProvider->formatIssuers();
}

public function displayAsSelect($storeId = null): bool
{
return $this->scopeConfig->getValue(
MethodIdeal::XPATH_IDEAL_SELECTION_TYPE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
) === '2';
}
}
1 change: 1 addition & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<item name="buckaroo_magento2_klarna" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_klarnain" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_klarnakp" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_creditcard" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_creditcards" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_afterpay" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_afterpay2" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
Expand Down
10 changes: 10 additions & 0 deletions view/frontend/layout/hyva_checkout_components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
</arguments>
</block>

<block name="checkout.payment.method.buckaroo_magento2_creditcard"
as="buckaroo_magento2_creditcard"
template="Buckaroo_HyvaCheckout::component/payment/method/creditcard.phtml">
<arguments>
<argument name="magewire" xsi:type="object">
\Buckaroo\HyvaCheckout\Magewire\Payment\Method\Creditcard
</argument>
</arguments>
</block>

<block name="checkout.payment.method.buckaroo_magento2_afterpay20"
as="buckaroo_magento2_afterpay20"
template="Buckaroo_HyvaCheckout::component/payment/method/afterpay20.phtml">
Expand Down
56 changes: 56 additions & 0 deletions view/frontend/templates/component/payment/method/creditcard.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);
/** @var Escaper $escaper */
/** @var \Buckaroo\HyvaCheckout\Magewire\Checkout\Payment\Method\Creditcard $magewire */
use Magento\Framework\Escaper;

$issuers = $magewire->getIssuers();

?>
<div class="col-span-6">
<div class="flex flex-col gap-y-2">
<?php if ($magewire->displayAsSelect()) { ?>
<label for="buckaroo_magento2_creditcard_issuer">
<?= $escaper->escapeHtml(__('Select a Credit Card or Debit Card:')); ?>
</label>
<select name="issuer" id="buckaroo_magento2_creditcard_issuer" wire:model="cardType" class="form-select">
<?php
foreach ($issuers as $issuer) {
?>
<option value="<?= $escaper->escapeHtmlAttr($issuer["code"]) ?>">
<?= $escaper->escapeHtml($issuer["name"]) ?>
</option>
<?php
}
?>
</select>
<?php } else { ?>
<p><?= $escaper->escapeHtml(__('Select a Credit Card or Debit Card:')); ?></p>
<?php
foreach ($issuers as $issuer) {
?>
<div class="flex flex-row flex-grow gap-x-2 items-center">
<input
type="radio"
name="issuer"
wire:model="cardType"
id="bk_credicard_issuer_<?= $escaper->escapeHtmlAttr($issuer["code"]) ?>"
value="<?= $escaper->escapeHtmlAttr($issuer["code"]) ?>"
/>
<label
for="bk_credicard_issuer_<?= $escaper->escapeHtmlAttr($issuer["code"]) ?>"
class="flex flex-row flex-grow gap-x-2 items-center">
<img src="<?= $escaper->escapeUrl($issuer["img"]) ?>"
alt="<?= $issuer["name"] ?>"
style="max-height:25px;"
/>
<?= $escaper->escapeHtml($issuer["name"]) ?>
</label>
</div>
<?php
}
?>
<?php } ?>
</div>
</div>

0 comments on commit d0dadc9

Please sign in to comment.