From 2ecd6b2e000898211e74f6cdf353ccec8892f893 Mon Sep 17 00:00:00 2001 From: netorodrigues Date: Wed, 24 Mar 2021 16:49:49 -0300 Subject: [PATCH 1/6] :bug: cancels all charges if one of them fail --- src/Kernel/Services/OrderService.php | 103 +++++++++++++++++---------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/src/Kernel/Services/OrderService.php b/src/Kernel/Services/OrderService.php index d062550..9290d28 100644 --- a/src/Kernel/Services/OrderService.php +++ b/src/Kernel/Services/OrderService.php @@ -19,6 +19,7 @@ use Mundipagg\Core\Kernel\Factories\ChargeFactory; use Mundipagg\Core\Payment\Aggregates\Order as PaymentOrder; use Exception; +use Mundipagg\Core\Kernel\ValueObjects\ChargeStatus; final class OrderService { @@ -100,6 +101,27 @@ public function updateAcquirerData(Order $order) $dataService->updateAcquirerData($order); } + public function cancelChargesAtMundipagg(Order $order){ + $messages = []; + $charges = $order->getCharges(); + + $APIService = new APIService(); + + foreach ($charges as $charge) { + if ($charge->getStatus()->equals(ChargeStatus::canceled()) || + $charge->getStatus()->equals(ChargeStatus::failed()) ){ + continue; + } + $result = $APIService->cancelCharge($charge); + if ($result !== null) { + $messages[$charge->getMundipaggId()->getValue()] = $result; + } + $order->updateCharge($charge); + } + + return $messages; + } + public function cancelAtMundipagg(Order $order) { $orderRepository = new OrderRepository(); @@ -112,21 +134,10 @@ public function cancelAtMundipagg(Order $order) return; } - $APIService = new APIService(); - - $charges = $order->getCharges(); - $results = []; - foreach ($charges as $charge) { - $result = $APIService->cancelCharge($charge); - if ($result !== null) { - $results[$charge->getMundipaggId()->getValue()] = $result; - } - $order->updateCharge($charge); - } - - $i18n = new LocalizationService(); + $results = $this->cancelChargesAtMundipagg($order); if (empty($results)) { + $i18n = new LocalizationService(); $order->setStatus(OrderStatus::canceled()); $order->getPlatformOrder()->setStatus(OrderStatus::canceled()); @@ -155,6 +166,12 @@ public function cancelAtMundipagg(Order $order) return; } + $this->addMessagesToPlatformHistory($results, $order); + + } + + public function addMessagesToPlatformHistory($results, $order){ + $i18n = new LocalizationService(); $history = $i18n->getDashboard("Some charges couldn't be canceled at Mundipagg. Reasons:"); $history .= "