-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.php
131 lines (112 loc) · 3.29 KB
/
Data.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
<?php
/**
* O2TI PagBank Dynamic Account Module.
*
* Copyright © 2024 O2TI. All rights reserved.
*
* @author Bruno Elisei <brunoelisei@o2ti.com>
* @license See LICENSE for license details.
*/
namespace O2TI\PagBankDynamicAccount\Helper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Math\Random;
use PagBank\PaymentMagento\Gateway\Config\Config;
class Data extends AbstractHelper
{
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var Config
*/
protected $configBase;
/**
* @var Random
*/
protected $mathRandom;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* @param ScopeConfigInterface $scopeConfig
* @param Config $configBase
* @param Random $mathRandom
* @param SerializerInterface $serializer
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Config $configBase,
Random $mathRandom,
SerializerInterface $serializer
) {
$this->scopeConfig = $scopeConfig;
$this->configBase = $configBase;
$this->mathRandom = $mathRandom;
$this->serializer = $serializer;
}
/**
* Get Config Data.
*
* @param int|null $storeId
*
* @return string|null
*/
public function getConfigDynamicAccounts($storeId = null)
{
$environment = $this->configBase->getAddtionalValue('environment', $storeId);
$pathPattern = 'dynamic_accounts_%s';
$data = [];
$sellers = $this->configBase->getAddtionalValue(
sprintf($pathPattern, $environment)
);
if ($sellers) {
$data = $this->serializer->unserialize($sellers);
}
return $data;
}
/**
* Get Random Seller.
*
* @param \Magento\Quote\Api\Data\CartInterface $cart
*
* @return array|null
*/
public function getRandomSeller($cart)
{
$storeId = $cart->getStoreId();
$sellers = $this->getConfigDynamicAccounts($storeId);
if (empty($sellers)) {
throw new \Magento\Framework\Exception\LocalizedException(__('No sellers available'));
}
// Filtra sellers com status 1
$activeSellers = array_filter($sellers, function ($seller) {
return isset($seller['status']) && (bool)$seller['status'] == 1;
});
if (empty($activeSellers)) {
throw new \Magento\Framework\Exception\LocalizedException(__('No active sellers available'));
}
$randomIndex = $this->mathRandom->getRandomNumber(0, count($activeSellers) - 1);
return $activeSellers[array_keys($activeSellers)[$randomIndex]];
}
/**
* Get oAuth By Account Id.
*
* @param string $accountId
*
* @return string|null
*/
public function getOauthByAccountId($accountId)
{
$sellers = $this->getConfigDynamicAccounts();
foreach ($sellers as $seller) {
if (isset($seller['account_id']) && $seller['account_id'] === $accountId) {
return $seller['access_token'];
}
}
return null;
}
}