Rust Requires Chilkat v11.0.0+
Rust
PayPal: Save a payable invoice QR code as a PNG
See more PayPal Examples
Generate a 300-pixel payment QR code for a sent, payable invoice. Decode the base64 image into a PNG for a printed invoice; the code opens the payment page, and is not proof of payment.
Chilkat Rust Downloads
// See Unlock Chilkat before making requests.
// For the merchant's internal backoffice application. Load the sandbox token
// from the merchant's credential store. Keep it out of publicly distributed apps.
// See Get a PayPal access token.
let http = chilkat::Http::new();
http.set_auth_token("SANDBOX_ACCESS_TOKEN");
let resp = chilkat::HttpResponse::new();
// Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
let _ = http.set_url_var("id", "INVOICE_ID");
let body = chilkat::JsonObject::new();
let _ = body.update_int("width", 300);
let _ = body.update_int("height", 300);
let _ = body.update_string("action", "pay");
if http.http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code", &body, "application/json", &resp).is_err() {
println!("No HTTP response: {}", http.last_error_text());
return;
}
if (resp.status_code() < 200) || (resp.status_code() >= 300) {
let error_info = chilkat::JsonObject::new();
let _ = error_info.load(&resp.body_str());
println!("HTTP {} {} {}", resp.status_code(), error_info.string_of("name").unwrap_or_default(), error_info.string_of("details[0].issue").unwrap_or_default());
println!("PayPal debug ID: {}", error_info.string_of("debug_id").unwrap_or_default());
return;
}
let result = chilkat::JsonObject::new();
if result.load(&resp.body_str()).is_err() {
println!("Expected a JSON response; HTTP {}", resp.status_code());
return;
}
let png = chilkat::BinData::new();
if !result.has_member("image") {
println!("QR response did not contain an image.");
return;
}
if png.append_encoded(&result.string_of("image").unwrap_or_default(), "base64").is_err() {
println!("Invalid base64 image.");
return;
}
if png.write_file("paypal-invoice-qr.png").is_err() {
println!("Could not write the invoice QR image.");
return;
}
println!("Saved paypal-invoice-qr.png.");