From 538951db854df6a1498656a3795dba3a51e6e1e9 Mon Sep 17 00:00:00 2001 From: "harsha.amaraweera" Date: Mon, 16 Oct 2023 15:54:35 +1030 Subject: [PATCH] use magneto json to handle json errors, use getBaseCurrencyCode to get the currecnyCode --- Gateway/CaptureCommand.php | 2 +- Gateway/VaultCaptureCommand.php | 2 +- Model/Gateway.php | 31 ++++++++++--------------------- Observer/AssignDataObserver.php | 6 +----- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/Gateway/CaptureCommand.php b/Gateway/CaptureCommand.php index 5f7415f..0663c31 100644 --- a/Gateway/CaptureCommand.php +++ b/Gateway/CaptureCommand.php @@ -114,7 +114,7 @@ public function execute(array $commandSubject) $reference = $this->pmntsHelper->getOrderReference($order); $fraudData = $this->pmntsHelper->buildFraudPayload($order); $paymentType = $payment->getAdditionalInformation('type'); - $currencyCode = $order->getBaseCurrency()->getCode(); + $currencyCode = $order->getBaseCurrencyCode(); /** @var array $result */ $result = $gateway->tokenPurchase( $pmntsToken, diff --git a/Gateway/VaultCaptureCommand.php b/Gateway/VaultCaptureCommand.php index 49d5f2d..01c366d 100644 --- a/Gateway/VaultCaptureCommand.php +++ b/Gateway/VaultCaptureCommand.php @@ -75,7 +75,7 @@ public function execute(array $commandSubject): void /** @var \PMNTS\Gateway\Model\Gateway $gateway */ $gateway = $this->getGateway($storeId); $fraudData = $this->pmntsHelper->buildFraudPayload($order); - $currencyCode = $order->getBaseCurrency()->getCode(); + $currencyCode = $order->getBaseCurrencyCode(); $result = $gateway->tokenPurchase( $token->getGatewayToken(), $currencyCode, diff --git a/Model/Gateway.php b/Model/Gateway.php index cbc9262..20eb0db 100755 --- a/Model/Gateway.php +++ b/Model/Gateway.php @@ -33,6 +33,7 @@ use Zend\Http\Client\Adapter\Exception\TimeoutException; use Magento\Framework\App\State; use Magento\Framework\App\Area; +use Magento\Framework\Serialize\Serializer\Json; /** * The Fat Zebra Gateway class for interfacing with Fat Zebra @@ -89,12 +90,18 @@ class Gateway */ public bool $testMode = true; + /** + * @var Json + */ + private Json $json; + /** * Creates a new instance of the Fat Zebra gateway object * * @param string $username * @param string $token * @param State $state + * @param Json $json * @param bool $testMode * @param string|null $gatewayUrl */ @@ -102,6 +109,7 @@ public function __construct( string $username, string $token, State $state, + Json $json, bool $testMode = true, string $gatewayUrl = null ) { @@ -125,6 +133,7 @@ public function __construct( $this->url = $gatewayUrl; } $this->state = $state; + $this->json = $json; } /** @@ -168,7 +177,7 @@ public function tokenPurchase( "customer_ip" => $customer_ip, "wallet" => [ "type" => "GOOGLE", - "token" => $this->prepareGoogleToken($pmntsToken) + "token" => $this->json->unserialize($pmntsToken) ] ]; } else { @@ -188,26 +197,6 @@ public function tokenPurchase( return $this->doRequest("POST", "/purchases", $payload); } - /** - * Validate token - * - * @param string $pmntsToken - * @return array - * @throws \JsonException - */ - private function prepareGoogleToken(string $pmntsToken) - { - try { - $token = json_decode($pmntsToken, true); - if (!is_array($token)) { - throw new \JsonException("JSON Syntax error. Invalid Token"); - } - } catch (\Exception $e) { - throw new \JsonException("JSON Syntax error. Invalid Token"); - } - return $token; - } - /** * Performs a refund against the FatZebra gateway * diff --git a/Observer/AssignDataObserver.php b/Observer/AssignDataObserver.php index d7d27bc..06489f3 100644 --- a/Observer/AssignDataObserver.php +++ b/Observer/AssignDataObserver.php @@ -30,11 +30,7 @@ public function execute(Observer $observer) if (array_key_exists('cc_token', $additionalData)) { $paymentInfo->setAdditionalInformation('pmnts_token', $additionalData['cc_token']); } - if (array_key_exists('type', $additionalData)) { - $paymentInfo->setAdditionalInformation('type', $additionalData['type']); - } else { - $paymentInfo->setAdditionalInformation('type', null); - } + $paymentInfo->setAdditionInformation('type', $additionalData['type'] ?? null); if (isset($additionalData['pmnts_id']) && !empty($additionalData['pmnts_id'])) { $paymentInfo->setAdditionalInformation('pmnts_device_id', $additionalData['pmnts_id']); }