Skip to content

Commit 7ed60e5

Browse files
committed
feat: added support for the subaccounts api route
1 parent 3cdefcc commit 7ed60e5

16 files changed

Lines changed: 280 additions & 45 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The client currently covers the following section of the API, and the sections t
2020
- [x] Customers
2121
- [x] Dedicated Virtual Account
2222
- [x] Apple Pay
23-
- [ ] Subaccounts
23+
- [x] Subaccounts
2424
- [ ] Plans
2525
- [ ] Subscriptions
2626
- [ ] Transfer Recipients

src/endpoints/apple_pay.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Apple Pay
22
//! THe Apple Pay API allows you register your application's top-level domain or subdomain.
33
4-
use super::BASE_URL;
4+
use super::PAYSTACK_BASE_URL;
55
use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult};
66
use serde_json::json;
77
use std::{marker::PhantomData, sync::Arc};
@@ -27,7 +27,7 @@ impl<T: HttpClient + Default> ApplePayEndpoints<T> {
2727
/// # Returns
2828
/// A new ApplePayEndpoints instance
2929
pub fn new(key: Arc<String>, http: Arc<T>) -> ApplePayEndpoints<T> {
30-
let base_url = format!("{}/apple-pay/domain", BASE_URL);
30+
let base_url = format!("{}/apple-pay/domain", PAYSTACK_BASE_URL);
3131
ApplePayEndpoints {
3232
key: key.to_string(),
3333
base_url,

src/endpoints/customers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! =========
33
//! Thse Customers API allows you to create and maange customers on your integration
44
5-
use super::BASE_URL;
5+
use super::PAYSTACK_BASE_URL;
66
use crate::{
77
CreateCustomerRequest, CustomerResponseData, HttpClient, PaystackAPIError, PaystackResult,
88
Response, RiskAction, UpdateCustomerRequest, ValidateCustomerRequest,
@@ -31,7 +31,7 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
3131
/// # Returns
3232
/// A new CustomersEndpoints instance
3333
pub fn new(key: Arc<String>, http: Arc<T>) -> CustomersEndpoints<T> {
34-
let base_url = format!("{}/customer", BASE_URL);
34+
let base_url = format!("{}/customer", PAYSTACK_BASE_URL);
3535
CustomersEndpoints {
3636
key: key.to_string(),
3737
base_url,

src/endpoints/dedicated_virtual_account.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! =========================
33
//! The Dedicated Virtual Account API enables Nigerian and Ghanaian merchants to manage unique payment accounts of their customers.
44
5-
use super::BASE_URL;
5+
use super::PAYSTACK_BASE_URL;
66
use crate::{
77
BankProviderData, DedicatedVirtualAccountRequest, DedicatedVirtualAccountResponseData,
88
HttpClient, ListDedicatedAccountFilter, PaystackAPIError, PaystackResult, Response,
@@ -29,7 +29,7 @@ impl<T: HttpClient + Default> DedicatedVirtualAccountEndpoints<T> {
2929
/// # Returns
3030
/// A new DedicatedVirtualAccountEndpoints instance
3131
pub fn new(key: Arc<String>, http: Arc<T>) -> DedicatedVirtualAccountEndpoints<T> {
32-
let base_url = format!("{}/dedicated_account", BASE_URL);
32+
let base_url = format!("{}/dedicated_account", PAYSTACK_BASE_URL);
3333
DedicatedVirtualAccountEndpoints {
3434
key: key.to_string(),
3535
base_url,

src/endpoints/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ pub use transaction_split::*;
1818
pub use virtual_terminal::*;
1919

2020
// Const for the base url, since it is used multiple times
21-
pub const BASE_URL: &str = "https://api.paystack.co";
21+
pub const PAYSTACK_BASE_URL: &str = "https://api.paystack.co";

src/endpoints/subaccount.rs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! The Subaccounts API allows you to create and manage subaccounts on your integration.
44
//! Subaccounts can be used to split payment between two accounts (your main account and a subaccount).
55
6-
use super::BASE_URL;
6+
use super::PAYSTACK_BASE_URL;
77
use crate::{
8-
HttpClient, PaystackAPIError, PaystackResult, Response, SubaccountRequest,
8+
CreateSubaccountRequest, HttpClient, PaystackAPIError, PaystackResult, Response,
99
SubaccountsResponseData,
1010
};
1111
use std::sync::Arc;
@@ -31,7 +31,7 @@ impl<T: HttpClient + Default> SubaccountEndpoints<T> {
3131
/// # Returns
3232
/// A new SubaccountEndpoints instance
3333
pub fn new(key: Arc<String>, http: Arc<T>) -> SubaccountEndpoints<T> {
34-
let base_url = format!("{}/subaccount", BASE_URL);
34+
let base_url = format!("{}/subaccount", PAYSTACK_BASE_URL);
3535
SubaccountEndpoints {
3636
key: key.to_string(),
3737
base_url,
@@ -43,13 +43,13 @@ impl<T: HttpClient + Default> SubaccountEndpoints<T> {
4343
///
4444
/// # Arguments
4545
/// * `subaccount_request` - The request data to create the subaccount.
46-
/// It should be created with the `SubaccountRequestBuilder` struct.
46+
/// It should be created with the `CreateSubaccountRequestBuilder` struct.
4747
///
4848
/// # Returns
4949
/// A Result containing the subaccount response data or an error
5050
pub async fn create_subaccount(
5151
&self,
52-
subaccount_request: SubaccountRequest,
52+
subaccount_request: CreateSubaccountRequest,
5353
) -> PaystackResult<SubaccountsResponseData> {
5454
let url = &self.base_url;
5555
let body = serde_json::to_value(subaccount_request)
@@ -80,6 +80,76 @@ impl<T: HttpClient + Default> SubaccountEndpoints<T> {
8080
page: Option<u32>,
8181
) -> PaystackResult<Vec<SubaccountsResponseData>> {
8282
let url = self.base_url.to_string();
83-
todo!()
83+
84+
let per_page = per_page.unwrap_or(50).to_string();
85+
let page = page.unwrap_or(1).to_string();
86+
let query = vec![("perPage", per_page.as_str()), ("page", page.as_str())];
87+
88+
let response = self
89+
.http
90+
.get(&url, &self.key, Some(&query))
91+
.await
92+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
93+
94+
let parsed_response: Response<Vec<SubaccountsResponseData>> =
95+
serde_json::from_str(&response)
96+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
97+
98+
Ok(parsed_response)
99+
}
100+
101+
/// Get the details of a subaccount on your integration
102+
///
103+
/// # Arguments
104+
/// * `id_or_code` - The subaccount ID or code you want to fetch
105+
///
106+
/// # Returns
107+
/// A Result containing the details of the subaccount or an error.
108+
pub async fn fetch_subaccount(
109+
&self,
110+
id_or_code: String,
111+
) -> PaystackResult<SubaccountsResponseData> {
112+
let url = format!("{}/{}", self.base_url, id_or_code);
113+
114+
let response = self
115+
.http
116+
.get(&url, &self.key, None)
117+
.await
118+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
119+
120+
let parsed_response: Response<SubaccountsResponseData> = serde_json::from_str(&response)
121+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
122+
123+
Ok(parsed_response)
124+
}
125+
126+
/// Update a subaccount details in your integration
127+
///
128+
/// # Arguments
129+
/// * `id_or_code` - Subaccount's ID or code
130+
/// * `update_request` - The request data to update the subaccount.
131+
/// It should be created with the `CreateSubaccountRequestBuilder` struct.
132+
///
133+
/// # Returns
134+
/// A Result containing the updated subaccount response data or an error
135+
pub async fn update_subaccount(
136+
&self,
137+
id_or_code: String,
138+
update_request: CreateSubaccountRequest,
139+
) -> PaystackResult<SubaccountsResponseData> {
140+
let url = format!("{}/{}", self.base_url, id_or_code);
141+
let body = serde_json::to_value(update_request)
142+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
143+
144+
let response = self
145+
.http
146+
.put(&url, &self.key, &body)
147+
.await
148+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
149+
150+
let parsed_response: Response<SubaccountsResponseData> = serde_json::from_str(&response)
151+
.map_err(|e| PaystackAPIError::Subaccount(e.to_string()))?;
152+
153+
Ok(parsed_response)
84154
}
85155
}

src/endpoints/terminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use std::{marker::PhantomData, sync::Arc};
1111

12-
use super::BASE_URL;
12+
use super::PAYSTACK_BASE_URL;
1313

1414
/// A struct to hold all the functions of the terminal API endpoint
1515
#[derive(Debug, Clone)]
@@ -32,7 +32,7 @@ impl<T: HttpClient + Default> TerminalEndpoints<T> {
3232
/// # Returns
3333
/// A new TerminalEndpoints instance
3434
pub fn new(key: Arc<String>, http: Arc<T>) -> TerminalEndpoints<T> {
35-
let base_url = format!("{}/terminal", BASE_URL);
35+
let base_url = format!("{}/terminal", PAYSTACK_BASE_URL);
3636
TerminalEndpoints {
3737
key: key.to_string(),
3838
base_url,

src/endpoints/transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! =============
33
//! The Transaction route allows to create and manage payments on your integration.
44
5-
use super::BASE_URL;
5+
use super::PAYSTACK_BASE_URL;
66
use crate::{
77
ChargeRequest, ChargeResponseData, Currency, ExportTransactionData, HttpClient,
88
PartialDebitTransactionRequest, PaystackAPIError, PaystackResult, Response, Status,
@@ -32,7 +32,7 @@ impl<T: HttpClient + Default> TransactionEndpoints<T> {
3232
/// # Returns
3333
/// A new TransactionEndpoints instance
3434
pub fn new(key: Arc<String>, http: Arc<T>) -> TransactionEndpoints<T> {
35-
let base_url = format!("{}/transaction", BASE_URL);
35+
let base_url = format!("{}/transaction", PAYSTACK_BASE_URL);
3636
TransactionEndpoints {
3737
key: key.to_string(),
3838
base_url,

src/endpoints/transaction_split.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! The Transaction Splits API enables merchants split the settlement for a
44
//! transaction across their payout account, and one or more subaccounts.
55
6-
use super::BASE_URL;
6+
use super::PAYSTACK_BASE_URL;
77
use crate::{
88
DeleteSubAccountBody, HttpClient, PaystackAPIError, PaystackResult, Response, SubaccountBody,
99
TransactionSplitRequest, TransactionSplitResponseData, UpdateTransactionSplitRequest,
@@ -31,7 +31,7 @@ impl<T: HttpClient + Default> TransactionSplitEndpoints<T> {
3131
/// # Returns
3232
/// A new TransactionSplitEndpoints instance
3333
pub fn new(key: Arc<String>, http: Arc<T>) -> TransactionSplitEndpoints<T> {
34-
let base_url = format!("{}/split", BASE_URL);
34+
let base_url = format!("{}/split", PAYSTACK_BASE_URL);
3535
TransactionSplitEndpoints {
3636
key: key.to_string(),
3737
base_url,

src/endpoints/virtual_terminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! ================
33
//! The Virtual Terminal API allows you to accept in-person payments without a POS device.
44
5-
use super::BASE_URL;
5+
use super::PAYSTACK_BASE_URL;
66
use crate::{
77
DestinationRequest, DestinationResponse, HttpClient, PaystackAPIError, PaystackResult,
88
Response, TransactionSplitResponseData, VirtualTerminalRequestData,
@@ -31,7 +31,7 @@ impl<T: HttpClient + Default> VirtualTerminalEndpoints<T> {
3131
/// # Returns
3232
/// A new VirtualTerminalEndpoints instance
3333
pub fn new(key: Arc<String>, http: Arc<T>) -> VirtualTerminalEndpoints<T> {
34-
let base_url = format!("{}/virtual_terminal", BASE_URL);
34+
let base_url = format!("{}/virtual_terminal", PAYSTACK_BASE_URL);
3535
VirtualTerminalEndpoints {
3636
key: key.to_string(),
3737
base_url,

0 commit comments

Comments
 (0)