2016-05-04 40 views
1

當客戶返回到以下URL(示例)時;我的訂單完整退貨方式是否正確?

http://prestashop.dev/index.php?action=completed&controller=callback&fc=module&hmac={valid-hmac}&merchant_order_id=14&module=chippin 

成功付款後,它會調用這個FrontController子類;

class ChippinCallbackModuleFrontController extends ModuleFrontController 
{ 
    public function postProcess() 
    { 
     $chippin = new Chippin(); 

     $payment_response = new PaymentResponse(); 
     $payment_response->getPostData(); 

     // if a valid response from gateway 
     if(ChippinValidator::isValidHmac($payment_response)) { 

      // "action" is passed as a param in the URL. don't worry, the Hmac can tell if it's valid or not. 
      if ($payment_response->getAction() === "completed") { 

       // payment_response->getMerchantOrderId() will just return the id_order from the orders table 
       $order_id = Order::getOrderByCartId((int) ($payment_response->getMerchantOrderId())); 
       $order = new Order($order_id); 
       // this will update the order status for the benefit of the merchant. 
       $order->setCurrentState(Configuration::get('CP_OS_PAYMENT_COMPLETED')); 

       // assign variables to smarty (copied this from another gateway, don't really understand smarty) 
       $this->context->smarty->assign(
        array(
         'order' => $order->reference, 
        ) 
       ); 

       // display this template 
       $this->setTemplate('confirmation.tpl'); 

我是Prestashop的新手。我只是不確定這是否在技術上完成。 confirmation.tlp視圖會顯示order->reference,訂單狀態更新爲「已完成」,但這是我需要的嗎?

有沒有其他的考慮?我有機會在此時調用hookDisplayPaymentReturn,但爲什麼我應該?

我似乎有一個非常標準的返回頁面。這夠了嗎;

enter image description here

更新 - 我只需要調用一個鉤子一樣的東西;

public function displayPaymentReturn() 
{ 

    $params = $this->displayHook(); 

    if ($params && is_array($params)) { 
     return Hook::exec('displayPaymentReturn', $params, (int) $this->module->id); 
    } 

    return false; 
} 

回答

1

據我所見,一切似乎都對我好。

您應該考慮加入hookDisplayPaymentReturn它允許其他模塊將代碼添加到您的確認頁面。例如,Google模塊可以添加用於在確認頁面上將訂單信息發送到分析的JavaScript代碼。


編輯

class ChippinCallbackModuleFrontController extends ModuleFrontController 
{ 
    public function postProcess() 
    { 
     $chippin = new Chippin(); 

     $payment_response = new PaymentResponse(); 
     $payment_response->getPostData(); 

     // if a valid response from gateway 
     if(ChippinValidator::isValidHmac($payment_response)) { 

      // "action" is passed as a param in the URL. don't worry, the Hmac can tell if it's valid or not. 
      if ($payment_response->getAction() === "completed") { 

       // payment_response->getMerchantOrderId() will just return the id_order from the orders table 
       $order_id = Order::getOrderByCartId((int) ($payment_response->getMerchantOrderId())); 
       $order = new Order($order_id); 
       // this will update the order status for the benefit of the merchant. 
       $order->setCurrentState(Configuration::get('CP_OS_PAYMENT_COMPLETED')); 

       // assign variables to smarty (copied this from another gateway, don't really understand smarty) 
       $this->context->smarty->assign(
        array(
         'order' => $order->reference, 
         'hookDisplayPaymentReturn' => Hook::exec('displayPaymentReturn', $params, (int) $this->module->id); 
        ) 
       ); 

       $cart = $this->context->cart; 
       $customer = new Customer($cart->id_customer); 

       Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.$order->id.'&key='.$customer->secure_key); 

而且你的模塊中:

class myPaymentModule extends PaymentModule 
{ 
    public function install() 
    { 
     if (!parent::install() || !$this->registerHook('paymentReturn')) 
      return false; 
     return true; 
    } 

    // Example taken from bankwire module 
    public function hookPaymentReturn($params) 
    { 
     $state = $params['objOrder']->getCurrentState(); 

     $this->smarty->assign(array(
      'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false), 
      'bankwireDetails' => Tools::nl2br($this->details), 
      'bankwireAddress' => Tools::nl2br($this->address), 
      'bankwireOwner' => $this->owner, 
      'status' => 'ok', 
      'id_order' => $params['objOrder']->id 
     )); 
     if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference)) 
      $this->smarty->assign('reference', $params['objOrder']->reference); 
     return $this->display(__FILE__, 'confirmation.tpl'); 
    } 
} 
+0

什麼,所以不是直接調用了'confirmation.tpl'頁面,我應該做的鉤?或者只是有一個空鉤子方法? - 查看我的更新 – mikelovelyuk

+0

您正在使用顯示確認模板的快捷方式。實際上,您應該重定向到訂單確認控制器,該控制器將爲您完成該過程。在你的模塊中,你可以鉤上'displayPaymentReturn'或'displayOrderConfirmation'。查看我的編輯 –

+0

查看'bankwire'模塊(這是默認的Prestashop模塊)。這是一個非常簡單的理解模塊。 –