Skip to content

Commit

Permalink
feat: alternate pix and billet info
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaMelazzo committed Feb 5, 2024
1 parent 19a9b85 commit bd13118
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Block/Payment/Billet.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function getPayment()
public function getBilletUrl()
{
$billetHelper = new BilletHelper();
return $billetHelper->getBilletUrl($this->getPayment());
$pagarmeTransaction = $this->checkoutSession->getPixOrBilletTransaction();
return $billetHelper->getBilletUrl($this->getPayment(), $pagarmeTransaction);
}
}
10 changes: 7 additions & 3 deletions Block/Payment/Pix.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class Billet
* Class Pix
*
* @author Open Source Team
* @copyright 2021 Pagar.me (https://pagar.me)
Expand All @@ -11,6 +11,8 @@

namespace Pagarme\Pagarme\Block\Payment;

use Exception;
use Magento\Framework\Phrase;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Checkout\Model\Session as CheckoutSession;
Expand Down Expand Up @@ -94,7 +96,7 @@ public function getErrorCopyMessage()
}

/**
* @return string
* @return Phrase
*/
public function getSuccessMessage()
{
Expand All @@ -103,11 +105,13 @@ public function getSuccessMessage()

/**
* @return PixHelper
* @throws Exception
*/
public function getPixInfo()
{
$pagarmeTransaction = $this->checkoutSession->getPixOrBilletTransaction();
if (empty($this->pixInfo)) {
$this->pixInfo = $this->pixHelper->getInfo($this->getPayment());
$this->pixInfo = $this->pixHelper->getInfo($this->getPayment(), $pagarmeTransaction);
}

return $this->pixInfo;
Expand Down
27 changes: 15 additions & 12 deletions Gateway/Transaction/Base/Command/InitializeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Pagarme\Pagarme\Gateway\Transaction\Base\Command;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Sales\Model\Order;
use Magento\Sales\Api\Data\OrderInterface;
Expand All @@ -28,25 +29,27 @@
use Pagarme\Pagarme\Model\Ui\TwoCreditCard\ConfigProvider as TwoCreditCardConfigProvider;
use Magento\Framework\Phrase;
use Magento\Framework\Webapi\Exception as M2WebApiException;
use Pagarme\Pagarme\Helper\RecurrenceProductHelper;
use Pagarme\Pagarme\Gateway\Transaction\Base\Config\Config;
use Pagarme\Pagarme\Service\Transaction\ThreeDSService;

class InitializeCommand implements CommandInterface
{

protected $config;
/**
* @var ThreeDSService
*/
protected $threeDSService;
/**
* @var CheckoutSession
*/
protected $checkoutSession;

public function __construct(
Config $config,
ThreeDSService $threeDSService
){
$this->config = $config;
ThreeDSService $threeDSService,
CheckoutSession $checkoutSession
)
{
$this->threeDSService = $threeDSService;
$this->checkoutSession = $checkoutSession;
}

/**
Expand Down Expand Up @@ -110,10 +113,7 @@ public function execute(array $commandSubject)
/** @return AbstractPlatformOrderDecorator */
private function doCoreDetour($payment)
{
$order = $payment->getOrder();
if($this->config->getAlwaysCreateOrder()){
$order->save();
}
$order = $payment->getOrder();
$log = new OrderLogService();

Magento2CoreSetup::bootstrap();
Expand Down Expand Up @@ -168,7 +168,10 @@ private function doCoreDetour($payment)

if (!$isSubscription) {
$orderService = new OrderService();
$orderService->createOrderAtPagarme($orderDecorator);
$pagarmeOrder = current($orderService->createOrderAtPagarme($orderDecorator));
if (!is_null($pagarmeOrder->getPixOrBilletTransaction())) {
$this->checkoutSession->setPixOrBilletTransaction($pagarmeOrder->getPixOrBilletTransaction());
}
}

$orderDecorator->save();
Expand Down
18 changes: 9 additions & 9 deletions Helper/Payment/Billet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
use Pagarme\Core\Recurrence\Repositories\SubscriptionRepository;

class Billet {
public function getBilletUrl($info)

public function getBilletUrl($info, $transaction = null)
{
$method = $info->getMethod();
if (strpos($method, "pagarme_billet") === false) {
return;
}

Magento2CoreSetup::bootstrap();
$boletoUrl = $this->getBoletoLinkFromOrder($info);
$boletoUrl = $this->getBoletoLinkFromOrder($info, $transaction);

if (!$boletoUrl) {
$boletoUrl = $this->getBoletoLinkFromSubscription($info);
}

return $boletoUrl;
}

private function getBoletoLinkFromOrder($info)
private function getBoletoLinkFromOrder($info, $transaction = null)
{
$lastTransId = $info->getLastTransId();
$orderId = null;
if ($lastTransId) {
$orderId = substr($lastTransId, 0, 19);
}

if (!$orderId) {
return null;
if (!$orderId && !is_null($transaction)) {
return $transaction->getBoletoUrl();
}

$orderRepository = new OrderRepository();
Expand Down Expand Up @@ -79,4 +79,4 @@ private function getBoletoLinkFromSubscription($info)

return null;
}
}
}
10 changes: 9 additions & 1 deletion Helper/Payment/Pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pagarme\Pagarme\Helper\Payment;

use Exception;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Core\Kernel\ValueObjects\Id\OrderId;
use Pagarme\Core\Payment\Services\OrderService;
Expand All @@ -13,8 +14,9 @@ class Pix

/**
* @return $this
* @throws Exception
*/
public function getInfo($info)
public function getInfo($info, $transaction = null)
{
$orderId = null;
$method = $info->getMethod();
Expand All @@ -27,6 +29,12 @@ public function getInfo($info)
$orderId = substr($lastTransId, 0, 19);
}

if (!$orderId && !is_null($transaction)) {
$this->setQrCode($transaction->getPostData()->qr_code);
$this->setQrCodeUrl($transaction->getPostData()->qr_code_url);
return $this;
}

Magento2CoreSetup::bootstrap();
$orderService = new OrderService();
$qrCodeData = $orderService->getPixQrCodeInfoFromOrder(new OrderId($orderId));
Expand Down

0 comments on commit bd13118

Please sign in to comment.