Skip to content

Commit

Permalink
Merge pull request #334 from mundipagg/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yanpantoja authored Jun 10, 2021
2 parents e1f6dcb + 43238a9 commit c2e9a9b
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Api/BulkApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MundiPagg\MundiPagg\Api;

interface BulkApiInterface
{
const HTTP_OK = 200;
const HTTP_BAD_REQUEST = 400;
const HTTP_INTERNAL_SERVER_ERROR = 500;
/**
* @return mixed
*/
public function execute();
}
13 changes: 13 additions & 0 deletions Api/BulkSingleResponseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MundiPagg\MundiPagg\Api;

use MundiPagg\MundiPagg\Model\Api\BulkSingleResponse;

interface BulkSingleResponseInterface
{
public function setStatus(int $status): BulkSingleResponse;
public function getStatus(): int;
public function setBody(array $body): BulkSingleResponse;
public function getBody(): array;
}
13 changes: 13 additions & 0 deletions Api/ChargeApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MundiPagg\MundiPagg\Api;

interface ChargeApiInterface
{

/**
* @param string $id
* @return MundiPagg\MundiPagg\Model\Api\ResponseMessage
*/
public function cancel($id);
}
2 changes: 1 addition & 1 deletion Concrete/integrityData

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Controller/Adminhtml/Charges/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public function execute()
}
return $this->responseFail($response->getMessage());
}
}
}
31 changes: 31 additions & 0 deletions Factories/BulkSingleResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace MundiPagg\MundiPagg\Factories;

use Magento\Framework\HTTP\Client\Curl;
use MundiPagg\MundiPagg\Model\Api\BulkSingleResponse;

class BulkSingleResponseFactory
{
public function createFromCurlResponse(Curl $curlResponse): BulkSingleResponse
{
$body = json_decode($curlResponse->getBody(), true);

if (!is_array($body)) {
$body = ['message' => $body];
}

$bulkSingleResponse = new BulkSingleResponse;
$bulkSingleResponse->setStatus($curlResponse->getStatus());
$bulkSingleResponse->setBody($body);
return $bulkSingleResponse;
}

public function createFromArrayData(array $arrayData): BulkSingleResponse
{
$bulkSingleResponse = new BulkSingleResponse;
$bulkSingleResponse->setStatus($arrayData['code']);
$bulkSingleResponse->setBody(['message' => $arrayData['message']]);
return $bulkSingleResponse;
}
}
187 changes: 187 additions & 0 deletions Model/Api/Bulk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace MundiPagg\MundiPagg\Model\Api;

use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\Webapi\Exception as MagentoException;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Setup\Exception;
use Magento\Store\Model\StoreManagerInterface;
use MundiPagg\MundiPagg\Api\BulkApiInterface;
use MundiPagg\MundiPagg\Concrete\Magento2CoreSetup;
use MundiPagg\MundiPagg\Factories\BulkSingleResponseFactory;

class Bulk implements BulkApiInterface
{
/**
* @var Request
*/
private $request;

/**
* @var Curl
*/
protected $curl;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var string
*/
private $baseUrl;

/**
* @var array
*/
private $responseArray;

public function __construct(
Request $request,
Curl $curl,
StoreManagerInterface $storeManager
)
{
$this->request = $request;
$this->curl = $curl;
$this->baseUrl = $storeManager->getStore()->getBaseUrl();
}

/**
* @return array|mixed
*/
public function execute()
{
$bodyParams = $this->request->getBodyParams();
$this->validateRequestsParam($bodyParams);

if (isset($bodyParams['access_token'])) {
$accessToken = $bodyParams['access_token'];
$this->curl->addHeader("Authorization", "Bearer {$accessToken}");
}

$this->curl->addHeader("Content-Type", "application/json");

foreach ($bodyParams['requests'] as $key => $request) {
$validationInfo = $this->validateSingleRequestParams($key, $request);

if ($validationInfo['code'] === self::HTTP_OK) {
$responseFactory = new BulkSingleResponseFactory();
$curlResponse = $this->executeCurl($key, $request);
$response = $responseFactory->createFromCurlResponse($curlResponse);
$this->setFormatedResponse($key, $response, $request);
}

if ($validationInfo['code'] === self::HTTP_BAD_REQUEST) {
$responseFactory = new BulkSingleResponseFactory();
$response = $responseFactory->createFromArrayData($validationInfo);
$this->setFormatedResponse($key, $response, $request);
}
}

return $this->getFormatedResponse();
}

private function validateRequestsParam($bodyParams)
{
if (!isset($bodyParams['requests'])) {
throw new MagentoException(
__("Requests parameter is required."),
0,
self::HTTP_BAD_REQUEST
);
}

if (empty($bodyParams['requests'])) {
throw new MagentoException(
__("Requests parameter can not be empty."),
0,
self::HTTP_BAD_REQUEST
);
}

if (!is_array($bodyParams['requests'])) {
throw new MagentoException(
__("Requests parameter must be an array."),
0,
self::HTTP_BAD_REQUEST
);
}
}

private function validateSingleRequestParams(
int $key,
array $request
): array {
if (!isset($request['method'])) {
return [
"message" => "Method parameter in array requests is required.",
"code" => self::HTTP_BAD_REQUEST
];
}

if (!isset($request['path'])) {
return [
"message" => "Path parameter in array requests is required.",
"code" => self::HTTP_BAD_REQUEST
];
}

if (!isset($request['params'])) {
return [
"message" => "Params parameter in array requests is required.",
"code" => self::HTTP_BAD_REQUEST
];
}

return [
"message" => "Successfully validated.",
"code" => self::HTTP_OK
];
}

private function executeCurl(int $key, array $request)
{
$method = $request['method'];
$apiUrl = $this->getApiBaseUrl() . $request['path'];
$params = json_encode($request['params']);

try {
$this->curl->$method($apiUrl, $params);
return $this->curl;
} catch (\Exception $exception) {
throw new MagentoException(
__($exception->getMessage()),
0,
self::HTTP_INTERNAL_SERVER_ERROR
);
}
}

private function getApiBaseUrl(): string
{
$defaultStoreViewCode = Magento2CoreSetup::getDefaultStoreViewCode();
return $this->baseUrl . "rest/{$defaultStoreViewCode}/V1/mundipagg";
}

private function setFormatedResponse(
int $index,
BulkSingleResponse $response,
array $request
): void {
$this->responseArray[] = array(
"index" => $index,
"status" => $response->getStatus(),
"body" => $response->getBody(),
"path" => $request['path'] ?? null,
"method" => $request['method'] ?? null,
);
}

private function getFormatedResponse(): array
{
return $this->responseArray;
}
}
36 changes: 36 additions & 0 deletions Model/Api/BulkSingleResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MundiPagg\MundiPagg\Model\Api;

use Magento\Framework\HTTP\Client\Curl;
use MundiPagg\MundiPagg\Api\BulkSingleResponseInterface;
use MundiPagg\MundiPagg\Model\Api\ResponseMessage;

class BulkSingleResponse implements BulkSingleResponseInterface
{
private $response;
private $status;
private $body;

public function setStatus(int $status): BulkSingleResponse
{
$this->status = $status;
return $this;
}

public function getStatus(): int
{
return $this->status;
}

public function setBody(array $body): BulkSingleResponse
{
$this->body = $body;
return $this;
}

public function getBody(): array
{
return $this->body;
}
}
50 changes: 50 additions & 0 deletions Model/Api/Charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace MundiPagg\MundiPagg\Model\Api;

use Magento\Framework\Webapi\Exception as MagentoException;
use MundiPagg\MundiPagg\Model\Api\ResponseMessage as ApiResponseMessage;
use MundiPagg\MundiPagg\Api\ChargeApiInterface;
use Mundipagg\Core\Kernel\Services\ChargeService;
use MundiPagg\MundiPagg\Concrete\Magento2CoreSetup;

class Charge implements ChargeApiInterface
{
/**
* @var ChargeService
*/
private $chargeService;

public function __construct()
{
Magento2CoreSetup::bootstrap();
$this->chargeService = new ChargeService();
}

/**
* @param string $id
* @return object
* @throws Exception
*/
public function cancel($id)
{
try {
$response = $this->chargeService->cancelById($id);

if (is_string($response)) {
throw new MagentoException(__($response), 0, 400);
}

if ($response->isSuccess()) {
$message = new ApiResponseMessage($response->getMessage());

return $message;
}

throw new MagentoException(__($response->getMessage()), 0, 400);

} catch (\Exception $exception) {
throw new MagentoException(__($exception->getMessage()), 0, 400);
}
}
}
22 changes: 22 additions & 0 deletions Model/Api/ResponseMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace MundiPagg\MundiPagg\Model\Api;

final class ResponseMessage
{
/** @var string */
private $message;

public function __construct($message)
{
$this->message = $message;
}

/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ For the future versions, just run composer update
## Requirements
* PHP >= 7.1
* Magento >= 2.1

(This PHP version is the recommended one, but version 5.6 has been kept in Packagist/Composer for compatibility reasons.)

## Configuration

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mundipagg/mundipagg-magento2-module",
"license": "MIT",
"version": "2.10.5",
"version": "2.11.0",
"type": "magento2-module",
"description": "Magento 2 Module Mundipagg",
"require": {
Expand Down
Loading

0 comments on commit c2e9a9b

Please sign in to comment.