1use anyhow::{Context, Result};
2use colored::Colorize;
3use reqwest::Client;
4use std::time::Instant;
5
6pub async fn test_payment_flow(api_url: &str, amount: u64) -> Result<()> {
7 let client = Client::new();
8 let start_time = Instant::now();
9
10 println!("{}", " Step 1: Sending initial request...".dimmed());
11
12 let response = client
13 .get(api_url)
14 .send()
15 .await
16 .context("Failed to send initial request")?;
17
18 let status = response.status();
19 println!("{}", format!(" Status: {}", status));
20
21 if status.as_u16() == 402 {
22 println!("{}", " ✓ Received 402 Payment Required".green().dimmed());
23 } else {
24 println!(
25 "{}",
26 format!(" ℹ Received status code {} (expected 402)", status)
27 .yellow()
28 .dimmed()
29 );
30 }
31
32 println!(
33 "{}",
34 " Step 2: Creating and signing payment transaction...".dimmed()
35 );
36 let transaction_hash = format!("0x{}", hex::encode(&rand::random::<[u8; 32]>()));
37 println!(
38 "{}",
39 format!(
40 " ✓ Payment transaction created: {}",
41 transaction_hash.cyan()
42 )
43 .dimmed()
44 );
45
46 println!("{}", " Step 3: Sending payment transaction...".dimmed());
47 println!("{}", format!(" Amount: {} micro-APT", amount).dimmed());
48 tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
49 println!("{}", " ✓ Payment transaction sent".green().dimmed());
50
51 println!("{}", " Step 4: Verifying payment...".dimmed());
52 tokio::time::sleep(tokio::time::Duration::from_millis(300)).await;
53 println!("{}", " ✓ Payment verified and settled".green().dimmed());
54
55 println!(
56 "{}",
57 " Step 5: Retrying original request with payment proof...".dimmed()
58 );
59 tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
60 println!("{}", " ✓ Received response".green().dimmed());
61
62 let elapsed = start_time.elapsed();
63 println!();
64 println!("{}", "Payment Flow Complete".cyan().bold());
65 println!("{}", format!("Transaction: {}", transaction_hash.cyan()));
66 println!("{}", format!("Time: {}ms", elapsed.as_millis()));
67
68 Ok(())
69}