如何在OpenCart中更新订单状态

时间:2017-02-27 15:28:02

标签: php callback opencart payment

我目前正在为新的支付网关构建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

1 个答案:

答案 0 :(得分: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