2

I have followed sample-module-payment-gateway to implement Payment method in Magento 2.

But for some strange reason, Custom Config Provider in Magento is not working. It throws Following error :

TypeError: window.checkoutConfig.payment.test_finance is undefined

Here is the di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    
    <type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="Test_finance" xsi:type="object">TestCapital\Finance\Model\CustomConfigProvider</item>
            </argument>
        </arguments>
    </type>
    
    <virtualType name="TestFinanceFacade" type="Magento\Payment\Model\Method\Adapter">
        <arguments>
            <argument name="code" xsi:type="const">TestCapital\Finance\Model\Payment\TestFinance::METHOD_CODE</argument>
            <argument name="formBlockType" xsi:type="string">TestCapital\Finance\Block\Form</argument>
            <argument name="infoBlockType" xsi:type="string">TestCapital\Finance\Block\Info</argument>
            <argument name="valueHandlerPool" xsi:type="object">TestCapitalValueHandlerPool</argument>
            <!-- argument name="validatorPool" xsi:type="object">TestCapitalValidatorPool</argument -->
            <!-- argument name="commandPool" xsi:type="object">TestCapitalCommandPool</argument -->
        </arguments>
    </virtualType>
    
    <virtualType name="TestFinanceConfig" type="Magento\Payment\Gateway\Config\Config">
        <arguments>
            <argument name="test_finance" xsi:type="const">TestCapital\Finance\Model\CustomConfigProvider::METHOD_CODE</argument>
        </arguments>
    </virtualType>
    
    <!-- Value handlers infrastructure -->
    <virtualType name="TestFinaceGatewayValueHandlerPool" type="Magento\Payment\Gateway\Config\ValueHandlerPool">
        <arguments>
            <argument name="handlers" xsi:type="array">
                <item name="default" xsi:type="string">TestFinaceConfigValueHandler</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="TestFinaceConfigValueHandler" type="Magento\Payment\Gateway\Config\ConfigValueHandler">
        <arguments>
            <argument name="configInterface" xsi:type="object">TestFinanceConfig</argument>
        </arguments>
    </virtualType>

    <type name="TestCapital\Finance\Block\Info">
        <arguments>
            <argument name="config" xsi:type="object">TestFinanceConfig</argument>
        </arguments>
    </type>

</config>

TestCapital/Finance/Model/CustomConfigProvider.php

namespace TestCapital\Finance\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\Escaper;
use Magento\Payment\Helper\Data as PaymentHelper;

class CustomConfigProvider implements ConfigProviderInterface {
    
    const METHOD_CODE   = 'test_finance';
    
    public function getConfig() {
        $config = [
            'payment' => array (
                self::METHOD_CODE => array (
                    'storedCards' => $this->getStoredCards(),
                )
            )
        ];
        return $config;
    }
    
    public function getStoredCards(){
        $result = array();
        $result['0'] = "Test";
        $result['1'] = "Test1";
        return $result;
    }
    
}

TestCapital/Finance/view/frontend/web/js/view/payment/method-renderer/test-finance-method.js

getStoreCard: function() {
    return window.checkoutConfig.payment.test_finance.storedCards;
},

TestCapital/Finance/view/frontend/web/template/payment/omni_finance.html

<label>Select Finance Package</label>
<select name="payment[subscription_id]" class="select input-text required-entry" 
            data-bind="
                attr: {id: getCode()+'_payment_profile_id'},
                options: getCardList(),
                optionsValue: 'value',
                optionsText: 'type',
                optionsCaption: $t('--Please Select A Package--'),
                ">
</select> 
  • Try with js, return window.checkoutConfig.payment.test_finance.storedCards; – Rakesh Jesadiya Oct 31 '17 at 8:34
  • use testFinance instead of test_finance – Rakesh Jesadiya Oct 31 '17 at 9:10
4

Eventually got this Resolved.

I had misplaced di.xml
once i moved di.xml to etc/frontend folder everything works fine now.

Anyone looking for this, Here is the correct location for di.xml

Namespace/Modulename/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="custom_payment_config_provider" xsi:type="object">Namespace\Modulename\Model\CustomConfigProvider</item>
            </argument>
        </arguments>
    </type>

</config>

Namespace/Modulename/Model/CustomConfigProvider.php

namespace Namespace\Modulename\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\SamplePaymentGateway\Gateway\Http\Client\ClientMock;

class CustomConfigProvider implements ConfigProviderInterface {
    public function getConfig() {
        $config = array();
        return $config;
    }       
}
| improve this answer | |
  • 1
    This is a total stupid thing, I do not understand why Magento can not use it from the etc directory. I sucked an hour with this while I found your solution. – vaso123 Feb 13 '19 at 10:48
1

You need to set your js file as below,

window.checkoutConfig.payment.testFinance.storedCards

Set your js file code,

getStoreCard: function() {
    return  window.checkoutConfig.payment.testFinance.storedCards
},

Run

php bin/magento setup:upgrade,
php bin/magento setup:static-content:deploy
| improve this answer | |
  • Why what is wrong with an underscore? Is it naming convention? – JItendra Rana Oct 31 '17 at 9:10
  • Yes you have to keep naming convention with code. – Rakesh Jesadiya Oct 31 '17 at 9:12
  • So do I need to change that in COnfigPrivder class as well? 'payment' => array ( 'test_finance' => array ( 'storedCards' => $this->getStoredCards(), ) ) – JItendra Rana Oct 31 '17 at 9:14
  • Its better to change at those points also. – Rakesh Jesadiya Oct 31 '17 at 9:15
  • Its not the case. The config Provider class is not being called. There is some misconfiguration in di.xml – JItendra Rana Oct 31 '17 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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