gnu-taler-payment-for-magento

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

Client.php (2903B)


      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 Exception;
     24 use Magento\Payment\Gateway\Http\ClientInterface;
     25 use Magento\Payment\Gateway\Http\TransferInterface;
     26 use Magento\Payment\Model\Method\Logger;
     27 use Magento\Framework\HTTP\Client\Curl;
     28 
     29 class Client implements ClientInterface
     30 {
     31 
     32     /**
     33      * @var Logger
     34      */
     35     private $_logger;
     36 
     37     /**
     38      * @var Curl
     39      */
     40     private $_curl;
     41 
     42     /**
     43      * @param Logger $logger
     44      * @param Curl $curl
     45      */
     46     public function __construct(
     47         Logger $logger,
     48         Curl   $curl
     49     )
     50     {
     51         $this->_logger = $logger;
     52         $this->_curl = $curl;
     53     }
     54 
     55     /**
     56      * Place request from transfer object data
     57      * @param TransferInterface $transferObject
     58      * @return array
     59      * @throws Exception
     60      */
     61     public function placeRequest(TransferInterface $transferObject)
     62     {
     63         $log = [
     64             'request_uri' => $transferObject->getUri(),
     65             'request_body' => $transferObject->getBody(),
     66             'request_headers' => $transferObject->getHeaders(),
     67         ];
     68         $this->_logger->debug($log);
     69 
     70         try {
     71             $this->_curl->setHeaders($transferObject->getHeaders());
     72 
     73             if ($transferObject->getMethod() == 'POST') {
     74                 $this->_curl->post($transferObject->getUri(), $transferObject->getBody());
     75                 $response = ($this->_curl->getStatus() == 200) ? true : false;
     76             } else {
     77                 $this->_curl->get($transferObject->getUri());
     78 
     79                 if (json_decode($this->_curl->getBody())->order_status != "paid") {
     80                     $this->_curl->get($transferObject->getUri());
     81                     $response = false;
     82                 } else
     83                     $response = true;
     84             }
     85 
     86         } catch (Exception $e) {
     87             throw new Exception($e);
     88         } finally {
     89             $body = $this->_curl->getBody();
     90             $log['response'] = $body;
     91             $this->_logger->debug($log);
     92         }
     93 
     94         $result = [
     95             "result" => $this->_curl->getStatus(),
     96             "response" => $response,
     97             "body" => $body
     98         ];
     99 
    100         return $result;
    101     }
    102 }