delopay 0.20.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `stripe_connect` resource: Stripe Connect onboarding — account
//! creation and the account link the merchant completes onboarding at.

use std::sync::Arc;

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

use crate::core::encoding::to_json_vec;
use crate::core::error::Result;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;

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

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

    async fn post(&self, path: &str, body: &[u8]) -> Result<Value> {
        self.inner
            .request(HttpMethod::Post, path, Some(body), &[], None)
            .await
    }

    /// Create a Stripe Connect account for a shop.
    pub async fn create_account(&self, params: &StripeConnectAccountRequest) -> Result<Value> {
        let body = to_json_vec(params)?;
        self.post("/connector-onboarding/stripe/accounts", &body)
            .await
    }

    /// Create a Stripe Connect account link — the URL the merchant completes
    /// onboarding at.
    pub async fn create_account_link(&self, params: &StripeConnectLinkRequest) -> Result<Value> {
        let body = to_json_vec(params)?;
        self.post("/connector-onboarding/stripe/account-links", &body)
            .await
    }
}

/// Parameters for [`StripeConnect::create_account`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct StripeConnectAccountRequest {
    /// Merchant the account belongs to.
    pub merchant_id: String,
    /// Shop (business profile) the account is created for.
    pub profile_id: String,
    /// Account country, ISO 3166-1 alpha-2.
    pub country: String,
    /// Stripe business type, e.g. `"individual"` or `"company"`.
    pub business_type: String,
    /// Contact email for the account.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
}

/// Parameters for [`StripeConnect::create_account_link`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct StripeConnectLinkRequest {
    /// Merchant the account belongs to.
    pub merchant_id: String,
    /// Shop (business profile) being onboarded.
    pub profile_id: String,
    /// URL Stripe returns the merchant to once onboarding completes.
    pub return_url: String,
    /// URL Stripe sends the merchant to if the link expired.
    pub refresh_url: String,
}

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

    #[tokio::test]
    async fn create_account_posts_to_the_stripe_accounts_path() {
        let (client, mock) = client_with(vec![Step::ok(
            r#"{"account_id":"acct_1","profile_id":"pro_1"}"#,
        )]);
        let req = StripeConnectAccountRequest {
            merchant_id: "mer_1".to_string(),
            profile_id: "pro_1".to_string(),
            country: "DE".to_string(),
            business_type: "company".to_string(),
            email: None,
        };
        client.stripe_connect().create_account(&req).await.unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "POST");
        assert_eq!(
            seen[0].url,
            "https://api.test/connector-onboarding/stripe/accounts"
        );
        let body = seen[0].body_json();
        assert_eq!(body["country"], "DE");
        assert_eq!(body["business_type"], "company");
        assert!(
            body.get("email").is_none(),
            "an unset email must be omitted, not null"
        );
    }

    #[tokio::test]
    async fn create_account_link_posts_to_the_account_links_path() {
        let (client, mock) = client_with(vec![Step::ok(
            r#"{"url":"https://connect.stripe.test/x","expires_at":1}"#,
        )]);
        let req = StripeConnectLinkRequest {
            merchant_id: "mer_1".to_string(),
            profile_id: "pro_1".to_string(),
            return_url: "https://example.com/done".to_string(),
            refresh_url: "https://example.com/retry".to_string(),
        };
        client
            .stripe_connect()
            .create_account_link(&req)
            .await
            .unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "POST");
        assert_eq!(
            seen[0].url,
            "https://api.test/connector-onboarding/stripe/account-links"
        );
        assert_eq!(
            seen[0].body_json()["return_url"],
            "https://example.com/done"
        );
    }
}