Skip to content

Commit

Permalink
Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Wojciechowski committed Jan 23, 2024
1 parent e5ae8b6 commit 73e84fa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
40 changes: 34 additions & 6 deletions Model/ApiFacade/CardTransaction/CardOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function __construct(TpayInterface $tpay, TpayTokensService $tokensServic
$this->tokensService = $tokensService;
$this->tpayService = $tpayService;
$this->tpayApi = new TpayApi($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$versions = $this->getPackagesVersions();
$this->tpayApi->setClientName(implode('|', [
'magento2:' . $this->getMagentoVersion(),
'tpay-com/tpay-openapi-php:' . $versions[0],
'tpay-com/tpay-php:' . $versions[1],
'PHP:' . phpversion()
]
);
$this->tpayApi->authorization();
}

Expand All @@ -44,7 +52,7 @@ public function makeCardTransaction(string $orderId): string
$this->tpayPaymentConfig = $this->tpay->getTpayFormData($orderId);

if (isset($additionalPaymentInformation['card_id']) && false !== $additionalPaymentInformation['card_id'] && $this->tpay->getCardSaveEnabled()) {
$cardId = (int) $additionalPaymentInformation['card_id'];
$cardId = (int)$additionalPaymentInformation['card_id'];

return $this->processSavedCardPayment($orderId, $cardId);
}
Expand Down Expand Up @@ -80,9 +88,9 @@ private function processSavedCardPayment(string $orderId, int $cardId): string
$paymentResult = $result['payments'] ?? [];

if (isset($paymentResult['status']) && 'declined' === $paymentResult['status']) {
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: '.$paymentResult['reason']);
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: ' . $paymentResult['reason']);
} else {
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, error: '.$paymentResult['err_desc']);
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, error: ' . $paymentResult['err_desc']);
}
} catch (Exception $e) {
return 'magento2basic/tpay/error';
Expand All @@ -104,7 +112,7 @@ private function addToPaymentData(string $orderId, string $key, $value)

private function processNewCardPayment(string $orderId, array $additionalPaymentInformation): string
{
$saveCard = isset($additionalPaymentInformation['card_save']) && $this->tpay->getCardSaveEnabled() ? (bool) $additionalPaymentInformation['card_save'] : false;
$saveCard = isset($additionalPaymentInformation['card_save']) && $this->tpay->getCardSaveEnabled() ? (bool)$additionalPaymentInformation['card_save'] : false;
try {
$transaction = $this->tpayApi->Transactions->createTransaction($this->handleDataStructure());
$request = [
Expand All @@ -123,7 +131,7 @@ private function processNewCardPayment(string $orderId, array $additionalPayment

if (isset($result['transactionPaymentUrl']) && 'pending' === $result['payments']['status']) {
$url3ds = $result['transactionPaymentUrl'];
$this->tpayService->addCommentToHistory($orderId, '3DS Transaction link '.$url3ds);
$this->tpayService->addCommentToHistory($orderId, '3DS Transaction link ' . $url3ds);
$this->addToPaymentData($orderId, 'transaction_url', $url3ds);
$this->saveCard($orderId, $saveCard, $additionalPaymentInformation);

Expand All @@ -149,7 +157,7 @@ private function saveCard(string $orderId, bool $saveCard, array $additionalPaym
private function handleDataStructure(): array
{
return [
'amount' => (float) $this->tpayPaymentConfig['amount'],
'amount' => (float)$this->tpayPaymentConfig['amount'],
'description' => $this->tpayPaymentConfig['description'],
'hiddenDescription' => $this->tpayPaymentConfig['crc'],
'payer' => [
Expand Down Expand Up @@ -184,4 +192,24 @@ private function handleValidParams(array $response): array

return $response;
}

private function getMagentoVersion()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('\Magento\Framework\App\ProductMetadataInterface');
return $productMetadata->getVersion();
}

private function getPackagesVersions()
{
$dir = __DIR__ . '/../../../composer.json';
if (file_exists($dir)) {
$composerJson = json_decode(
file_get_contents(__DIR__ . '/../../../composer.json'), true
)['require'] ?? [];

return [$composerJson['tpay-com/tpay-openapi-php'], $composerJson['tpay-com/tpay-php']];
}
return ['n/a', 'n/a'];
}
}
36 changes: 36 additions & 0 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@

class OpenApi extends TpayApi
{
public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read')
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->productionMode = $productionMode;
$this->scope = $scope;
$versions = $this->getPackagesVersions();
$this->setClientName(implode('|', [
'magento2:' . $this->getMagentoVersion(),
'tpay-com/tpay-openapi-php:' . $versions[0],
'tpay-com/tpay-php:' . $versions[1],
'PHP:' . phpversion()
]
);
}

public function create(array $data): array
{
if (!empty($data['blikPaymentData'])) {
Expand Down Expand Up @@ -164,4 +180,24 @@ private function waitForBlikAccept(array $result): array

return $result;
}

private function getMagentoVersion()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('\Magento\Framework\App\ProductMetadataInterface');
return $productMetadata->getVersion();
}

private function getPackagesVersions()
{
$dir = __DIR__ . '/../../composer.json';
if (file_exists($dir)) {
$composerJson = json_decode(
file_get_contents(__DIR__ . '/../../../composer.json'), true
)['require'] ?? [];

return [$composerJson['tpay-com/tpay-openapi-php'], $composerJson['tpay-com/tpay-php']];
}
return ['n/a', 'n/a'];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"magento/module-checkout": "^100.0",
"magento/module-payment": "^100.0",
"magento/module-sales": "^100.0",
"tpay-com/tpay-openapi-php": "^1.6.4",
"tpay-com/tpay-openapi-php": "^1.6.6",
"tpay-com/tpay-php": "^2.4.3"
},
"autoload": {
Expand Down

0 comments on commit 73e84fa

Please sign in to comment.