Skip to content

Commit 7ff107b

Browse files
authored
Merge pull request #66 from morukele/feature/subaccounts
Feature/subaccounts
2 parents 00e5832 + 7ed60e5 commit 7ff107b

32 files changed

Lines changed: 808 additions & 622 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ The client currently covers the following section of the API, and the sections t
1919
- [x] Virtual Terminal
2020
- [x] Customers
2121
- [x] Dedicated Virtual Account
22-
- [ ] Apple Pay
23-
- [ ] Subaccounts
22+
- [x] Apple Pay
23+
- [x] Subaccounts
2424
- [ ] Plans
2525
- [ ] Subscriptions
2626
- [ ] Transfer Recipients

src/endpoints/apple_pay.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Apple Pay
22
//! THe Apple Pay API allows you register your application's top-level domain or subdomain.
33
4-
use std::{marker::PhantomData, sync::Arc};
5-
4+
use super::PAYSTACK_BASE_URL;
5+
use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult};
66
use serde_json::json;
7-
8-
use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult, Response};
7+
use std::{marker::PhantomData, sync::Arc};
98

109
#[derive(Debug, Clone)]
1110
pub struct ApplePayEndpoints<T: HttpClient + Default> {
@@ -28,7 +27,7 @@ impl<T: HttpClient + Default> ApplePayEndpoints<T> {
2827
/// # Returns
2928
/// A new ApplePayEndpoints instance
3029
pub fn new(key: Arc<String>, http: Arc<T>) -> ApplePayEndpoints<T> {
31-
let base_url = String::from("https://api.paystack.co/apple-pay/domain");
30+
let base_url = format!("{}/apple-pay/domain", PAYSTACK_BASE_URL);
3231
ApplePayEndpoints {
3332
key: key.to_string(),
3433
base_url,
@@ -47,66 +46,67 @@ impl<T: HttpClient + Default> ApplePayEndpoints<T> {
4746
&self,
4847
domain_name: String,
4948
) -> PaystackResult<PhantomData<String>> {
50-
let url = format!("{}", self.base_url);
49+
let url = &self.base_url;
5150
let body = json!({
5251
"domainName": domain_name
5352
});
5453

55-
let response = self.http.post(&url, &self.key, &body).await;
54+
let response = self
55+
.http
56+
.post(&url, &self.key, &body)
57+
.await
58+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
5659

57-
match response {
58-
Ok(response) => {
59-
let parsed_response: Response<PhantomData<String>> =
60-
serde_json::from_str(&response)
61-
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
60+
let parsed_response = serde_json::from_str(&response)
61+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
6262

63-
Ok(parsed_response)
64-
}
65-
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
66-
}
63+
Ok(parsed_response)
6764
}
6865

6966
/// Lists all domains registered on your integration
7067
///
7168
/// # Returns
7269
/// A Result containing the list of registered domains or an error
7370
pub async fn list_domains(&self) -> PaystackResult<ApplePayResponseData> {
74-
let url = format!("{}", self.base_url);
71+
let url = &self.base_url;
7572

76-
let response = self.http.get(&url, &self.key, None).await;
73+
let response = self
74+
.http
75+
.get(&url, &self.key, None)
76+
.await
77+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
7778

78-
match response {
79-
Ok(response) => {
80-
let parsed_response: Response<ApplePayResponseData> =
81-
serde_json::from_str(&response)
82-
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
79+
let parsed_response = serde_json::from_str(&response)
80+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
8381

84-
Ok(parsed_response)
85-
}
86-
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
87-
}
82+
Ok(parsed_response)
8883
}
8984

85+
/// Unregister a top-level domain or subdomain previously used for your Apple Pay integration.
86+
///
87+
/// # Arguments
88+
/// * `domain_name` - The name of the domain to unregister
89+
///
90+
/// # Returns
91+
/// A result containing a success message without data.
9092
pub async fn unregister_domain(
9193
&self,
9294
domain_name: String,
9395
) -> PaystackResult<PhantomData<String>> {
94-
let url = format!("{}", self.base_url);
96+
let url = &self.base_url;
9597
let body = json!({
9698
"domainName": domain_name
9799
});
98100

99-
let response = self.http.delete(&url, &self.key, &body).await;
101+
let response = self
102+
.http
103+
.delete(&url, &self.key, &body)
104+
.await
105+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
100106

101-
match response {
102-
Ok(response) => {
103-
let parsed_response: Response<PhantomData<String>> =
104-
serde_json::from_str(&response)
105-
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
107+
let parsed_response = serde_json::from_str(&response)
108+
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
106109

107-
Ok(parsed_response)
108-
}
109-
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
110-
}
110+
Ok(parsed_response)
111111
}
112112
}

src/endpoints/customers.rs

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
//! =========
33
//! Thse Customers API allows you to create and maange customers on your integration
44
5-
use std::{marker::PhantomData, sync::Arc};
6-
7-
use serde_json::json;
8-
5+
use super::PAYSTACK_BASE_URL;
96
use crate::{
107
CreateCustomerRequest, CustomerResponseData, HttpClient, PaystackAPIError, PaystackResult,
118
Response, RiskAction, UpdateCustomerRequest, ValidateCustomerRequest,
129
};
10+
use serde_json::json;
11+
use std::{marker::PhantomData, sync::Arc};
1312

1413
/// A struct to hold all the functions of the customers API endpoint
1514
#[derive(Debug, Clone)]
@@ -32,7 +31,7 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
3231
/// # Returns
3332
/// A new CustomersEndpoints instance
3433
pub fn new(key: Arc<String>, http: Arc<T>) -> CustomersEndpoints<T> {
35-
let base_url = String::from("https://api.paystack.co/customer");
34+
let base_url = format!("{}/customer", PAYSTACK_BASE_URL);
3635
CustomersEndpoints {
3736
key: key.to_string(),
3837
base_url,
@@ -52,22 +51,20 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
5251
&self,
5352
create_customer_request: CreateCustomerRequest,
5453
) -> PaystackResult<CustomerResponseData> {
55-
let url = format!("{}", self.base_url);
54+
let url = &self.base_url;
5655
let body = serde_json::to_value(create_customer_request)
5756
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
5857

59-
let response = self.http.post(&url, &self.key, &body).await;
58+
let response = self
59+
.http
60+
.post(&url, &self.key, &body)
61+
.await
62+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
6063

61-
match response {
62-
Ok(response) => {
63-
let parsed_response: Response<CustomerResponseData> =
64-
serde_json::from_str(&response)
65-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
64+
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
65+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
6666

67-
Ok(parsed_response)
68-
}
69-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
70-
}
67+
Ok(parsed_response)
7168
}
7269

7370
/// Lists customers available on your integration
@@ -83,24 +80,22 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
8380
per_page: Option<u8>,
8481
page: Option<u8>,
8582
) -> PaystackResult<Vec<CustomerResponseData>> {
86-
let url = format!("{}", self.base_url);
83+
let url = &self.base_url;
8784

8885
let per_page = per_page.unwrap_or(50).to_string();
8986
let page = page.unwrap_or(1).to_string();
9087
let query = vec![("perPage", per_page.as_str()), ("page", page.as_str())];
9188

92-
let response = self.http.get(&url, &self.key, Some(&query)).await;
89+
let response = self
90+
.http
91+
.get(&url, &self.key, Some(&query))
92+
.await
93+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
9394

94-
match response {
95-
Ok(response) => {
96-
let parsed_response: Response<Vec<CustomerResponseData>> =
97-
serde_json::from_str(&response)
98-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
95+
let parsed_response: Response<Vec<CustomerResponseData>> = serde_json::from_str(&response)
96+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
9997

100-
Ok(parsed_response)
101-
}
102-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
103-
}
98+
Ok(parsed_response)
10499
}
105100

106101
/// Gets details of a customer on your integration
@@ -116,18 +111,16 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
116111
) -> PaystackResult<CustomerResponseData> {
117112
let url = format!("{}/{}", self.base_url, email_or_code);
118113

119-
let response = self.http.get(&url, &self.key, None).await;
114+
let response = self
115+
.http
116+
.get(&url, &self.key, None)
117+
.await
118+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
120119

121-
match response {
122-
Ok(response) => {
123-
let parsed_response: Response<CustomerResponseData> =
124-
serde_json::from_str(&response)
125-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
120+
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
121+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
126122

127-
Ok(parsed_response)
128-
}
129-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
130-
}
123+
Ok(parsed_response)
131124
}
132125

133126
/// Updates a customer's details on your integration
@@ -148,18 +141,16 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
148141
let body = serde_json::to_value(update_customer_request)
149142
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
150143

151-
let response = self.http.put(&url, &self.key, &body).await;
144+
let response = self
145+
.http
146+
.put(&url, &self.key, &body)
147+
.await
148+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
152149

153-
match response {
154-
Ok(response) => {
155-
let parsed_response: Response<CustomerResponseData> =
156-
serde_json::from_str(&response)
157-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
150+
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
151+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
158152

159-
Ok(parsed_response)
160-
}
161-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
162-
}
153+
Ok(parsed_response)
163154
}
164155

165156
/// Validates a customer's identity
@@ -180,18 +171,16 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
180171
let body = serde_json::to_value(customer_validation_request)
181172
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
182173

183-
let response = self.http.post(&url, &self.key, &body).await;
174+
let response = self
175+
.http
176+
.post(&url, &self.key, &body)
177+
.await
178+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
184179

185-
match response {
186-
Ok(response) => {
187-
let parsed_response: Response<PhantomData<String>> =
188-
serde_json::from_str(&response)
189-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
180+
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
181+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
190182

191-
Ok(parsed_response)
192-
}
193-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
194-
}
183+
Ok(parsed_response)
195184
}
196185

197186
/// Whitelists or blacklists a customer on your integration
@@ -213,18 +202,16 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
213202
"risk_action": risk_action
214203
});
215204

216-
let response = self.http.post(&url, &self.key, &body).await;
205+
let response = self
206+
.http
207+
.post(&url, &self.key, &body)
208+
.await
209+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
217210

218-
match response {
219-
Ok(response) => {
220-
let parsed_response: Response<CustomerResponseData> =
221-
serde_json::from_str(&response)
222-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
211+
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
212+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
223213

224-
Ok(parsed_response)
225-
}
226-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
227-
}
214+
Ok(parsed_response)
228215
}
229216

230217
/// Deactivates an authorization when the card needs to be forgotten
@@ -243,17 +230,15 @@ impl<T: HttpClient + Default> CustomersEndpoints<T> {
243230
"authorization_code": authorization_code
244231
});
245232

246-
let response = self.http.post(&url, &self.key, &body).await;
233+
let response = self
234+
.http
235+
.post(&url, &self.key, &body)
236+
.await
237+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
247238

248-
match response {
249-
Ok(response) => {
250-
let parsed_response: Response<PhantomData<String>> =
251-
serde_json::from_str(&response)
252-
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
239+
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
240+
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
253241

254-
Ok(parsed_response)
255-
}
256-
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
257-
}
242+
Ok(parsed_response)
258243
}
259244
}

0 commit comments

Comments
 (0)