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()
{
?>
:
';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');
}