From 4bee3ed0010e2a0977b6eff7a7538fbbbccdd0ed Mon Sep 17 00:00:00 2001 From: Chris Kruger Date: Fri, 22 Dec 2023 06:31:42 +0800 Subject: [PATCH] add balance --- examples/charge.rs | 22 ++++++++++++++-- src/client/async_std.rs | 2 +- src/client/pinpayments.rs | 2 +- src/params.rs | 4 +-- src/resources.rs | 2 ++ src/resources/balance.rs | 23 +++++++++++++++++ tests/balance.rs | 45 +++++++++++++++++++++++++++++++++ tests/fixtures/get-balance.json | 16 ++++++++++++ 8 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 src/resources/balance.rs create mode 100644 tests/balance.rs create mode 100644 tests/fixtures/get-balance.json diff --git a/examples/charge.rs b/examples/charge.rs index 09bec76..4f7967b 100644 --- a/examples/charge.rs +++ b/examples/charge.rs @@ -1,9 +1,10 @@ -use pinpayments::{Client, Charge}; +use pinpayments::{Client, Charge, ChargeSearchParams, SortByField, SortDirection}; +//use time::macros::datetime; #[tokio::main] async fn main() { let secret_key = std::env::var("PINPAYMENTS_SECRET_KEY").expect("Missing PINPAYMENTS_SECRET_KEY in env"); - let client = Client::new(secret_key); + let client = Client::from_url(pinpayments::DEFAULT_TEST_API_BASE_URL, secret_key); let charges = match Charge::list(&client, None, None).await { Ok(c) => Some(c), @@ -25,6 +26,23 @@ async fn main() { }; println!("Result is {charge:?}"); + +// let search_params = ChargeSearchParams { +// query: Some("Kruger"), +// start_date: Some(datetime!(2017-02-24 3:10:49 UTC)), +// end_date: None, +// sort_by: Some(SortByField::CreatedAt), +// direction: Some(SortDirection::Asc), +// page: None, +// per_page: None +// }; +// + let search_params = ChargeSearchParams { ..Default::default() }; + + let searched_charges = Charge::search(&client, search_params).await.unwrap(); + + println!("Search for charges {searched_charges:?}"); + // let charge = Charge::create( // &client, // CreateCharge { diff --git a/src/client/async_std.rs b/src/client/async_std.rs index 2e240ba..95a2f1a 100644 --- a/src/client/async_std.rs +++ b/src/client/async_std.rs @@ -20,7 +20,7 @@ pub(crate) fn err(err: PinError) -> Response { Box::pin(future::ready(Err(err))) } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct BaseClient { client: surf::Client, } diff --git a/src/client/pinpayments.rs b/src/client/pinpayments.rs index 22ffc1a..61e9c58 100644 --- a/src/client/pinpayments.rs +++ b/src/client/pinpayments.rs @@ -15,7 +15,7 @@ const USER_AGENT: &str = concat!("PinPayments/1 RustClient/", env!("CARGO_PKG_VE pub const DEFAULT_API_BASE_URL: &str = "https://api.pinpayments.com/1/"; pub const DEFAULT_TEST_API_BASE_URL: &str = "https://test-api.pinpayments.com/1/"; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Client { client: crate::client::BaseClient, secret_key: String, diff --git a/src/params.rs b/src/params.rs index 6874f71..9de09e6 100644 --- a/src/params.rs +++ b/src/params.rs @@ -10,7 +10,7 @@ use crate::{ client::{Response}, }; -#[derive(Clone, Default)] +#[derive(Clone, Debug, Default)] pub struct AppInfo { pub name: String, pub url: Option, @@ -28,7 +28,7 @@ impl ToString for AppInfo { } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Headers { pub user_agent: String, } diff --git a/src/resources.rs b/src/resources.rs index 6af72a4..a34743f 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -2,8 +2,10 @@ mod currency; mod charge; mod card; mod refund; +mod balance; pub use currency::*; pub use charge::*; pub use card::*; pub use refund::*; +pub use balance::*; diff --git a/src/resources/balance.rs b/src/resources/balance.rs new file mode 100644 index 0000000..5ca895c --- /dev/null +++ b/src/resources/balance.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize}; + +use crate::client::{Client, Response}; +use crate::params::{unpack_contained}; +use crate::resources::{Currency}; + +#[derive(Clone, Debug, Deserialize)] +pub struct AmountCurrency { + pub amount: i64, + pub currency: Currency +} + +#[derive(Clone, Debug, Deserialize)] +pub struct Balance { + pub available: Vec, + pub pending: Vec +} + +impl Balance { + pub fn retrieve(client: &Client) -> Response { + unpack_contained(client.get("/balance")) + } +} diff --git a/tests/balance.rs b/tests/balance.rs new file mode 100644 index 0000000..4c196f3 --- /dev/null +++ b/tests/balance.rs @@ -0,0 +1,45 @@ +use pinpayments::{Client, Currency, Balance}; +use std::fs::File; +use httptest::{ServerPool, Expectation, matchers::*, responders::*}; +use surf::http::auth::BasicAuth; + +static SERVER_POOL: ServerPool = ServerPool::new(1); + +fn get_fixture(path: &str) -> serde_json::Value { + let file = File::open(path) + .expect("file should open read only"); + serde_json::from_reader(file).expect("file should be JSON") +} + +#[tokio::test] +async fn get_balance_test() { + let json = get_fixture("tests/fixtures/get-balance.json"); + + println!("{json}"); + + let auth = BasicAuth::new("sk_test_12345", ""); + + let server = SERVER_POOL.get_server(); + + server.expect( + Expectation::matching( + all_of![ + request::method_path("GET", "/1/balance"), + request::headers( + contains((String::from(auth.name().as_str()), String::from(auth.value().as_str()))) + ), + ]). + respond_with(json_encoded(json)) + ); + + let client = Client::from_url(server.url_str("/1/").as_str(), "sk_test_12345"); + + let balance = Balance::retrieve(&client) + .await + .unwrap(); + + assert_eq!(balance.available[0].amount, 400); + assert_eq!(balance.available[0].currency, Currency::AUD); + assert_eq!(balance.pending[0].amount, 1200); + assert_eq!(balance.pending[0].currency, Currency::AUD); +} diff --git a/tests/fixtures/get-balance.json b/tests/fixtures/get-balance.json new file mode 100644 index 0000000..68c596a --- /dev/null +++ b/tests/fixtures/get-balance.json @@ -0,0 +1,16 @@ +{ + "response": { + "available": [ + { + "amount": 400, + "currency": "AUD" + } + ], + "pending": [ + { + "amount": 1200, + "currency": "AUD" + } + ] + } +}