Skip to content

Commit

Permalink
Getnet Magento 2.4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
elisei committed Dec 19, 2023
1 parent 07370ae commit 89bed0b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
28 changes: 18 additions & 10 deletions Model/Console/Command/Administrative/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\HTTP\ZendClientFactory;
use Laminas\Http\ClientFactory;
use Laminas\Http\Request;
use Magento\Framework\Serialize\Serializer\Json;

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ class Token extends AbstractModel
protected $json;

/**
* @var ZendClientFactory
* @var ClientFactory
*/
protected $httpClientFactory;

Expand All @@ -84,7 +84,7 @@ class Token extends AbstractModel
* @param GetnetHelper $getnetHelper
* @param Config $config
* @param Json $json
* @param ZendClientFactory $httpClientFactory
* @param ClientFactory $httpClientFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -97,7 +97,7 @@ public function __construct(
GetnetHelper $getnetHelper,
Config $config,
Json $json,
ZendClientFactory $httpClientFactory
ClientFactory $httpClientFactory
) {
parent::__construct(
$logger
Expand Down Expand Up @@ -174,25 +174,33 @@ private function getNewToken(): array
'scope' => 'mgm',
'grant_type' => 'client_credentials',
];

$client = $this->httpClientFactory->create();
$client->setUri($uri.'credenciamento/auth/oauth/v2/token');
$client->setAuth($clientId, $clientSecret);
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
$client->setOptions(['maxredirects' => 0, 'timeout' => 30]);
$client->setHeaders(['content' => 'application/x-www-form-urlencoded']);
$client->setParameterPost($dataSend);
$client->setMethod(ZendClient::POST);
$client->setMethod(Request::METHOD_POST);

try {
$result = $client->request()->getBody();
$result = $client->send()->getBody();
$response = $this->json->unserialize($result);
$this->logger->info($this->json->serialize(['response' => $response]));
$this->logger->info($this->json->serialize([
'uri' => $uri.'credenciamento/auth/oauth/v2/token',
'response' => $response
]));

return [
'success' => true,
'response' => $response,
];
} catch (Exception $exc) {
$this->logger->debug($this->json->serialize(['error' => $exc->getMessage()]));
$this->logger->info($this->json->serialize([
'uri' => $uri.'credenciamento/auth/oauth/v2/token',
'response' => $client->send()->getBody(),
'error' => $exc->getMessage()
]));

return ['success' => false, 'error' => $exc->getMessage()];
}
Expand Down
29 changes: 18 additions & 11 deletions Model/Console/Command/Synchronize/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Getnet\SubSellerMagento\Model\Console\Command\AbstractModel;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\HTTP\ZendClientFactory;
use Laminas\Http\ClientFactory;
use Laminas\Http\Request;
use Magento\Framework\Serialize\Serializer\Json;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ class Create extends AbstractModel
protected $json;

/**
* @var ZendClientFactory
* @var ClientFactory
*/
protected $httpClientFactory;

Expand All @@ -69,7 +69,7 @@ class Create extends AbstractModel
* @param GetnetHelper $getnetHelper
* @param SubSellerRepositoryInterface $subSellerRepository
* @param Json $json
* @param ZendClientFactory $httpClientFactory
* @param ClientFactory $httpClientFactory
*/
public function __construct(
State $state,
Expand All @@ -78,7 +78,7 @@ public function __construct(
GetnetHelper $getnetHelper,
SubSellerRepositoryInterface $subSellerRepository,
Json $json,
ZendClientFactory $httpClientFactory
ClientFactory $httpClientFactory
) {
$this->state = $state;
$this->logger = $logger;
Expand Down Expand Up @@ -220,20 +220,27 @@ protected function sendData(
): \Magento\Framework\DataObject {
$uri = $this->getnetConfig->getUri();
$bearer = $this->getnetConfig->getAuth();
$client = $this->httpClientFactory->create();
$send = $this->json->serialize($sellerFomarted);

$headers = [
'Authorization' => 'Bearer '.$bearer,
'Content-Type' => 'application/json'
];

$client = $this->httpClientFactory->create();
$client->setUri($uri.'v1/mgm/pf/create-presubseller');
if ($typePersona) {
$client->setUri($uri.'v1/mgm/pj/create-presubseller');
}
$client->setHeaders('Authorization', 'Bearer '.$bearer);
$client->setConfig(['maxredirects' => 0, 'timeout' => 40]);
$client->setRawData($send, 'application/json');
$client->setMethod(ZendClient::POST);
$client->setHeaders($headers);
$client->setOptions(['maxredirects' => 0, 'timeout' => 30]);
$client->setRawBody($send);
$client->setMethod(Request::METHOD_POST);

$getnetData = new \Magento\Framework\DataObject();

try {
$result = $client->request()->getBody();
$result = $client->send()->getBody();
$response = $this->json->unserialize($result);
$this->logger->info(
$this->json->serialize([
Expand Down
23 changes: 15 additions & 8 deletions Model/Console/Command/Synchronize/GetUpdateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Getnet\SubSellerMagento\Model\Console\Command\AbstractModel;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\HTTP\ZendClientFactory;
use Laminas\Http\ClientFactory;
use Laminas\Http\Request;
use Magento\Framework\Serialize\Serializer\Json;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ class GetUpdateData extends AbstractModel
protected $json;

/**
* @var ZendClientFactory
* @var ClientFactory
*/
protected $httpClientFactory;

Expand All @@ -69,7 +69,7 @@ class GetUpdateData extends AbstractModel
* @param GetnetHelper $getnetHelper
* @param SubSellerRepositoryInterface $subSellerRepository
* @param Json $json
* @param ZendClientFactory $httpClientFactory
* @param ClientFactory $httpClientFactory
*/
public function __construct(
State $state,
Expand All @@ -78,7 +78,7 @@ public function __construct(
GetnetHelper $getnetHelper,
SubSellerRepositoryInterface $subSellerRepository,
Json $json,
ZendClientFactory $httpClientFactory
ClientFactory $httpClientFactory
) {
$this->state = $state;
$this->logger = $logger;
Expand Down Expand Up @@ -181,6 +181,10 @@ protected function getDataOnGetnet(
$client = $this->httpClientFactory->create();
$uriBase = $this->getnetConfig->getUri();
$bearer = $this->getnetConfig->getAuth();
$headers = [
'Authorization' => 'Bearer '.$bearer,
'Content-Type' => 'application/json'
];
$type = 'pf';

if ($typePersona) {
Expand All @@ -189,13 +193,16 @@ protected function getDataOnGetnet(

$uri = sprintf('%sv1/mgm/%s/callback/%s/%s', $uriBase, $type, $merchantId, $legalNumber);

$client = $this->httpClientFactory->create();
$client->setUri($uri);
$client->setHeaders('Authorization', 'Bearer '.$bearer);
$client->setMethod(ZendClient::GET);
$client->setHeaders($headers);
$client->setOptions(['maxredirects' => 0, 'timeout' => 30]);
$client->setMethod(Request::METHOD_GET);

$getnetData = new \Magento\Framework\DataObject();

try {
$result = $client->request()->getBody();
$result = $client->send()->getBody();
$response = $this->json->unserialize($result);
$getnetData->setData($response);

Expand Down
23 changes: 14 additions & 9 deletions Model/Console/Command/Synchronize/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Getnet\SubSellerMagento\Model\Console\Command\AbstractModel;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\HTTP\ZendClientFactory;
use Laminas\Http\ClientFactory;
use Laminas\Http\Request;
use Magento\Framework\Serialize\Serializer\Json;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ class Update extends AbstractModel
protected $json;

/**
* @var ZendClientFactory
* @var ClientFactory
*/
protected $httpClientFactory;

Expand All @@ -69,7 +69,7 @@ class Update extends AbstractModel
* @param GetnetHelper $getnetHelper
* @param SubSellerRepositoryInterface $subSellerRepository
* @param Json $json
* @param ZendClientFactory $httpClientFactory
* @param ClientFactory $httpClientFactory
*/
public function __construct(
State $state,
Expand All @@ -78,7 +78,7 @@ public function __construct(
GetnetHelper $getnetHelper,
SubSellerRepositoryInterface $subSellerRepository,
Json $json,
ZendClientFactory $httpClientFactory
ClientFactory $httpClientFactory
) {
$this->state = $state;
$this->logger = $logger;
Expand Down Expand Up @@ -207,6 +207,10 @@ protected function updateData(
$client = $this->httpClientFactory->create();
$send = $this->json->serialize($sellerFomarted);
$client->setUri($uri.'v1/mgm/pf/update-subseller');
$headers = [
'Authorization' => 'Bearer '.$bearer,
'Content-Type' => 'application/json'
];

if ($typePersona) {
$client->setUri($uri.'v1/mgm/pj/update-subseller');
Expand All @@ -220,10 +224,11 @@ protected function updateData(
}
}

$client->setHeaders('Authorization', 'Bearer '.$bearer);
$client->setConfig(['maxredirects' => 0, 'timeout' => 40]);
$client->setRawData($send, 'application/json');
$client->setMethod(ZendClient::PUT);
$client->setHeaders($headers);
$client->setOptions(['maxredirects' => 0, 'timeout' => 30]);

$client->setRawBody($send);
$client->setMethod(Request::METHOD_PUT);
$getnetData = new \Magento\Framework\DataObject();

try {
Expand Down

0 comments on commit 89bed0b

Please sign in to comment.