Rust
Rust
Adyen HMAC Signature Calculation for Hosted Payment Pages
See more Adyen Examples
Demonstrates how to do the HMAC Signature Calculation for a hosted payment page (HPP) in Adyen.For a C# ASP.NET Razor Pages example showing the HTML Form with HMAC signature code, see Adyen HMAC Signature Calculation in C#
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let str_html = "<table class=\"od\"><tr><th>Description</th><th>Quantity</th><th>Amount</th></tr><tr><td>1 Digital Camera</td><td class=\"r\">1</td><td class=\"r\">100 GBP</td></tr><tr><td class=\"b\">Total</td><td class=\"r\"></td><td class=\"b r\">100.00 GBP</td></tr></table>".to_string();
let gzip = chilkat::Gzip::new();
let gz_order_data = gzip.compress_string_enc(&str_html, "utf-8", "base64").unwrap_or_default();
let xml = chilkat::Xml::new();
xml.set_tag("keyValuePairs");
xml.new_child2("orderData", &gz_order_data);
// required, The payment deadline; the payment needs to occur within the specified time value.
let session_validity = "2019-08-11T10:30:00Z".to_string();
xml.new_child2("sessionValidity", &session_validity);
// optional. Normally we'll let Adyen automatically know the country based on the IP address.
// By default, the payment methods offered to a shopper are filtered based on the country the shopper's IP address is mapped to.
// In this way, shoppers are not offered payment methods that are not available in the country they are carrying out the transaction from.
// This IP-to-country mapping is not 100% accurate, so if you have already established the country of the shopper, you can set it explicitly
// in the countryCode parameter.
let country_code = "GB".to_string();
xml.new_child2("countryCode", &country_code);
// optional
let shopper_locale = "en_GB".to_string();
// If not specified, the locale preference is set to en_GB by default.
// When it is not necessary to include the country-specific part, use only the language code.
// For example: it instead of it_IT to set the locale preferences to Italian.
xml.new_child2("shopperLocale", &shopper_locale);
// required, A reference to uniquely identify the payment. This reference is used in all communication with you about the payment status.
// We recommend using a unique value per payment; however, it is not a requirement. If you need to provide multiple references for a transaction,
// you can enter them in this field. Separate each reference value with a hyphen character ("-"). This field has a length restriction:
// you can enter max. 80 characters.
let merchant_reference = "paymentTest1234".to_string();
xml.new_child2("merchantReference", &merchant_reference);
// required, The merchant account identifier you want to process the (transaction) request with.
let merchant_account = "ChilkatSoftwareIncCOM".to_string();
xml.new_child2("merchantAccount", &merchant_account);
// required. 10000 for $100.00
let payment_amount = "10000".to_string();
xml.new_child2("paymentAmount", &payment_amount);
// required, The three-character ISO currency code
let currency_code = "GBP".to_string();
xml.new_child2("currencyCode", ¤cy_code);
// required.
let skin_code = "S7uWsvfB".to_string();
xml.new_child2("skinCode", &skin_code);
// optional, A unique identifier for the shopper, for example, a customer ID.
// We recommend providing this information, as it is used in velocity fraud checks. It is also the key in recurring payments.
// This field is mandatory in recurring payments.
let shopper_reference = "somebody@example.com".to_string();
xml.new_child2("shopperReference", &shopper_reference);
// optional
let shopper_email = "somebody@example.com".to_string();
xml.new_child2("shopperEmail", &shopper_email);
// optional, An integer value that adds up to the normal fraud score.
// The value can be either a positive or negative integer.
xml.new_child2("offset", "0");
// Apparently this is a required field.
let ship_before_date = "2019-06-04".to_string();
xml.new_child2("shipBeforeDate", &ship_before_date);
xml.sort_by_tag(true);
// Encode...
// "\" (backslash) as "\\"
// ":" (colon) as "\:"
let sb_tags = chilkat::StringBuilder::new();
let sb_values = chilkat::StringBuilder::new();
let sb_content = chilkat::StringBuilder::new();
let n = xml.num_children();
let mut i = 0;
while i < n {
if i > 0 {
let _ = sb_tags.append(":");
let _ = sb_values.append(":");
}
let _ = xml.get_child2(i);
let _ = sb_tags.append(&xml.tag());
let _ = sb_content.set_string(&xml.content());
let mut num_replaced = sb_content.replace("\\", "\\\\");
num_replaced = sb_content.replace(":", "\\:");
let _ = sb_values.append_sb(&sb_content);
let _ = xml.get_parent2();
i = i + 1;
}
let sb_signing_str = chilkat::StringBuilder::new();
let _ = sb_signing_str.append_sb(&sb_tags);
let _ = sb_signing_str.append(":");
let _ = sb_signing_str.append_sb(&sb_values);
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha256");
crypt.set_mac_algorithm("hmac");
let hmac_key = "934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739".to_string();
let _ = crypt.set_mac_key_encoded(&hmac_key, "hex");
crypt.set_encoding_mode("base64");
let merchant_sig = crypt.mac_string_enc(&sb_signing_str.get_as_string().unwrap_or_default()).unwrap_or_default();
println!("{}", merchant_sig);