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

PayPal: Remove an external payment recorded on the wrong invoice

See more PayPal Examples

Use the saved external payment record ID. Verify that it belongs to the selected invoice, then remove the bookkeeping entry. The bank transfer itself is unaffected. Record it on the correct invoice as a separate step.

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

let _ = http.set_url_var("id", "INVOICE_ID");

if http.http_no_body("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", &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 payment_id = "EXTERNAL_PAYMENT_ID".to_string();
let mut matched = false;
let mut i = 0;
while i < result.size_of_array("payments.transactions") {
    result.set_i(i);
    if (result.string_of_equals("payments.transactions[i].payment_id", &payment_id, true)) && (result.string_of_equals("payments.transactions[i].type", "EXTERNAL", true)) {
        matched = true;
    }

    i = i + 1;
}

if !matched {
    println!("This invoice has no matching external payment record.");
    return;
}

let _ = http.set_url_var("paymentId", &payment_id);
if http.http_no_body("DELETE", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}", &resp).is_err() {
    println!("No HTTP response: {}", http.last_error_text());
    return;
}

if (resp.status_code() < 200) || (resp.status_code() >= 300) {
    let remove_error = chilkat::JsonObject::new();
    let _ = remove_error.load(&resp.body_str());
    println!("HTTP {} {} {}", resp.status_code(), remove_error.string_of("name").unwrap_or_default(), remove_error.string_of("details[0].issue").unwrap_or_default());
    println!("PayPal debug ID: {}", remove_error.string_of("debug_id").unwrap_or_default());
    return;
}

println!("External payment entry removed.");