diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..21b6bfd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,44 @@ +1.12.0 +- Checkout sem endereço (para produtos do tipo 'virtual' e 'downloadable') +- Habilitar/desabilitar recuperação de carrinho do PagSeguro via admin +- Tela de listar transações no admin, permitindo ver detalhes da transação +- Estorno parcial +- Disconto por meio de pagamento via configuração do módulo (admin) para o checkout padrão/lightbox +- Atualizada versão do pagseguro-php-sdk no composer.json para utilizar as versões 4.+ +- Adicionada compatibilidade com endereços de 4 linhas, no formato: 1 rua/endereço, 2 número, 3 complemento, 4 bairro (padrão brasileiro) +- Valida se o telefone do comprador foi configurado antes de tentar usar o telefone do endereço de entrega +- Fix: Corrigido id dos itens do pedido (carrinho) enviados para o PagSeguro + +1.4.0 +- Alterado o fluxo do checkout transparente (na própria tela de checkout do Magento) +- Alterada a forma de configurar o módulo e os meios de pagamento do PagSeguro, que agora são configurados individualmente. +- Melhorias gerais e correções de bugs: transações do admin, css muito abrangente, remoção de arquivos velhos e desnecessários, refatorações. + +1.3.0 +- Adicionada validação e mensagens de erro (frontend) nos formulários do checkout transparente + +1.2.6 +- Melhoria na configuração do log na interface administrativa +- Adicionada seção de atualização do módulo e atualização geral da documentação (README.md) +- Correção de bugs quando o pedido deixava de existir ou a sessão era encerrada +- Correçao para aceitar CVV de 4 digitos +- Melhoria no acesso aos dados do endereço do cliente + +1.2.1 +- Alterada a biblioteca JavaScript utilizada nas máscaras. + +1.2.0 +- Adicionada opção para utilizar o Checkout Transparente. + +1.1.0 +- Possibilidade de consultar e solicitar o cancelamento de transações; +- Possibilidade de consultar e solicitar o estorno de transações; +- Possibilidade de definir descontos com base no meio de pagamento escolhido durante o checkout PagSeguro; + +1.0.0 +- Adicionando opção para utilização do Checkout Lightbox. +- Integração com API de Notificação. +- Integração com API de Pagamento do PagSeguro. +- Configuração do Setup do módulo. +- Adicionado meio de pagamento ao Magento2 +- Versão inicial. \ No newline at end of file diff --git a/Controller/Adminhtml/Refund/Refund.php b/Controller/Adminhtml/Refund/Refund.php index 0d98f65..f5c0897 100755 --- a/Controller/Adminhtml/Refund/Refund.php +++ b/Controller/Adminhtml/Refund/Refund.php @@ -62,9 +62,8 @@ public function execute() $this->_objectManager->create('UOL\PagSeguro\Helper\Library'), $this->_objectManager->create('UOL\PagSeguro\Helper\Crypt') ); - try { - return $this->whenSuccess($refund->execute($this->getRequest()->getParam('data'))); + return $this->whenSuccess($refund->execute($this->getRequest()->getParam('data'), $this->getRequest()->getParam('value'))); } catch (\Exception $exception) { return $this->whenError($exception->getMessage()); } diff --git a/Helper/Data.php b/Helper/Data.php index 5d116b7..f83a2c7 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -44,7 +44,8 @@ class Data 6 => "pagseguro_devolvida", 7 => "pagseguro_cancelada", 8 => "pagseguro_chargeback_debitado", - 9 => "pagseguro_em_contestacao" + 9 => "pagseguro_em_contestacao", + 10 => "partially_refunded" ); /** diff --git a/Model/Direct/BoletoMethod.php b/Model/Direct/BoletoMethod.php index d430279..dda7e7e 100644 --- a/Model/Direct/BoletoMethod.php +++ b/Model/Direct/BoletoMethod.php @@ -218,7 +218,7 @@ private function setSenderDocument() private function setItemsInformation($product) { $this->_paymentRequest->addItems()->withParameters( - $product->getId(), //id + $product->getProduct()->getId(), //id \UOL\PagSeguro\Helper\Data::fixStringLength($product->getName(), 255), //description $product->getSimpleQtyToShip(), //quantity \UOL\PagSeguro\Helper\Data::toFloat($product->getPrice()), //amount diff --git a/Model/Direct/CreditCardMethod.php b/Model/Direct/CreditCardMethod.php index a8d0b3d..2029e41 100644 --- a/Model/Direct/CreditCardMethod.php +++ b/Model/Direct/CreditCardMethod.php @@ -303,7 +303,7 @@ private function setSenderDocument() private function setItemsInformation($product) { $this->_paymentRequest->addItems()->withParameters( - $product->getId(), //id + $product->getProduct()->getId(), //id \UOL\PagSeguro\Helper\Data::fixStringLength($product->getName(), 255), //description $product->getSimpleQtyToShip(), //quantity \UOL\PagSeguro\Helper\Data::toFloat($product->getPrice()), //amount diff --git a/Model/Direct/DebitMethod.php b/Model/Direct/DebitMethod.php index 61010a3..4501e94 100644 --- a/Model/Direct/DebitMethod.php +++ b/Model/Direct/DebitMethod.php @@ -224,7 +224,7 @@ private function setSenderDocument() private function setItemsInformation($product) { $this->_paymentRequest->addItems()->withParameters( - $product->getId(), //id + $product->getProduct()->getId(), //id \UOL\PagSeguro\Helper\Data::fixStringLength($product->getName(), 255), //description $product->getSimpleQtyToShip(), //quantity \UOL\PagSeguro\Helper\Data::toFloat($product->getPrice()), //amount diff --git a/Model/PaymentMethod.php b/Model/PaymentMethod.php index 95c4854..3a46368 100644 --- a/Model/PaymentMethod.php +++ b/Model/PaymentMethod.php @@ -123,7 +123,7 @@ private function setItemsInformation() { foreach ($this->_checkoutSession->getLastRealOrder()->getAllVisibleItems() as $product) { $this->_paymentRequest->addItems()->withParameters( - $product->getId(), //id + $product->getProduct()->getId(), //id \UOL\PagSeguro\Helper\Data::fixStringLength($product->getName(), 255), //description $product->getSimpleQtyToShip(), //quantity \UOL\PagSeguro\Helper\Data::toFloat($product->getPrice()), //amount diff --git a/Model/Transactions/Method.php b/Model/Transactions/Method.php index b48b15b..818d914 100644 --- a/Model/Transactions/Method.php +++ b/Model/Transactions/Method.php @@ -111,7 +111,9 @@ public function searchTransactions() } if (!empty($this->_status)) { - $select = $select->where('order.status = ?', $this->getStatusFromPaymentKey($this->_status)); + $select = $this->getStatusFromPaymentKey($this->_status) == 'partially_refunded' + ? $select->where('ps.partially_refunded = ?', 1) + : $select->where('order.status = ?', $this->getStatusFromPaymentKey($this->_status)); } if (!empty($this->_dateBegin) && !empty($this->_dateEnd)) { @@ -278,12 +280,15 @@ protected function formatPagSeguroStatus($payment) } /** - * @param $order + * @param string $status + * @param integer $isPartiallyRefunded * @return bool|string */ - protected function formatMagentoStatus($order) + protected function formatMagentoStatus($status, $isPartiallyRefunded = 0) { - return $this->getStatusString($this->getKeyFromOrderStatus($order)); + return $isPartiallyRefunded + ? $this->getStatusString($this->getKeyFromOrderStatus($status)) . ' (estornada parcialmente)' + : $this->getStatusString($this->getKeyFromOrderStatus($status)); } /** @@ -380,6 +385,49 @@ private function getPrefixTableName($table) return $this->_resource->getTableName($table); } + /** + * Update column 'partially_refunded' the `pagseguro_orders` table + * + * @param int $orderId + * @return void + */ + protected function updatePartiallyRefundedPagSeguro($orderId) + { + $this->getConnection() + ->query(sprintf( + "UPDATE `%s` SET partially_refunded = 1 WHERE entity_id='%s'", + $this->getPrefixTableName('pagseguro_orders'), + $orderId + )); + } + + /** + * Get all pagseguro partially refunded orders id + * + * @return array + */ + protected function getPartiallyRefundedOrders() + { + $pagseguroOrdersIdArray = array(); + + $connection = $this->getConnection(); + $select = $connection->select() + ->from( ['ps' => $this->getPrefixTableName('pagseguro_orders')], ['order_id'] ) + ->where('ps.partially_refunded = ?', '1'); + + if ($this->_scopeConfig->getValue('payment/pagseguro/environment')) { + $select = $select->where('ps.environment = ?', $this->_scopeConfig->getValue('payment/pagseguro/environment')); + } + + $connection->prepare($select); + + foreach ($connection->fetchAll($select) as $value) { + $pagseguroOrdersIdArray[] = $value['order_id']; + } + + return $pagseguroOrdersIdArray; + } + /** * @param $order * @param $payment diff --git a/Model/Transactions/Methods/Refund.php b/Model/Transactions/Methods/Refund.php index 25eaf18..baa409a 100755 --- a/Model/Transactions/Methods/Refund.php +++ b/Model/Transactions/Methods/Refund.php @@ -130,21 +130,24 @@ public function __construct( * Refund one transaction * * @param $data + * @param $value * @return bool * @throws \Exception */ - public function execute($data) { - + public function execute($data, $value = null) { try { $config = $this->sanitizeConfig($data); + if ($value != null) + $config->value = number_format(floatval($value), 2, '.', ''); $this->isConciliate($config); if (!$this->doRefund($config)) - throw new \Exception('impossible to refund'); + throw new \Exception('impossible to refund'); $this->doUpdates($config); return true; } catch (\Exception $exception) { - throw $exception; + $error = simplexml_load_string($exception->getMessage()); + throw new \Exception((string)$error->error->code); } } @@ -164,9 +167,17 @@ private function isConciliate($config) private function doUpdates($config) { try { - $this->addStatusToOrder($config->order_id, 'pagseguro_devolvida'); - $this->updateSalesOrder($config->order_id, $config->pagseguro_id); - $this->updatePagSeguroOrders($config->order_id, $config->pagseguro_id); + /* if have refund value is an partially refund, so the status should be keeped */ + if ($config->value) { + $comment = 'Estornado valor de R$' . $config->value . ' do seu pedido.'; + $this->setPartiallyRefundedStatus($config->order_id); + $this->notifyCustomer($config->order_id, $config->pagseguro_status, $comment); + } else { + $this->addStatusToOrder($config->order_id, 'pagseguro_devolvida'); + $this->updateSalesOrder($config->order_id, $config->pagseguro_id); + $this->updatePagSeguroOrders($config->order_id, $config->pagseguro_id); + } + unset($order); } catch (\Exception $exception) { throw $exception; @@ -216,7 +227,8 @@ private function requestRefund($config) try { return \PagSeguro\Services\Transactions\Refund::create( $this->_library->getPagSeguroCredentials(), - $config->pagseguro_id + $config->pagseguro_id, + $config->value ); } catch (\Exception $exception) { throw $exception; @@ -233,9 +245,15 @@ public function request() { $this->getTransactions(); if (! is_null($this->_PagSeguroPaymentList->getTransactions())) { + $partiallyRefundedOrdersArray = $this->getPartiallyRefundedOrders(); + foreach ($this->_PagSeguroPaymentList->getTransactions() as $payment) { - if (! $this->addPayment($this->decryptOrderById($payment), $payment)) - continue; + $order = $this->decryptOrderById($payment); + + if (!in_array($order->getId(), $partiallyRefundedOrdersArray)) { + if (! $this->addPayment($this->decryptOrderById($payment), $payment)) + continue; + } } } return $this->_arrayPayments; @@ -285,7 +303,8 @@ private function toArray($payment, $order, $conciliate = false) 'magento_status' => $this->formatMagentoStatus($order), 'pagseguro_id' => $payment->getCode(), 'order_id' => $order->getId(), - 'details' => $this->details($order, $payment, ['conciliate' => $conciliate]) + 'details' => $this->details($order, $payment, ['conciliate' => $conciliate]), + 'value' => $payment->getGrossAmount(), ]; } @@ -304,7 +323,8 @@ protected function details($order, $payment, $options) 'order_id' => $order->getId(), 'pagseguro_status' => $payment->getStatus(), 'pagseguro_id' => $payment->getCode(), - 'needConciliate' => $options['conciliate'] + 'needConciliate' => $options['conciliate'], + 'value' => null ]) ); } @@ -332,14 +352,14 @@ private function checkConciliation($payment, $order) */ private function compareStatus($order, $payment) { - if (! (in_array($order->getStatus(), [ + if ((in_array($order->getStatus(), [ $this->getStatusFromPaymentKey(3), $this->getStatusFromPaymentKey(4), $this->getStatusFromPaymentKey(5), - ]) || in_array($payment->getStatus(), [3, 4, 5]))) { - return false; + ]) == 1 && in_array($payment->getStatus(), [3, 4, 5]) == 1)) { + return true; } - return true; + return false; } /** @@ -367,4 +387,28 @@ private function hasOrder($order) return false; return true; } + + /** + * Updates respective order partially refunded status to 1 in pagseguro_orders table + * + * @param string $orderId + * @return void + */ + private function setPartiallyRefundedStatus($orderId) + { + $this->updatePartiallyRefundedPagSeguro($orderId); + } + + /** + * @param $orderId + * @param $orderStatus + * @param $comment + */ + public function notifyCustomer($orderId, $orderStatus, $comment = null) + { + $notify = true; + $order = $this->_order->load($orderId); + $order->addStatusToHistory($this->getStatusFromPaymentKey($orderStatus), $comment, $notify); + $order->save(); + } } diff --git a/Model/Transactions/Methods/Transactions.php b/Model/Transactions/Methods/Transactions.php index edd7f39..0d78141 100755 --- a/Model/Transactions/Methods/Transactions.php +++ b/Model/Transactions/Methods/Transactions.php @@ -163,7 +163,7 @@ public function request() 'magento_id' => $transaction['increment_id'], 'pagseguro_id' => $transaction['transaction_code'], 'environment' => $transaction['environment'], - 'magento_status' => $this->formatMagentoStatus($transaction['status']), + 'magento_status' => $this->formatMagentoStatus($transaction['status'], $transaction['partially_refunded']), 'order_id' => $transaction['entity_id'] ); } diff --git a/README.md b/README.md index a292236..0c3cda5 100644 --- a/README.md +++ b/README.md @@ -160,39 +160,7 @@ Caso tenha dúvidas ou precise de suporte, acesse nosso [fórum]. Changelog --------- -1.4.0 -- Alterado o fluxo do checkout transparente (na própria tela de checkout do Magento) -- Alterada a forma de configurar o módulo e os meios de pagamento do PagSeguro, que agora são configurados individualmente. -- Melhorias gerais e correções de bugs: transações do admin, css muito abrangente, remoção de arquivos velhos e desnecessários, refatorações. - -1.3.0 -- Adicionada validação e mensagens de erro (frontend) nos formulários do checkout transparente - -1.2.6 -- Melhoria na configuração do log na interface administrativa -- Adicionada seção de atualização do módulo e atualização geral da documentação (README.md) -- Correção de bugs quando o pedido deixava de existir ou a sessão era encerrada -- Correçao para aceitar CVV de 4 digitos -- Melhoria no acesso aos dados do endereço do cliente - -1.2.1 -- Alterada a biblioteca JavaScript utilizada nas máscaras. - -1.2.0 -- Adicionada opção para utilizar o Checkout Transparente. - -1.1.0 -- Possibilidade de consultar e solicitar o cancelamento de transações; -- Possibilidade de consultar e solicitar o estorno de transações; -- Possibilidade de definir descontos com base no meio de pagamento escolhido durante o checkout PagSeguro; - -1.0.0 -- Adicionando opção para utilização do Checkout Lightbox. -- Integração com API de Notificação. -- Integração com API de Pagamento do PagSeguro. -- Configuração do Setup do módulo. -- Adicionado meio de pagamento ao Magento2 -- Versão inicial. +Para consultar o log de alterações acesse o arquivo [CHANGELOG.md](CHANGELOG.md). Licença ------- diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 0157b17..d8d0245 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -49,6 +49,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $this->integratePagSeguroAndOrdersGrid($setup); $this->cleanUiBookmark($setup); } + + if (version_compare($context->getVersion(), '2.0.2') < 0) { + $this->addPartiallyRefundedColumnPagseguroOrders($setup); + } $setup->endSetup(); @@ -174,4 +178,25 @@ private function cleanUiBookmark($setup) $setup->getConnection() ->delete($setup->getTable('ui_bookmark'), "namespace='sales_order_grid'"); } + + private function addPartiallyRefundedColumnPagSeguroOrders($setup) + { + // Get pagseguro orders table + $tableName = $setup->getTable(self::PAGSEGURO_ORDERS); + + // Check if the table already exists + if ($setup->getConnection()->isTableExists($tableName) == true && $setup->getConnection()->tableColumnExists($tableName, 'partially_refunded') === false) { + $setup->getConnection() + ->addColumn( + $tableName, + 'partially_refunded', + array( + 'type' => Table::TYPE_BOOLEAN, + 'nullable' => false, + 'default' => 0, + 'comment' => 'Show if order is already partially refunded', + ) + ); + } + } } diff --git a/etc/module.xml b/etc/module.xml index 0c3f983..cfa68d6 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -23,7 +23,7 @@ */ --> 
 - + diff --git a/view/adminhtml/templates/refund/content.phtml b/view/adminhtml/templates/refund/content.phtml index 3dd1aa6..fa8081e 100755 --- a/view/adminhtml/templates/refund/content.phtml +++ b/view/adminhtml/templates/refund/content.phtml @@ -17,10 +17,37 @@ + + + \ No newline at end of file diff --git a/view/adminhtml/templates/refund/header.phtml b/view/adminhtml/templates/refund/header.phtml index 4ad1a1d..1047302 100755 --- a/view/adminhtml/templates/refund/header.phtml +++ b/view/adminhtml/templates/refund/header.phtml @@ -1,3 +1,8 @@

Com esta funcionalidade você poderá estornar os valores de transações que estejam nos status “Paga”, “Disponível” e “Em disputa”. É aconselhável que antes de usar esta funcionalidade você faça a conciliação de suas transações para obter os status mais atuais.

+

+ Atenção: Ao realizar um Estorno total a transação será estornada e terá seu status alterado no PagSeguro para + "Devolvida". Ao realizar um Estorno parcial será estornado o valor inserido, contudo, o status da transação no PagSeguro não será + alterado (a transação manterá o seu último status). +

\ No newline at end of file diff --git a/view/adminhtml/templates/transactions/content.phtml b/view/adminhtml/templates/transactions/content.phtml old mode 100755 new mode 100644 index 2c0dfc9..c67b278 --- a/view/adminhtml/templates/transactions/content.phtml +++ b/view/adminhtml/templates/transactions/content.phtml @@ -34,7 +34,7 @@ - + @@ -46,7 +46,7 @@ -
+