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

Commit

Permalink
Merge pull request #54 from pagseguro/desenvolvimento
Browse files Browse the repository at this point in the history
Desenvolvimento
  • Loading branch information
s2it-moscou authored Apr 11, 2017
2 parents b261072 + 077416a commit 231b980
Show file tree
Hide file tree
Showing 17 changed files with 654 additions and 141 deletions.
23 changes: 17 additions & 6 deletions Controller/Direct/Boleto.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
}
23 changes: 14 additions & 9 deletions Model/Direct/BoletoMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ private function setShippingInformation()
{
$shipping = $this->getShippingData();
$address = \UOL\PagSeguro\Helper\Data::addressConfig($shipping['street']);

$this->_paymentRequest->setShipping()->setAddress()->withParameters(
$this->getShippingAddress($address[0], $shipping),
$this->getShippingAddress($address[1]),
$this->getShippingAddress($address[0]),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping['postcode']),
$shipping['city'],
$this->getRegionAbbreviation($shipping['region']),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping->getPostcode()),
$shipping->getCity(),
$this->getRegionAbbreviation($shipping),
$this->getCountryName($shipping['country_id']),
$this->getShippingAddress($address[2])
);
Expand Down Expand Up @@ -360,15 +360,20 @@ private function getOrderStoreReference()
/**
* Get a brazilian region name and return the abbreviation if it exists
*
* @param string $regionName
* @param shipping $shipping
* @return string
*/
private function getRegionAbbreviation($regionName)
private function getRegionAbbreviation($shipping)
{
if (strlen($shipping->getRegionCode()) == 2) {
return $shipping->getRegionCode();
}

$regionAbbreviation = new \PagSeguro\Enum\Address();
return (is_string($regionAbbreviation->getType($regionName))) ?
$regionAbbreviation->getType($regionName) :
$regionName;

return (is_string($regionAbbreviation->getType($shipping->getRegion()))) ?
$regionAbbreviation->getType($shipping->getRegion()) :
$shipping->getRegion();
}

/**
Expand Down
27 changes: 16 additions & 11 deletions Model/Direct/CreditCardMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ private function setBillingInformation()
$this->getShippingAddress($address[0], $billing),
$this->getShippingAddress($address[1]),
$this->getShippingAddress($address[0]),
\UOL\PagSeguro\Helper\Data::fixPostalCode($billing['postcode']),
$billing['city'],
$this->getRegionAbbreviation($billing['region']),
\UOL\PagSeguro\Helper\Data::fixPostalCode($billing->getPostcode()),
$billing->getCity(),
$this->getRegionAbbreviation($billing),
$this->getCountryName($billing['country_id']),
$this->getShippingAddress($address[2])
);
Expand Down Expand Up @@ -374,9 +374,9 @@ private function setShippingInformation()
$this->getShippingAddress($address[0], $shipping),
$this->getShippingAddress($address[1]),
$this->getShippingAddress($address[0]),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping['postcode']),
$shipping['city'],
$this->getRegionAbbreviation($shipping['region']),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping->getPostcode()),
$shipping->getCity(),
$this->getRegionAbbreviation($shipping),
$this->getCountryName($shipping['country_id']),
$this->getShippingAddress($address[2])
);
Expand Down Expand Up @@ -436,15 +436,20 @@ private function getOrderStoreReference()
/**
* Get a brazilian region name and return the abbreviation if it exists
*
* @param string $regionName
* @param shipping $shipping
* @return string
*/
private function getRegionAbbreviation($regionName)
private function getRegionAbbreviation($shipping)
{
if (strlen($shipping->getRegionCode()) == 2) {
return $shipping->getRegionCode();
}

$regionAbbreviation = new \PagSeguro\Enum\Address();
return (is_string($regionAbbreviation->getType($regionName))) ?
$regionAbbreviation->getType($regionName) :
$regionName;

return (is_string($regionAbbreviation->getType($shipping->getRegion()))) ?
$regionAbbreviation->getType($shipping->getRegion()) :
$shipping->getRegion();
}

/**
Expand Down
21 changes: 13 additions & 8 deletions Model/Direct/DebitMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ private function setShippingInformation()
$this->getShippingAddress($address[0], $shipping),
$this->getShippingAddress($address[1]),
$this->getShippingAddress($address[0]),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping['postcode']),
$shipping['city'],
$this->getRegionAbbreviation($shipping['region']),
\UOL\PagSeguro\Helper\Data::fixPostalCode($shipping->getPostcode()),
$shipping->getCity(),
$this->getRegionAbbreviation($shipping),
$this->getCountryName($shipping['country_id']),
$this->getShippingAddress($address[2])
);
Expand Down Expand Up @@ -366,15 +366,20 @@ private function getOrderStoreReference()
/**
* Get a brazilian region name and return the abbreviation if it exists
*
* @param string $regionName
* @param shipping $shipping
* @return string
*/
private function getRegionAbbreviation($regionName)
private function getRegionAbbreviation($shipping)
{
if (strlen($shipping->getRegionCode()) == 2) {
return $shipping->getRegionCode();
}

$regionAbbreviation = new \PagSeguro\Enum\Address();
return (is_string($regionAbbreviation->getType($regionName))) ?
$regionAbbreviation->getType($regionName) :
$regionName;

return (is_string($regionAbbreviation->getType($shipping->getRegion()))) ?
$regionAbbreviation->getType($shipping->getRegion()) :
$shipping->getRegion();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Model/Direct/InstallmentsMethod.php
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 231b980

Please sign in to comment.