hatang 0.1.1

Official Rust client for the Ha Tang (Hạ Tầng) API: transactional email, live chat and support CRM, VietQR payments, AI chatbot knowledge and webhook verification. Async (Tokio) and blocking.
Documentation
//! Values your backend computes itself, the same way Ha Tang's servers and the app SDKs do.

use hmac::{Hmac, Mac};
use sha2::Sha256;
use uuid::Uuid;

/// The signature that makes a chat identity trusted: hex HMAC-SHA256 of the user id with the site's identity
/// secret (Ha Tang → Support → Channels → the site → Widget). Pass it as `hash` with `userId` to `identify` in the
/// widget or the app SDKs; never send the secret to the browser or the app.
pub fn identity_hash(user_id: &str, identity_secret: &str) -> String {
    let mut mac = Hmac::<Sha256>::new_from_slice(identity_secret.as_bytes())
        .expect("HMAC takes keys of any length");
    mac.update(user_id.as_bytes());
    crate::webhooks::hex(&mac.finalize().into_bytes())
}

/// The app account token for one of your users: pass it as StoreKit `appAccountToken` / Google Play
/// `obfuscatedAccountId` so the stores' notifications reach that customer in Ha Tang. A UUID v5 of the user id in
/// the site's namespace; the app SDKs' `purchaseToken` gives the same value.
pub fn purchase_token(site_id: &str, user_id: &str) -> String {
    let namespace = Uuid::new_v5(
        &Uuid::NAMESPACE_URL,
        format!("hatang:site:{site_id}").as_bytes(),
    );
    Uuid::new_v5(&namespace, user_id.as_bytes()).to_string()
}