diff --git a/src/app/code/Komoju/Payments/Api/KomojuApi.php b/src/app/code/Komoju/Payments/Api/KomojuApi.php index 10da5ddb4..8ccf540c3 100644 --- a/src/app/code/Komoju/Payments/Api/KomojuApi.php +++ b/src/app/code/Komoju/Payments/Api/KomojuApi.php @@ -16,12 +16,14 @@ class KomojuApi private Config $config; private Curl $curl; private string $endpoint; + private LoggerInterface $logger; - public function __construct(Config $config, Curl $curl) + public function __construct(Config $config, Curl $curl, LoggerInterface $logger) { $this->endpoint = 'https://komoju.com'; $this->config = $config; $this->curl = $curl; + $this->logger = $logger; } public function paymentMethods() @@ -88,32 +90,36 @@ private function get($uri) // ); private function post($uri, $payload) { - $url = $this->endpoint . $uri; + $ch = curl_init($this->endpoint . $uri); $data_json = json_encode($payload); - $options = [CURLOPT_POST => true, - CURLOPT_HTTPHEADER => ['Content-Type: application/json','Komoju-Via: magento'], - CURLOPT_RETURNTRANSFER => true, - CURLOPT_USERPWD => $this->config->getSecretKey() . ':' - ]; - $this->curl->setOptions($options); - try { - $this->curl->post($url, $data_json); - } catch (Exception $e) { - throw new KomojuExceptionBadServer($e->getMessage()); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'komoju-via: magento' + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERPWD, $this->config->getSecretKey() . ':'); + $result = curl_exec($ch); + + if (curl_errno($ch)) { + $error = curl_error($ch); + curl_close($ch); + throw new KomojuExceptionBadServer($error); } - $body = $this->curl->getBody(); - $http_code = $this->curl->getStatus(); - + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code !== 200) { - $komojuException = new KomojuExceptionBadServer($body); + $komojuException = new KomojuExceptionBadServer($result); $komojuException->httpCode = $http_code; throw $komojuException; } - $decoded = json_decode($body); + curl_close($ch); + $decoded = json_decode($result); + if (!empty(json_last_error())) { $errorMsg = (__("KOMOJU Payments JSON Decoding Failure. Error: %1", json_last_error_msg())); throw new InvalidJsonException($errorMsg); diff --git a/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php b/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php index 26f452694..589fd0c28 100644 --- a/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php +++ b/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php @@ -15,6 +15,7 @@ use Komoju\Payments\Model\ExternalPayment; use Psr\Log\LoggerInterface; +use Exception; class ProcessToken extends Action { @@ -51,11 +52,7 @@ public function execute() $result = $this->jsonResultFactory->create(); $order = $this->getOrder(); - if ($order) { - $this->logger->debug('Order Data' . json_encode($order->getEntityId())); - $externalPayment = $this->createExternalPayment($order); - $this->logger->info('ExternalPayment: ' . $externalPayment); - } else { + if (!$order) { $this->logger->debug('Executing KomojuSessionData controller' . 'No order found'); return $result->setData(['success' => false, 'message' => 'No order found']); } @@ -64,9 +61,8 @@ public function execute() $postData = $this->getRequest()->getContent(); $tokenData = json_decode($postData); - // $this->logger->debug('Executing KomojuSessionData controller' . json_encode($tokenData)); - $currencyCode = $order->getOrderCurrencyCode(); + $externalPayment = $this->createExternalPayment($order); $session = $this->komojuApi->createSession([ 'amount' => $order->getGrandTotal(), @@ -82,12 +78,16 @@ public function execute() ], ]); - $data = $this->komojuApi->paySession($session->id, [ - 'customer_email' => $order->getCustomerEmail(), - 'payment_details' => $tokenData->token->id - ]); + try { + $data = $this->komojuApi->paySession($session->id, [ + 'payment_details' => (string) $tokenData->token->id + ]); - return $result->setData(['success' => true, 'message' => 'Token processed successfully', 'data' => $data]); + return $result->setData(['success' => true, 'message' => 'Token processed successfully', 'data' => $data]); + } catch (Exception $e) { + $data = ['redirect_url' => $session->session_url]; + return $result->setData(['success' => true, 'message' => 'Cannot process token, redirect', 'data' => $data]); + } } return $result->setData(['success' => false, 'message' => 'Invalid request']);