4

Inside of my payment class in

public function capture(\Magento\Payment\Model\InfoInterface $payment)

object $payment has method $payment->getCcType() which returns short code 'VI' or 'AE'for example. And I need to get full name of credit card brand, which $payment doesn't has (I've looked at realization of InfoInterface - there is nothing about it). The name should match one of this list AMEX, DISCOVER, DINERSCLUB, VISA, MASTERCARD, JCB, UNIONPAY, VOYAGER, CARTEBLEUE, DANKORT, MAESTRO, VPAY, VISAELECTRON, TOKEN, VISADELTADEBIT, MASTERCARDDEBIT (doesn't matter whether it is lower or uppercase). How it could be converted using magento framework or payment modules?

2 Answers 2

4

After debugging couple of hours i have resolved this issue by following below method.

template.phtml

<div class="ccType"> 
   <span> Card Type: <span> 
   <?php echo $block->getCardFullName("cardCode") // VI / AE... ?>
</div>

Block.php

<?php

namespace [VendorName]\[ModuleName]\Block\Customer\Cards;

class Listing extends \Magento\Framework\View\Element\Template
{

    public $ccConfig;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Payment\Model\CcConfig $ccCongig,
        array $data = []
    ) {  
        $this->ccConfig = $ccCongig;
        parent::__construct($context, $data);
     }

    public function getCardFullName($CardCode)
    {
        $return = "";
        if($CardCode){
            $cardTypes = $this->ccConfig->getCcAvailableTypes();
            $return = $cardTypes[$CardCode];
        }
        return $return; // Visa / American Express ...
    }
}
3

What you need to do is to instatiate Magento\Payment\Model\CcConfig class, and use getCcAvailableTypes method of it.

It'll return this array:

Array
(
    [AE] => American Express
    [VI] => Visa
    [MC] => MasterCard
    [DI] => Discover
    [JCB] => JCB
    [SM] => Switch/Maestro
    [DN] => Diners
    [SO] => Solo
    [MI] => Maestro International
    [MD] => Maestro Domestic
    [OT] => Other
)
1
  • Thanx a lot. But what if some of my credit cards are not in list? VISAELECTRON, TOKEN, VISADELTADEBIT, MASTERCARDDEBIT for example. Do I have some options to use them? And what about names - I guess I need to write some converter method, for example to get 'AMEX' from '[AE] => American Express', right? Is there some other way?
    – white-imp
    Commented May 13, 2016 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.