Skip to content

Commit

Permalink
Fix error with wrong token
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinwy committed Jul 5, 2024
1 parent 205760e commit 282b35f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
40 changes: 23 additions & 17 deletions src/app/code/Komoju/Payments/Api/KomojuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Komoju\Payments\Model\ExternalPayment;

use Psr\Log\LoggerInterface;
use Exception;

class ProcessToken extends Action
{
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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(),
Expand All @@ -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']);
Expand Down

0 comments on commit 282b35f

Please sign in to comment.