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
}
pub async fn action_url(&self, params: &Value) -> Result<Value> {
let body = to_json_vec(params)?;
self.post("/connector-onboarding/action-url", &body).await
}
pub async fn sync_onboarding(&self, params: &Value) -> Result<Value> {
let body = to_json_vec(params)?;
self.post("/connector-onboarding/sync", &body).await
}
pub async fn reset_tracking_id(&self, params: &Value) -> Result<Value> {
let body = to_json_vec(params)?;
self.post("/connector-onboarding/reset-tracking-id", &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};
use serde_json::json;
#[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"
);
}
#[tokio::test]
async fn generic_onboarding_endpoints_post_their_open_bodies() {
let (client, mock) = client_with(vec![
Step::ok(r#"{"url":"https://onboard.test"}"#),
Step::ok(r#"{"status":"synced"}"#),
Step::ok(r#"{"reset":true}"#),
]);
let sc = client.stripe_connect();
sc.action_url(&json!({"connector": "stripe"}))
.await
.unwrap();
sc.sync_onboarding(&json!({"connector": "adyen"}))
.await
.unwrap();
sc.reset_tracking_id(&json!({"connector": "paypal"}))
.await
.unwrap();
let seen = mock.seen.lock().unwrap();
let expected = [
("https://api.test/connector-onboarding/action-url", "stripe"),
("https://api.test/connector-onboarding/sync", "adyen"),
(
"https://api.test/connector-onboarding/reset-tracking-id",
"paypal",
),
];
for (i, (url, connector)) in expected.iter().enumerate() {
assert_eq!(seen[i].method, "POST");
assert_eq!(seen[i].url, *url);
assert_eq!(seen[i].body_json()["connector"], *connector);
}
}
}