Skip to content

CryptoGateHQ/rust-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

cryptogate

Official Rust SDK for the CryptoGate payment API.

Installation

[dependencies]
cryptogate = "1.0.0"

Quick Start

use cryptogate::CryptoGateClient;

#[tokio::main]
async fn main() {
    let cg = CryptoGateClient::new("sk_live_your_api_key");

    let tx = cg.create_transaction("BTC", 50.0).await.unwrap();
    println!("{}", tx.txid);        // MTX-A1B2C3D4
    println!("{}", tx.payment_url); // Redirect your customer here
}

API Reference

let tx     = cg.create_transaction("BTC", 25.0).await?;
let tx     = cg.get_transaction("MTX-A1B2C3D4").await?;
let result = cg.list_transactions(1, 20).await?;
let tx     = cg.cancel_transaction("MTX-A1B2C3D4").await?;

let cryptos = cg.get_supported_cryptos().await?;
let rates   = cg.get_exchange_rates().await?;

Webhook Verification

use cryptogate::verify_webhook;

async fn webhook(
    headers: HeaderMap,
    body: Bytes,
) -> impl IntoResponse {
    let signature = headers
        .get("X-CryptoGate-Signature")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");

    if !verify_webhook(&std::env::var("WEBHOOK_SECRET").unwrap(), &body, signature) {
        return StatusCode::BAD_REQUEST;
    }

    // handle event
    StatusCode::OK
}

Error Handling

use cryptogate::CryptoGateError;

match cg.get_transaction("MTX-INVALID").await {
    Ok(tx) => println!("{:?}", tx),
    Err(CryptoGateError::NotFound(msg))      => eprintln!("Not found: {}", msg),
    Err(CryptoGateError::Authentication(msg)) => eprintln!("Auth error: {}", msg),
    Err(e)                                    => eprintln!("Error: {}", e),
}

License

MIT

About

Official Rust SDK for the CryptoGate payment API. Rust 1.70+.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages