1pub(crate) mod chains;
13pub(crate) mod discovery;
14pub mod error;
15pub mod fund;
16pub mod types;
17pub mod wallet;
18
19mod x402;
21
22pub use error::{PayError, PayErrorCode};
23pub use types::{DiscoverResult, PayResult, PaymentInfo, Protocol, Service};
24pub use wallet::{Account, WalletAccess};
25
26pub async fn pay(
31 wallet: &dyn WalletAccess,
32 url: &str,
33 method: &str,
34 body: Option<&str>,
35) -> Result<PayResult, PayError> {
36 let client = reqwest::Client::new();
37
38 let initial = x402::build_request(&client, url, method, body, None)?
40 .send()
41 .await?;
42
43 if initial.status().as_u16() != 402 {
45 let status = initial.status().as_u16();
46 let text = initial.text().await.unwrap_or_default();
47 return Ok(PayResult {
48 protocol: Protocol::X402,
49 status,
50 body: text,
51 payment: None,
52 });
53 }
54
55 let headers = initial.headers().clone();
57 let body_402 = initial.text().await.unwrap_or_default();
58
59 x402::handle_x402(wallet, url, method, body, &headers, &body_402).await
61}
62
63pub async fn discover(
68 query: Option<&str>,
69 limit: Option<u64>,
70 offset: Option<u64>,
71) -> Result<DiscoverResult, PayError> {
72 discovery::discover_all(query, limit, offset).await
73}