id = 'appota_payment'; // Đặt language cho phương thức thanh toán $this->lang = 'vi'; // Đặt Icon trong cấu hình cho phương thức $this->icon = plugins_url('images/appota-plugin-icon.png', __FILE__); // Không hiện trường ngoài thanh toán người dùng $this->has_fields = false; // Tên phương thức thanh toán $this->method_title = __('Appota Payment', 'woocommerce'); // Mô tả phương thức thanh toán $this->method_description = "Phương thức thanh toán an toàn với chi phí thấp qua cổng thanh toán Appotapay.com"; // Có dùng SSL verify khi gọi API Appota hay không. True: có, False: không $this->ssl_verify = False; // Gọi init_form_fields theo chuẩn Woocommerce $this->init_form_fields(); // Thực hiện chuyển cấu hình init_form_fields thành form cấu hình trong admin $this->init_settings(); // Lấy thông tin tiêu đề phương thức thanh toán $this->title = $this->get_option('title'); // Mô tả phương thức thanh toán $this->description = $this->get_option('description'); // Lấy tên cửa hàng bán $this->appota_merchant_name = $this->get_option('appota_merchant_name'); // Lấy api key được lưu trong cấu hình $this->appota_api_key = $this->get_option('appota_api_key'); // Lấy api secret được lưu trong cấu hình $this->appota_api_secret = $this->get_option('appota_api_secret'); // Lấy tên log file. $this->appota_log_file = $this->get_option('appota_log_file'); add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); add_action('woocommerce_api_wc_gateway_appota_payment', array($this, 'payment_complete')); if (!$this->is_valid_for_use()) { $this->enabled = false; } } /** * Cấu hình các trường dữ liệu cần lưu trong quản trị */ public function init_form_fields() { parent::init_form_fields(); $this->form_fields = array( 'enabled' => array( 'title' => __('Sử dụng phương thức', 'woocommerce'), 'type' => 'checkbox', 'label' => __('Đồng ý', 'woocommerce'), 'default' => 'yes' ), 'title' => array( 'title' => __('Tiêu đề', 'woocommerce'), 'type' => 'text', 'description' => __('Tiêu đề của phương thức thanh toán bạn muốn hiển thị cho người dùng.', 'woocommerce'), 'default' => __('Appota Payment', 'woocommerce'), 'desc_tip' => true, ), 'description' => array( 'title' => __('Mô tả phương thức thanh toán', 'woocommerce'), 'type' => 'textarea', 'description' => __('Mô tả của phương thức thanh toán bạn muốn hiển thị cho người dùng.', 'woocommerce'), 'default' => __('Thanh toán an toàn với Appota Payment. Thực hiện thanh toán với thẻ cào hoặc tài khoản ngân hàng trực tuyến', 'woocommerce') ), 'account_config' => array( 'title' => __('Cấu hình tài khoản', 'woocommerce'), 'type' => 'title', 'description' => '', ), 'appota_merchant_name' => array( 'title' => __('Tên cửa hàng', 'woocommerce'), 'type' => 'text', 'description' => __('Tên cửa hàng của người bán hàng sử dụng cổng thanh toán Appota Pay.', 'woocommerce'), 'default' => '', 'desc_tip' => true, ), 'appota_api_key' => array( 'title' => __('Appota API Key', 'woocommerce'), 'type' => 'text', 'description' => __('API Key của tài khoản.', 'woocommerce'), 'default' => '', 'desc_tip' => true, ), 'appota_api_secret' => array( 'title' => __('Appota API Secret', 'woocommerce'), 'type' => 'text', 'description' => __('API Secret của tài khoản.', 'woocommerce'), 'default' => '', 'desc_tip' => true, ), 'appota_log_file' => array( 'title' => __('Tên file lưu log', 'woocommerce'), 'type' => 'text', 'description' => sprintf(__('Tên file lưu trữ log trong quá trình thực hiện thanh toán bằng cổng Appota Payment, truy cập file log woocommerce/logs/appota-payment-%s.log', 'woocommerce'), date("d-m-Y")), 'default' => 'appota-payment', 'desc_tip' => true, ), ); } /** * Kiểm tra xem loại tiền tệ hệ thống dùng thanh toán có phù hợp với cổng thanh toán không * * @access public * @return bool */ function is_valid_for_use() { if (!in_array(get_woocommerce_currency(), apply_filters('woocommerce_appota_supported_currencies', array('VND')))) return false; return true; } /** * Admin Panel Options * - Hiển thị quản trị cấu hình cho plugins * * @since 1.0.0 */ public function admin_options() { ?>

Hướng dẫn cấu hình hệ thống chi tiết tại đây

is_valid_for_use()) : ?> generate_settings_html(); ?>

:

receive_payment_url($order); if (empty($result)) { $message = "Không nhận được thông tin trả về!"; $logger->writeLog("Failure: " . $message); wc_add_notice(__('Payment error:', 'woothemes') . " " . $message, 'error'); return; } // Nếu có lỗi, hiện thông báo lỗi thanh toán if ($result['error'] != 0) { $logger->writeLog("Failure: " . $result['message']); wc_add_notice(__('Payment error:', 'woothemes') . " " . $result['message'], 'error'); return; } // Nếu không có lỗi, chuyển hướng url sang trang thanh toán của Appota $appota_payment_url = $result['data']['payment_url']; $logger->writeLog("Success: Redirect Payment Url -> " . $appota_payment_url); return array( 'result' => 'success', 'redirect' => $appota_payment_url ); } /** * Lấy thông tin đơn hàng, gửi sang cổng thanh toán để nhận đường dẫn redirect * @param mixed $order * @internal param order_id Mã đơn hàng * @internal param total_amount Giá trị đơn hàng * @internal param shipping_fee Phí vận chuyển * @internal param tax_fee Thuế * @internal param currency_code Mã tiền tệ * @internal param order_description Mô tả đơn hàng * @internal param url_success Url trả về khi thanh toán thành công * @internal param url_cancel Url trả về khi hủy thanh toán * @internal param url_detail Url chi tiết đơn hàng * @internal param payer_name Thông tin thanh toán * @internal param payer_email * @internal param payer_phone_no * @internal param shipping_address * @access public * @return array */ function receive_payment_url($order) { // Tạo đường dẫn nhận kết quả trả về sau khi thanh toán thành công $url_success = get_bloginfo('wpurl') . "/wc-api/WC_Gateway_Appota_Payment"; // Tạo đường dẫn nhận kết quả trả về sau khi thanh toán bị dừng $url_cancel = $order->get_cancel_order_url(); // Tạo mảng thông tin sẽ chuyển sang Appota pay để nhận đường link thanh toán $params['order_id'] = $order->id; $params['total_amount'] = strval($order->order_total); $params['shipping_fee'] = strval($order->order_shipping); //isset($method->no_shipping) ? $method->no_shipping : 0, $params['tax_fee'] = strval($order->order_tax); $params['currency_code'] = strval(get_woocommerce_currency()); $params['url_success'] = $url_success; $params['url_cancel'] = $url_cancel; $params['order_description'] = preg_replace('/[^a-zA-Z0-9\_-]/', '', $order->customer_note); $params['payer_name'] = strval($order->billing_first_name . " " . $order->billing_last_name); $params['payer_email'] = strval($order->billing_email); $params['payer_phone_no'] = strval($order->billing_phone); $params['payer_address'] = strval($order->shipping_address_1); $params['ip'] = $this->auto_reverse_proxy_pre_comment_user_ip(); $params['user_agent'] = $_SERVER['HTTP_USER_AGENT']; global $woocommerce; $items = $woocommerce->cart->get_cart(); $items_data = array(); foreach ($items as $values) { $product = $values['data']->post; $items_data[$values['product_id']]['id'] = $values['product_id']; $items_data[$values['product_id']]['name'] = $product->post_title; $items_data[$values['product_id']]['quantity'] = $values['quantity']; $items_data[$values['product_id']]['price'] = get_post_meta($values['product_id'], '_price', true); } $params['product_info'] = json_encode($items_data); $config = array(); $config['api_key'] = $this->appota_api_key; $config['lang'] = $this->lang; $config['secret_key'] = $this->appota_api_secret; $config['ssl_verify'] = $this->ssl_verify; // Gọi resful API của Appota Pay $call_api = new Appota_CallApi($config); $result = $call_api->getPaymentUrl($params); // echo '
';print_r($result);die;
                return $result;
            }

            function payment_complete()
            {
                global $woocommerce;
                include(WP_PLUGIN_DIR . '/appota-payment/appota_receiver.php');
                include(WP_PLUGIN_DIR . '/appota-payment/appota_logger.php');

                $receiver = new WC_Appota_Receiver();
                $logger = new WC_Appota_Logger();

                $check_valid_request = $receiver->checkValidRequest($_GET);
                if ($check_valid_request['error_code'] == 0) {
                    $check_valid_order = $receiver->checkValidOrder($_GET);
                    if ($check_valid_order['error_code'] == 0) {
                        $order_id = (int)$_GET['order_id'];
                        $transaction_id = $_GET['transaction_id'];
                        $total_amount = floatval($_GET['amount']);
                        $order = new WC_Order($order_id);
                        $comment_status = 'Thực hiện thanh toán thành công với đơn hàng ' . $order_id . '. Giao dịch hoàn thành. Cập nhật trạng thái cho đơn hàng thành công';
                        $order->add_order_note(__($comment_status, 'woocommerce'));
                        $order->payment_complete();
                        $order->update_status('completed');

                        $order->update_meta_data('transaction_id', $transaction_id);
                        update_post_meta($order_id, 'appotapay_transaction_id', $transaction_id);
                        $woocommerce->cart->empty_cart();
                        $order_status = 'complete';

                        $message = "Appota Pay xác nhận đơn hàng: [Order ID: {$order_id}] - [Transaction ID: {$transaction_id}] - [Total: {$total_amount}] - [{$order_status}]";
                        $logger->writeLog($message);

                        wp_redirect(add_query_arg('utm_nooverride', '1', $this->get_return_url($order)));
                    } else {
                        $message = "Mã Lỗi: {$check_valid_order['error_code']} - Message: {$check_valid_order['message']}";
                        $logger->writeLog($message);

                        $redirect_url = add_query_arg('wc_error', urlencode($message . " Hãy thanh toán lại!"), '/thanh-toan/');
                        wp_redirect($redirect_url);
                    }
                } else {
                    $message = "Mã Lỗi: {$check_valid_request['error_code']} - Message: {$check_valid_request['message']}";
                    $logger->writeLog($message);
                    $redirect_url = add_query_arg('wc_error', urlencode($message . " Hãy thanh toán lại!"), '/thanh-toan/');
                    wp_redirect($redirect_url);
                }
            }

            function auto_reverse_proxy_pre_comment_user_ip()
            {
                $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
                if (!empty($_SERVER['X_FORWARDED_FOR'])) {
                    $X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
                    if (!empty($X_FORWARDED_FOR)) {
                        $REMOTE_ADDR = trim($X_FORWARDED_FOR[0]);
                    }
                } /*
                 * Some php environments will use the $_SERVER['HTTP_X_FORWARDED_FOR'] 
                 * variable to capture visitor address information.
                 */ elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                    $HTTP_X_FORWARDED_FOR = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                    if (!empty($HTTP_X_FORWARDED_FOR)) {
                        $REMOTE_ADDR = trim($HTTP_X_FORWARDED_FOR[0]);
                    }
                }
                return preg_replace('/[^0-9a-f:\., ]/si', '', $REMOTE_ADDR);
            }

        }

    }

    add_action( 'woocommerce_admin_order_data_after_order_details', 'appotapay_transaction_id_order_meta', 10, 1 );

    function appotapay_transaction_id_order_meta($order){
        echo '

' . get_post_meta( $order->id, 'appotapay_transaction_id', true ). '

'; } } else { /** * Thông báo cài đặt hoặc kích hoạt Woocommerce nếu plugin chưa được cài đặt hoặc kích hoạt */ function appota_payment_missing_woocommerce_notice() { $class = 'notice notice-error'; $message = __('Hệ thống chưa cài đặt hoặc kích hoạt plugin Woocommerce! Bạn cần cài đặt hoặc kích hoạt Woocommerce để sử dụng plugin Appota Payment', 'woocomerce-missing-plugin'); printf('

%2$s

', $class, $message); } add_action('admin_notices', 'appota_payment_missing_woocommerce_notice'); }