0

enter image description here

I am integrating a custom payment module. I need to get the API keys in my controller. If possible in JS file also.

My Controller where I need to get the config values.

<?php
namespace vendorName\moduleName\Controller\Index;

class GetPrice extends \Magento\Framework\App\Action\Action
{
    protected $customHelper;

    public function __construct( 
        \Magento\Framework\App\Action\Context $context,
        \Vendor\Module\Helper\Data $customHelper 
    ) {    
        $this->customHelper = $customHelper;
        parent::__construct($context);
    }

    public function execute(){
        echo $this->customHelper->getConfig("payment/title/cmc_api_key");
    }
}

system.xml

    <section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label">
        <group id="mypay" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
            <label>MyPay</label>
                <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
                <label>Title</label>
            </field>

Thanks In Advance.

Garry
  • 145
  • 18

3 Answers3

1

You can create one Helper and in that helper you can create one function there by using that function you can get config value by passing only key of your configuration everywhere in Blocks, Controllers and Model etc..

app/code/Vendor/Module/Helper/Data.php

<?php
namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{    
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getConfig($configPath)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Now you just need to inject this Helper class in your Controller like this

namespace Vendor\Module\Controller\Index;

use \Magento\Framework\App\Action\Context;
use \Vendor\Module\Helper\Data as Helper;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_helper;

    public function __construct(
        Context $context,
        Helper $helper
    ){
        $this->_helper = $helper;
        parent::__construct($context);
    }

    public function execute(){
        echo $this->_helper->getConfig("sectionid/groupid/fieldid");
    }
}

And in phtml you can get config value like this..

$test = $this->helper('Vendor\Module\Helper\Data')->getConfig('sectionid/groupid/fieldid');

You can download reference module here. I've added code in this module how you can access config data in your JS file in this module.

Hope this will help you!

Kishan Savaliya
  • 7,736
  • 1
  • 12
  • 27
  • Any way for including key config in JS? – Garry Nov 14 '19 at 07:44
  • I've to use this in ajax controller. Can i use it like this? public function __construct( \Magento\Framework\App\Action\Context $context, \Vendor\Module\Helper\Data $customHelper) { $this->customHelper = $customHelper; return parent::__construct($context, $this->customHelper); } – Garry Nov 14 '19 at 07:56
  • You can use like this – Kishan Savaliya Nov 14 '19 at 07:59
  • public function __construct( \Magento\Framework\App\Action\Context $context, \Vendor\Module\Helper\Data $customHelper ) { $this->customHelper = $customHelper; parent::__construct($context); } – Kishan Savaliya Nov 14 '19 at 07:59
  • I am getting Error here when i request price with AJAX. jquery.js:10260 POST http://127.0.0.1/magento3/modulename/Index/GetPrice 500 (Internal Server Error) Here Is my section

    <section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label">

    Group <group id="mynewgroup" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">

    FieldID <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">

    – Garry Nov 14 '19 at 08:07
  • This controller works okay, If i remove this code... how can i resolve this??? – Garry Nov 14 '19 at 08:24
  • Remove generated folder and try – Rakesh Varma Nov 14 '19 at 08:42
  • @RakeshVarma I am new to magento, may i know which generated folder? – Garry Nov 14 '19 at 09:37
  • @Garry, generated folder is available on Magento Root directory – Kishan Savaliya Nov 14 '19 at 09:39
  • You can rename that from generated to generated1 and try to run setup upgrade – Kishan Savaliya Nov 14 '19 at 09:39
  • Whenever you will change anything in construct please run setup:di:compile command – Kishan Savaliya Nov 14 '19 at 09:40
  • This is my code. I've renamed the generated folder. then i run this command
    1. php bin/magento setup:di:compile
    2. php bin/magento setup:upgrade
    3. php bin/magento setup:static-content:deploy -f

    ERROR POST http://127.0.0.1/magento3/iostpaymagento/Index/GetPrice 500 (Internal Server Error) This is my code `protected $customHelper;

    public function __construct( \Magento\Framework\App\Action\Context $context, \Vendor\Module\Helper\Data $customHelper ) {

     $this->customHelper = $customHelper;
     parent::__construct($context);
    

    } `

    – Garry Nov 14 '19 at 10:03
  • Can you please update your file code in your question please ? – Kishan Savaliya Nov 14 '19 at 10:05
  • @KishanSavaliya File updated. Thanks – Garry Nov 14 '19 at 10:09
1

Get a value in your controller like, According to your system.xml you given wrong path, use $configPath variable path to get your configuration

<?php
namespace vendorName\moduleName\Controller\Index;

class GetPrice extends \Magento\Framework\App\Action\Action
{
    protected $scopeConfig;

    public function __construct( 
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {    
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    public function execute(){
        $configPath = 'payment/mypay/title';
        echo $this->scopeConfig->getValue(
                  $configPath,
                  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
             );
    }
}
Ranganathan
  • 3,220
  • 2
  • 18
  • 37
0

To get value in JS file you can use the below code.

In your module etc/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="api_key" xsi:type="object">Vendor\Module\Model\ApiKeyConfigProvider</item>
           </argument>
       </arguments>
   </type>
</config>

and in your ApiKeyConfigProvider.php add below code.

<?php

namespace Vendor\Module\Model;
class ApiKeyConfigProvider implements \Magento\Checkout\Model\ConfigProviderInterface
{
   public function getConfig()
   {
       $output['api_key'] = 'your_api_key';
       return $output;
   }
}

You can access above api key in js file like this.

window.checkoutConfig.api_key
Rakesh Varma
  • 2,226
  • 10
  • 18