Skip to content

Commit

Permalink
Working magento
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Jan 5, 2024
1 parent 47a5873 commit 9810f37
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 33 deletions.
35 changes: 25 additions & 10 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ public function create(array $data): array
return $this->updateRedirectUrl($transaction);
}

public function createWithInstantRedirect(array $data)
{
$transactionData = $this->handleDataStructure($data);
$transaction = $this->Transactions->createTransactionWithInstantRedirection($transactionData);

return $this->updateRedirectUrl($transaction);
}

public function makeRefund(InfoInterface $payment, string $amount): array
{
return $this->Transactions->createRefundByTransactionId(['amount' => number_format($amount, 2)], $payment->getAdditionalInformation('transaction_id'));
}

public function channels(): array
{
return $this->transactions()->getChannels();
}

private function handleDataStructure(array $data): array
{
$paymentData = [
Expand All @@ -35,16 +48,10 @@ private function handleDataStructure(array $data): array
'city' => $data['city'],
'country' => $data['country'],
],
'pay' => [
'groupId' => $data['group'],
],
'callbacks' => [
'payerUrls' => [
'success' => $data['return_url'],
'error' => $data['return_error_url'],
],
'notification' => [
'url' => $data['result_url'],
"callbacks" => [
"payerUrls" => [
"success" => $data['return_url'],
"error" => $data['return_error_url']
],
],
];
Expand All @@ -55,6 +62,14 @@ private function handleDataStructure(array $data): array
];
}

if (isset($data['group'])) {
$paymentData['pay'] = ['groupId' => $data['group']];
}

if (isset($data['channel'])) {
$paymentData['pay'] = ['channelId' => $data['channel']];
}

return $paymentData;
}

Expand Down
42 changes: 40 additions & 2 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace tpaycom\magento2basic\Model\ApiFacade\Transaction;

use Exception;
use Magento\Framework\App\CacheInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\OpenApi;

class TransactionApiFacade
{
private const CHANNELS_CACHE_KEY = 'tpay_channels';
private const CACHE_LIFETIME = 86400;

/** @var TransactionOriginApi */
private $originApi;

Expand All @@ -17,10 +21,14 @@ class TransactionApiFacade
/** @var bool */
private $useOpenApi;

public function __construct(TpayInterface $tpay)
/** @var CacheInterface */
private $cache;

public function __construct(TpayInterface $tpay, CacheInterface $cache)
{
$this->originApi = new TransactionOriginApi($tpay->getApiPassword(), $tpay->getApiKey(), $tpay->getMerchantId(), $tpay->getSecurityCode(), !$tpay->useSandboxMode());
$this->createOpenApiInstance($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$this->createOpenApiInstance($tpay->getClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$this->cache = $cache;
}

public function isOpenApiUse()
Expand All @@ -33,11 +41,41 @@ public function create(array $config)
return $this->getCurrentApi()->create($config);
}

public function createWithInstantRedirection(array $config)
{
if (!$this->useOpenApi) {
throw new TpayException('OpenAPI not availabile - Failed to create transaction with instant redirection');
}

return $this->openApi->createWithInstantRedirect($config);
}

public function blik($blikTransactionId, $blikCode)
{
return $this->originApi->blik($blikTransactionId, $blikCode);
}

public function channels(): array
{
$channels = $this->cache->load(self::CHANNELS_CACHE_KEY);

if ($channels) {
return json_decode($channels, true);
}

if (false === $this->useOpenApi) {
return [];
}

$channels = array_filter($this->openApi->channels()['channels'], function (array $channel) {
return $channel['available'] === true && empty($channel['constraints']) === true;
});

$this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, []);

return $channels;
}

private function getCurrentApi()
{
return $this->useOpenApi ? $this->openApi : $this->originApi;
Expand Down
4 changes: 2 additions & 2 deletions Model/GenericOnSiteConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace tpaycom\magento2basic\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Payment\Helper\Data as PaymentHelper;
Expand All @@ -12,6 +11,7 @@
use Magento\Store\Model\ScopeInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionOriginApi;

#[\AllowDynamicProperties]
class GenericOnSiteConfigProvider implements ConfigProviderInterface
Expand Down Expand Up @@ -51,7 +51,7 @@ public function getConfig()
'addCSS' => $this->createCSS('tpaycom_magento2basic::css/tpay.css'),
'blikStatus' => $this->getPaymentMethodInstance()->checkBlikLevel0Settings(),
'onlyOnlineChannels' => $this->getPaymentMethodInstance()->onlyOnlineChannels(),
'getBlikChannelID' => TransactionModel::BLIK_CHANNEL,
'getBlikChannelID' => TransactionOriginApi::BLIK_CHANNEL,
'isInstallmentsAmountValid' => $this->getPaymentMethodInstance()->getInstallmentsAmountValid(),
],
],
Expand Down
60 changes: 60 additions & 0 deletions Model/GenericOnsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,64 @@ public function getOpenApiPassword(): string
{
return '';
}

public function getOpenApiSecurityCode(): ?string
{
return '';
}

public function getCardApiKey(): ?string
{
return '';
}

public function getCardApiPassword(): ?string
{
return '';
}

public function getCardSaveEnabled(): bool
{
return '';
}

public function getCheckoutCustomerId(): ?string
{
return '';
}

public function getRSAKey(): string
{
return '';
}

public function isCustomerLoggedIn(): bool
{
return '';
}

public function getHashType(): string
{
return '';
}

public function getVerificationCode(): string
{
return '';
}

public function getCustomerId($orderId)
{
return '';
}

public function isCustomerGuest($orderId)
{
return '';
}

public function getOpenApiClientId()
{
return '';
}
}
2 changes: 1 addition & 1 deletion Model/Tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function getMerchantId(): int

public function getOpenApiClientId()
{
return $this->getConfigData('open_api_client_id');
return $this->getConfigData('open_api_client_id') ?? '';
}

public function getSecurityCode(): string
Expand Down
19 changes: 1 addition & 18 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
<field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Description</label>
</field>
<field id="client_id" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Client ID OPEN API</label>
<validate>validate-length maximum-length-64</validate>
</field>
<field id="open_api_password" translate="label comment" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
<label>API password OPEN API</label>
<validate>validate-length maximum-length-64</validate>
</field>
<field id="merchant_id" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Merchant ID</label>
<validate>validate-no-empty validate-number validate-length maximum-length-10</validate>
Expand All @@ -45,8 +37,6 @@
<label>API password</label>
<validate>validate-no-empty no-whitespace validate-length maximum-length-40</validate>
</field>


<field id="card_api_key_tpay" translate="label comment" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Card API key</label>
<validate>validate-no-empty no-whitespace validate-length maximum-length-126</validate>
Expand All @@ -69,12 +59,8 @@
</field>
<field id="hash_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hash type</label>
<source_model>tpaycom\magento2cards\Model\Config\Source\HashTypes</source_model>
<source_model>tpaycom\magento2basic\Model\Config\Source\HashTypes</source_model>
</field>




<field id="show_payment_channels_online" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Only online methods</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down Expand Up @@ -120,8 +106,6 @@
<label>Generic onsites</label>
<source_model>tpaycom\magento2basic\Model\Config\Source\OnsiteChannels</source_model>
</field>


<field id="open_api_client_id" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Client ID OPEN API</label>
<validate>validate-length maximum-length-64</validate>
Expand All @@ -130,7 +114,6 @@
<label>API password OPEN API</label>
<validate>validate-length maximum-length-64</validate>
</field>

<field id="open_api_security_code" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Open Api Secure code</label>
<validate>validate-no-empty validate-length maximum-length-32</validate>
Expand Down

0 comments on commit 9810f37

Please sign in to comment.