Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Fixed bugs in direct payment when there is no order associated with c…
Browse files Browse the repository at this point in the history
…urrent session in payment screen

& Log path in configurations only show when log is enabled
& Fixed amex CVV mask (4 digits)
& Added validation in all direct payment form inputs
& General fixes and improvements
  • Loading branch information
gabriellucius committed Feb 22, 2017
1 parent 48db063 commit ed34e06
Show file tree
Hide file tree
Showing 9 changed files with 543 additions and 85 deletions.
23 changes: 17 additions & 6 deletions Controller/Direct/Boleto.php
Expand Up @@ -41,6 +41,9 @@ class Boleto extends \Magento\Framework\App\Action\Action
/** @var \Magento\Framework\Controller\Result\Json */
protected $result;

/** @var Magento\Sales\Model\Order */
protected $order;

/**
* Boleto payment constructor.
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -62,12 +65,13 @@ public function __construct(
public function execute()
{
try {
$this->order = $this->loadOrder();
/** @var \UOL\PagSeguro\Model\Direct\BoletoMethod $boleto */
$boleto = new BoletoMethod(
$this->_objectManager->create('Magento\Directory\Api\CountryInformationAcquirerInterface'),
$this->_objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface'),
$this->_objectManager->create('Magento\Framework\Module\ModuleList'),
$this->loadOrder(),
$this->order,
$this->_objectManager->create('UOL\PagSeguro\Helper\Library'),
$data = [
'sender_document' => $this->helperData()->formatDocument($this->getRequest()->getParam('sender_document')),
Expand All @@ -77,7 +81,9 @@ public function execute()
);
return $this->placeOrder($boleto);
} catch (\Exception $exception) {
$this->changeOrderHistory('pagseguro_cancelada');
if (!is_null($this->order)) {
$this->changeOrderHistory('pagseguro_cancelada');
}
$this->clearSession();
return $this->whenError($exception->getMessage());
}
Expand Down Expand Up @@ -189,7 +195,13 @@ private function baseUrl()
*/
private function lastRealOrderId()
{
return $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();
$lastRealOrderId = $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();

if (is_null($lastRealOrderId)) {
throw new \Exception("There is no order associated with this session.");
}

return $lastRealOrderId;
}

/**
Expand All @@ -209,10 +221,9 @@ private function session()
*/
private function changeOrderHistory($status)
{
$order = $this->loadOrder();
/** change payment status in magento */
$order->addStatusToHistory($status, null, true);
$this->order->addStatusToHistory($status, null, true);
/** save order */
$order->save();
$this->order->save();
}
}
26 changes: 19 additions & 7 deletions Controller/Direct/Debit.php
Expand Up @@ -41,6 +41,9 @@ class Debit extends \Magento\Framework\App\Action\Action
/** @var \Magento\Framework\Controller\Result\Json */
protected $result;

/** @var Magento\Sales\Model\Order */
protected $order;

/**
* Checkout constructor.
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -63,11 +66,12 @@ public function __construct(
public function execute()
{
try {
$this->order = $this->loadOrder();
$debit = new DebitMethod(
$this->_objectManager->create('Magento\Directory\Api\CountryInformationAcquirerInterface'),
$this->_objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface'),
$this->_objectManager->create('Magento\Framework\Module\ModuleList'),
$this->loadOrder(),
$this->order,
$this->_objectManager->create('UOL\PagSeguro\Helper\Library'),
$data = [
'sender_document' => $this->helperData()->formatDocument($this->getRequest()->getParam('sender_document')),
Expand All @@ -78,7 +82,9 @@ public function execute()
);
return $this->placeOrder($debit);
} catch (\Exception $exception) {
$this->changeOrderHistory('pagseguro_cancelada');
if (!is_null($this->order)) {
$this->changeOrderHistory('pagseguro_cancelada');
}
$this->clearSession();
return $this->whenError($exception->getMessage());
}
Expand Down Expand Up @@ -226,8 +232,14 @@ private function baseUrl()
* @return string id
*/
private function lastRealOrderId()
{
return $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();
{
$lastRealOrderId = $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();

if (is_null($lastRealOrderId)) {
throw new \Exception("There is no order associated with this session.");
}

return $lastRealOrderId;
}

/**
Expand All @@ -247,10 +259,10 @@ private function session()
*/
private function changeOrderHistory($status)
{
$order = $this->loadOrder();
/** change payment status in magento */
$order->addStatusToHistory($status, null, true);
$this->order->addStatusToHistory($status, null, true);

/** save order */
$order->save();
$this->order->save();
}
}
30 changes: 23 additions & 7 deletions Controller/Direct/Installments.php
Expand Up @@ -43,6 +43,9 @@ class Installments extends \Magento\Framework\App\Action\Action
/** @var \Magento\Framework\Controller\Result\Json */
protected $result;

/** @var Magento\Sales\Model\Order */
protected $order;

/**
* installments constructor
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -56,6 +59,7 @@ public function __construct(
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->result = $this->resultJsonFactory->create();
$this->order = null;
}

/**
Expand All @@ -65,10 +69,11 @@ public function __construct(
public function execute()
{
try {
$this->order = $this->loadOrder();
$installments = new InstallmentsMethod(
$this->_objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface'),
$this->_objectManager->create('Magento\Framework\Module\ModuleList'),
$this->loadOrder(),
$this->order,
$this->_objectManager->create('UOL\PagSeguro\Helper\Library'),
$data = [
'brand' => $this->getRequest()->getParam('credit_card_brand'),
Expand All @@ -77,9 +82,13 @@ public function execute()
);
return $this->place($installments);

} catch (\Exception $exception) {
$this->changeOrderHistory('pagseguro_cancelada');
}
catch (\Exception $exception) {
if (!is_null($this->order)) {
$this->changeOrderHistory('pagseguro_cancelada');
}
$this->clearSession();

return $this->whenError($exception->getMessage());
}
}
Expand Down Expand Up @@ -170,10 +179,17 @@ private function baseUrl()
* Get last real order id
*
* @return string id
* @throws \Exception
*/
private function lastRealOrderId()
{
return $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();
$lastRealOrderId = $this->_objectManager->create('\Magento\Checkout\Model\Session')->getLastRealOrder()->getId();

if (is_null($lastRealOrderId)) {
throw new \Exception("There is no order associated with this session.");
}

return $lastRealOrderId;
}

/**
Expand All @@ -193,10 +209,10 @@ private function session()
*/
private function changeOrderHistory($status)
{
$order = $this->loadOrder();
/** change payment status in magento */
$order->addStatusToHistory($status, null, true);
$this->order->addStatusToHistory($status, null, true);

/** save order */
$order->save();
$this->order->save();
}
}
2 changes: 1 addition & 1 deletion Model/Direct/InstallmentsMethod.php
Expand Up @@ -89,11 +89,11 @@ public function create($amount = false, $maxInstallment = false)
try {
$this->config();
$this->setOptions($this->getTotalAmount($amount), $this->getBrand());

$installments = \PagSeguro\Services\Installment::create(
$this->_library->getPagSeguroCredentials(),
$this->getOptions()
);

return $this->output($installments->getInstallments(), $maxInstallment);
} catch (PagSeguroServiceException $exception) {
throw $exception;
Expand Down
2 changes: 1 addition & 1 deletion Observer/CreatePagSeguroOrder.php
Expand Up @@ -93,7 +93,7 @@ public function __construct(
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
{
$order = $observer->getEvent()->getOrder();

//verify pagseguro transaction
Expand Down
7 changes: 6 additions & 1 deletion etc/adminhtml/system.xml
Expand Up @@ -69,7 +69,12 @@
</field>
<field id="log_file" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<label>NOME DO ARQUIVO DE LOG</label>
<comment>Nome do arquivo de log a ser utilizado, com caminho relativo ou absoluto e que tenham permissões de leitura e escrita (Ex.: var/log/pagseguro.log).</comment>
<depends>
<field id="*/*/log">1</field>
</depends>
<comment>
Nome do arquivo de log a ser utilizado, com caminho relativo ou absoluto, que tenha permissões de leitura e escrita (Ex.: var/log/pagseguro.log).
</comment>
<tooltip>
Para que ocorra normalmente a geração de logs é estritamente necessário que o diretório e o arquivo de log configurados tenham permissões de leitura e escrita.
</tooltip>
Expand Down

0 comments on commit ed34e06

Please sign in to comment.