Rust Requires Chilkat v11.0.0+
Rust
PayPal: Pay with a previously saved card
See more PayPal Examples
Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.
When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.
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();
// Resolve the saved token under this signed-in buyer. A vault ID from a different
// customer must never be accepted from the browser.
// Create and persist this key once per business operation. On a timeout,
// retry the same request with its saved key, not a newly generated one.
let crypt = chilkat::Crypt2::new();
let request_id = crypt.generate_uuid().unwrap_or_default();
http.set_request_header("PayPal-Request-Id", &request_id);
http.set_request_header("Prefer", "return=representation");
let body = chilkat::JsonObject::new();
let _ = body.update_string("intent", "CAPTURE");
let _ = body.update_string("purchase_units[0].amount.currency_code", "USD");
let _ = body.update_string("purchase_units[0].amount.value", "30.00");
let _ = body.update_string("payment_source.card.vault_id", "SAVED_CARD_TOKEN");
let _ = body.update_string("payment_source.card.stored_credential.payment_initiator", "CUSTOMER");
let _ = body.update_string("payment_source.card.stored_credential.payment_type", "ONE_TIME");
let _ = body.update_string("payment_source.card.stored_credential.usage", "SUBSEQUENT");
let _ = body.update_string("payment_source.card.experience_context.return_url", "https://merchant.example/paypal/return");
let _ = body.update_string("payment_source.card.experience_context.cancel_url", "https://merchant.example/paypal/cancel");
if http.http_json("POST", "https://api-m.sandbox.paypal.com/v2/checkout/orders", &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());
// Link order is not fixed. Send the buyer to the matching URL in a browser.
let mut link_index = 0;
while link_index < result.size_of_array("links") {
result.set_i(link_index);
if result.string_of_equals("links[i].rel", "payer-action", true) {
println!("Approval URL: {}", result.string_of("links[i].href").unwrap_or_default());
}
link_index = link_index + 1;
}
// A purchase unit may have no captures yet, or more than one.
let mut i = 0;
while i < result.size_of_array("purchase_units") {
result.set_i(i);
let mut j = 0;
while j < result.size_of_array("purchase_units[i].payments.captures") {
result.set_j(j);
println!("Unit: {} capture: {}", result.string_of("purchase_units[i].reference_id").unwrap_or_default(), result.string_of("purchase_units[i].payments.captures[j].id").unwrap_or_default());
println!(" {} {} {}", result.string_of("purchase_units[i].payments.captures[j].status").unwrap_or_default(), result.string_of("purchase_units[i].payments.captures[j].amount.value").unwrap_or_default(), result.string_of("purchase_units[i].payments.captures[j].amount.currency_code").unwrap_or_default());
j = j + 1;
}
i = i + 1;
}