stripe-sdk 0.3.0

Rust SDK for working with the Stripe API
docs.rs failed to build stripe-sdk-0.3.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: stripe-sdk-0.1.0

stripe-sdk

This repository provides an SDK written in Rust to help you interact with the Stripe API.

Developed by Finite Field, K.K.

Japanese: translations/ja/README.md

Implementation Approach

  • build.rs reads spec/openapi.spec3.json and automatically generates dedicated methods for each operationId.
  • Requests are handled with operation-specific *Request types.
  • Responses are returned as operation-specific *Response types, and body is also an operation-specific type (*ResponseBody).

Usage

use serde_json::json;
use stripe_sdk::{
    GetCustomersRequest, PostCheckoutSessionsRequest, PostCustomersRequest, StripeClient,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = StripeClient::new("sk_test_xxx")?;

    let list = client
        .get_customers(GetCustomersRequest::new().with_limit(3))
        .await?;
    println!("list status = {}", list.status);

    let created = client
        .post_customers(
            PostCustomersRequest::new().with_body(json!({
                "name": "Acme Inc.",
                "email": "dev@example.com"
            })),
        )
        .await?;
    println!("create status = {}", created.status);

    let checkout = client
        .post_checkout_sessions(
            PostCheckoutSessionsRequest::new()
                .with_header("Idempotency-Key", "checkout_session_order_123")
                .with_body(json!({
                    "mode": "payment",
                    "success_url": "https://example.com/success",
                    "cancel_url": "https://example.com/cancel",
                    "line_items": [{
                        "price": "price_123",
                        "quantity": 1
                    }]
                })),
        )
        .await?;
    println!("checkout status = {}", checkout.status);

    Ok(())
}

Webhook Helpers

use stripe_sdk::webhook;

fn handle_webhook(raw_body: &[u8], stripe_signature: &str) -> Result<(), webhook::StripeWebhookError> {
    let endpoint_secret = "whsec_xxx";

    let event = webhook::construct_event(raw_body, stripe_signature, endpoint_secret)?;
    println!("event type = {}", event.param_type);

    Ok(())
}
  • Verify only: webhook::verify_signature(...)
  • Parse to custom type: webhook::construct_event_as::<T>(...)