Skip to content

Commit

Permalink
add balance
Browse files Browse the repository at this point in the history
  • Loading branch information
montdidier committed Dec 21, 2023
1 parent 079bda0 commit 4bee3ed
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 6 deletions.
22 changes: 20 additions & 2 deletions examples/charge.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/client/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn err<T: Send + 'static>(err: PinError) -> Response<T> {
Box::pin(future::ready(Err(err)))
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct BaseClient {
client: surf::Client,
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/pinpayments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
client::{Response},
};

#[derive(Clone, Default)]
#[derive(Clone, Debug, Default)]
pub struct AppInfo {
pub name: String,
pub url: Option<String>,
Expand All @@ -28,7 +28,7 @@ impl ToString for AppInfo {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Headers {
pub user_agent: String,
}
Expand Down
2 changes: 2 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
23 changes: 23 additions & 0 deletions src/resources/balance.rs
Original file line number Diff line number Diff line change
@@ -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<AmountCurrency>,
pub pending: Vec<AmountCurrency>
}

impl Balance {
pub fn retrieve(client: &Client) -> Response<Balance> {
unpack_contained(client.get("/balance"))
}
}
45 changes: 45 additions & 0 deletions tests/balance.rs
Original file line number Diff line number Diff line change
@@ -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);
}
16 changes: 16 additions & 0 deletions tests/fixtures/get-balance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"response": {
"available": [
{
"amount": 400,
"currency": "AUD"
}
],
"pending": [
{
"amount": 1200,
"currency": "AUD"
}
]
}
}

0 comments on commit 4bee3ed

Please sign in to comment.