Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endel/359483 to your computer and use it in GitHub Desktop.
Save endel/359483 to your computer and use it in GitHub Desktop.
Retorno do PagSeguro customizado. Veja o módulo completo em: http://visie.com.br/pagseguro/plataforma/magento/
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category Mage
* @package PagSeguro
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* PagSeguro Standard Checkout Controller
*
* @author Michael Granados <mike@visie.com.br>
*/
class PagSeguro_StandardController
extends Mage_Core_Controller_Front_Action
{
/**
* Order instance
*/
protected $_order;
/**
* Get order
*
* @param none
* @return Mage_Sales_Model_Order
*/
public function getOrder()
{
if ($this->_order == null) {
}
return $this->_order;
}
/**
* Get singleton with pagseguro strandard order transaction information
*
* @return PagSeguro_Model_Standard
*/
public function getStandard()
{
return Mage::getSingleton('pagseguro/standard');
}
/**
* When a customer chooses Paypal on Checkout/Payment page
*
*/
public function redirectAction()
{
$session = Mage::getSingleton('checkout/session');
$session->setPaypalStandardQuoteId($session->getQuoteId());
$this->getResponse()->setBody($this->getLayout()->createBlock('pagseguro/standard_redirect')->toHtml());
$session->unsQuoteId();
}
/**
* Retorno dos dados feito pelo PagSeguro
*/
public function obrigadoAction()
{
$standard = $this->getStandard();
# É um $_GET, trate normalmente
if (!$this->getRequest()->isPost()) {
$session = Mage::getSingleton('checkout/session');
$session->setQuoteId($session->getPaypalStandardQuoteId(true));
/**
* set the quote as inactive after back from pagseguro
*/
Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save();
/**
* send confirmation email to customer
*/
$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('checkout/session')->getLastOrderId());
if($order->getId()){
$order->sendNewOrderEmail();
}
$url = $standard->getConfigData('retorno');
$this->_redirect($url);
} else {
// Vamos ao retorno automático
/*if (!defined('RETORNOPAGSEGURO_NOT_AUTORUN')) {
define('RETORNOPAGSEGURO_NOT_AUTORUN', true);
define('PAGSEGURO_AMBIENTE_DE_TESTE', true);
}*/
// Incluindo a biblioteca escrita pela Visie
include_once(dirname(__FILE__).'/retorno.php');
// Brincanco com a biblioteca
RetornoPagSeguro::verifica($_POST, false, array($this, 'retornoPagSeguro'));
}
}
public function enviarEmailConfirmacaoPagamento($order)
{
/*if (!Mage::helper('sales')->canSendNewOrderEmail($order->getStore()->getId())) {
return $this;
}*/
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($order->getStore()->getId());
$mailTemplate = Mage::getModel('core/email_template');
if ($order->getCustomerIsGuest()) {
$template = Mage::getStoreConfig('sales_email/order/guest_template', $order->getStoreId());
$customerName = $order->getBillingAddress()->getName();
} else {
$template = Mage::getStoreConfig('sales_email/invoice/template', $order->getStoreId());
$customerName = $order->getCustomerName();
}
$sendTo = array(
array(
'email' => $order->getCustomerEmail(),
'name' => $customerName
)
);
foreach ($sendTo as $recipient) {
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$order->getStoreId()))
->sendTransactional(
$template,
Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()),
$recipient['email'],
$recipient['name'],
array(
'order' => $order,
'billing' => $order->getBillingAddress(),
'payment_html' => $paymentBlock->toHtml(),
)
);
}
$translate->setTranslateInline(true);
return $this;
}
public function retornoPagSeguro($referencia, $status, $valorFinal, $produtos, $post)
{
$order = Mage::getModel('sales/order');
$order->loadByIncrementId($referencia);
if (in_array(strtolower($status), array('completo', 'aprovado'))) {
if (strtolower($status) == 'completo')
{
$order->setState( Mage_Sales_Model_Order::STATE_COMPLETE, true);
} else {
foreach ($order->getAllItems() as $item) {
$stock_item = Mage::getModel('cataloginventory/stock_item')->loadByProduct($item->getProductId());
$stock_item->setQty( ($stock_item->getQty() >= $item->getQtyOrdered()) ? $stock_item->getQty() - $item->getQtyOrdered() : 0 );
$stock_item->save();
}
$order->setTotalPaid($order->getGrandTotal());
$order->setState( Mage_Sales_Model_Order::STATE_PROCESSING, true);
$this->enviarEmailConfirmacaoPagamento($order);
}
} else {
// Não está completa, vamos processar...
$comment = $status;
if ( strtolower(trim($status))=='cancelado' ) {
$changeTo = Mage_Sales_Model_Order::STATE_CANCELED;
} else {
// Esquecer o Cancelado e o Aprovado/Concluído
$changeTo = Mage_Sales_Model_Order::STATE_HOLDED;
$comment .= ' - ' . $post->TipoPagamento;
}
$order->addStatusToHistory(
$changeTo,
$comment,
$notified = false
);
}
$order->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment