diff --git a/README.md b/README.md index 14fa266..78da173 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This is the repository of Pagar.me's payment module core for all supported e-com - [Pagar.me Magento payment module for Magento 2.3+](https://github.com/pagarme/magento2). ## Dependencies -* ``PHP`` Version 7.1 - 7.4 +* ``PHP`` Version 7.1 - 8.1 ## Install Require by composer diff --git a/composer.json b/composer.json index cc64c71..77f23bc 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "pagarme/ecommerce-module-core", "description": "Core component for Pagar.me e-commerce platform modules.", "license": "MIT", - "version": "2.2.1", + "version": "2.2.2", "authors": [ { "name":"Open Source Team" @@ -12,7 +12,7 @@ "require": { "php": ">=7.1", "monolog/monolog": "<3", - "pagarme/pagarmecoreapi": "5.6.1", + "pagarme/pagarmecoreapi": "5.6.2", "ext-json": "*" }, "require-dev": { diff --git a/src/Hub/Aggregates/InstallToken.php b/src/Hub/Aggregates/InstallToken.php index 7969378..45c5184 100644 --- a/src/Hub/Aggregates/InstallToken.php +++ b/src/Hub/Aggregates/InstallToken.php @@ -7,7 +7,7 @@ final class InstallToken extends AbstractEntity { - const LIFE_SPAN = 1800; //time in seconds + const LIFE_SPAN = 43200; //time in seconds /** * diff --git a/src/Hub/Services/HubIntegrationService.php b/src/Hub/Services/HubIntegrationService.php index 5b9cb3f..2f3710f 100644 --- a/src/Hub/Services/HubIntegrationService.php +++ b/src/Hub/Services/HubIntegrationService.php @@ -58,7 +58,13 @@ public function endHubIntegration( $rawToken = $installToken; $installToken = $tokenRepo->findByPagarmeId(new HubInstallToken($installToken)); - + + if (is_null($installToken)) { + $message = "Received an invalid installToken. NULL: $rawToken"; + $exception = new \Exception($message); + $this->logService->exception($exception); + throw $exception; + } if (empty($installToken)) { $message = "installToken not found in database. Raw Token: $rawToken"; diff --git a/src/Kernel/Abstractions/AbstractModuleCoreSetup.php b/src/Kernel/Abstractions/AbstractModuleCoreSetup.php index a6b82e2..faf6ffc 100644 --- a/src/Kernel/Abstractions/AbstractModuleCoreSetup.php +++ b/src/Kernel/Abstractions/AbstractModuleCoreSetup.php @@ -87,27 +87,21 @@ protected static function setApiBaseUrl() protected static function updateModuleConfiguration() { - $configurationRepository = new ConfigurationRepository; - static::loadSavedConfiguration(); - $savedConfig = static::$moduleConfig; static::$instance->loadModuleConfigurationFromPlatform(); static::$moduleConfig->setStoreId(static::getCurrentStoreId()); - if ( $savedConfig !== null && ($savedConfigId = $savedConfig->getId()) !== null ) { static::$moduleConfig->setid($savedConfigId); } - if (self::getDefaultConfigSaved() === null) { static::$moduleConfig->setStoreId(static::getDefaultStoreId()); - $configurationRepository->save(static::$moduleConfig); + static::saveModuleConfig(); static::$moduleConfig->setStoreId(static::getCurrentStoreId()); } - if ( static::$moduleConfig->getStoreId() != static::getDefaultStoreId() && $savedConfig === null @@ -116,10 +110,16 @@ protected static function updateModuleConfiguration() static::$moduleConfig->setInheritAll(true); static::$moduleConfig->setId(null); } - - $configurationRepository->save(static::$moduleConfig); + static::saveModuleConfig(); } + protected static function saveModuleConfig() + { + if (strpos(static::$instance->getPlatformVersion(), 'Wordpress') === false) { + $configurationRepository = new ConfigurationRepository; + $configurationRepository->save(static::$moduleConfig); + } + } protected static function loadSavedConfiguration() { $store = static::getCurrentStoreId(); diff --git a/src/Kernel/Aggregates/Charge.php b/src/Kernel/Aggregates/Charge.php index e49b539..7e0a8b2 100644 --- a/src/Kernel/Aggregates/Charge.php +++ b/src/Kernel/Aggregates/Charge.php @@ -339,6 +339,11 @@ public function failed() $this->status = ChargeStatus::failed(); } + public function chargedback() + { + $this->status = ChargeStatus::chargedback(); + } + /** * * @param Transaction $newTransaction diff --git a/src/Kernel/Aggregates/Configuration.php b/src/Kernel/Aggregates/Configuration.php index 26e3c0a..76c1f92 100644 --- a/src/Kernel/Aggregates/Configuration.php +++ b/src/Kernel/Aggregates/Configuration.php @@ -331,10 +331,12 @@ protected function isTestMode() */ public function isHubEnabled() { - if ($this->hubInstallId === null) { - return false; + if ($this->getHubInstallId() && $this->getHubInstallId() instanceof GUID) { + return + $this->getHubInstallId()->getValue() !== null && + $this->getHubInstallId()->getValue() !== "00000000-0000-0000-0000-000000000000"; } - return $this->hubInstallId->getValue() !== null; + return false; } public function setHubInstallId(GUID $hubInstallId) @@ -580,10 +582,7 @@ public function setAntifraudMinAmount($antifraudMinAmount) $minAmount = preg_replace($numbers, $replace, $antifraudMinAmount ?? ''); if ($minAmount < 0) { - throw new InvalidParamException( - 'AntifraudMinAmount should be at least 0!', - $minAmount - ); + $minAmount = 0; } $this->antifraudMinAmount = $minAmount; } diff --git a/src/Kernel/Services/OrderService.php b/src/Kernel/Services/OrderService.php index efd5494..b100e80 100644 --- a/src/Kernel/Services/OrderService.php +++ b/src/Kernel/Services/OrderService.php @@ -287,7 +287,9 @@ public function createOrderAtPagarme(PlatformOrderInterface $platformOrder) throw new \Exception($message, 400); } - $platformOrder->save(); + if (strpos(MPSetup::getPlatformVersion(), 'Wordpress') === false) { + $platformOrder->save(); + } $orderFactory = new OrderFactory(); $order = $orderFactory->createFromPostData($response); diff --git a/src/Payment/Aggregates/Payments/AbstractPayment.php b/src/Payment/Aggregates/Payments/AbstractPayment.php index 3792098..6fc4de2 100644 --- a/src/Payment/Aggregates/Payments/AbstractPayment.php +++ b/src/Payment/Aggregates/Payments/AbstractPayment.php @@ -59,7 +59,8 @@ public function convertToSDKRequest() $newPayment->customer = $this->getCustomer()->convertToSDKRequest(); } - if ($this->moduleConfig->getMarketplaceConfig()->isEnabled()) { + $marketplaceConfig = $this->moduleConfig->getMarketplaceConfig(); + if ($marketplaceConfig && $marketplaceConfig->isEnabled()) { $newPayment->split = static::getSplitData(); $newPayment->split = $this->extractRequestsFromArray( $newPayment->split diff --git a/src/Webhook/Services/ChargeOrderService.php b/src/Webhook/Services/ChargeOrderService.php index 1e9fc75..c801067 100644 --- a/src/Webhook/Services/ChargeOrderService.php +++ b/src/Webhook/Services/ChargeOrderService.php @@ -363,6 +363,52 @@ protected function handlePaymentFailed(Webhook $webhook) ]; } + /** + * @param Webhook $webhook + * @return array + */ + protected function handleChargedback(Webhook $webhook) + { + $order = $this->order; + /** + * @var Charge $charge + */ + $charge = $webhook->getEntity(); + + $transaction = $charge->getLastTransaction(); + + $outdatedCharge = $this->chargeRepository->findByPagarmeId( + $charge->getPagarmeId() + ); + + if ($outdatedCharge !== null) { + $charge = $outdatedCharge; + } + /** + * @var Charge $outdatedCharge + */ + if ($transaction !== null) { + $charge->addTransaction($transaction); + } + + $charge->chargedback(); + $order->updateCharge($charge); + $this->orderRepository->save($order); + + $history = $this->prepareHistoryComment($charge); + $order->getPlatformOrder()->addHistoryComment($history, false); + + $this->orderService->syncPlatformWith($order, false); + + $returnMessage = $this->prepareReturnMessage($charge); + + return [ + "code" => 200, + "message" => $returnMessage + ]; + + } + /** * @return string */ @@ -490,6 +536,10 @@ public function prepareHistoryComment(ChargeInterface $charge) return $this->i18n->getDashboard('Charge failed.'); } + if ($charge->getStatus()->equals(ChargeStatus::chargedback())) { + return $this->i18n->getDashboard('Charge chargedback.'); + } + $amountInCurrency = $this->moneyService->centsToFloat($charge->getRefundedAmount()); $history = $this->i18n->getDashboard( 'Charge canceled.'