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;
#[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
}
pub async fn create_account(&self, params: &StripeConnectAccountRequest) -> Result<Value> {
let body = to_json_vec(params)?;
self.post("/connector-onboarding/stripe/accounts", &body)
.await
}
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
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct StripeConnectAccountRequest {
pub merchant_id: String,
pub profile_id: String,
pub country: String,
pub business_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct StripeConnectLinkRequest {
pub merchant_id: String,
pub profile_id: String,
pub return_url: String,
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"
);
}
}