diff --git a/Api/BulkApiInterface.php b/Api/BulkApiInterface.php new file mode 100644 index 0000000..0248e4a --- /dev/null +++ b/Api/BulkApiInterface.php @@ -0,0 +1,14 @@ +responseFail($response->getMessage()); } -} \ No newline at end of file +} diff --git a/Factories/BulkSingleResponseFactory.php b/Factories/BulkSingleResponseFactory.php new file mode 100644 index 0000000..a300c42 --- /dev/null +++ b/Factories/BulkSingleResponseFactory.php @@ -0,0 +1,31 @@ +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; + } +} diff --git a/Model/Api/Bulk.php b/Model/Api/Bulk.php new file mode 100644 index 0000000..59e2d7b --- /dev/null +++ b/Model/Api/Bulk.php @@ -0,0 +1,187 @@ +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; + } +} diff --git a/Model/Api/BulkSingleResponse.php b/Model/Api/BulkSingleResponse.php new file mode 100644 index 0000000..8f3028a --- /dev/null +++ b/Model/Api/BulkSingleResponse.php @@ -0,0 +1,36 @@ +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; + } +} diff --git a/Model/Api/Charge.php b/Model/Api/Charge.php new file mode 100644 index 0000000..1b50a8b --- /dev/null +++ b/Model/Api/Charge.php @@ -0,0 +1,50 @@ +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); + } + } +} diff --git a/Model/Api/ResponseMessage.php b/Model/Api/ResponseMessage.php new file mode 100644 index 0000000..1e88829 --- /dev/null +++ b/Model/Api/ResponseMessage.php @@ -0,0 +1,22 @@ +message = $message; + } + + /** + * @return string + */ + public function getMessage() + { + return $this->message; + } +} diff --git a/README.md b/README.md index fa36bcc..9743d0a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 70e9380..ac3288b 100755 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/etc/di.xml b/etc/di.xml index 3a8cbb8..b05b472 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -38,6 +38,12 @@ + + + + MundiPagg\MundiPagg\Model\Installments\BuilderByBrand diff --git a/etc/module.xml b/etc/module.xml index b51d7ba..89ff5c6 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -9,7 +9,7 @@ */ --> - + diff --git a/etc/webapi.xml b/etc/webapi.xml index a7ce166..4670e93 100755 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -189,4 +189,20 @@ + + + + + + + + + + + + + +