Skip to content

Commit

Permalink
Merge pull request #50 from mundipagg/develop
Browse files Browse the repository at this point in the history
Merge Develop into Master
  • Loading branch information
GabrielDeveloper authored Apr 26, 2019
2 parents 97fde12 + 5b64a94 commit 9a0f8f2
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mundipagg/ecommerce-module-core",
"license": "MIT",
"version": "1.6.4",
"version": "1.7.0",
"authors":[
{
"name":"MundiPagg Embeddables Team",
Expand Down
4 changes: 3 additions & 1 deletion src/Kernel/I18N/ENUS.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected function getTable()
"Order payment failed" => null,
"The order will be canceled" => null,
"An error occurred when trying to create the order. Please try again. Error Reference: %s." => null,
"Can't cancel current order. Please cancel it by Mundipagg panel" => null
"Can't cancel current order. Please cancel it by Mundipagg panel" => null,
"Charge canceled with success" => null,
'Invalid address. Please fill the street lines and try again.' => null
];
}
}
4 changes: 3 additions & 1 deletion src/Kernel/I18N/PTBR.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected function getTable()
"Order payment failed" => "Pagamento do pedido falhou",
"The order will be canceled" => "O pedido será cancelado",
"An error occurred when trying to create the order. Please try again. Error Reference: %s" => 'Ocorreu um erro ao tentar criar o pedido. Por favor, tente novamente. Referência do erro: %s',
"Can't cancel current order. Please cancel it by Mundipagg panel" => "Não foi possível cancelar o pedido. Por favor, realize o cancelamento no portal Mundipagg."
"Can't cancel current order. Please cancel it by Mundipagg panel" => "Não foi possível cancelar o pedido. Por favor, realize o cancelamento no portal Mundipagg.",
"Charge canceled with success" => "Charge cancelada com sucesso",
'Invalid address. Please fill the street lines and try again.' => 'Endereço inválido. Preencha rua, número e bairro e tente novamente.'
];
}
}
3 changes: 3 additions & 0 deletions src/Kernel/Interfaces/PlatformOrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ public function getItemCollection();
public function getPaymentMethodCollection();
/** @return null|Shipping */
public function getShipping();

/** @since 1.6.5 */
public function getTotalCanceled();
}
18 changes: 18 additions & 0 deletions src/Kernel/Repositories/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ public function findByMundipaggId(AbstractValidString $mundipaggId)
return $factory->createFromDbData($result->row);
}

public function findByPlatformId($platformID)
{
$orderTable = $this->db->getTable(AbstractDatabaseDecorator::TABLE_ORDER);

$query = "SELECT * FROM `$orderTable` ";
$query .= "WHERE code = '{$platformID}';";

$result = $this->db->fetch($query);

if ($result->num_rows === 0) {
return null;
}

$factory = new OrderFactory();

return $factory->createFromDbData($result->row);
}

public function listEntities($limit, $listDisabled)
{
// TODO: Implement listEntities() method.
Expand Down
36 changes: 36 additions & 0 deletions src/Kernel/Responses/ServiceResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Mundipagg\Core\Kernel\Responses;

final class ServiceResponse
{

/** @var bool */
private $success;
/** @var string */
private $message;
/** @var object */
private $object;

public function __construct($success, $message, $object = null)
{
$this->message = $message;
$this->success = $success;
$this->object = $object;
}

public function isSuccess()
{
return $this->success;
}

public function getMessage()
{
return $this->message;
}

public function getObject()
{
return $this->object;
}
}
22 changes: 20 additions & 2 deletions src/Kernel/Services/APIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MundiAPILib\APIException;
use MundiAPILib\Exceptions\ErrorException;
use MundiAPILib\Models\CreateCancelChargeRequest;
use MundiAPILib\Models\CreateCaptureChargeRequest;
use MundiAPILib\MundiAPIClient;
use Mundipagg\Core\Kernel\Abstractions\AbstractModuleCoreSetup as MPSetup;
use Mundipagg\Core\Kernel\Aggregates\Charge;
Expand All @@ -23,22 +24,39 @@ public function __construct()
$this->logService = new OrderLogService(2);
}

public function cancelCharge(Charge &$charge)
public function cancelCharge(Charge &$charge, $amount = 0)
{
try {
$chargeId = $charge->getMundipaggId()->getValue();
$request = new CreateCancelChargeRequest();
$request->amount = $amount;

$chargeController = $this->getChargeController();
$result = $chargeController->cancelCharge($chargeId, $request);
$charge->cancel();
$charge->cancel($amount);

return null;
} catch (APIException $e) {
return $e->getMessage();
}
}

public function captureCharge(Charge &$charge, $amount = 0)
{
try {
$chargeId = $charge->getMundipaggId()->getValue();
$request = new CreateCaptureChargeRequest;
$request->amount = $amount;

$chargeController = $this->getChargeController();
$result = $chargeController->captureCharge($chargeId, $request);

return $result;
} catch (APIException $e) {
return $e->getMessage();
}
}

public function createOrder(Order $order)
{
$endpoint = $this->getAPIBaseEndpoint();
Expand Down
200 changes: 200 additions & 0 deletions src/Kernel/Services/ChargeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace Mundipagg\Core\Kernel\Services;

use MundiAPILib\Models\GetChargeResponse;
use Mundipagg\Core\Kernel\Aggregates\Charge;
use Mundipagg\Core\Kernel\Repositories\ChargeRepository;
use Mundipagg\Core\Kernel\Repositories\OrderRepository;
use Mundipagg\Core\Kernel\Responses\ServiceResponse;
use Mundipagg\Core\Kernel\ValueObjects\ChargeStatus;
use Mundipagg\Core\Kernel\ValueObjects\Id\ChargeId;
use Mundipagg\Core\Kernel\ValueObjects\Id\OrderId;
use Mundipagg\Core\Kernel\ValueObjects\OrderState;
use Mundipagg\Core\Kernel\ValueObjects\OrderStatus;
use Mundipagg\Core\Payment\Services\ResponseHandlers\OrderHandler;
use Mundipagg\Core\Webhook\Services\ChargeHandlerService;
use Unirest\Exception;

class ChargeService
{
/** @var LogService */
protected $logService;

public function __construct()
{
$this->logService = new LogService(
'ChargeService',
true
);
}

public function captureById($chargeId, $amount = 0)
{
try {

$chargeRepository = new ChargeRepository();
$charge = $chargeRepository->findByMundipaggId(
new ChargeId($chargeId)
);

if ($charge === null) {
throw new Exception("Charge not found");
}

return $this->capture($charge, $amount);

} catch (Exception $exception) {
return $exception->getMessage();
}
}

public function cancelById($chargeId, $amount = 0)
{
try {

$chargeRepository = new ChargeRepository();
$charge = $chargeRepository->findByMundipaggId(
new ChargeId($chargeId)
);

if ($charge === null) {
throw new Exception("Charge not found");
}

return $this->cancel($charge, $amount);

} catch (Exception $exception) {
return $exception->getMessage();
}
}

public function capture(Charge $charge, $amount = 0)
{
$order = (new OrderRepository)->findByMundipaggId(
new OrderId($charge->getOrderId()->getValue())
);

$this->logService->info("Charge capture");
$orderRepository = new OrderRepository();
$orderService = new OrderService();
$chargeHandlerService = new ChargeHandlerService();

$platformOrder = $order->getPlatformOrder();

$apiService = new APIService();
$this->logService->info(
"Capturing charge on Mundipagg - " . $charge->getMundipaggId()->getValue()
);

$resultApi = $apiService->captureCharge($charge, $amount);

if ($resultApi instanceof GetChargeResponse) {

if (!$charge->getStatus()->equals(ChargeStatus::paid())) {
$this->logService->info(
"Pay charge - " . $charge->getMundipaggId()->getValue()
);
$charge->pay($amount);
}

if ($charge->getPaidAmount() == 0) {
$charge->setPaidAmount($amount);
}

$this->logService->info("Update Charge on Order");
$order->updateCharge($charge);
$orderRepository->save($order);

$this->logService->info("Adding history on Order");
$history = $chargeHandlerService->prepareHistoryComment($charge);
$platformOrder->addHistoryComment($history);

$this->logService->info("Synchronizing with platform Order");
$orderService->syncPlatformWith($order);

$this->logService->info("Change Order status");
$order->setStatus(OrderStatus::paid());
$orderHandlerService = new OrderHandler();
$orderHandlerService->handle($order);

$message = $chargeHandlerService->prepareReturnMessage($charge);

return new ServiceResponse(true, $message);
}

return new ServiceResponse(false, $resultApi);
}

public function cancel(Charge $charge, $amount = 0)
{

$order = (new OrderRepository)->findByMundipaggId(
new OrderId($charge->getOrderId()->getValue())
);

$this->logService->info("Charge cancel");

$orderRepository = new OrderRepository();
$orderService = new OrderService();
$moneyService = new MoneyService();
$chargeHandlerService = new ChargeHandlerService();
$i18n = new LocalizationService();

$platformOrder = $order->getPlatformOrder();

$apiService = new APIService();
$this->logService->info(
"Cancel charge on Mundipagg - " . $charge->getMundipaggId()->getValue()
);

$resultApi = $apiService->cancelCharge($charge, $amount);

if ($resultApi === null) {

$this->logService->info("Update Charge on Order");
$order->updateCharge($charge);
$orderRepository->save($order);
$history = $chargeHandlerService->prepareHistoryComment($charge);

$this->logService->info("Adding history on Order");
$order->getPlatformOrder()->addHistoryComment($history);

$this->logService->info("Synchronizing with platform Order");
$orderService->syncPlatformWith($order);

$platformOrderGrandTotal = $moneyService->floatToCents(
$platformOrder->getGrandTotal()
);
$platformOrderTotalCanceled = $moneyService->floatToCents(
$platformOrder->getTotalCanceled()
);

$platformOrderTotalRefunded = $moneyService->floatToCents(
$platformOrder->getTotalRefunded()
);
if (
$platformOrderGrandTotal === $platformOrderTotalCanceled ||
$platformOrderGrandTotal === $platformOrderTotalRefunded
) {
$this->logService->info("Change Order status");

$order->setStatus(OrderStatus::canceled());
$order->getPlatformOrder()->setState(OrderState::canceled());
$order->getPlatformOrder()->save();

$order->getPlatformOrder()->addHistoryComment(
$i18n->getDashboard('Order canceled.')
);

$orderRepository->save($order);
$orderService->syncPlatformWith($order);
}

$message = $i18n->getDashboard("Charge canceled with success");
return new ServiceResponse(true, $message);
}

return new ServiceResponse(false, $resultApi);
}
}
Loading

0 comments on commit 9a0f8f2

Please sign in to comment.