-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from mundipagg/develop
Develop
- Loading branch information
Showing
15 changed files
with
394 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,4 @@ public function execute() | |
} | ||
return $this->responseFail($response->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.