Tài liệu / Tích hợp/ Tích hợp WordPress/WooCommerce

Tích hợp WordPress/WooCommerce

Tích hợp Cập nhật: 22/03/2026

Tích hợp ThueAPI.VN với WooCommerce

Tự động xác nhận đơn hàng WooCommerce khi nhận được chuyển khoản ngân hàng qua ThueAPI.

Ý tưởng tích hợp

  1. Khách đặt hàng → WooCommerce tạo đơn hàng với trạng thái pending
  2. Khách chuyển khoản, nội dung chứa mã đơn hàng (VD: DH12345)
  3. ThueAPI nhận giao dịch → gửi webhook đến WordPress
  4. Plugin đọc nội dung, tìm mã đơn → cập nhật trạng thái processing

Code tích hợp (functions.php hoặc plugin)

<?php
/**
 * ThueAPI WooCommerce Integration
 * Thêm vào functions.php của theme hoặc tạo plugin riêng
 */

// Webhook endpoint để nhận thông báo từ ThueAPI
add_action('rest_api_init', function() {
    register_rest_route('thueapi/v1', '/webhook', [
        'methods'             => 'POST',
        'callback'            => 'thueapi_handle_webhook',
        'permission_callback' => '__return_true',
    ]);
});

function thueapi_handle_webhook(WP_REST_Request $request): WP_REST_Response
{
    $secret    = defined('THUEAPI_WEBHOOK_SECRET') ? THUEAPI_WEBHOOK_SECRET : '';
    $payload   = $request->get_body();
    $signature = $request->get_header('x_webhook_signature');

    // Xác thực chữ ký HMAC-SHA256
    $expected = hash_hmac('sha256', $payload, $secret);
    if (!hash_equals($expected, $signature)) {
        return new WP_REST_Response(['error' => 'Invalid signature'], 401);
    }

    $data = json_decode($payload, true);
    foreach ($data['transactions'] as $tx) {
        if ($tx['transferType'] !== 'IN') continue;

        // Tìm mã đơn hàng trong nội dung chuyển khoản
        // Ví dụ nội dung: "Thanh toan DH12345"
        $order_id = thueapi_extract_order_id($tx['content']);

        if ($order_id) {
            $order = wc_get_order($order_id);
            if ($order && $order->get_status() === 'pending') {
                // Xác nhận đơn hàng
                $order->payment_complete($tx['transactionNumber']);
                $order->add_order_note(sprintf(
                    'Thanh toán xác nhận qua ThueAPI: %s %s VND lúc %s',
                    $tx['transactionNumber'],
                    number_format($tx['transferAmount']),
                    $tx['transactionDate']
                ));
            }
        }
    }

    return new WP_REST_Response(['success' => true], 200);
}

/**
 * Trích xuất mã đơn hàng từ nội dung chuyển khoản
 * Điều chỉnh regex theo quy ước nội dung của bạn
 */
function thueapi_extract_order_id(string $content): ?int
{
    // Ví dụ: "DH12345", "ORDER12345", "HD12345"
    if (preg_match('/(?:DH|ORDER|HD)(\d+)/i', $content, $matches)) {
        return (int) $matches[1];
    }
    return null;
}

Cấu hình

// Thêm vào wp-config.php
define('THUEAPI_WEBHOOK_SECRET', 'your_webhook_secret_here');

URL webhook để nhập vào ThueAPI Dashboard:

https://your-shop.com/wp-json/thueapi/v1/webhook
Mẹo: Hướng dẫn khách hàng nhập mã đơn hàng vào nội dung chuyển khoản. Ví dụ: hiển thị rõ ràng "Nội dung chuyển khoản: DH{order_id}" trên trang thanh toán.
ThueAPI.VN
Đăng nhập với Google
hoặc đăng nhập bằng email
Quên mật khẩu?

Chưa có tài khoản?

Đăng ký với Google
hoặc đăng ký bằng email

Bằng cách đăng ký, bạn đồng ý với Điều khoản dịch vụChính sách bảo mật của chúng tôi.

Đã có tài khoản?