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 ;
77use crate :: {
8- HttpClient , PaystackAPIError , PaystackResult , Response , SubaccountRequest ,
8+ CreateSubaccountRequest , HttpClient , PaystackAPIError , PaystackResult , Response ,
99 SubaccountsResponseData ,
1010} ;
1111use 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}
0 commit comments