2 releases

0.1.1 Nov 12, 2025
0.1.0 Nov 5, 2025

#713 in Authentication


Used in dintero

MIT license

45KB
1.5K SLoC

Dintero Rust SDK

Crates.io Documentation License: MIT

A comprehensive Rust SDK for the Dintero API, providing convenient and type-safe access to all Dintero services.

Features

  • Full API Coverage: Complete implementation of all Dintero APIs
  • Type-Safe: Strongly typed request and response models
  • Async/Await: Built on tokio and reqwest for async operations
  • Modular: Enable only the features you need
  • Builder Pattern: Ergonomic API design with builder patterns
  • Flexible Authentication: Support for various authentication methods

Available Modules

  • Checkout: Payment sessions, card tokens, and transactions
  • Orders: Order management and operations
  • Payments: Payment operations and captures
  • Accounts: Account management and profiles
  • Loyalty: Loyalty programs and rewards
  • Insights: Analytics and reporting

Installation

Add this to your Cargo.toml:

[dependencies]
dintero = "0.1"

Feature Flags

By default, all features are enabled. You can selectively enable only what you need:

[dependencies]
dintero = { version = "0.1", default-features = false, features = ["checkout", "orders"] }

Available features:

  • checkout - Checkout API support
  • orders - Orders API support
  • payments - Payments API support
  • accounts - Accounts API support
  • loyalty - Loyalty API support
  • insights - Insights API support

Quick Start

Basic Usage

use dintero::{DinteroClient, DinteroConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client
    let config = DinteroConfig::new(
        "your-account-id",
        "your-client-id",
        "your-client-secret"
    );

    let client = DinteroClient::new(config);

    Ok(())
}

Using Environment Variables

use dintero::DinteroClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Reads from DINTERO_ACCOUNT_ID, DINTERO_CLIENT_ID, DINTERO_CLIENT_SECRET
    let client = DinteroClient::from_env()?;

    Ok(())
}

Creating a Checkout Session

use dintero::{DinteroClient, DinteroConfig};

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

    #[cfg(feature = "checkout")]
    {
        use dintero_checkout::{SessionCreateRequest, Order, OrderLine, Money};

        let request = SessionCreateRequest::builder()
            .url("https://example.com/return".to_string())
            .order(
                Order::builder()
                    .amount(Money::new(10000, "NOK"))
                    .add_item(
                        OrderLine::builder()
                            .id("item-1".to_string())
                            .description("Test Item".to_string())
                            .quantity(1)
                            .amount(10000)
                            .build()
                    )
                    .build()
            )
            .build();

        let session = client.checkout().create_session(request).await?;
        println!("Checkout URL: {}", session.url);
    }

    Ok(())
}

Managing Orders

use dintero::{DinteroClient, DinteroConfig};

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

    #[cfg(feature = "orders")]
    {
        // Get an order
        let order = client.orders().get_order("order-id").await?;
        println!("Order status: {:?}", order.status);

        // List orders
        let orders = client.orders().list_orders().await?;
        println!("Found {} orders", orders.len());
    }

    Ok(())
}

Payment Operations

use dintero::{DinteroClient, DinteroConfig};

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

    #[cfg(feature = "payments")]
    {
        use dintero_payments::CaptureRequest;

        // Capture a payment
        let capture = CaptureRequest::builder()
            .amount(5000)
            .build();

        let result = client.payments()
            .capture_payment("transaction-id", capture)
            .await?;

        println!("Captured: {}", result.amount);
    }

    Ok(())
}

Examples

The repository includes comprehensive examples in the dintero/examples/ directory:

  • basic.rs - Basic client setup and configuration
  • checkout_session.rs - Creating checkout sessions
  • orders.rs - Order management operations
  • payments.rs - Payment and capture operations
  • accounts.rs - Account management
  • loyalty.rs - Loyalty program integration
  • insights.rs - Analytics and reporting

Run an example:

cargo run --example basic --features checkout

Authentication

The SDK supports multiple authentication methods:

Environment Variables

export DINTERO_ACCOUNT_ID="your-account-id"
export DINTERO_CLIENT_ID="your-client-id"
export DINTERO_CLIENT_SECRET="your-client-secret"

Configuration Object

use dintero::DinteroConfig;

let config = DinteroConfig::new(
    "account-id",
    "client-id",
    "client-secret"
);

Builder Pattern

use dintero::DinteroConfig;

let config = DinteroConfig::builder()
    .account_id("account-id")
    .client_id("client-id")
    .client_secret("client-secret")
    .base_url("https://api.dintero.com") // optional
    .build();

Error Handling

The SDK uses a custom error type that wraps all possible errors:

use dintero::{DinteroClient, DinteroError};

async fn example() -> Result<(), DinteroError> {
    let client = DinteroClient::from_env()?;

    match client.checkout().get_session("session-id").await {
        Ok(session) => println!("Session found: {}", session.id),
        Err(DinteroError::NotFound) => println!("Session not found"),
        Err(DinteroError::Authentication) => println!("Authentication failed"),
        Err(e) => println!("Error: {}", e),
    }

    Ok(())
}

Testing

Run all tests:

cargo test --all-features

Run tests for a specific feature:

cargo test --features checkout

Documentation

Full API documentation is available at docs.rs/dintero.

Build documentation locally:

cargo doc --all-features --open

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright © 2024 Budna Marketplace AB. All rights reserved.

Author: Marcus Cvjeticanin

Support

Dependencies

~4–18MB
~186K SLoC