gnu-taler-payment-for-magento

Adobe Commerce (Magento) plugin to enable payments with GNU Taler
Log | Files | Refs | README

TransferFactory.php (2522B)


      1 <?php
      2 /**
      3  *
      4  * This file is part of TALER
      5  * Copyright (C) 2024 Taler Systems SA
      6  *
      7  * TALER is free software; you can redistribute it and/or modify it under the
      8  * terms of the GNU General Public License as published by the Free Software
      9  * Foundation; either version 3, or (at your option) any later version.
     10  *
     11  * TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     13  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License along with
     16  * TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     17  *
     18  * @author Nicola Eigel
     19  */
     20 
     21 namespace GNU\Taler\Gateway\Http;
     22 
     23 use Magento\Framework\App\Config\ScopeConfigInterface;
     24 use Magento\Payment\Gateway\Http\TransferBuilder;
     25 use Magento\Payment\Gateway\Http\TransferFactoryInterface;
     26 use Magento\Payment\Gateway\Http\TransferInterface;
     27 use Magento\Store\Model\ScopeInterface;
     28 
     29 class TransferFactory implements TransferFactoryInterface
     30 {
     31 
     32     const XML_PATH_SETTING_DATA = 'payment/gnu_taler';
     33 
     34     /**
     35      * @var ScopeConfigInterface
     36      */
     37     protected $_scopeConfig;
     38 
     39     /**
     40      * @var TransferBuilder
     41      */
     42     private $_transferBuilder;
     43 
     44     /**
     45      * @param TransferBuilder $transferBuilder
     46      * @param ScopeConfigInterface $scopeConfig
     47      */
     48     public function __construct(
     49         TransferBuilder      $transferBuilder,
     50         ScopeConfigInterface $scopeConfig
     51     )
     52     {
     53         $this->_transferBuilder = $transferBuilder;
     54         $this->_scopeConfig = $scopeConfig;
     55     }
     56 
     57     /**
     58      * Create request object for client
     59      * @param array $request
     60      * @return TransferInterface
     61      */
     62     public function create(array $request): TransferInterface
     63     {
     64         $config = $this->getConfig();
     65 
     66         $headers = [
     67             "Authorization" => "Bearer secret-token:" . $config['bearer_token'],
     68             "Content-Type" => "application/json"
     69         ];
     70 
     71         return $this->_transferBuilder
     72             ->setUri($request['url'])
     73             ->setBody(json_encode($request['body']))
     74             ->setHeaders($headers)
     75             ->setMethod($request['method'])
     76             ->build();
     77     }
     78 
     79     /**
     80      * Get config
     81      * @return mixed
     82      */
     83     private function getConfig()
     84     {
     85         $storeScope = ScopeInterface::SCOPE_STORE;
     86         return $this->_scopeConfig->getValue(self::XML_PATH_SETTING_DATA, $storeScope);
     87     }
     88 }