DataAssignObserver.php (7050B)
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 *checkoutConfig.payment.taler_gateway) 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\Observer; 22 23 use Exception; 24 use Magento\Framework\App\Config\ScopeConfigInterface; 25 use Magento\Framework\Event\Observer; 26 use Magento\Framework\Event\ObserverInterface; 27 use GNU\Taler\Gateway\Http\TransferFactory; 28 use GNU\Taler\Gateway\Http\Client; 29 use Magento\Framework\App\Action\Context; 30 use Magento\Framework\Session\SessionManagerInterface; 31 use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory; 32 use Magento\Framework\Stdlib\CookieManagerInterface; 33 use Magento\Store\Model\ScopeInterface; 34 use Magento\Quote\Model\QuoteFactory; 35 36 class DataAssignObserver implements ObserverInterface 37 { 38 39 const XML_PATH_SETTING_DATA = 'payment/gnu_taler'; 40 const COOKIE_DURATION = 120; 41 42 /** 43 * @var ScopeConfigInterface 44 */ 45 protected $_scopeConfig; 46 47 /** 48 * @var TransferFactory 49 */ 50 private $_transferFactory; 51 52 /** 53 * @var Client 54 */ 55 private $_client; 56 57 /** 58 * @var CookieManagerInterface 59 */ 60 private $_cookieManager; 61 62 /** 63 * @var CookieMetadataFactory 64 */ 65 private $_cookieMetadataFactory; 66 67 /** 68 * @var SessionManagerInterface 69 */ 70 private $_sessionManager; 71 72 /** 73 * @var Context 74 */ 75 private $_context; 76 77 /** 78 * @var QuoteFactory 79 */ 80 private $_quoteFactory; 81 82 /** 83 * @param TransferFactory $transferFactory 84 * @param Client $client 85 * @param Context $context 86 * @param CookieManagerInterface $cookieManager 87 * @param CookieMetadataFactory $cookieMetadataFactory 88 * @param SessionManagerInterface $sessionManager 89 * @param ScopeConfigInterface $scopeConfig 90 * @param QuoteFactory $quoteFactory 91 */ 92 public function __construct( 93 TransferFactory $transferFactory, 94 Client $client, 95 Context $context, 96 CookieManagerInterface $cookieManager, 97 CookieMetadataFactory $cookieMetadataFactory, 98 SessionManagerInterface $sessionManager, 99 ScopeConfigInterface $scopeConfig, 100 QuoteFactory $quoteFactory 101 ) 102 { 103 $this->_transferFactory = $transferFactory; 104 $this->_client = $client; 105 $this->_cookieManager = $cookieManager; 106 $this->_cookieMetadataFactory = $cookieMetadataFactory; 107 $this->_sessionManager = $sessionManager; 108 $this->_context = $context; 109 $this->_scopeConfig = $scopeConfig; 110 $this->_quoteFactory = $quoteFactory; 111 } 112 113 /** 114 * Observes payment_method_is_active event and stores Taler payment data in cookie to read for frontend 115 * @param Observer $observer 116 * @return void 117 * @throws \Magento\Payment\Gateway\Http\ClientException 118 * @throws \Magento\Payment\Gateway\Http\ConverterException 119 * @throws Exception 120 */ 121 public function execute(Observer $observer) 122 { 123 if ($observer->getData()['method_instance']->getCode() == "gnu_taler") { 124 try { 125 $config = $this->getConfig(); 126 $quote = $this->_quoteFactory->create()->loadActive($observer->getData()['quote']->getData()['entity_id']); 127 $items = $quote->getAllVisibleItems(); 128 129 $request = []; 130 $amount = 0.0; 131 $orderItems = []; 132 133 foreach ($items as $item) { 134 array_push($orderItems, ["product_id" => $item->getSku(), "quantity" => (int)$item->getQty()]); 135 $amount += $item->getPrice()*$item->getQty(); 136 } 137 138 $fulfillmentUrlProtocol = ($_SERVER["SERVER_PORT"] == 80) ? "http://" : "https://"; 139 140 $order = [ 141 "amount" => $config['currency'] . ":" . $amount, 142 "summary" => "Magento store payment", 143 "pay_deadline" => [ 144 "t_s" => time() + (int)$config["pay_deadline_in_seconds"] 145 ], 146 "fulfillment_url" => $fulfillmentUrlProtocol . $_SERVER["SERVER_NAME"] . "/taler-checkout/service/index/" . $quote->getId() . time(), 147 ]; 148 149 $request["body"]["order"] = $order; 150 $request["body"]["inventory_products"] = $orderItems; 151 $request["body"]["create_token"] = true; 152 $request["method"] = "POST"; 153 $request["url"] = $config["backend_url"]; 154 155 $transferObject = $this->_transferFactory->create($request); 156 $response = $this->_client->placeRequest($transferObject); 157 if ($response["response"] != false) { 158 $data = json_decode($response["body"]); 159 $orderId = $data->order_id; 160 $token = $data->token; 161 $url = $config['backend_url'] . "/" . $orderId . "?token=" . $token; 162 $paymentUrl = str_replace("private/", "", $url); 163 $this->set($orderId, "order_id"); 164 $this->set($paymentUrl, "payment_url"); 165 } 166 } catch (Exception $e) { 167 throw new Exception($e); 168 } 169 } 170 } 171 172 /** 173 * Get's config for backend transfer data 174 * @return mixed 175 */ 176 private function getConfig() 177 { 178 $storeScope = ScopeInterface::SCOPE_STORE; 179 return $this->_scopeConfig->getValue(self::XML_PATH_SETTING_DATA, $storeScope); 180 } 181 182 /** 183 * Sets cookie from key value pair 184 * @param $value 185 * @param $cookieName 186 * @param $duration 187 * @return void 188 * @throws \Magento\Framework\Exception\InputException 189 * @throws \Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException 190 * @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException 191 */ 192 public function set($value, $cookieName) 193 { 194 $metadata = $this->_cookieMetadataFactory 195 ->createPublicCookieMetadata() 196 ->setDuration(self::COOKIE_DURATION) // Cookie will expire after one day (86400 seconds) 197 ->setPath($this->_sessionManager->getCookiePath()) 198 ->setDomain($this->_sessionManager->getCookieDomain()); 199 200 $this->_cookieManager->setPublicCookie( 201 $cookieName, 202 $value, 203 $metadata 204 ); 205 } 206 }