需要帮助返回 Magento 并在第 3 方支付网关后更新订单状态

Posted

技术标签:

【中文标题】需要帮助返回 Magento 并在第 3 方支付网关后更新订单状态【英文标题】:Need help returning to Magento & updating order status after 3rd party payment gateway 【发布时间】:2014-09-24 23:27:28 【问题描述】:

我有一个第三方支付网关已启动并正在运行,但我真的可以使用您的帮助将信息发送回 magento 并在支付完成后重定向到成功页面。

Magento 在结帐过程中通过“下订单”(通过getOrderPlacedRedirectUrl)重定向到我们的支付网关,并将订单提交到商店仪表板。我们的支付网关有一个可自定义的重定向 url,但是当我尝试路由回 magento 的成功页面时,我收到了 403 禁止错误。

我想将用户发送回成功页面并更新订单状态/根据我的支付网关的响应参数从 magento 发送确认电子邮件。

我有一个 PaymentConroller 和一个 redirectActionresponseActioncancelAction 方法(虽然我不认为 responseAction 被调用过)。

额外信息:我也尝试过以与 PayPal Express 相同的方式将其选为付款方式(在“查看订单”步骤之前通过 getCheckoutRedirecturl)定向到我的付款网关,但我又遇到了之后返回 magento 并出现 403 错误的问题。这将是我理想的设置,并在付款后下订单。这甚至可能吗?

基本上我的问题围绕着在我的付款完成后回到 magento 和当前订单。

提前感谢您的帮助!

我的代码如下。

PaymentController (controllers/Paymentcontroller.php):

<?php

/**
* @method redirectAction()
* @method responseAction()
* @method cancelAction()
*/
class Knox_KnoxGateway_PaymentController extends Mage_Core_Controller_Front_Action 
 public function redirectAction() 
$this->loadLayout();
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','KnoxGateway',array('template' => 'KnoxGateway/redirect.phtml'));
$this->getLayout()->getBlock('content')->append($block);
    $this->renderLayout();
 

 /** 
 * @var $validated is initialized to true
 * @var $orderId is set to 'default', might make it a number
*/
public function responseAction() 
if($this->getRequest()->isPost()) 
  $validated = true;
  $orderId = 'default';

  if($validated) 
    $order = Mage::getModel('sales/order');
    $order->loadByIncrementId($orderId);
    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Knox has authorized the payment.');

    $order->sendNewOrderEmail();
    $order->setEmailSent(true);

    $order->save();

    Mage::getSingleton('checkout/session')->unsQuoteId();

    Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true));
    

  else 
    $this->cancelAction();
    Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure'=>true));
  

else
  Mage_Core_Controller_Varien_Action::_redirect('');


public function cancelAction() 
    if (Mage::getSingleton('checkout/session')->getLastRealOrderId()) 
        $order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
        if($order->getId()) 
    $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Knox has declined the payment.')->save();
  
    


标准 (Models/Standard.php):

<?php

class Knox_KnoxGateway_Model_Standard extends Mage_Payment_Model_Method_Abstract 
/**
 * @var $_code defines the name of our plugin when we register to magento
 */
protected $_code = 'KnoxGateway';

/**
 * @var $_isInitializeNeeded is set to true to declare we need
 * to initialize while the order is in place
 */
protected $_isInitializeNeeded      = true;
/**
 * @var $_canUseInternal is set to true to declare that people can pay
 * with knox from the admin pages
 */
protected $_canUseInternal          = true;
/**
 * @var $_canUseForMultishipping is set to false so that we don't try
 * to send to multiple shipping addresses
 */
protected $_canUseForMultishipping  = false;
/**
 * @var $_canUseCheckout is set to true due to the fact that we want to
 * be used like any other normal payment gateway
 */
protected $_canUseCheckout          = true;



/**
 * @return getOrderPlacedRedirectUrl simply returns a redirect to Knox 
 */
public function getOrderPlaceRedirectUrl() 
    $key = Mage::getStoreConfig('payment/KnoxGateway/api_key');
    $grandTotal = Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal();//'11.50';
    $reccur = "ot";
    $callback = Mage::getStoreConfig('payment/KnoxGateway/callback_url');//"https://www.knoxpayments.com";//
    $info = Mage::getStoreConfig('payment/KnoxGateway/info_request');
    $invoice = Mage::getStoreConfig('payment/KnoxGateway/invoice_detail');
  return "https://knoxpayments.com/pay?api_key=".$key."&amount=".$grandTotal."&redirect_url=".$callback."&recurring=".$reccur."&information_request=".$info."&invoice_detail=".$invoice."";
      // "https://knoxpayments.com/newflow/?api_key='$this->$API_KEY'&api_password='$this->$API_PASSWORD'&amount='$this->$DATA_AMOUNT'&redirect_url='$this->$CALLBACK_URL'&recurring=ot&information_request='$this->$INFO_REQUEST'&invoice_detail='$this->$INVOICE_DETAIL'&user_request";

     





?>

配置(etc/config.xml):

<?xml version="1.0"?>
<config>
<modules>
  <Knox_KnoxGateway>
    <version>0.1.0</version>
  </Knox_KnoxGateway>
</modules>
<global>
 <models>
  <KnoxGateway>
    <class>Knox_KnoxGateway_Model</class>
  </KnoxGateway>
 </models>
<helpers>
  <KnoxGateway>
    <class>Knox_KnoxGateway_Helper</class>
  </KnoxGateway>
</helpers>
<blocks>
  <KnoxGateway>
    <class>Knox_KnoxGateway_Block</class>
  </KnoxGateway>
</blocks>
</global>
<default>
 <payment>
  <KnoxGateway>
    <model>KnoxGateway/standard</model>
    <active>1</active>
    <order_status>payment_review</order_status>
    <title>Knox Gateway</title>
    <payment_action>sale</payment_action>
    <sort_order>1</sort_order>
  </KnoxGateway>
 </payment>
</default>
<frontend>
 <routers>
   <KnoxGateway>
     <use>standard</use>
     <args>
       <module>Knox_KnoxGateway</module>
       <frontName>KnoxGateway</frontName>
     </args>
   </KnoxGateway>
 </routers>
</frontend> 
</config>

系统(etc/system.xml):

<?xml version="1.0"?>
<config>
<sections>
<payment>
  <groups>
    <KnoxGateway translate="label comment" module="paygate">
      <label>Knox Gateway</label>
      <frontend_type>text</frontend_type>
      <sort_order>0</sort_order>
      <show_in_default>1</show_in_default>
      <show_in_website>1</show_in_website>
      <show_in_store>1</show_in_store>
      <fields>
        <active translate="label">
          <label>Enabled</label>
          <frontend_type>select</frontend_type>
          <source_model>adminhtml/system_config_source_yesno</source_model>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>0</show_in_store>
        </active>
        <title translate="label">
          <label>Title</label>
          <frontend_type>text</frontend_type>
          <sort_order>20</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </title>
        <callback_url translate="label">
          <label>Knox Callback URL</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </callback_url>
        <info_request translate="label">
          <label>Information request</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </info_request>
        <why_who translate="label">
          <label>Why Who</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </why_who>
        <invoice_detail translate="label">
          <label>Invoice Detail</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
          <show_in_default>1</show_in_default>
        </invoice_detail>
        <api_key translate="label">
          <label>Knox Api Key</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </api_key>
        <api_password translate="label">
          <label>Knox Api Password</label>
          <frontend_type>text</frontend_type>
          <sort_order>10</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
        </api_password>
      </fields>
    </KnoxGateway>
  </groups>
 </payment>
 </sections>
</config>

【问题讨论】:

即使您在浏览器上手动输入网址,您是否检查过该操作是否对您有效?我建议尝试一下,然后告诉我们。 【参考方案1】:

在 responseAction() 函数(Paymentcontroller.php)中你有:

if($this->getRequest()->isPost()) 
   ...
else
   Mage_Core_Controller_Varien_Action::_redirect('');

只有当您在控制器中发送 POST 数据时,第一个代码块才会起作用。您写了“我们的支付网关有一个可自定义的重定向网址......”,可能您只是将重定向回 Magento,这意味着 GET 请求。所以你输入到

Mage_Core_Controller_Varien_Action::_redirect('');

并得到 403 服务器错误。

P.S.:将 controllers/Paymentcontroller.php 重命名为 controllers/PaymentController.php

【讨论】:

以上是关于需要帮助返回 Magento 并在第 3 方支付网关后更新订单状态的主要内容,如果未能解决你的问题,请参考以下文章

为特定产品启用支付网关 - Magento 2

Magento 自定义支付网关

Magento 安全交易支付方式需要持有支付选项

Magento 付款重定向订单

在magento上集成paypal支付方式

Magento 支付工作流程和事件订单已支付