2017-02-27 26 views
0

我目前正在爲新的支付網關構建OpenCart支付擴展。現在,我可以成功進行付款並重定向到商戶網站,但我不知道如何編寫代碼,以便它也更新訂單狀態。如何在OpenCart中更新訂單狀態

我所有的訂單現在都顯示在遺漏的訂單下,我覺得是因爲這個原因。我沒有回調函數,我不知道如何去做。我想更新代碼,以便它可以在付款成功時更新我的​​訂單狀態,或者在付款失敗時重新導向結帳,但仍會更新訂單狀態。

這是我下面的代碼:

<?php 
class ControllerExtensionPaymentSCPAY extends Controller { 
    public function index() { 
     $this->load->language('extension/payment/sc_pay'); 

     $data['button_confirm'] = $this->language->get('button_confirm'); 

     $data['testmode'] = $this->config->get('sc_pay_test'); 

     $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); 
     $total = $order_info['total']; 

     $newbutton = str_replace("50", $total, $this->config->get('sc_pay_button_link')); 

     $newbutton = $newbutton . "&redirect_url=" .$this->url->link('checkout/success'); 


     if (!$this->config->get('sc_pay_test')) { 
      $data['action'] = $newbutton; 
     } 
else { 
      $data['action'] = ''; 
     } 

     $this->load->model('checkout/order'); 

     $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); 

     if ($order_info) { 
      $data['business'] = $this->config->get('sc_pay_email'); 
      $data['item_name'] = html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'); 

      $data['products'] = array(); 

      foreach ($this->cart->getProducts() as $product) { 
       $option_data = array(); 

       foreach ($product['option'] as $option) { 
        if ($option['type'] != 'file') { 
         $value = $option['value']; 
        } 
else { 
         $upload_info = $this->model_tool_upload->getUploadByCode($option['value']); 

         if ($upload_info) { 
          $value = $upload_info['name']; 
         } 
else { 
          $value = ''; 
         } 
        } 

        $option_data[] = array(
         'name' => $option['name'], 
         'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) 
        ); 
       } 

       $data['products'][] = array(
        'name'  => htmlspecialchars($product['name']), 
        'model' => htmlspecialchars($product['model']), 
        'price' => $this->currency->format($product['price'], $order_info['currency_code'], false, false), 
        'quantity' => $product['quantity'], 
        'option' => $option_data, 
        'weight' => $product['weight'] 
       ); 
      } 

      $data['discount_amount_cart'] = 0; 

      $total = $this->currency->format($order_info['total'] - $this->cart->getSubTotal(), $order_info['currency_code'], false, false); 

      if ($total > 0) { 
       $data['products'][] = array(
        'name'  => $this->language->get('text_total'), 
        'model' => '', 
        'price' => $total, 
        'quantity' => 1, 
        'option' => array(), 
        'weight' => 0 
       ); 
      } 
else { 
       $data['discount_amount_cart'] -= $total; 
      } 

      $data['currency_code'] = $order_info['currency_code']; 
      $data['first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8'); 
      $data['last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8'); 
      $data['address1'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8'); 
      $data['address2'] = html_entity_decode($order_info['payment_address_2'], ENT_QUOTES, 'UTF-8'); 
      $data['city'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8'); 
      $data['zip'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8'); 
      $data['country'] = $order_info['payment_iso_code_2']; 
      $data['email'] = $order_info['email']; 
      $data['invoice'] = $this->session->data['order_id'] . ' - ' . html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8') . ' ' . html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8'); 
      $data['lc'] = $this->session->data['language']; 
      $data['return'] = $this->url->link('checkout/success'); 
      $data['notify_url'] = $this->url->link('extension/payment/sc_pay/callback', '', true); 
      $data['cancel_return'] = $this->url->link('checkout/checkout', '', true); 

      $data['custom'] = $this->session->data['order_id']; 

      return $this->load->view('extension/payment/sc_pay', $data); 
     } 
    } 

enter code here 

回答

0

我從您正在使用2.3.0.2版本Opencart的的控制器文件假設。

//some validation here 
$this->model_checkout_order->addOrderHistory($order_id, $order_status_id,print_r($callback,true),true,false); 

// if order id is not 0, it will update order status to whatever you defined in admin/payment settings (integer). if 0, it will add new order 
+0

我應該在哪一行添加代碼?我對Opencart非常陌生 – oriolowonancy

+0

我已經嘗試過了,但仍然缺少訂單。你的代碼提到哪個回調函數? @trinkal – oriolowonancy

+0

你需要創建回調函數,檢查其他支付網關的控制器文件,你會得到幫助。 –

相關問題