gnu-taler-payment-for-magento

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

RequestBuilder.php (3331B)


      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\Request;
     22 
     23 use Magento\Framework\Exception\InputException;
     24 use Magento\Framework\Stdlib\Cookie\FailureToSendException;
     25 use Magento\Payment\Gateway\Request\BuilderInterface;
     26 use Magento\Framework\Stdlib\CookieManagerInterface;
     27 use Magento\Framework\App\Config\ScopeConfigInterface;
     28 use Magento\Store\Model\ScopeInterface;
     29 
     30 class RequestBuilder implements BuilderInterface
     31 {
     32     const XML_PATH_SETTING_DATA = 'payment/gnu_taler';
     33 
     34     const COOKIE_NAME_ID = 'order_id';
     35     const COOKIE_NAME_ID_PAID = 'order_id_paid';
     36     const COOKIE_NAME_URL = 'payment_url';
     37 
     38     /**
     39      * @var ScopeConfigInterface
     40      */
     41     protected $_scopeConfig;
     42 
     43     /**
     44      * @var CookieManagerInterface
     45      */
     46     protected $_cookieManager;
     47 
     48     /**
     49      * @param CookieManagerInterface $cookieManager
     50      * @param ScopeConfigInterface $scopeConfig
     51      */
     52     public function __construct(
     53         CookieManagerInterface $cookieManager,
     54         ScopeConfigInterface   $scopeConfig
     55     )
     56     {
     57         $this->_cookieManager = $cookieManager;
     58         $this->_scopeConfig = $scopeConfig;
     59     }
     60 
     61     /**
     62      * @param array $buildSubject
     63      * @return array
     64      */
     65     public function build(array $buildSubject)
     66     {
     67         $config = $this->getConfig();
     68         $tokenId = $this->getCookie();
     69         $this->deleteCookies();
     70 
     71 
     72         $request["body"] = "";
     73         $request["method"] = "GET";
     74         $request["url"] = $config["backend_url"] . "/" . $tokenId;
     75 
     76         return $request;
     77     }
     78 
     79     /**
     80      * Get's config for backend transfer data
     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 
     89     /**
     90      * Gets cookie value
     91      * @return mixed
     92      */
     93     private function getCookie()
     94     {
     95         return $this->_cookieManager->getCookie(self::COOKIE_NAME_ID);
     96     }
     97 
     98     /**
     99      * Deletes used cookies
    100      * @return void
    101      */
    102     public function deleteCookies()
    103     {
    104         try {
    105             $this->_cookieManager->deleteCookie(
    106                 self::COOKIE_NAME_ID
    107             );
    108             $this->_cookieManager->deleteCookie(
    109                 self::COOKIE_NAME_ID_PAID
    110             );
    111             $this->_cookieManager->deleteCookie(
    112                 self::COOKIE_NAME_URL
    113             );
    114         } catch (InputException $e) {
    115             $test = $e;
    116             //TODO add logger
    117         } catch (FailureToSendException $e) {
    118             //TODO add logger
    119             $test = $e;
    120         }
    121     }
    122 }