delopay 0.4.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `verification` resource: Apple Pay domain registration.
//!
//! Apple requires every domain that shows an Apple Pay button to be registered
//! against the merchant connector account serving it.

use std::sync::Arc;

use serde::Serialize;
use serde_json::Value;

use crate::core::encoding::{encode_segment, to_json_vec};
use crate::core::error::Result;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;

/// The `verification` resource, obtained via [`crate::Delopay::verification`].
#[derive(Debug, Clone)]
pub struct Verification {
    inner: Arc<Inner>,
}

impl Verification {
    pub(crate) fn new(inner: Arc<Inner>) -> Self {
        Self { inner }
    }

    /// Register domains for Apple Pay against a merchant.
    pub async fn register_apple_pay_domains(
        &self,
        merchant_id: &str,
        params: &ApplePayVerificationRequest,
    ) -> Result<Value> {
        let path = format!("/verify/apple-pay/{}", encode_segment(merchant_id));
        let body = to_json_vec(params)?;
        self.inner
            .request(HttpMethod::Post, &path, Some(&body), &[], None)
            .await
    }

    /// List the domains currently verified for Apple Pay. Query keys are open
    /// — pass `&[]` for none; see the API reference for the accepted filters.
    pub async fn apple_pay_verified_domains(&self, query: &[(&str, String)]) -> Result<Value> {
        self.inner
            .request(
                HttpMethod::Get,
                "/verify/applepay-verified-domains",
                None,
                query,
                None,
            )
            .await
    }
}

/// Parameters for [`Verification::register_apple_pay_domains`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct ApplePayVerificationRequest {
    /// Domains to register, e.g. `["shop.example.com"]`.
    pub domain_names: Vec<String>,
    /// The merchant connector account the domains are served through.
    pub merchant_connector_account_id: String,
}

impl ApplePayVerificationRequest {
    /// A request registering `domain_names` against `merchant_connector_account_id`.
    pub fn new(
        domain_names: Vec<String>,
        merchant_connector_account_id: impl Into<String>,
    ) -> Self {
        Self {
            domain_names,
            merchant_connector_account_id: merchant_connector_account_id.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http::mock::{Step, client_with};

    #[tokio::test]
    async fn register_encodes_the_merchant_id_and_posts_the_domains() {
        let (client, mock) = client_with(vec![Step::ok(r#"{"status_message":"verified"}"#)]);
        let req = ApplePayVerificationRequest::new(vec!["shop.example.com".to_string()], "mca_1");
        client
            .verification()
            .register_apple_pay_domains("mer/1", &req)
            .await
            .unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "POST");
        assert_eq!(seen[0].url, "https://api.test/verify/apple-pay/mer%2F1");
        let body = seen[0].body_json();
        assert_eq!(body["domain_names"][0], "shop.example.com");
        assert_eq!(body["merchant_connector_account_id"], "mca_1");
    }

    #[tokio::test]
    async fn verified_domains_gets_its_path_with_the_query() {
        let (client, mock) = client_with(vec![Step::ok(r#"{"verified_domains":[]}"#)]);
        client
            .verification()
            .apple_pay_verified_domains(&[("profile_id", "pro_1".to_string())])
            .await
            .unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "GET");
        assert_eq!(
            seen[0].url,
            "https://api.test/verify/applepay-verified-domains?profile_id=pro_1"
        );
    }
}