Yêu cầu bảo mật

Sử dụng giao thức https

Chữ ký điện tử & Mã hoá dữ liệu

Signature là một chuỗi ký tự được tạo ra từ một thuật toán cho trước, sử dụng để kiểm tra tính đúng đắn của dữ liệu trên đường truyền giữa 2 hệ thống. Một số thuật toán đang sử dụng là MD5, SHA1, SHA256 và Hmac.

Trong tài liệu này VZpay sử dụng các yếu tố sau để tạo Signature :

  • Dữ liệu đầu vào bao gồm SecretKey & data.

  • Thuật toán: RSA – Sign HASH.

    • HASH: HmacSHA512.

    • Keysize: 2048.

  • Cách tạo chữ ký điện tử:

    Truyền dữ liệu: (lưu ý: Chỉ mã hoá các trường thông tin dưới đây & truyền theo đúng thứ tự các giá trị)

+ Tạo chữ ký khi tạo mới đơn hàng

String hash_data = String.join("|",
                            paymentCode,
                            referenceId,
                            ipAddress,
                            clientId,
                            expireDate,
                            createDate,
                            successRedirectUrl,
                            failureRedirectUrl,
                            amount,
                            currency,
                            orderInfo);
String signature = Utils.hmacSHA512(getSecretKey(), hash_data);

+ Tạo chữ ký khi nhận kết quả thanh toán qua webhook

String hash_data = String.join("|",
                    paymentCode,
                    clientId,
                    transactionId,
                    amount,
                    referenceId,
                    paymentDate,
                    bankCode,
                    paymentStatus
            );
String signature = Utils.hmacSHA512(secretKey, hash_data);

Một số ví dụ mã hoá theo ngôn ngữ lập trình

public static String hmacSHA512(final String key, final String data) {
    try {
        if (key == null || data == null) {
            throw new NullPointerException();
        }
        final Mac hmac512 = Mac.getInstance("HmacSHA512");
        byte[] hmacKeyBytes = key.getBytes();
        final SecretKeySpec secretKey = new SecretKeySpec(hmacKeyBytes, "HmacSHA512");
        hmac512.init(secretKey);
        byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
        byte[] result = hmac512.doFinal(dataBytes);
        StringBuilder sb = new StringBuilder(2 * result.length);
        for (byte b : result) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();

    } catch (Exception ex) {
        return ex.getMessage();
    }
}

Last updated