Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

PayPal: Create an itemized draft invoice

See more PayPal Examples

Bill a sandbox recipient for two inspections at USD 45 each, due in 30 days. Request the invoice representation and save its ID for sending. Creating the draft does not notify the customer.

PayPal documentation

Chilkat Rust Downloads

Rust

// 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();

// Replace buyer@example.com with your sandbox buyer. PayPal uses the authenticated
// merchant as the invoicer.
http.set_request_header("Prefer", "return=representation");
let body = chilkat::JsonObject::new();
let _ = body.update_string("detail.currency_code", "USD");
let _ = body.update_string("detail.payment_term.term_type", "NET_30");
let _ = body.update_string("primary_recipients[0].billing_info.email_address", "buyer@example.com");
let _ = body.update_string("items[0].name", "Equipment inspection");
let _ = body.update_string("items[0].quantity", "2");
let _ = body.update_string("items[0].unit_amount.currency_code", "USD");
let _ = body.update_string("items[0].unit_amount.value", "45.00");

if http.http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices", &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;
}

println!("ID: {}", result.string_of("id").unwrap_or_default());
println!("Status: {}", result.string_of("status").unwrap_or_default());
println!("Invoice number: {}", result.string_of("detail.invoice_number").unwrap_or_default());
println!("Total: {}", result.string_of("amount.value").unwrap_or_default());