Skip to content

Commit

Permalink
use magneto json to handle json errors, use getBaseCurrencyCode to ge…
Browse files Browse the repository at this point in the history
…t the currecnyCode
  • Loading branch information
harshaaligent committed Oct 16, 2023
1 parent daeece2 commit 538951d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Gateway/CaptureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Gateway/VaultCaptureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 10 additions & 21 deletions Model/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,19 +90,26 @@ 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
*/
public function __construct(
string $username,
string $token,
State $state,
Json $json,
bool $testMode = true,
string $gatewayUrl = null
) {
Expand All @@ -125,6 +133,7 @@ public function __construct(
$this->url = $gatewayUrl;
}
$this->state = $state;
$this->json = $json;
}

/**
Expand Down Expand Up @@ -168,7 +177,7 @@ public function tokenPurchase(
"customer_ip" => $customer_ip,
"wallet" => [
"type" => "GOOGLE",
"token" => $this->prepareGoogleToken($pmntsToken)
"token" => $this->json->unserialize($pmntsToken)
]
];
} else {
Expand All @@ -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
*
Expand Down
6 changes: 1 addition & 5 deletions Observer/AssignDataObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down

0 comments on commit 538951d

Please sign in to comment.