Rust
Rust
Payeezy Place Temp Authorization Hold on Buyer’s Credit Card
See more HTTP Misc Examples
Demonstrates how to place a temporary authorization hold for the desired amount on the buyer’s credit card. You can Capture the authorized amount on completion of service or Void/Refund the transaction as required.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": "credit_card",
// "amount": "1299",
// "currency_code": "USD",
// "credit_card": {
// "type": "visa",
// "cardholder_name": "John Smith",
// "card_number": "4788250000028291",
// "exp_date": "1020",
// "cvv": "123"
// }
// }
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", "credit_card");
let _ = json.update_string("amount", "1299");
let _ = json.update_string("currency_code", "USD");
let _ = json.update_string("credit_card.type", "visa");
let _ = json.update_string("credit_card.cardholder_name", "John Smith");
let _ = json.update_string("credit_card.card_number", "4788250000028291");
let _ = json.update_string("credit_card.exp_date", "1020");
let _ = json.update_string("credit_card.cvv", "123");
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// 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();
println!("hexHash = {}", hex_hash);
// 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());
// -----------------------------------------------------------
// Now that we have the value for the Authorization header, send the POST containing the JSON.
let http = chilkat::Http::new();
http.set_accept("*/*");
http.set_user_agent("");
http.set_request_header("Authorization", &sb_base64_hash.get_as_string().unwrap_or_default());
http.set_request_header("apikey", &api_key);
http.set_request_header("nonce", &nonce);
http.set_request_header("timestamp", ×tamp);
http.set_request_header("token", &token);
http.set_session_log_filename("qa_output/payeezy.txt");
let url = "https://api-cert.payeezy.com/v1/transactions".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("response status code = {}", resp.status_code());
let _ = json.load(&resp.body_str());
println!("{}", json.emit().unwrap_or_default());
// Sample JSON response:
// {
// "correlation_id": "228.4604632998994",
// "transaction_status": "approved",
// "validation_status": "success",
// "transaction_type": "authorize",
// "transaction_id": "ET175628",
// "transaction_tag": "2313721985",
// "method": "credit_card",
// "amount": "1299",
// "currency": "USD",
// "cvv2": "M",
// "token": {
// "token_type": "FDToken",
// "token_data": {
// "value": "9732261336638291"
// }
// },
// "card": {
// "type": "visa",
// "cardholder_name": "John Smith",
// "card_number": "8291",
// "exp_date": "1020"
// },
// "bank_resp_code": "100",
// "bank_message": "Approved",
// "gateway_resp_code": "00",
// "gateway_message": "Transaction Normal"
// }
//
println!("{}", json.string_of("correlation_id").unwrap_or_default());
println!("{}", json.string_of("transaction_status").unwrap_or_default());
println!("{}", json.string_of("validation_status").unwrap_or_default());
println!("{}", json.string_of("transaction_type").unwrap_or_default());
println!("{}", json.string_of("transaction_id").unwrap_or_default());
println!("{}", json.string_of("transaction_tag").unwrap_or_default());
println!("{}", json.string_of("method").unwrap_or_default());
println!("{}", json.string_of("amount").unwrap_or_default());
println!("{}", json.string_of("currency").unwrap_or_default());
println!("{}", json.string_of("cvv2").unwrap_or_default());
println!("{}", json.string_of("token.token_type").unwrap_or_default());
println!("{}", json.string_of("token.token_data.value").unwrap_or_default());
println!("{}", json.string_of("card.type").unwrap_or_default());
println!("{}", json.string_of("card.cardholder_name").unwrap_or_default());
println!("{}", json.string_of("card.card_number").unwrap_or_default());
println!("{}", json.string_of("card.exp_date").unwrap_or_default());
println!("{}", json.string_of("bank_resp_code").unwrap_or_default());
println!("{}", json.string_of("bank_message").unwrap_or_default());
println!("{}", json.string_of("gateway_resp_code").unwrap_or_default());
println!("{}", json.string_of("gateway_message").unwrap_or_default());