Skip to content

Commit

Permalink
Novo layout do cartão de crédito na página de checkout (#138)
Browse files Browse the repository at this point in the history
* feat: removed deprecated magento 2 classes

* feat: removed deprecated magento 2 classes

* fix: remove unused patch

* fix: call non existing class

* fix: return composer version

* Versionamento da 2.1.0

* feat: new card layout

* fix: same input id for different fields

* fix: cacheable false for product view

* fix: add cache key info product block

* feat: added all available brands

---------

Co-authored-by: Iago Cedran <[email protected]>
Co-authored-by: Thiago Contardi <[email protected]>
Co-authored-by: Thais Kusuki <[email protected]>
Co-authored-by: Thais Kusuki <[email protected]>
  • Loading branch information
5 people authored Jan 9, 2025
1 parent 265bc9d commit ca8fe90
Show file tree
Hide file tree
Showing 21 changed files with 4,517 additions and 309 deletions.
2 changes: 1 addition & 1 deletion Block/InfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getCcValue($totalQtyCard = 1, $cardPosition = 1)
*/
public function getCcBrand()
{
$brands = $this->paymentMethod->getCreditCardTypes();
$brands = $this->paymentMethod->getCreditCardCodes();
$CardCode = $this->getOrder()->getPayment()->getCcType();

return isset($brands[$CardCode]) ? $brands[$CardCode] : null;
Expand Down
25 changes: 25 additions & 0 deletions Block/Product/ProductRecurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public function __construct(
$this->_localeFormat = $localeFormat;
}

/**
* Get cache key informative items
*
* @return array
*/
public function getCacheKeyInfo()
{
return [
'BLOCK_TPL',
$this->_storeManager->getStore()->getCode(),
$this->getTemplateFile(),
'base_url' => $this->getBaseUrl(),
'template' => $this->getTemplate(),
'current_product' => $this->getCurrentProduct()->getId(),
];
}

/**
* Returns the current product from the registry.
*
Expand All @@ -82,6 +99,14 @@ public function getCurrentProduct()
return $this->_registry->registry('current_product');
}

public function getProductId(): int
{
try {
return $this->getCurrentProduct()->getId();
} catch (\Exception $e) {
return 0;
}
}
/**
* Returns the name of a plan by its ID.
*
Expand Down
20 changes: 14 additions & 6 deletions Controller/PaymentProfile/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\View\Result\PageFactory;
use Vindi\Payment\Model\Payment\PaymentMethod;
use Vindi\Payment\Model\Payment\Profile as PaymentProfileManager;
use Vindi\Payment\Model\PaymentProfileFactory;
use Vindi\Payment\Model\PaymentProfileRepository;
Expand Down Expand Up @@ -80,6 +81,11 @@ class Save extends Action
*/
protected $api;

/**
* @var PaymentMethod
*/
protected $paymentMethod;

/**
* @param Context $context
* @param PageFactory $resultPageFactory
Expand All @@ -93,6 +99,7 @@ class Save extends Action
* @param SubscriptionFactory $subscriptionFactory
* @param SubscriptionResource $subscriptionResource
* @param Api $api
* @param PaymentMethod $paymentMethod
*/
public function __construct(
Context $context,
Expand All @@ -106,7 +113,8 @@ public function __construct(
VindiCustomer $vindiCustomer,
SubscriptionFactory $subscriptionFactory,
SubscriptionResource $subscriptionResource,
Api $api
Api $api,
PaymentMethod $paymentMethod
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
Expand All @@ -120,6 +128,7 @@ public function __construct(
$this->subscriptionFactory = $subscriptionFactory;
$this->subscriptionResource = $subscriptionResource;
$this->api = $api;
$this->paymentMethod = $paymentMethod;
}

/**
Expand Down Expand Up @@ -210,7 +219,7 @@ public function execute()
* @param int $customerId
* @return array
*/
private function formatPaymentProfileData($data, $customerId)
private function formatPaymentProfileData(array $data, $customerId): array
{
$cardNumber = preg_replace('/\D/', '', $data['cc_number']);

Expand All @@ -219,17 +228,16 @@ private function formatPaymentProfileData($data, $customerId)
$cardExpiration = $expirationParts[0] . '/' . $expirationYear;
$paymentCompanyCode = $data['cc_type'];

$formattedData = [
$ccTypeCode = $this->paymentMethod->getCreditCardApiCode($paymentCompanyCode);
return [
'holder_name' => $data['cc_name'],
'card_expiration' => $cardExpiration,
'card_number' => $cardNumber,
'card_cvv' => $data['cc_cvv'],
'customer_id' => $customerId,
'payment_company_code' => $paymentCompanyCode,
'payment_company_code' => $ccTypeCode,
'payment_method_code' => 'credit_card',
];

return $formattedData;
}

/**
Expand Down
25 changes: 24 additions & 1 deletion Model/Config/Source/CardImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,39 @@ public function toOptionArray()
return [
[
'value' => 'mc.png',
'code' => 'MC',
'label' => __('mastercard')
],
[
'value' => 'vi.png',
'code' => 'VI',
'label' => __('visa')
],
[
'value' => 'ae.png',
'code' => 'AE',
'label' => __('american_express')
]
],
[
'value' => 'elo.png',
'code' => 'ELO',
'label' => __('elo')
],
[
'value' => 'hc.png',
'code' => 'HC',
'label' => __('hipercard')
],
[
'value' => 'dn.png',
'code' => 'DN',
'label' => __('diners_club')
],
[
'value' => 'jcb.png',
'code' => 'JCB',
'label' => __('jcb')
],
];
}
}
84 changes: 64 additions & 20 deletions Model/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Directory\Model\Currency;
use Magento\Framework\View\Asset\Source;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Payment\Model\CcConfig;
use Magento\Payment\Model\CcGenericConfigProvider;
use Vindi\Payment\Helper\Data;
use Vindi\Payment\Model\Config\Source\CardImages as CardImagesSource;
use Vindi\Payment\Model\Payment\PaymentMethod;
Expand All @@ -20,55 +22,65 @@
* Class ConfigProvider
* @package Vindi\Payment\Model
*/
class ConfigProvider implements ConfigProviderInterface
class ConfigProvider extends CcGenericConfigProvider implements ConfigProviderInterface
{
private $helperData;
public const CODE = 'vindi';

/**
* @var string
*/
protected $_methodCode = 'vindi';

protected $icons = [];

protected $helperData;
/**
* @var CcConfig
*/
private $ccConfig;
protected $ccConfig;
/**
* @var Source
*/
private $assetSource;
protected $assetSource;

/**
* @var CheckoutSession
*/
private $checkoutSession;
protected $checkoutSession;

/**
* @var Currency
*/
private $currency;
protected $currency;

/**
* @var PaymentMethod
*/
private $paymentMethod;
protected $paymentMethod;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;
protected $productRepository;

/**
* @var CustomerSession
*/
private $customerSession;
protected $customerSession;

/**
* @var PaymentProfileCollection
*/
private $paymentProfileCollection;
protected $paymentProfileCollection;

/**
* @var CardImagesSource
*/
private $creditCardTypeSource;
protected $creditCardTypeSource;

public function __construct(
CcConfig $ccConfig,
PaymentHelper $paymentHelper,
Source $assetSource,
Data $data,
CheckoutSession $checkoutSession,
Expand All @@ -79,7 +91,7 @@ public function __construct(
PaymentProfileCollection $paymentProfileCollection,
CardImagesSource $creditCardTypeSource
) {

parent::__construct($ccConfig, $paymentHelper, [self::CODE]);
$this->ccConfig = $ccConfig;
$this->assetSource = $assetSource;
$this->helperData = $data;
Expand All @@ -92,20 +104,15 @@ public function __construct(
$this->creditCardTypeSource = $creditCardTypeSource;
}

/**
* @var string
*/
protected $_methodCode = 'vindi_cc';

/**
* {@inheritdoc}
*/
public function getConfig()
{
return [
'payment' => [
'vindi_cc' => [
'availableTypes' => [$this->_methodCode => $this->paymentMethod->getCreditCardTypes()],
'vindi' => [
'availableTypes' => $this->paymentMethod->getCreditCardCodes(),
'months' => [$this->_methodCode => $this->ccConfig->getCcMonths()],
'years' => [$this->_methodCode => $this->ccConfig->getCcYears()],
'hasVerification' => [$this->_methodCode => $this->ccConfig->hasVerification()],
Expand All @@ -115,7 +122,8 @@ public function getConfig()
'hasPlanInCart' => (int) $this->hasPlanInCart(),
'planIntervalCountMaxInstallments' => (int) $this->planIntervalCountMaxInstallments(),
'saved_cards' => $this->getPaymentProfiles(),
'credit_card_images' => $this->getCreditCardImages()
'credit_card_images' => $this->getCreditCardImages(),
'icons' => $this->getIcons(),
]
]
];
Expand Down Expand Up @@ -218,11 +226,47 @@ public function getCreditCardImages(): array

foreach ($creditCardOptionArray as $creditCardOption) {
$ccImages[] = [
'code' => $creditCardOption['code'],
'label' => $creditCardOption['label'],
'value' => $creditCardOption['value']
];
}

return $ccImages;
}


/**
* Get icons for available payment methods
*
* @return array
*/
public function getIcons()
{
if (!empty($this->icons)) {
return $this->icons;
}

$types = $this->getCreditCardImages();
foreach ($types as $type) {
$code = $type['code'];
$label = $type['label'];

if (!array_key_exists($code, $this->icons)) {
$asset = $this->ccConfig->createAsset('Vindi_Payment::images/cc/' . strtolower($code) . '.png');
$placeholder = $this->assetSource->findSource($asset);
if ($placeholder) {
list($width, $height) = getimagesize($asset->getSourceFile());
$this->icons[$code] = [
'url' => $asset->getUrl(),
'width' => $width,
'height' => $height,
'title' => $label,
];
}
}
}

return $this->icons;
}
}
Loading

0 comments on commit ca8fe90

Please sign in to comment.