Skip to main content

bitmomo/
lib.rs

1//! BitMomo merchant SDK: accept crypto payments and verify BitMomo webhooks.
2//!
3//! > **Test money only.** BitMomo currently runs on test networks. Live API
4//! > keys are disabled, so nothing you do with this SDK moves real money.
5//!
6//! ```no_run
7//! use bitmomo::{Client, CreatePaymentParams, Decimal};
8//!
9//! # async fn run() -> Result<(), bitmomo::Error> {
10//! let client = Client::new(std::env::var("BITMOMO_API_KEY").unwrap(), "https://api.dev.bitmomo.app")?;
11//! let params = CreatePaymentParams::new(Decimal::new("150.00")?, "USDT_TRC20")
12//!     .order_id("order-1001")
13//!     .idempotency_key("order-1001"); // reuse the same key if you retry
14//! let payment = client.create_payment(&params).await?;
15//! println!("{} {} {}", payment.deposit_address, payment.crypto_amount, payment.qr_code_data);
16//! # Ok(())
17//! # }
18//! ```
19//!
20//! Verify webhooks with [`webhooks::verify_event`]. Every failure is an
21//! [`Error`] with the HTTP `status`, the API's `code`, a `message` and a
22//! `retriable` hint. Money is [`Decimal`], an exact decimal string.
23//!
24//! The `client` feature (on by default) provides the async [`Client`]; turn
25//! default features off to use only the webhook helpers. TLS uses rustls by
26//! default (`rustls-tls`); enable `native-tls` instead if you prefer.
27//!
28//! Guides and the API reference: <https://docs.bitmomo.app>
29#![forbid(unsafe_code)]
30
31mod decimal;
32mod error;
33pub mod models;
34pub mod webhooks;
35
36#[cfg(feature = "client")]
37mod client;
38
39#[cfg(feature = "client")]
40pub use client::{Client, ClientBuilder};
41pub use decimal::Decimal;
42pub use error::Error;
43pub use models::*;
44
45/// The SDK version, sent in the User-Agent header.
46pub const VERSION: &str = env!("CARGO_PKG_VERSION");
47
48/// A random UUID (version 4) to use as an idempotency key. Keep it with the
49/// business intent and reuse it when you retry.
50pub fn new_idempotency_key() -> String {
51    let mut b = [0u8; 16];
52    getrandom::getrandom(&mut b).expect("bitmomo: the operating system random source failed");
53    b[6] = (b[6] & 0x0f) | 0x40;
54    b[8] = (b[8] & 0x3f) | 0x80;
55    let h = hex::encode(b);
56    format!(
57        "{}-{}-{}-{}-{}",
58        &h[0..8],
59        &h[8..12],
60        &h[12..16],
61        &h[16..20],
62        &h[20..32]
63    )
64}