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