diff --git a/Cargo.toml b/Cargo.toml index 2639b89..fed64fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,14 +14,14 @@ edition = "2018" [dependencies] reqwest = { version = "0.11.0", features = ["json"] } -serde = { version = "1.0.118", features = ["derive"] } +serde = { version = "1.0.119", features = ["derive"] } serde_json = "1.0.61" serde_with = "1.6.2" chrono = { version = "0.4.19", features = ["serde"] } jsonwebtoken = "7.2.0" base64 = "0.13.0" -log = "0.4.11" -bytes = "1.0.0" +log = "0.4.13" +bytes = "1.0.1" [dev-dependencies] tokio = { version = "1.0.1", features = ["macros", "rt-multi-thread"] } diff --git a/examples/invoice.rs b/examples/invoice.rs new file mode 100644 index 0000000..2c2ae71 --- /dev/null +++ b/examples/invoice.rs @@ -0,0 +1,81 @@ +use paypal_rs::{ + Client, + HeaderParams, + Prefer, + invoice::*, + common::*, + errors::*, +}; + +#[tokio::main] +async fn main() -> Result<(), ResponseError> { + dotenv::dotenv().ok(); + + let clientid = std::env::var("PAYPAL_CLIENTID").unwrap(); + let secret = std::env::var("PAYPAL_SECRET").unwrap(); + + let mut client = Client::new(clientid, secret, true); + + let payload = InvoicePayload { + detail: InvoiceDetail { + currency_code: Currency::EUR, + //reference: Some("deal-ref".to_owned()), + ..Default::default() + }, + invoicer: Some(InvoicerInfo { + name: Some(Name { + given_name: Some("Lucas".to_owned()), + prefix: None, + suffix: None, + surname: None, + full_name: None, + middle_name: None, + alternate_full_name: None, + }), + phones: None, + tax_id: None, + website: None, + business_name: "Lucas Corp".to_owned(), + logo_url: None, + // needs to be a valid address... + email_address: Some("merchant@example.com".to_owned()), + additional_notes: None, + }), + items: vec![ + Item { + id: None, + name: "My item".to_owned(), + unit_amount: Money { + currency_code: Currency::EUR, + value: "10.0".to_owned() + }, + quantity: "1".to_owned(), + discount: None, + item_date: None, + description: Some("A random item".to_owned()), + tax: Some(Tax { + name: "Sales tax".to_owned(), + percent: "7".to_owned(), + amount: None, + }), + unit_of_measure: Some(UnitOfMeasure::Quantity), + } + ], + ..Default::default() + }; + match client.create_draft_invoice(payload, HeaderParams::default()).await { + Ok(r) => { + println!("{:#?}", r); + }, + Err(ResponseError::HttpError(e)) => { + println!("{}", e); + }, + Err(e) => { + println!("{:#?}", e); + } + } + + // some stuff is not sent when representation is minimal. + + Ok(()) +} diff --git a/src/common.rs b/src/common.rs index 72b92d5..a485802 100644 --- a/src/common.rs +++ b/src/common.rs @@ -94,7 +94,7 @@ pub struct LinkDescription { /// The complete target URL. pub href: String, /// The link relation type, which serves as an ID for a link that unambiguously describes the semantics of the link. - pub rel: String, + pub rel: Option, /// The HTTP method required to make the related call. pub method: Option, } diff --git a/src/errors.rs b/src/errors.rs index cfb1523..dd9ba98 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -11,9 +11,9 @@ pub struct PaypalError { /// The error name. pub name: String, /// The error message. - pub message: String, + pub message: Option, /// Paypal debug id - pub debug_id: String, + pub debug_id: Option, /// Error details pub details: Vec>, /// Only available on Identity errors diff --git a/src/invoice.rs b/src/invoice.rs index 244a3a3..3bf24de 100644 --- a/src/invoice.rs +++ b/src/invoice.rs @@ -113,7 +113,7 @@ pub struct InvoiceDetail { /// The reference data. Includes a post office (PO) number. pub reference: Option, /// The three-character ISO-4217 currency code that identifies the currency. - pub currency_code: String, + pub currency_code: Currency, /// A note to the invoice recipient. Also appears on the invoice notification email. pub note: Option, /// The general terms of the invoice. Can include return or cancellation policy and other terms and conditions. @@ -123,13 +123,13 @@ pub struct InvoiceDetail { /// An array of PayPal IDs for the files that are attached to an invoice. pub attachments: Option>, /// The invoice number. Default is the number that is auto-incremented number from the last number. - pub invoice_number: String, + pub invoice_number: Option, /// The invoice date as specificed by the sender - pub invoice_date: chrono::NaiveDate, + pub invoice_date: Option>, /// The payment due date for the invoice. pub payment_term: Option, /// The audit metadata - pub metadata: Metadata, + pub metadata: Option, } /// A name to be used as recipient, etc. @@ -258,7 +258,7 @@ pub struct Discount { /// The unit of measure for the invoiced item. #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] -pub enum UnitOffMeasure { +pub enum UnitOfMeasure { /// The unit of measure is quantity. This invoice template is typically used for physical goods. Quantity, /// The unit of measure is hours. This invoice template is typically used for services. @@ -289,7 +289,7 @@ pub struct Item { /// Discount as a percent or amount at invoice level. The invoice discount amount is subtracted from the item total. pub discount: Option, /// The unit of measure for the invoiced item. For AMOUNT the unit_amount and quantity are not shown on the invoice. - pub unit_of_measure: Option, + pub unit_of_measure: Option, } /// The partial payment details. @@ -369,7 +369,7 @@ pub struct Breakdown { #[derive(Debug, Default, Serialize, Deserialize)] pub struct Amount { /// The [three-character ISO-4217 currency code](https://developer.paypal.com/docs/integration/direct/rest/currency-codes/) that identifies the currency. - pub currency_code: String, + pub currency_code: Currency, /// The value, which might be: /// - An integer for currencies like JPY that are not typically fractional. /// - A decimal fraction for currencies like TND that are subdivided into thousandths. @@ -382,9 +382,9 @@ pub struct Amount { impl Amount { /// Creates a new amount with the required values. - pub fn new>(currency_code: S, value: S) -> Self { + pub fn new(currency_code: Currency, value: &str) -> Self { Amount { - currency_code: currency_code.into(), + currency_code, value: value.into(), breakdown: None, } @@ -690,8 +690,9 @@ impl super::Client { let res = build.json(&invoice).send().await?; if res.status().is_success() { - let x = res.json::().await?; - Ok(x) + println!("{:#?}", res.text().await?); + //let x = res.json::().await?; + Ok(()) } else { Err(res.json::().await?.into()) } @@ -911,4 +912,5 @@ mod tests { println!("{:?}", list); } -} \ No newline at end of file +} +