Skip to content

Commit 29c9c86

Browse files
authored
Merge pull request #1 from mbissonho/develop
Develop
2 parents 1e68368 + b58a94e commit 29c9c86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2754
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.txt
2+
/.idea/
3+
/vendor/
4+
*.lock

Block/Adminhtml/Form.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Block\Adminhtml;
4+
5+
class Form extends \Magento\Payment\Block\Form
6+
{
7+
protected $_template = 'Mbissonho_BancoInter::form/boleto.phtml';
8+
}

Block/Info.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Block;
4+
5+
class Info extends \Magento\Payment\Block\Info
6+
{
7+
protected $_template = 'Mbissonho_BancoInter::info/boleto.phtml';
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Controller\Adminhtml\Boleto;
4+
5+
use Magento\Backend\App\Action;
6+
use Magento\Backend\App\Action\Context;
7+
use Magento\Framework\App\Action\HttpGetActionInterface;
8+
use Mbissonho\BancoInter\Controller\DownloadPdfAction;
9+
10+
class DownloadPdf extends Action implements HttpGetActionInterface
11+
{
12+
protected DownloadPdfAction $downloadPdfAction;
13+
14+
public function __construct(
15+
Context $context,
16+
DownloadPdfAction $downloadPdfAction
17+
)
18+
{
19+
$this->downloadPdfAction = $downloadPdfAction;
20+
parent::__construct($context);
21+
}
22+
23+
public function execute()
24+
{
25+
return $this->downloadPdfAction->execute();
26+
}
27+
28+
protected function _isAllowed()
29+
{
30+
return $this->_authorization->isAllowed('Magento_Sales::actions_view');
31+
}
32+
33+
}

Controller/Boleto/DownloadPdf.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Controller\Boleto;
4+
5+
use Magento\Framework\App\Action\HttpGetActionInterface;
6+
use Mbissonho\BancoInter\Controller\DownloadPdfAction;
7+
8+
class DownloadPdf implements HttpGetActionInterface
9+
{
10+
protected DownloadPdfAction $downloadPdfAction;
11+
12+
public function __construct(
13+
DownloadPdfAction $downloadPdfAction
14+
)
15+
{
16+
$this->downloadPdfAction = $downloadPdfAction;
17+
}
18+
19+
public function execute()
20+
{
21+
return $this->downloadPdfAction->execute();
22+
}
23+
}

Controller/DownloadPdfAction.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Controller;
4+
5+
use Magento\Framework\App\RequestInterface;
6+
use Magento\Framework\Controller\Result\RedirectFactory;
7+
use Magento\Sales\Model\Order\PaymentFactory as OrderPaymentFactory;
8+
use Magento\Sales\Model\ResourceModel\Order\Payment as OrderPaymentResource;
9+
use Mbissonho\BancoInter\Logger\LoggerInterface;
10+
use Mbissonho\BancoInter\Model\Boleto\PdfDownloader;
11+
12+
class DownloadPdfAction
13+
{
14+
15+
protected LoggerInterface $logger;
16+
17+
protected RequestInterface $request;
18+
19+
protected RedirectFactory $redirectFactory;
20+
21+
protected OrderPaymentFactory $orderPaymentFactory;
22+
23+
protected OrderPaymentResource $orderPaymentResource;
24+
25+
protected PdfDownloader $pdfDownloader;
26+
27+
public function __construct(
28+
LoggerInterface $logger,
29+
RequestInterface $request,
30+
RedirectFactory $redirectFactory,
31+
OrderPaymentFactory $orderPaymentFactory,
32+
OrderPaymentResource $orderPaymentResource,
33+
PdfDownloader $pdfDownloader
34+
)
35+
{
36+
$this->logger = $logger;
37+
$this->request = $request;
38+
$this->redirectFactory = $redirectFactory;
39+
$this->orderPaymentFactory = $orderPaymentFactory;
40+
$this->orderPaymentResource = $orderPaymentResource;
41+
$this->pdfDownloader = $pdfDownloader;
42+
}
43+
44+
public function execute()
45+
{
46+
try {
47+
48+
$orderPaymentId = $this->request->getParam('order_payment_id');
49+
50+
if (!$orderPaymentId) {
51+
return $this->redirectFactory->create()->setRefererUrl();
52+
}
53+
54+
$orderPayment = $this->orderPaymentFactory->create();
55+
56+
$this->orderPaymentResource->load($orderPayment, $orderPaymentId);
57+
58+
if (!$orderPayment->getEntityId()) {
59+
return $this->redirectFactory->create()->setRefererUrl();
60+
}
61+
62+
return $this->pdfDownloader->execute($orderPayment);
63+
} catch (\Throwable $e) {
64+
$this->logger->critical($e, ['order_payment_id' => $orderPaymentId]);
65+
return $this->redirectFactory->create()->setRefererUrl();
66+
}
67+
}
68+
}

Cron/UpdatePaymentStatus.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Cron;
4+
5+
use Magento\Framework\Data\Collection;
6+
use Magento\Framework\Exception\LocalizedException;
7+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
8+
use Magento\Sales\Model\Order;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
use Mbissonho\BancoInter\Logger\LoggerInterface;
11+
use Mbissonho\BancoInter\Model\Api\Client;
12+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
13+
use Mbissonho\BancoInter\Model\Ui\ConfigProvider as GeneralConfigProvider;
14+
use Magento\Framework\DB\TransactionFactory;
15+
16+
class UpdatePaymentStatus
17+
{
18+
19+
protected TimezoneInterface $timezone;
20+
protected StoreManagerInterface $storeManager;
21+
protected OrderCollectionFactory $orderCollectionFactory;
22+
protected GeneralConfigProvider $generalConfigProvider;
23+
protected Client $client;
24+
protected LoggerInterface $logger;
25+
protected TransactionFactory $transactionFactory;
26+
27+
28+
public function __construct(
29+
TransactionFactory $transactionFactory,
30+
TimezoneInterface $timezone,
31+
StoreManagerInterface $storeManager,
32+
OrderCollectionFactory $orderCollectionFactory,
33+
GeneralConfigProvider $generalConfigProvider,
34+
Client $client,
35+
LoggerInterface $logger
36+
)
37+
{
38+
$this->transactionFactory = $transactionFactory;
39+
$this->timezone = $timezone;
40+
$this->storeManager = $storeManager;
41+
$this->orderCollectionFactory = $orderCollectionFactory;
42+
$this->generalConfigProvider = $generalConfigProvider;
43+
$this->client = $client;
44+
$this->logger = $logger;
45+
}
46+
47+
public function execute()
48+
{
49+
try {
50+
51+
foreach ($this->storeManager->getStores() as $storeId => $store) {
52+
53+
if(!$this->generalConfigProvider->isModuleEnabled($storeId)) {
54+
continue;
55+
}
56+
57+
$collection = $this->orderCollectionFactory->create();
58+
$collection
59+
->addFieldToSelect('*')
60+
->addFieldToFilter('state', ['eq' => Order::STATE_PENDING_PAYMENT])
61+
->addFieldToFilter('store_id', ['eq' => $storeId ])
62+
->join(
63+
['sop' => $collection->getTable('sales_order_payment')],
64+
'main_table.entity_id = sop.parent_id and sop.method IN ("mbissonho_bancointer_boleto")',
65+
['add_info' => 'additional_information']
66+
)
67+
->addOrder('created_at', Collection::SORT_ORDER_ASC);
68+
69+
/* @var \Magento\Sales\Model\Order $olderPendingOrder */
70+
/* @var \Magento\Sales\Model\Order $newestPendingOrder */
71+
/* @var \Magento\Sales\Model\Order $order */
72+
73+
$olderPendingOrder = $collection->getFirstItem();
74+
$newestPendingOrder = $collection->getLastItem();
75+
76+
$ordersRelatedPayments = $this->client->getPaymentCollection(
77+
$this->timezone->date(new \DateTime($olderPendingOrder->getCreatedAt()))->format('Y-m-d'),
78+
$this->timezone->date(new \DateTime($newestPendingOrder->getCreatedAt()))->format('Y-m-d'),
79+
$storeId
80+
);
81+
82+
foreach ($collection as $order) {
83+
84+
$additionalInformation = $order->getPayment()->getAdditionalInformation();
85+
86+
try {
87+
if(isset($ordersRelatedPayments[$additionalInformation['our_number']])) {
88+
89+
$relatedPayment = $ordersRelatedPayments[$additionalInformation['our_number']];
90+
91+
if($relatedPayment->situacao !== 'PAGO') {
92+
continue;
93+
}
94+
95+
if(!$order->hasInvoices()) {
96+
97+
$message = sprintf("Pagamento %s aprovado", $relatedPayment->nossoNumero);
98+
99+
$status = $order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING);
100+
101+
$amountPaid = $order->getGrandTotal();
102+
$baseAmountPaid = $order->getBaseGrandTotal();
103+
104+
$invoice = $order->prepareInvoice();
105+
106+
$invoice->setRequestedCaptureCase(Order\Invoice::CAPTURE_OFFLINE);
107+
108+
$invoice->setBaseAmountPaid($baseAmountPaid);
109+
$invoice->setAmountPaid($amountPaid);
110+
$invoice->setBaseGrandTotal($baseAmountPaid);
111+
$invoice->setGrandTotal($amountPaid);
112+
$invoice->setOrder($order);
113+
114+
$invoice->register();
115+
116+
$order->setState(Order::STATE_PROCESSING)
117+
->setStatus($status)
118+
->addCommentToStatusHistory($message, true, true);
119+
120+
$dbTransaction = $this->transactionFactory->create();
121+
122+
$dbTransaction
123+
->addObject($invoice)
124+
->addObject($invoice->getOrder())
125+
->save();
126+
127+
$this->logger->info("{$order->getIncrementId()} - $relatedPayment->situacao");
128+
}
129+
130+
}
131+
} catch (\Throwable $e) {
132+
$this->logger->critical($e->getMessage());
133+
}
134+
135+
}
136+
137+
}
138+
139+
} catch (\Throwable $e) {
140+
$this->logger->critical($e->getMessage());
141+
throw new LocalizedException(__("An error occurred while running cron. Check 'mbissonho_bancointer_cron.log' file"));
142+
}
143+
}
144+
145+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Mbissonho\BancoInter\Gateway\Boleto\Http\Client;
4+
5+
use GuzzleHttp\Exception\ClientException;
6+
use Magento\Framework\Exception\LocalizedException;
7+
use Magento\Payment\Gateway\Http\ClientInterface;
8+
use Magento\Payment\Gateway\Http\TransferInterface;
9+
use Magento\Sales\Api\OrderRepositoryInterface;
10+
use Mbissonho\BancoInter\Logger\LoggerInterface;
11+
use Mbissonho\BancoInter\Model\Api\Client;
12+
use Mbissonho\BancoInter\Model\Ui\Boleto\ConfigProvider;
13+
14+
class CancelBoletoClient implements ClientInterface
15+
{
16+
17+
protected Client $client;
18+
19+
protected OrderRepositoryInterface $orderRepository;
20+
21+
protected ConfigProvider $boletoConfigProvider;
22+
23+
protected LoggerInterface $logger;
24+
25+
public function __construct(
26+
Client $client,
27+
OrderRepositoryInterface $orderRepository,
28+
ConfigProvider $boletoConfigProvider,
29+
LoggerInterface $logger
30+
)
31+
{
32+
$this->client = $client;
33+
$this->orderRepository = $orderRepository;
34+
$this->boletoConfigProvider = $boletoConfigProvider;
35+
$this->logger = $logger;
36+
}
37+
38+
public function placeRequest(TransferInterface $transferObject)
39+
{
40+
try {
41+
$requestData = $transferObject->getBody();
42+
43+
if(!$this->boletoConfigProvider->shouldCancelPaymentWhenCancelOrder($requestData['store_id'])) {
44+
return [
45+
'status_code' => 200
46+
];
47+
}
48+
49+
$order = $this->orderRepository->get($requestData['order_id']);
50+
51+
$response = $this->client->cancelPayment(
52+
$order->getPayment()->getAdditionalInformation('our_number'),
53+
$order->getStoreId()
54+
);
55+
56+
return [
57+
'status_code' => $response->getStatusCode()
58+
];
59+
60+
} catch (ClientException |\Throwable $e) {
61+
$this->logger->critical($e);
62+
63+
throw new LocalizedException(__('Something went wrong when trying to cancel the payment associated with the order'));
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)