add example

This commit is contained in:
Edgar 2020-06-10 13:03:18 +02:00
parent b9845d284f
commit 15ac7ded4c
No known key found for this signature in database
GPG key ID: 8731E6C0166EAA85
3 changed files with 96 additions and 26 deletions

View file

@ -10,7 +10,64 @@ Documentation: https://docs.rs/paypal-rs
Currently in early development.
## Example
```rust
use paypal_rs::{
Client,
HeaderParams,
Prefer,
orders::{OrderPayload, Intent, PurchaseUnit, Amount}
};
#[tokio::main]
async fn main() {
let clientid = std::env::var("PAYPAL_CLIENTID").unwrap();
let secret = std::env::var("PAYPAL_SECRET").unwrap();
let mut client = Client::new(clientid.as_str(), secret.as_str(), true);
client.get_access_token().await.unwrap();
let order_payload = OrderPayload::new(
Intent::Authorize,
vec![PurchaseUnit::new(Amount::new(
"EUR", "10.0",
))],
);
let order = client
.create_order(
order_payload,
HeaderParams {
prefer: Some(Prefer::Representation),
..Default::default()
},
)
.await
.unwrap();
}
```
## Testing
You need the enviroment variables PAYPAL_CLIENTID and PAYPAL_SECRET to be set.
`cargo test --lib`
`cargo test --lib`
## TODO
- [ ] Orders API
- [ ] Invoicing API
- [ ] Tracking API
- [ ] Subscriptions API
- [ ] Identity API
- [ ] Disputes API
- [ ] Catalog Products API
- [ ] Partner Referrals API
- [ ] Payment Experience Web Profiles API
- [ ] Payments API
- [ ] Payouts API
- [ ] Transaction Search API
- [ ] Referenced Payouts API
- [ ] Vault API
- [ ] Webhooks Management API

View file

@ -151,13 +151,13 @@ struct AuthAssertionClaims {
impl Client {
/// Returns a new client, you must get_access_token afterwards to interact with the api.
///
/// Example:
/// # Examples
///
/// ```
/// let clientid = env::var("PAYPAL_CLIENTID").unwrap();
/// let secret = env::var("PAYPAL_SECRET").unwrap();
/// let clientid = std::env::var("PAYPAL_CLIENTID").unwrap();
/// let secret = std::env::var("PAYPAL_SECRET").unwrap();
///
/// let mut client = Client::new(
/// let mut client = paypal_rs::Client::new(
/// clientid.as_str(),
/// secret.as_str(),
/// true,

View file

@ -1,4 +1,7 @@
use crate::*;
use crate::{
orders::{Amount, Intent, OrderPayload, OrderStatus, PurchaseUnit},
Client, HeaderParams, Prefer,
};
use std::env;
#[tokio::test]
@ -9,30 +12,40 @@ async fn it_works() {
let mut client = Client::new(clientid.as_str(), secret.as_str(), true);
assert_eq!(
client.get_access_token().await.is_err(),
false,
"should not error"
assert_eq!(client.get_access_token().await.is_err(), false, "should not error");
let order = OrderPayload::new(Intent::Authorize, vec![PurchaseUnit::new(Amount::new("EUR", "10.0"))]);
let ref_id = format!(
"TEST-{:?}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
);
let order = orders::OrderPayload::new(
orders::Intent::Authorize,
vec![orders::PurchaseUnit::new(orders::Amount::new(
"EUR", "10.0",
))],
);
let order_created = client
.create_order(
order,
HeaderParams {
prefer: Some(Prefer::Representation),
request_id: Some(ref_id.clone()),
..Default::default()
},
)
.await
.unwrap();
let ref_id = format!("TEST-{:?}", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs());
let order_created = client.create_order(order, HeaderParams {
prefer: Some(Prefer::Representation),
request_id: Some(ref_id.clone()),
..Default::default()
}).await.unwrap();
assert!(order_created.id != "", "order id is not empty");
assert_eq!(order_created.status, orders::OrderStatus::Created, "order status is created");
assert_eq!(order_created.status, OrderStatus::Created, "order status is created");
assert_eq!(order_created.links.len(), 4, "order links exist");
client.update_order(order_created.id, Some(orders::Intent::Capture), Some(order_created.purchase_units.expect("to exist"))).await.unwrap();
client
.update_order(
order_created.id,
Some(Intent::Capture),
Some(order_created.purchase_units.expect("to exist")),
)
.await
.unwrap();
}