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::core::models::common::ListResponse;
use crate::core::models::payment_link::{
CheckoutLinkResolveResponse, PaymentLinkResponse, RetrievePaymentLinkResponse,
};
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;
#[derive(Debug, Clone)]
pub struct PaymentLinks {
inner: Arc<Inner>,
}
impl PaymentLinks {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
pub async fn retrieve(&self, link_id: &str) -> Result<RetrievePaymentLinkResponse> {
let path = format!("/payment-link/{}", encode_segment(link_id));
self.inner
.request(HttpMethod::Get, &path, None, &[], None)
.await
}
pub async fn resolve(&self, payment_link_id: &str) -> Result<CheckoutLinkResolveResponse> {
let path = format!("/payment-link/resolve/{}", encode_segment(payment_link_id));
self.inner
.request(HttpMethod::Get, &path, None, &[], None)
.await
}
pub async fn list(
&self,
params: Option<&PaymentLinkListParams>,
) -> Result<ListResponse<PaymentLinkResponse>> {
let body: Option<Vec<u8>> = match params {
Some(p) => Some(to_json_vec(p)?),
None => None,
};
self.inner
.request(
HttpMethod::Post,
"/payment-link/list",
body.as_deref(),
&[],
None,
)
.await
}
pub async fn initiate(&self, merchant_id: &str, payment_id: &str) -> Result<Value> {
let path = format!(
"/payment-link/{}/{}",
encode_segment(merchant_id),
encode_segment(payment_id)
);
self.inner
.request(HttpMethod::Get, &path, None, &[], None)
.await
}
pub async fn status(&self, merchant_id: &str, payment_id: &str) -> Result<Value> {
let path = format!(
"/payment-link/status/{}/{}",
encode_segment(merchant_id),
encode_segment(payment_id)
);
self.inner
.request(HttpMethod::Get, &path, None, &[], None)
.await
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct PaymentLinkListParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<String>,
#[serde(rename = "created.lt", skip_serializing_if = "Option::is_none")]
pub created_lt: Option<String>,
#[serde(rename = "created.gt", skip_serializing_if = "Option::is_none")]
pub created_gt: Option<String>,
#[serde(rename = "created.lte", skip_serializing_if = "Option::is_none")]
pub created_lte: Option<String>,
#[serde(rename = "created.gte", skip_serializing_if = "Option::is_none")]
pub created_gte: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::mock::{Step, client_with};
const LINK_JSON: &str = r#"{
"link": "https://checkout.test/pay/mer_1/pay_1",
"payment_link_id": "plink_1",
"site_origin": "https://shop.test"
}"#;
#[tokio::test]
async fn resolve_gets_the_resolve_path_with_the_encoded_link_id() {
let (client, mock) = client_with(vec![Step::ok(
r#"{"merchant_id":"mer_1","payment_id":"pay_1","surprise_field":true}"#,
)]);
let out = client.payment_links().resolve("plink/1").await.unwrap();
assert_eq!(out.merchant_id, "mer_1");
assert_eq!(out.payment_id, "pay_1");
assert_eq!(out.extra["surprise_field"], true);
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "GET");
assert_eq!(
seen[0].url,
"https://api.test/payment-link/resolve/plink%2F1"
);
assert!(seen[0].body.is_none());
}
#[tokio::test]
async fn retrieve_gets_the_payment_link_path() {
let (client, mock) = client_with(vec![Step::ok(
r#"{"payment_link_id":"plink_1","merchant_id":"mer_1",
"link_to_pay":"https://checkout.test/pay/plink_1","amount":1999,
"created_at":"2026-09-01T00:00:00Z","expiry":null,"status":"active",
"currency":"EUR","secure_link":null,"surprise_field":1}"#,
)]);
let out = client.payment_links().retrieve("plink_1").await.unwrap();
assert_eq!(out.payment_link_id, "plink_1");
assert_eq!(out.link_to_pay, "https://checkout.test/pay/plink_1");
assert_eq!(out.amount, 1999);
assert_eq!(out.status, "active");
assert_eq!(out.extra["surprise_field"], 1);
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "GET");
assert_eq!(seen[0].url, "https://api.test/payment-link/plink_1");
}
#[tokio::test]
async fn list_posts_filters_with_dotted_wire_names() {
let (client, mock) = client_with(vec![Step::ok(&format!(
r#"{{"size":1,"data":[{LINK_JSON}]}}"#
))]);
let params = PaymentLinkListParams {
limit: Some(10),
created_lte: Some("2026-01-01T00:00:00Z".to_string()),
..Default::default()
};
let out = client.payment_links().list(Some(¶ms)).await.unwrap();
assert_eq!(
out.data[0].site_origin.as_deref(),
Some("https://shop.test")
);
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "POST");
assert_eq!(seen[0].url, "https://api.test/payment-link/list");
let body = seen[0].body_json();
assert_eq!(body["limit"], 10);
assert_eq!(body["created.lte"], "2026-01-01T00:00:00Z");
}
#[tokio::test]
async fn initiate_and_status_hit_their_paths() {
let (client, mock) = client_with(vec![
Step::ok(r#"{"html":"..."}"#),
Step::ok(r#"{"status":"paid"}"#),
]);
client
.payment_links()
.initiate("mer_1", "pay_1")
.await
.unwrap();
client
.payment_links()
.status("mer_1", "pay_1")
.await
.unwrap();
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].url, "https://api.test/payment-link/mer_1/pay_1");
assert_eq!(
seen[1].url,
"https://api.test/payment-link/status/mer_1/pay_1"
);
}
}