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

PayPal - Get Sale Details

See more PayPal Examples

Shows details for a sale transaction, by ID.

Chilkat Rust Downloads

Rust

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Load our previously obtained access token. (see PayPal OAuth2 Access Token)
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/paypal.json");

// Build the Authorization request header field value.
let sb_auth = chilkat::StringBuilder::new();
// token_type should be "Bearer"
let _ = sb_auth.append(&json_token.string_of("token_type").unwrap_or_default());
let _ = sb_auth.append(" ");
let _ = sb_auth.append(&json_token.string_of("access_token").unwrap_or_default());

// Make the initial connection.
// A single REST object, once connected, can be used for many PayPal REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
let rest = chilkat::Rest::new();
let b_auto_reconnect = true;
if rest.connect("api.sandbox.paypal.com", 443, true, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// ----------------------------------------------------------------------------------------------
// The code above this comment could be placed inside a function/subroutine within the application
// because the connection does not need to be made for every request.  Once the connection is made
// the app may send many requests..
// ----------------------------------------------------------------------------------------------

// Clear the REST object of any headers or query params from previous requests.
let _ = rest.clear_all_headers();
let _ = rest.clear_all_query_params();

let _ = rest.add_header("Authorization", &sb_auth.get_as_string().unwrap_or_default());

// The sale ID is added to the path.
// The sale ID is obtained from payment records as shown here: Find Completed Sales
let sb_path = chilkat::StringBuilder::new();
let _ = sb_path.append("/v1/payments/sale/sale_id");
let num_replaced = sb_path.replace("sale_id", "1XC17935MN507400J");

// Send the GET request and get the JSON response.
let sb_json_response = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("GET", &sb_path.get_as_string().unwrap_or_default(), &sb_json_response).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load_sb(&sb_json_response);

println!("Response Status Code = {}", rest.response_status_code());

// Did we get a 200 success response?
if rest.response_status_code() != 200 {
    println!("{}", json.emit().unwrap_or_default());
    println!("Failed.");
    return;
}

// Sample JSON response is shown below.
println!("{}", json.emit().unwrap_or_default());
println!("----------------------");

// Show how to get a timestamp, such as update_time.
let date_time = chilkat::DateTime::new();
let _ = date_time.set_from_timestamp(&json.string_of("update_time").unwrap_or_default()).is_ok();
println!("utc update_time: {}", date_time.get_as_timestamp(false).unwrap_or_default());
println!("local update_time: {}", date_time.get_as_timestamp(true).unwrap_or_default());
println!("utc rfc822: {}", date_time.get_as_rfc822(false).unwrap_or_default());
println!("local rfc822: {}", date_time.get_as_rfc822(true).unwrap_or_default());

let dt_utc = chilkat::DtObj::new();
date_time.to_dt_obj(false, &dt_utc);

println!("{}/{}/{}", dt_utc.month(), dt_utc.day(), dt_utc.year());

println!("success");

// ------------------------------------------------
// A sample JSON response:

// 	{ 
// 	  "id": "1XC17935MN507400J",
// 	  "create_time": "2016-11-25T15:42:50Z",
// 	  "update_time": "2016-11-25T15:42:56Z",
// 	  "amount": { 
// 	    "total": "289.00",
// 	    "currency": "USD"
// 	  },
// 	  "payment_mode": "INSTANT_TRANSFER",
// 	  "state": "completed",
// 	  "protection_eligibility": "ELIGIBLE",
// 	  "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
// 	  "parent_payment": "PAY-2AA764602T410180HLA4FX6Q",
// 	  "transaction_fee": { 
// 	    "value": "8.68",
// 	    "currency": "USD"
// 	  },
// 	  "links": [
// 	    { 
// 	      "href": "https://api.sandbox.paypal.com/v1/payments/sale/1XC17935MN507400J",
// 	      "rel": "self",
// 	      "method": "GET"
// 	    },
// 	    { 
// 	      "href": "https://api.sandbox.paypal.com/v1/payments/sale/1XC17935MN507400J/refund",
// 	      "rel": "refund",
// 	      "method": "POST"
// 	    },
// 	    { 
// 	      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2AA764602T410180HLA4FX6Q",
// 	      "rel": "parent_payment",
// 	      "method": "GET"
// 	    }
// 	  ]
// 	}