Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The client currently covers the following section of the API, and the sections t
- [x] Customers
- [x] Dedicated Virtual Account
- [x] Apple Pay
- [ ] Subaccounts
- [x] Subaccounts
- [ ] Plans
- [ ] Subscriptions
- [ ] Transfer Recipients
Expand Down
76 changes: 38 additions & 38 deletions src/endpoints/apple_pay.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Apple Pay
//! THe Apple Pay API allows you register your application's top-level domain or subdomain.

use std::{marker::PhantomData, sync::Arc};

use super::PAYSTACK_BASE_URL;
use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult};
use serde_json::json;

use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult, Response};
use std::{marker::PhantomData, sync::Arc};

#[derive(Debug, Clone)]
pub struct ApplePayEndpoints<T: HttpClient + Default> {
Expand All @@ -28,7 +27,7 @@ impl<T: HttpClient + Default> ApplePayEndpoints<T> {
/// # Returns
/// A new ApplePayEndpoints instance
pub fn new(key: Arc<String>, http: Arc<T>) -> ApplePayEndpoints<T> {
let base_url = String::from("https://api.paystack.co/apple-pay/domain");
let base_url = format!("{}/apple-pay/domain", PAYSTACK_BASE_URL);
ApplePayEndpoints {
key: key.to_string(),
base_url,
Expand All @@ -47,66 +46,67 @@ impl<T: HttpClient + Default> ApplePayEndpoints<T> {
&self,
domain_name: String,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}", self.base_url);
let url = &self.base_url;
let body = json!({
"domainName": domain_name
});

let response = self.http.post(&url, &self.key, &body).await;
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<PhantomData<String>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
let parsed_response = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.get(&url, &self.key, None).await;
let response = self
.http
.get(&url, &self.key, None)
.await
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<ApplePayResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
let parsed_response = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
}
Ok(parsed_response)
}

/// Unregister a top-level domain or subdomain previously used for your Apple Pay integration.
///
/// # Arguments
/// * `domain_name` - The name of the domain to unregister
///
/// # Returns
/// A result containing a success message without data.
pub async fn unregister_domain(
&self,
domain_name: String,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}", self.base_url);
let url = &self.base_url;
let body = json!({
"domainName": domain_name
});

let response = self.http.delete(&url, &self.key, &body).await;
let response = self
.http
.delete(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<PhantomData<String>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
let parsed_response = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::ApplePay(e.to_string())),
}
Ok(parsed_response)
}
}
139 changes: 62 additions & 77 deletions src/endpoints/customers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
//! =========
//! Thse Customers API allows you to create and maange customers on your integration

use std::{marker::PhantomData, sync::Arc};

use serde_json::json;

use super::PAYSTACK_BASE_URL;
use crate::{
CreateCustomerRequest, CustomerResponseData, HttpClient, PaystackAPIError, PaystackResult,
Response, RiskAction, UpdateCustomerRequest, ValidateCustomerRequest,
};
use serde_json::json;
use std::{marker::PhantomData, sync::Arc};

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

let response = self.http.post(&url, &self.key, &body).await;
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<CustomerResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

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

let response = self.http.get(&url, &self.key, Some(&query)).await;
let response = self
.http
.get(&url, &self.key, Some(&query))
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<Vec<CustomerResponseData>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<Vec<CustomerResponseData>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.get(&url, &self.key, None).await;
let response = self
.http
.get(&url, &self.key, None)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<CustomerResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.put(&url, &self.key, &body).await;
let response = self
.http
.put(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<CustomerResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.post(&url, &self.key, &body).await;
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<PhantomData<String>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.post(&url, &self.key, &body).await;
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<CustomerResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<CustomerResponseData> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}

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

let response = self.http.post(&url, &self.key, &body).await;
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

match response {
Ok(response) => {
let parsed_response: Response<PhantomData<String>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::Customer(e.to_string()))?;

Ok(parsed_response)
}
Err(e) => Err(PaystackAPIError::Customer(e.to_string())),
}
Ok(parsed_response)
}
}
Loading
Loading