### Create a payment with redirect confirmation Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Initializes a client with basic authentication (shop_id and secret_key) and creates a payment with a redirect confirmation URL. The Idempotence-Key is generated automatically when None is passed. The payment's confirmation URL is used to redirect the user. ```rust use yookassa::{Auth, Client, CreatePaymentRequest, CurrencyCode, MoneyAmount}; #[tokio::main] async fn main() -> yookassa::Result<()> { let client = Client::new(Auth::Basic { shop_id: "123456".into(), secret_key: "test_...".into(), })?; let payment = client .create_payment( &CreatePaymentRequest { amount: MoneyAmount::new(100, CurrencyCode::Rub), description: Some("Order #1".into()), confirmation: Some(yookassa::ConfirmationData::Redirect { return_url: "https://example.com/return".into(), enforce: None, }), ..Default::default() }, None, // a fresh Idempotence-Key is generated automatically ) .await?; // Send the user to payment.confirmation's URL (redirect flow) Ok(()) } ``` -------------------------------- ### Create a webhook with OAuth authentication Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Creates a webhook using OAuth authentication. The client is initialized with an OAuth token, and create_webhook is called with a WebhookEvent and a callback URL. ```rust # async fn example(oauth_token: &str) -> yookassa::Result<()> { let client = yookassa::Client::new(yookassa::Auth::OAuth(oauth_token.into()))?; let webhook = client .create_webhook( &yookassa::CreateWebhookRequest::new( yookassa::WebhookEvent::PaymentSucceeded.as_str(), "https://example.com/yookassa-hook", ), None, ) .await?; # Ok(()) # } ``` -------------------------------- ### Capture, cancel, and refund payments Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Shows how to confirm a two-stage payment hold with capture_payment, cancel it with cancel_payment, and refund a succeeded payment with create_refund. Each call accepts an optional Idempotence-Key (None generates a fresh one). ```rust # async fn example(client: &yookassa::Client, payment_id: &str) -> yookassa::Result<()> { // two-stage payments: confirm the hold let captured = client.capture_payment(payment_id, &Default::default(), None).await?; // or cancel it let canceled = client.cancel_payment(payment_id, None).await?; // refund a succeeded payment let refund = client .create_refund( &yookassa::CreateRefundRequest { payment_id: payment_id.into(), amount: yookassa::MoneyAmount::new(10, yookassa::CurrencyCode::Rub), ..Default::default() }, None, ) .await?; # Ok(()) # } ``` -------------------------------- ### Build 54-FZ mark code from scanned DataMatrix Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Constructs a ProductCode from a scanned DataMatrix string and verifies the resulting type and product_code hex string. Also shows creating a MarkCodeInfo with an EAN-13 value. ```rust use yookassa::{MarkCodeInfo, ProductCode}; let scanned = "010460406000590021N4N57RTCBUZTQ"; let code = ProductCode::new(scanned); assert_eq!(code.type_, "gs_10"); assert_eq!( code.product_code, "44 4D 04 2F F7 5C 76 0C 4E 34 4E 35 37 52 54 43 42 55 5A 54 51" ); let _mark_info = MarkCodeInfo { ean_13: Some("4006670128002".into()), ..Default::default() }; ``` -------------------------------- ### List payments with cursor pagination Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Fetches a page of payments with a limit and status filter, then uses the next_cursor from the response to set the cursor for the next page. ```rust # async fn example(client: &yookassa::Client) -> yookassa::Result<()> { let mut query = yookassa::PaymentListQuery { limit: Some(50), status: Some("succeeded".into()), ..Default::default() }; let page = client.list_payments(&query).await?; if let Some(cursor) = page.next_cursor { query.cursor = Some(cursor); // next page } # Ok(()) # } ``` -------------------------------- ### Add yookassa dependency to Cargo.toml Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Adds the yookassa crate version 0.1 to a Rust project's dependencies. ```toml [dependencies] yookassa = "0.1" ``` -------------------------------- ### Handle API and transport errors with yookassa::Error Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Use this snippet to handle errors from yookassa client calls. It matches on yookassa::Error::Api to print status, code, and description, and checks for ApiErrorCode::Refusal to print the refusal reason. Transport errors are printed separately. ```rust # async fn example(client: &yookassa::Client) { match client.get_payment("does-not-exist").await { Ok(_) => {} Err(yookassa::Error::Api(e)) => { eprintln!("{} {}: {:?}", e.status, e.code, e.description); if e.code == yookassa::ApiErrorCode::Refusal { eprintln!("refusal reason: {:?}", e.reason); } } Err(e) => eprintln!("transport: {e}"), } # } ``` -------------------------------- ### Handle incoming webhook notifications with IP trust check Source: https://github.com/amnesiaof/yookassa/blob/master/README.md Verifies the sender IP with is_ip_trusted before parsing the JSON body with WebhookNotification::parse. Ignores notifications from untrusted IPs and matches event types like payment.succeeded and payment.canceled. ```rust use yookassa::{is_ip_trusted, WebhookNotification}; fn handle(body: &str, remote_ip: &str) -> yookassa::Result<()> { if !is_ip_trusted(remote_ip) { return Ok(()); // not from YooKassa — ignore } let notification = WebhookNotification::parse(body)?; match notification.event.as_str() { "payment.succeeded" => { /* deliver goods */ } "payment.canceled" => { /* release reserved stock */ } _ => {} } Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.