/**
* Plugin Name: Ngan Luong Paygate for WooCommerce
* Plugin URI: https://xmup.shop
* Description: Plugin tích hợp cổng thanh toán Ngân Lượng (NganLuong.vn) cho WooCommerce.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://xmup.shop
* License: GPL2
*/
if (!defined(‘ABSPATH’)) {
exit; // Bảo mật: Ngăn truy cập trực tiếp
}
// Đặt timezone
date_default_timezone_set(‘Asia/Bangkok’);
// Định nghĩa các hằng số
define(‘NL_ENDPOINT’, ‘https://www.nganluong.vn/checkoutseamless.api.nganluong.post.php’);
define(‘NL_MERCHANT_ID’, ‘68441’); // Mã merchant
define(‘NL_MERCHANT_PASSWORD’, ’92bbe6c3b156ebfb8bfaa5190095495e’); // Mật khẩu kết nối
define(‘NL_MERCHANT_EMAIL’, ‘nguyenminhthuy831998@gmail.com’); // Email tài khoản Ngân Lượng
// Tải plugin
add_action(‘plugins_loaded’, ‘nganluong_woocommerce_init’, 0);
function nganluong_woocommerce_init() {
if (!class_exists(‘WC_Payment_Gateway’)) {
return;
}
class WC_NganLuong_Payment_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = ‘nganluong’;
$this->icon = ‘https://www.nganluong.vn/images/logo-nl.png’;
$this->has_fields = false;
$this->method_title = ‘Cổng thanh toán Ngân Lượng’;
$this->method_description = ‘Thanh toán an toàn qua cổng Ngân Lượng (NganLuong.vn).’;
// Load các thiết lập
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option(‘title’);
$this->description = $this->get_option(‘description’);
add_action(‘woocommerce_update_options_payment_gateways_’ . $this->id, array($this, ‘process_admin_options’));
add_action(‘woocommerce_receipt_nganluong’, array($this, ‘receipt_page’));
}
public function init_form_fields() {
$this->form_fields = array(
‘enabled’ => array(
‘title’ => ‘Bật / Tắt’,
‘type’ => ‘checkbox’,
‘label’ => ‘Bật thanh toán qua Ngân Lượng’,
‘default’ => ‘yes’,
),
‘title’ => array(
‘title’ => ‘Tiêu đề’,
‘type’ => ‘text’,
‘default’ => ‘Thanh toán qua Ngân Lượng’,
),
‘description’ => array(
‘title’ => ‘Mô tả’,
‘type’ => ‘textarea’,
‘default’ => ‘Thanh toán trực tuyến an toàn qua Ngân Lượng.’,
),
);
}
public function receipt_page($order_id) {
echo ‘
Cảm ơn bạn đã đặt hàng! Nhấn nút bên dưới để thanh toán qua Ngân Lượng.
‘;
echo $this->generate_nganluong_payment_form($order_id);
}
private function generate_nganluong_payment_form($order_id) {
$order = wc_get_order($order_id);
$return_url = $this->get_return_url($order);
// Tạo URL thanh toán với các tham số cần thiết
$payment_url = NL_ENDPOINT . ‘?merchant_id=’ . NL_MERCHANT_ID
. ‘&merchant_password=’ . NL_MERCHANT_PASSWORD
. ‘&email=’ . NL_MERCHANT_EMAIL
. ‘&order_id=’ . $order->get_id()
. ‘&order_description=’ . urlencode(‘Thanh toán đơn hàng #’ . $order->get_id())
. ‘&amount=’ . $order->get_total()
. ‘¤cy=VND’
. ‘&return_url=’ . urlencode($return_url);
// Kiểm tra URL có hợp lệ không
if (filter_var($payment_url, FILTER_VALIDATE_URL) === false) {
return ‘
Đã có lỗi xảy ra khi tạo URL thanh toán.
‘;
}
return ‘
Thanh toán ngay‘;
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
$order->update_status(‘pending’, __(‘Chờ thanh toán qua Ngân Lượng.’, ‘woocommerce’));
return array(
‘result’ => ‘success’,
‘redirect’ => $order->get_checkout_payment_url(true),
);
}
}
// Thêm cổng thanh toán vào WooCommerce
add_filter(‘woocommerce_payment_gateways’, ‘add_nganluong_payment_gateway’);
function add_nganluong_payment_gateway($gateways) {
$gateways[] = ‘WC_NganLuong_Payment_Gateway’;
return $gateways;
}
}