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

PayPal: Record an external refund against an invoice

See more PayPal Examples

Record USD 5 already returned by bank transfer for a paid invoice. This call does not move money. To refund a PayPal capture, use the Payments v2 capture refund example instead.

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

// Record only money already returned outside PayPal. Reconcile first if a previous
// attempt timed out.

let _ = http.set_url_var("id", "INVOICE_ID");
let body = chilkat::JsonObject::new();
let _ = body.update_string("method", "BANK_TRANSFER");
let _ = body.update_string("amount.currency_code", "USD");
let _ = body.update_string("amount.value", "5.00");

// Use the actual date of the transfer; this example records one made today.
let date = chilkat::DateTime::new();
let _ = date.set_from_current_system_time();
let _ = body.update_string("refund_date", &date.get_as_iso8601("YYYY-MM-DD", false).unwrap_or_default());

if http.http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/refunds", &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!("Record ID: {}", result.string_of("refund_id").unwrap_or_default());