Rust Requires Chilkat v11.0.0+
Rust
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
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();
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;
}
if !result.string_of_equals("status", "DRAFT", true) {
println!("Select an unsent draft.");
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
let now = chilkat::DateTime::new();
let _ = now.set_from_current_system_time();
let stamp = chilkat::StringBuilder::new();
let _ = stamp.append(&result.string_of("detail.invoice_date").unwrap_or_default());
let _ = stamp.append("T00:00:00Z");
let issue = chilkat::DateTime::new();
let valid_date = issue.set_from_timestamp(&stamp.get_as_string().unwrap_or_default()).is_ok();
if (!valid_date) || (now.diff_seconds(&issue) < 0) {
println!("Select a draft with an issue date of today or earlier.");
return;
}
let body = chilkat::JsonObject::new();
let _ = body.update_bool("send_to_recipient", false);
let _ = body.update_bool("send_to_invoicer", false);
if http.http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send", &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 send_error = chilkat::JsonObject::new();
let _ = send_error.load(&resp.body_str());
println!("HTTP {} {} {}", resp.status_code(), send_error.string_of("name").unwrap_or_default(), send_error.string_of("details[0].issue").unwrap_or_default());
println!("PayPal debug ID: {}", send_error.string_of("debug_id").unwrap_or_default());
return;
}
// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
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 invoice_error = chilkat::JsonObject::new();
let _ = invoice_error.load(&resp.body_str());
println!("HTTP {} {} {}", resp.status_code(), invoice_error.string_of("name").unwrap_or_default(), invoice_error.string_of("details[0].issue").unwrap_or_default());
println!("PayPal debug ID: {}", invoice_error.string_of("debug_id").unwrap_or_default());
return;
}
let invoice = chilkat::JsonObject::new();
if invoice.load(&resp.body_str()).is_err() {
println!("Expected a JSON response; HTTP {}", resp.status_code());
return;
}
if ((invoice.string_of_equals("status", "SENT", true)) || ((invoice.string_of_equals("status", "UNPAID", true)) || (invoice.string_of_equals("status", "SHARED", true)))) && (invoice.has_member("detail.metadata.recipient_view_url")) {
println!("Customer payment page: {}", invoice.string_of("detail.metadata.recipient_view_url").unwrap_or_default());
return;
}
println!("Invoice is {}. Retrieve it again when payable.", invoice.string_of("status").unwrap_or_default());