Rust
Rust
Payeezy HMAC Computation
See more HTTP Misc Examples
Demonstrates how to calculate the HMAC for a Payeezy REST request.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let crypt = chilkat::Crypt2::new();
let prng = chilkat::Prng::new();
// An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
let api_key = "my_api_key".to_string();
// An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
let api_secret = "my_api_secret".to_string();
// A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
let token = "my_merchant_token".to_string();
// The nonce is a random number (bytes), something like "6057786719490086000"
let nonce = prng.gen_random(8, "decimal").unwrap_or_default();
println!("nonce = {}", nonce);
let dt_now = chilkat::DateTime::new();
let _ = dt_now.set_from_current_system_time();
let sb_timestamp = chilkat::StringBuilder::new();
// Get the epoch timestamp in seconds
let _ = sb_timestamp.append(&dt_now.get_as_unix_time_str(false).unwrap_or_default());
// Change it to milliseconds
let _ = sb_timestamp.append("000");
// The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
let timestamp = sb_timestamp.get_as_string().unwrap_or_default();
println!("timestamp = {}", timestamp);
// Generate the following JSON request body:
// {
// "merchant_ref": "Astonishing-Sale",
// "transaction_type": "authorize",
// "method": "token",
// "amount": "200",
// "currency_code": "USD",
// "token": {
// "token_type": "FDToken",
// "token_data": {
// "type": "visa",
// "value": "2537446225198291",
// "cardholder_name": "JohnSmith",
// "exp_date": "1030",
// "special_payment": "B"
// }
// }
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("merchant_ref", "Astonishing-Sale");
let _ = json.update_string("transaction_type", "authorize");
let _ = json.update_string("method", "token");
let _ = json.update_string("amount", "200");
let _ = json.update_string("currency_code", "USD");
let _ = json.update_string("token.token_type", "FDToken");
let _ = json.update_string("token.token_data.type", "visa");
let _ = json.update_string("token.token_data.value", "2537446225198291");
let _ = json.update_string("token.token_data.cardholder_name", "JohnSmith");
let _ = json.update_string("token.token_data.exp_date", "1030");
let _ = json.update_string("token.token_data.special_payment", "B");
// string hashData = apiKey + nonce + timestamp + token + jsonString;
let sb_hmac_data = chilkat::StringBuilder::new();
let _ = sb_hmac_data.append(&api_key);
let _ = sb_hmac_data.append(&nonce);
let _ = sb_hmac_data.append(×tamp);
let _ = sb_hmac_data.append(&token);
let _ = sb_hmac_data.append(&json.emit().unwrap_or_default());
// HMAC the data to produce a hex string.
crypt.set_encoding_mode("hexlower");
crypt.set_mac_algorithm("hmac");
let _ = crypt.set_mac_key_string(&api_secret);
crypt.set_hash_algorithm("sha256");
crypt.set_charset("utf-8");
let hex_hash = crypt.mac_string_enc(&sb_hmac_data.get_as_string().unwrap_or_default()).unwrap_or_default();
// Now base64 encode the hex string:
let sb_base64_hash = chilkat::StringBuilder::new();
let _ = sb_base64_hash.append(&hex_hash);
let _ = sb_base64_hash.encode("base64", "utf-8");
println!("This is the Authorization header to be sent with the payeezy request:");
println!("Authorization: {}", sb_base64_hash.get_as_string().unwrap_or_default());