-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7217666
commit d0dadc9
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
view/frontend/templates/component/payment/method/creditcard.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |