-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConfigOpen.php
executable file
·140 lines (119 loc) · 5.04 KB
/
ConfigOpen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Tpay\Magento2\Model\ApiFacade\TpayConfig;
use Magento\Framework\View\Asset\Repository;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionOriginApi;
use Tpay\Magento2\Service\TpayTokensService;
use tpaySDK\Api\TpayApi;
class ConfigOpen extends TpayApi
{
/** @var TpayTokensService */
protected $tokensService;
/** @var TpayInterface */
private $tpay;
/** @var TpayConfigInterface */
private $tpayConfig;
/** @var Repository */
private $assetRepository;
public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService)
{
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->assetRepository = $assetRepository;
$this->tokensService = $tokensService;
parent::__construct($tpayConfig->getOpenApiClientId(), $tpayConfig->getOpenApiPassword(), !$tpayConfig->useSandboxMode());
}
public function getConfig(): array
{
return [
'tpay' => [
'payment' => [
'redirectUrl' => $this->tpay->getPaymentRedirectUrl(),
'tpayLogoUrl' => $this->generateURL('Tpay_Magento2::images/logo_tpay.png'),
'tpayCardsLogoUrl' => $this->generateURL('Tpay_Magento2::images/card.svg'),
'showPaymentChannels' => $this->showChannels(),
'getTerms' => $this->getTerms(),
'addCSS' => $this->createCSS('Tpay_Magento2::css/tpay.css'),
'blikStatus' => $this->tpay->checkBlikLevel0Settings(),
'getBlikChannelID' => TransactionOriginApi::BLIK_CHANNEL,
'useSandbox' => $this->tpayConfig->useSandboxMode(),
'grandTotal' => number_format($this->tpay->getCheckoutTotal(), 2, '.', ''),
'groups' => $this->transactions()->getBankGroups((bool) $this->tpayConfig->onlyOnlineChannels())['groups'],
],
],
];
}
public function generateURL(string $name): string
{
return $this->assetRepository->createAsset($name)->getUrl();
}
public function showChannels(): ?string
{
$script = 'Tpay_Magento2::js/open_render_channels.js';
return $this->createScript($script);
}
public function createScript(string $script): string
{
return "
<script type=\"text/javascript\">
require(['jquery'], function ($) {
$.getScript('{$this->generateURL($script)}');
});
</script>";
}
public function getTerms(): ?string
{
return $this->tpayConfig->getTermsURL();
}
public function createCSS(string $css): string
{
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->generateURL($css)}\">";
}
public function getCardConfig()
{
$customerTokensData = [];
if ($this->tpayConfig->getCardSaveEnabled() && $this->tpay->isCustomerLoggedIn()) {
$customerTokens = $this->tokensService->getCustomerTokens($this->tpay->getCheckoutCustomerId(), true);
foreach ($customerTokens as $value) {
$customerTokensData[] = [
'cardShortCode' => $value['cardShortCode'],
'id' => $value['tokenId'],
'vendor' => $value['vendor'],
];
}
}
return [
'tpaycards' => [
'payment' => [
'tpayLogoUrl' => $this->generateURL('Tpay_Magento2::images/logo_tpay.png'),
'tpayCardsLogoUrl' => $this->generateURL('Tpay_Magento2::images/card.svg'),
'getTpayLoadingGif' => $this->generateURL('Tpay_Magento2::images/loading.gif'),
'getRSAkey' => $this->tpayConfig->getRSAKey(),
'fetchJavaScripts' => $this->fetchJavaScripts(),
'addCSS' => $this->createCSS('Tpay_Magento2::css/tpaycards.css'),
'redirectUrl' => $this->tpay->getPaymentRedirectUrl(),
'isCustomerLoggedIn' => $this->tpay->isCustomerLoggedIn(),
'customerTokens' => $customerTokensData,
'isSavingEnabled' => $this->tpayConfig->getCardSaveEnabled(),
'getTerms' => $this->getTerms(),
],
],
];
}
public function fetchJavaScripts()
{
$script = [];
$script[] = 'Tpay_Magento2::js/jquery.payment.min.js';
$script[] = 'Tpay_Magento2::js/jsencrypt.min.js';
$script[] = 'Tpay_Magento2::js/string_routines.js';
$script[] = 'Tpay_Magento2::js/tpayCards.js';
$script[] = 'Tpay_Magento2::js/renderSavedCards.js';
$script[] = 'Tpay_Magento2::js/tpayGeneric.js';
$scripts = '';
foreach ($script as $key => $value) {
$scripts .= $this->createScript($value);
}
return $scripts;
}
}