add example
This commit is contained in:
parent
b9845d284f
commit
15ac7ded4c
59
README.md
59
README.md
|
@ -10,7 +10,64 @@ Documentation: https://docs.rs/paypal-rs
|
||||||
|
|
||||||
Currently in early development.
|
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
|
## Testing
|
||||||
You need the enviroment variables PAYPAL_CLIENTID and PAYPAL_SECRET to be set.
|
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
|
|
@ -151,13 +151,13 @@ struct AuthAssertionClaims {
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Returns a new client, you must get_access_token afterwards to interact with the api.
|
/// 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 clientid = std::env::var("PAYPAL_CLIENTID").unwrap();
|
||||||
/// let secret = env::var("PAYPAL_SECRET").unwrap();
|
/// let secret = std::env::var("PAYPAL_SECRET").unwrap();
|
||||||
///
|
///
|
||||||
/// let mut client = Client::new(
|
/// let mut client = paypal_rs::Client::new(
|
||||||
/// clientid.as_str(),
|
/// clientid.as_str(),
|
||||||
/// secret.as_str(),
|
/// secret.as_str(),
|
||||||
/// true,
|
/// true,
|
||||||
|
|
55
src/tests.rs
55
src/tests.rs
|
@ -1,4 +1,7 @@
|
||||||
use crate::*;
|
use crate::{
|
||||||
|
orders::{Amount, Intent, OrderPayload, OrderStatus, PurchaseUnit},
|
||||||
|
Client, HeaderParams, Prefer,
|
||||||
|
};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
@ -9,30 +12,40 @@ async fn it_works() {
|
||||||
|
|
||||||
let mut client = Client::new(clientid.as_str(), secret.as_str(), true);
|
let mut client = Client::new(clientid.as_str(), secret.as_str(), true);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(client.get_access_token().await.is_err(), false, "should not error");
|
||||||
client.get_access_token().await.is_err(),
|
|
||||||
false,
|
let order = OrderPayload::new(Intent::Authorize, vec![PurchaseUnit::new(Amount::new("EUR", "10.0"))]);
|
||||||
"should not error"
|
|
||||||
|
let ref_id = format!(
|
||||||
|
"TEST-{:?}",
|
||||||
|
std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_secs()
|
||||||
);
|
);
|
||||||
|
|
||||||
let order = orders::OrderPayload::new(
|
let order_created = client
|
||||||
orders::Intent::Authorize,
|
.create_order(
|
||||||
vec![orders::PurchaseUnit::new(orders::Amount::new(
|
order,
|
||||||
"EUR", "10.0",
|
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!(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");
|
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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue