Skip to main content

paystack/endpoints/
apple_pay.rs

1//! Apple Pay
2//! THe Apple Pay API allows you register your application's top-level domain or subdomain.
3
4use super::PAYSTACK_BASE_URL;
5use crate::{ApplePayResponseData, HttpClient, PaystackAPIError, PaystackResult};
6use serde_json::json;
7use std::{marker::PhantomData, sync::Arc};
8
9#[derive(Debug, Clone)]
10pub struct ApplePayEndpoints<T: HttpClient + Default> {
11    /// Paystack API key
12    key: String,
13    /// Base URL for the apple pay route
14    base_url: String,
15    /// Http client for the route
16    http: Arc<T>,
17}
18
19impl<T: HttpClient + Default> ApplePayEndpoints<T> {
20    /// Creates a new ApplePayEndpoints instance
21    ///Creates a new ApplePayEndpoints instance
22    ///
23    /// # Arguments
24    /// * `key` - The Paystack API key
25    /// * `http` - The HTTP client implementation to use for API requests
26    ///
27    /// # Returns
28    /// A new ApplePayEndpoints instance
29    pub fn new(key: Arc<String>, http: Arc<T>) -> ApplePayEndpoints<T> {
30        let base_url = format!("{PAYSTACK_BASE_URL}/apple-pay/domain");
31        ApplePayEndpoints {
32            key: key.to_string(),
33            base_url,
34            http,
35        }
36    }
37
38    /// Register a top-level domain or subdomain for your Apple Pay integration.
39    ///
40    /// # Arguments
41    /// * `domain_name` - The domain name to be registered with Apple Pay
42    ///
43    /// # Returns
44    /// A Result containing the registration response or an error
45    pub async fn register_domain(
46        &self,
47        domain_name: String,
48    ) -> PaystackResult<PhantomData<String>> {
49        let url = &self.base_url;
50        let body = json!({
51            "domainName": domain_name
52        });
53
54        let response = self
55            .http
56            .post(url, &self.key, &body)
57            .await
58            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
59
60        let parsed_response = serde_json::from_str(&response)
61            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
62
63        Ok(parsed_response)
64    }
65
66    /// Lists all domains registered on your integration
67    ///
68    /// # Returns
69    /// A Result containing the list of registered domains or an error
70    pub async fn list_domains(&self) -> PaystackResult<ApplePayResponseData> {
71        let url = &self.base_url;
72
73        let response = self
74            .http
75            .get(url, &self.key, None)
76            .await
77            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
78
79        let parsed_response = serde_json::from_str(&response)
80            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
81
82        Ok(parsed_response)
83    }
84
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.
92    pub async fn unregister_domain(
93        &self,
94        domain_name: String,
95    ) -> PaystackResult<PhantomData<String>> {
96        let url = &self.base_url;
97        let body = json!({
98            "domainName": domain_name
99        });
100
101        let response = self
102            .http
103            .delete(url, &self.key, &body)
104            .await
105            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
106
107        let parsed_response = serde_json::from_str(&response)
108            .map_err(|e| PaystackAPIError::ApplePay(e.to_string()))?;
109
110        Ok(parsed_response)
111    }
112}