Rust
Rust
Global Payments Card Authorization
See more Global Payments Examples
Demonstrates how to send a card payments authorization request.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// if you don't have a Client ID yet you can still quickly test some basic request types using the following URL and credentials:
// Test URL - https://test.realexpayments.com/epage-remote.cgi
// Client ID: realexsandbox
// Shared Secret: Po8lRRT67a
let test_url = "https://test.realexpayments.com/epage-remote.cgi".to_string();
let client_id = "realexsandbox".to_string();
let shared_secret = "Po8lRRT67a".to_string();
let amount = "1001".to_string();
let currency = "EUR".to_string();
let card_number = "4263970000005262".to_string();
// We'll be sending the following XML in the body of the request:
// <?xml version="1.0" encoding="UTF-8"?>
// <request type="auth" timestamp="20180613141207">
// <merchantid>MerchantId</merchantid>
// <account>internet</account>
// <channel>ECOM</channel>
// <orderid>N6qsk4kYRZihmPrTXWYS6g</orderid>
// <amount currency="EUR">1001</amount>
// <card>
// <number>4263970000005262</number>
// <expdate>0425</expdate>
// <chname>James Mason</chname>
// <type>VISA</type>
// <cvn>
// <number>123</number>
// <presind>1</presind>
// </cvn>
// </card>
// <autosettle flag="1"/>
// <sha1hash>87707637a34ba651b6185718c863abc64b673f20</sha1hash>
// </request>
// Use this online tool to generate code from sample XML:
// Generate Code to Create XML
// Get the current date/time in this format: 20180613141207
let dt = chilkat::DateTime::new();
let _ = dt.set_from_current_system_time();
let dt_str = dt.get_as_iso8601("YYYYMMDDhhmmss", true).unwrap_or_default();
// Generate a unique order ID
let prng = chilkat::Prng::new();
let order_id = prng.gen_random(32, "base64url").unwrap_or_default();
// Compute the sha1hash
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha1");
crypt.set_encoding_mode("hexlower");
let sb_a = chilkat::StringBuilder::new();
let _ = sb_a.append("timestamp.merchantid.orderid.amount.currency.cardnumber");
let mut num_replaced = sb_a.replace("timestamp", &dt_str);
num_replaced = sb_a.replace("merchantid", &client_id);
num_replaced = sb_a.replace("orderid", &order_id);
num_replaced = sb_a.replace("amount", &amount);
num_replaced = sb_a.replace("currency", ¤cy);
num_replaced = sb_a.replace("cardnumber", &card_number);
let hash_a = crypt.hash_string_enc(&sb_a.get_as_string().unwrap_or_default()).unwrap_or_default();
let sb_b = chilkat::StringBuilder::new();
let _ = sb_b.append(&hash_a);
let _ = sb_b.append(".");
let _ = sb_b.append(&shared_secret);
let hash_b = crypt.hash_string_enc(&sb_b.get_as_string().unwrap_or_default()).unwrap_or_default();
let xml = chilkat::Xml::new();
xml.set_tag("request");
let _ = xml.add_attribute("type", "auth");
let _ = xml.add_attribute("timestamp", &dt_str);
xml.update_child_content("merchantid", &client_id);
xml.update_child_content("account", "internet");
xml.update_child_content("channel", "ECOM");
xml.update_child_content("orderid", &order_id);
let _ = xml.update_attr_at("amount", true, "currency", ¤cy);
xml.update_child_content("amount", &amount);
xml.update_child_content("card|number", &card_number);
xml.update_child_content("card|expdate", "0425");
xml.update_child_content("card|chname", "James Mason");
xml.update_child_content("card|type", "VISA");
xml.update_child_content("card|cvn|number", "123");
xml.update_child_content("card|cvn|presind", "1");
let _ = xml.update_attr_at("autosettle", true, "flag", "1");
xml.update_child_content("sha1hash", &hash_b);
let resp = chilkat::HttpResponse::new();
if http.http_str("POST", &test_url, &xml.get_xml().unwrap_or_default(), "utf-8", "application/xml", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("Response Status Code: {}", resp.status_code());
println!("Response Body:");
println!("{}", resp.body_str());
if resp.status_code() != 200 {
println!("Failed.");
return;
}
// A status code of 200 indicates we received an XML response, but it could be an error message.
// Here's an example of an error response:
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <response timestamp="20200418142356">
// <orderid>N6qsk4kYRZihmPrTXWYS6g</orderid>
// <result>508</result>
// <message>Invalid timestamp: '{' Value exceeds allowable limit: '}'</message>
// </response>
// We need to check the "result" within the XML.
let _ = xml.load_xml(&resp.body_str());
let result = xml.get_child_int_value("result");
println!("result = {}", result);
// A successful result looks like this:
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <response timestamp="20200418145519">
// <merchantid>realexsandbox</merchantid>
// <account>internet</account>
// <orderid>Cw93I1nFWVZuaATh46qMUCBlCcfrOvLo65C2cq5X-AY</orderid>
// <result>00</result>
// <authcode>L3pHww</authcode>
// <message>AUTH CODE: L3pHww</message>
// <pasref>96838535689610806</pasref>
// <cvnresult>M</cvnresult>
// <timetaken>87</timetaken>
// <authtimetaken>67</authtimetaken>
// <batchid>6322506</batchid>
// <sha1hash>2fd2f15f97f1775779fe9bda536dc8317a4b39f6</sha1hash>
// </response>
if result == 0 {
println!("authcode = {}", xml.get_child_content("authcode").unwrap_or_default());
println!("Success.");
} else {
println!("Failed.");
}