Skip to content

Commit

Permalink
Merge pull request #1 from mbissonho/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mbissonho committed Mar 31, 2023
2 parents 1e68368 + b58a94e commit 29c9c86
Show file tree
Hide file tree
Showing 62 changed files with 2,754 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.txt
/.idea/
/vendor/
*.lock
8 changes: 8 additions & 0 deletions Block/Adminhtml/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Mbissonho\BancoInter\Block\Adminhtml;

class Form extends \Magento\Payment\Block\Form
{
protected $_template = 'Mbissonho_BancoInter::form/boleto.phtml';
}
8 changes: 8 additions & 0 deletions Block/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Mbissonho\BancoInter\Block;

class Info extends \Magento\Payment\Block\Info
{
protected $_template = 'Mbissonho_BancoInter::info/boleto.phtml';
}
33 changes: 33 additions & 0 deletions Controller/Adminhtml/Boleto/DownloadPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mbissonho\BancoInter\Controller\Adminhtml\Boleto;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Mbissonho\BancoInter\Controller\DownloadPdfAction;

class DownloadPdf extends Action implements HttpGetActionInterface
{
protected DownloadPdfAction $downloadPdfAction;

public function __construct(
Context $context,
DownloadPdfAction $downloadPdfAction
)
{
$this->downloadPdfAction = $downloadPdfAction;
parent::__construct($context);
}

public function execute()
{
return $this->downloadPdfAction->execute();
}

protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_Sales::actions_view');
}

}
23 changes: 23 additions & 0 deletions Controller/Boleto/DownloadPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Mbissonho\BancoInter\Controller\Boleto;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Mbissonho\BancoInter\Controller\DownloadPdfAction;

class DownloadPdf implements HttpGetActionInterface
{
protected DownloadPdfAction $downloadPdfAction;

public function __construct(
DownloadPdfAction $downloadPdfAction
)
{
$this->downloadPdfAction = $downloadPdfAction;
}

public function execute()
{
return $this->downloadPdfAction->execute();
}
}
68 changes: 68 additions & 0 deletions Controller/DownloadPdfAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Mbissonho\BancoInter\Controller;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Sales\Model\Order\PaymentFactory as OrderPaymentFactory;
use Magento\Sales\Model\ResourceModel\Order\Payment as OrderPaymentResource;
use Mbissonho\BancoInter\Logger\LoggerInterface;
use Mbissonho\BancoInter\Model\Boleto\PdfDownloader;

class DownloadPdfAction
{

protected LoggerInterface $logger;

protected RequestInterface $request;

protected RedirectFactory $redirectFactory;

protected OrderPaymentFactory $orderPaymentFactory;

protected OrderPaymentResource $orderPaymentResource;

protected PdfDownloader $pdfDownloader;

public function __construct(
LoggerInterface $logger,
RequestInterface $request,
RedirectFactory $redirectFactory,
OrderPaymentFactory $orderPaymentFactory,
OrderPaymentResource $orderPaymentResource,
PdfDownloader $pdfDownloader
)
{
$this->logger = $logger;
$this->request = $request;
$this->redirectFactory = $redirectFactory;
$this->orderPaymentFactory = $orderPaymentFactory;
$this->orderPaymentResource = $orderPaymentResource;
$this->pdfDownloader = $pdfDownloader;
}

public function execute()
{
try {

$orderPaymentId = $this->request->getParam('order_payment_id');

if (!$orderPaymentId) {
return $this->redirectFactory->create()->setRefererUrl();
}

$orderPayment = $this->orderPaymentFactory->create();

$this->orderPaymentResource->load($orderPayment, $orderPaymentId);

if (!$orderPayment->getEntityId()) {
return $this->redirectFactory->create()->setRefererUrl();
}

return $this->pdfDownloader->execute($orderPayment);
} catch (\Throwable $e) {
$this->logger->critical($e, ['order_payment_id' => $orderPaymentId]);
return $this->redirectFactory->create()->setRefererUrl();
}
}
}
145 changes: 145 additions & 0 deletions Cron/UpdatePaymentStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Mbissonho\BancoInter\Cron;

use Magento\Framework\Data\Collection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;
use Mbissonho\BancoInter\Logger\LoggerInterface;
use Mbissonho\BancoInter\Model\Api\Client;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Mbissonho\BancoInter\Model\Ui\ConfigProvider as GeneralConfigProvider;
use Magento\Framework\DB\TransactionFactory;

class UpdatePaymentStatus
{

protected TimezoneInterface $timezone;
protected StoreManagerInterface $storeManager;
protected OrderCollectionFactory $orderCollectionFactory;
protected GeneralConfigProvider $generalConfigProvider;
protected Client $client;
protected LoggerInterface $logger;
protected TransactionFactory $transactionFactory;


public function __construct(
TransactionFactory $transactionFactory,
TimezoneInterface $timezone,
StoreManagerInterface $storeManager,
OrderCollectionFactory $orderCollectionFactory,
GeneralConfigProvider $generalConfigProvider,
Client $client,
LoggerInterface $logger
)
{
$this->transactionFactory = $transactionFactory;
$this->timezone = $timezone;
$this->storeManager = $storeManager;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->generalConfigProvider = $generalConfigProvider;
$this->client = $client;
$this->logger = $logger;
}

public function execute()
{
try {

foreach ($this->storeManager->getStores() as $storeId => $store) {

if(!$this->generalConfigProvider->isModuleEnabled($storeId)) {
continue;
}

$collection = $this->orderCollectionFactory->create();
$collection
->addFieldToSelect('*')
->addFieldToFilter('state', ['eq' => Order::STATE_PENDING_PAYMENT])
->addFieldToFilter('store_id', ['eq' => $storeId ])
->join(
['sop' => $collection->getTable('sales_order_payment')],
'main_table.entity_id = sop.parent_id and sop.method IN ("mbissonho_bancointer_boleto")',
['add_info' => 'additional_information']
)
->addOrder('created_at', Collection::SORT_ORDER_ASC);

/* @var \Magento\Sales\Model\Order $olderPendingOrder */
/* @var \Magento\Sales\Model\Order $newestPendingOrder */
/* @var \Magento\Sales\Model\Order $order */

$olderPendingOrder = $collection->getFirstItem();
$newestPendingOrder = $collection->getLastItem();

$ordersRelatedPayments = $this->client->getPaymentCollection(
$this->timezone->date(new \DateTime($olderPendingOrder->getCreatedAt()))->format('Y-m-d'),
$this->timezone->date(new \DateTime($newestPendingOrder->getCreatedAt()))->format('Y-m-d'),
$storeId
);

foreach ($collection as $order) {

$additionalInformation = $order->getPayment()->getAdditionalInformation();

try {
if(isset($ordersRelatedPayments[$additionalInformation['our_number']])) {

$relatedPayment = $ordersRelatedPayments[$additionalInformation['our_number']];

if($relatedPayment->situacao !== 'PAGO') {
continue;
}

if(!$order->hasInvoices()) {

$message = sprintf("Pagamento %s aprovado", $relatedPayment->nossoNumero);

$status = $order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING);

$amountPaid = $order->getGrandTotal();
$baseAmountPaid = $order->getBaseGrandTotal();

$invoice = $order->prepareInvoice();

$invoice->setRequestedCaptureCase(Order\Invoice::CAPTURE_OFFLINE);

$invoice->setBaseAmountPaid($baseAmountPaid);
$invoice->setAmountPaid($amountPaid);
$invoice->setBaseGrandTotal($baseAmountPaid);
$invoice->setGrandTotal($amountPaid);
$invoice->setOrder($order);

$invoice->register();

$order->setState(Order::STATE_PROCESSING)
->setStatus($status)
->addCommentToStatusHistory($message, true, true);

$dbTransaction = $this->transactionFactory->create();

$dbTransaction
->addObject($invoice)
->addObject($invoice->getOrder())
->save();

$this->logger->info("{$order->getIncrementId()} - $relatedPayment->situacao");
}

}
} catch (\Throwable $e) {
$this->logger->critical($e->getMessage());
}

}

}

} catch (\Throwable $e) {
$this->logger->critical($e->getMessage());
throw new LocalizedException(__("An error occurred while running cron. Check 'mbissonho_bancointer_cron.log' file"));
}
}

}
66 changes: 66 additions & 0 deletions Gateway/Boleto/Http/Client/CancelBoletoClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Mbissonho\BancoInter\Gateway\Boleto\Http\Client;

use GuzzleHttp\Exception\ClientException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Http\ClientInterface;
use Magento\Payment\Gateway\Http\TransferInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Mbissonho\BancoInter\Logger\LoggerInterface;
use Mbissonho\BancoInter\Model\Api\Client;
use Mbissonho\BancoInter\Model\Ui\Boleto\ConfigProvider;

class CancelBoletoClient implements ClientInterface
{

protected Client $client;

protected OrderRepositoryInterface $orderRepository;

protected ConfigProvider $boletoConfigProvider;

protected LoggerInterface $logger;

public function __construct(
Client $client,
OrderRepositoryInterface $orderRepository,
ConfigProvider $boletoConfigProvider,
LoggerInterface $logger
)
{
$this->client = $client;
$this->orderRepository = $orderRepository;
$this->boletoConfigProvider = $boletoConfigProvider;
$this->logger = $logger;
}

public function placeRequest(TransferInterface $transferObject)
{
try {
$requestData = $transferObject->getBody();

if(!$this->boletoConfigProvider->shouldCancelPaymentWhenCancelOrder($requestData['store_id'])) {
return [
'status_code' => 200
];
}

$order = $this->orderRepository->get($requestData['order_id']);

$response = $this->client->cancelPayment(
$order->getPayment()->getAdditionalInformation('our_number'),
$order->getStoreId()
);

return [
'status_code' => $response->getStatusCode()
];

} catch (ClientException |\Throwable $e) {
$this->logger->critical($e);

throw new LocalizedException(__('Something went wrong when trying to cancel the payment associated with the order'));
}
}
}
Loading

0 comments on commit 29c9c86

Please sign in to comment.