use crate::client::Client;
use crate::error::Result;
use crate::models::*;
use serde_json::{json, Value};
fn ensure_order_id(params: Value) -> Value {
match params {
Value::Object(mut m) => {
let has = m
.get("order_id")
.and_then(|v| v.as_str())
.map(|s| !s.is_empty())
.unwrap_or(false);
if !has {
m.insert("order_id".into(), json!(format!("idem-{}", crate::random::hex16())));
}
Value::Object(m)
}
other => other,
}
}
fn lookup(uuid: Option<&str>, order_id: Option<&str>) -> Value {
let mut m = serde_json::Map::new();
if let Some(u) = uuid {
m.insert("uuid".into(), json!(u));
}
if let Some(o) = order_id {
m.insert("order_id".into(), json!(o));
}
Value::Object(m)
}
pub struct Payments<'a> {
pub(crate) client: &'a Client,
}
impl Payments<'_> {
pub fn create(&self, params: Value) -> Result<Payment> {
self.client.request("/v1/payment", &ensure_order_id(params))
}
pub fn info(&self, uuid: Option<&str>, order_id: Option<&str>) -> Result<Payment> {
self.client
.request("/v1/payment/info", &lookup(uuid, order_id))
}
pub fn history(&self, params: Value) -> Result<PaymentList> {
self.client.request("/v1/payment/history", ¶ms)
}
pub fn services(&self) -> Result<Vec<ServiceMethod>> {
self.client.request("/v1/payment/services", &json!({}))
}
pub fn qr(&self, uuid: Option<&str>, order_id: Option<&str>) -> Result<Value> {
self.client
.request("/v1/payment/qr", &lookup(uuid, order_id))
}
pub fn resend(&self, uuid: Option<&str>, order_id: Option<&str>) -> Result<Value> {
self.client
.request("/v1/payment/resend", &lookup(uuid, order_id))
}
pub fn refund(&self, params: Value) -> Result<Value> {
self.client.request("/v1/payment/refund", ¶ms)
}
pub fn list_accepted(&self) -> Result<Value> {
self.client.request("/v1/payment/accepted/list", &json!({}))
}
pub fn set_accepted(&self, accepted: Vec<AcceptedMethod>) -> Result<Value> {
self.client
.request("/v1/payment/accepted/set", &json!({ "accepted": accepted }))
}
pub fn get_accuracy(&self) -> Result<Value> {
self.client.request("/v1/payment/accuracy/get", &json!({}))
}
pub fn set_accuracy(&self, params: Value) -> Result<Value> {
self.client.request("/v1/payment/accuracy/set", ¶ms)
}
pub fn get_autorefund(&self) -> Result<Value> {
self.client
.request("/v1/payment/autorefund/get", &json!({}))
}
pub fn set_autorefund(&self, params: Value) -> Result<Value> {
self.client.request("/v1/payment/autorefund/set", ¶ms)
}
pub fn set_discount(&self, params: Value) -> Result<Value> {
self.client.request("/v1/payment/discount/set", ¶ms)
}
pub fn list_discounts(&self) -> Result<Value> {
self.client.request("/v1/payment/discount/list", &json!({}))
}
}
pub struct Payouts<'a> {
pub(crate) client: &'a Client,
}
impl Payouts<'_> {
pub fn create(&self, params: Value) -> Result<Payout> {
self.client.request("/v1/payout", ¶ms)
}
pub fn create_mass(
&self,
payouts: Vec<Value>,
source: Option<&str>,
) -> Result<MassPayoutResult> {
let mut body = json!({ "payouts": payouts });
if let Some(s) = source {
body["source"] = json!(s);
}
self.client.request("/v1/payout/mass", &body)
}
pub fn info(&self, uuid: Option<&str>, order_id: Option<&str>) -> Result<Payout> {
self.client
.request("/v1/payout/info", &lookup(uuid, order_id))
}
pub fn history(&self, params: Value) -> Result<PayoutList> {
self.client.request("/v1/payout/history", ¶ms)
}
pub fn services(&self) -> Result<Vec<ServiceMethod>> {
self.client.request("/v1/payout/services", &json!({}))
}
pub fn calculate(&self, params: Value) -> Result<PayoutCalculation> {
self.client.request("/v1/payout/calculate", ¶ms)
}
pub fn approve(&self, uuid: &str) -> Result<Value> {
self.client
.request("/v1/payout/approve", &json!({ "uuid": uuid }))
}
pub fn refund(&self, params: Value) -> Result<Value> {
self.client.request("/v1/payment/refund", ¶ms)
}
pub fn get_fee_config(&self) -> Result<Value> {
self.client.request("/v1/payout/fee-config/get", &json!({}))
}
pub fn set_fee_config(&self, fee_on_recipient: bool) -> Result<Value> {
self.client.request(
"/v1/payout/fee-config/set",
&json!({ "fee_on_recipient": fee_on_recipient }),
)
}
pub fn get_refund_fee_config(&self) -> Result<Value> {
self.client
.request("/v1/payout/refund-fee-config/get", &json!({}))
}
pub fn set_refund_fee_config(&self, fee_on_customer: bool) -> Result<Value> {
self.client.request(
"/v1/payout/refund-fee-config/set",
&json!({ "fee_on_customer": fee_on_customer }),
)
}
}
pub struct Wallets<'a> {
pub(crate) client: &'a Client,
}
impl Wallets<'_> {
pub fn create(&self, params: Value) -> Result<Wallet> {
self.client.request("/v1/wallet", ¶ms)
}
pub fn block(&self, address: &str, force_block: Option<bool>) -> Result<Value> {
let mut body = json!({ "address": address });
if let Some(f) = force_block {
body["is_force_block"] = json!(f);
}
self.client.request("/v1/wallet/block", &body)
}
pub fn blocked_address_refund(&self, uuid: &str, address: &str) -> Result<Value> {
self.client.request(
"/v1/wallet/blocked-address-refund",
&json!({ "uuid": uuid, "address": address }),
)
}
pub fn qr(&self, address: &str) -> Result<Value> {
self.client
.request("/v1/wallet/qr", &json!({ "address": address }))
}
}
pub struct Account<'a> {
pub(crate) client: &'a Client,
}
impl Account<'_> {
pub fn balance(&self) -> Result<Balance> {
#[derive(serde::Deserialize)]
struct Wrapper {
balance: Balance,
}
let w: Wrapper = self.client.request("/v1/balance", &json!({}))?;
Ok(w.balance)
}
pub fn referral(&self) -> Result<ReferralInfo> {
self.client.request("/v1/referral/info", &json!({}))
}
pub fn transfer_to_personal(&self, params: Value) -> Result<Value> {
self.client
.request("/v1/transfer/to-personal", &ensure_order_id(params))
}
pub fn vrcs(&self, enabled: Option<bool>) -> Result<Value> {
let body = match enabled {
Some(e) => json!({ "enabled": e }),
None => json!({}),
};
self.client.request("/v1/vrcs", &body)
}
}
pub struct Webhooks<'a> {
pub(crate) client: &'a Client,
}
impl Webhooks<'_> {
pub fn register(&self, url: &str) -> Result<WebhookRegistration> {
self.client.request("/v1/webhooks", &json!({ "url": url }))
}
pub fn deliveries(&self) -> Result<Vec<Delivery>> {
#[derive(serde::Deserialize)]
struct Wrapper {
#[serde(default)]
deliveries: Vec<Delivery>,
}
let w: Wrapper = self.client.request("/v1/webhooks/deliveries", &json!({}))?;
Ok(w.deliveries)
}
pub fn test_payment(&self, params: Value) -> Result<Value> {
self.client.request("/v1/test-webhook/payment", ¶ms)
}
pub fn test_wallet(&self, params: Value) -> Result<Value> {
self.client.request("/v1/test-webhook/wallet", ¶ms)
}
pub fn test_payout(&self, params: Value) -> Result<Value> {
self.client.request("/v1/test-webhook/payout", ¶ms)
}
}
pub struct Settings<'a> {
pub(crate) client: &'a Client,
}
impl Settings<'_> {
pub fn list_auto_withdraw(&self) -> Result<Vec<AutoWithdrawRule>> {
#[derive(serde::Deserialize)]
struct Wrapper {
#[serde(default)]
rules: Vec<AutoWithdrawRule>,
}
let w: Wrapper = self.client.request("/v1/auto-withdraw/list", &json!({}))?;
Ok(w.rules)
}
pub fn set_auto_withdraw(&self, params: Value) -> Result<Value> {
self.client.request("/v1/auto-withdraw/set", ¶ms)
}
pub fn delete_auto_withdraw(&self, currency: &str) -> Result<Value> {
self.client
.request("/v1/auto-withdraw/delete", &json!({ "currency": currency }))
}
pub fn list_allowlist(&self) -> Result<Value> {
self.client.request("/v1/api-allowlist/list", &json!({}))
}
pub fn add_allowlist(&self, cidr: &str) -> Result<Value> {
self.client
.request("/v1/api-allowlist/add", &json!({ "cidr": cidr }))
}
pub fn remove_allowlist(&self, cidr: &str) -> Result<Value> {
self.client
.request("/v1/api-allowlist/remove", &json!({ "cidr": cidr }))
}
pub fn enable_allowlist(&self, enabled: bool) -> Result<Value> {
self.client
.request("/v1/api-allowlist/enable", &json!({ "enabled": enabled }))
}
}
pub struct Rates<'a> {
pub(crate) client: &'a Client,
}
impl Rates<'_> {
pub fn list(&self, currency_from: Option<&str>) -> Result<Vec<ExchangeRate>> {
let body = match currency_from {
Some(c) => json!({ "currency_from": c }),
None => json!({}),
};
self.client.request_public("/v1/exchange-rate/list", &body)
}
pub fn currencies(&self) -> Result<Vec<Currency>> {
#[derive(serde::Deserialize)]
struct Wrap {
#[serde(default)]
currencies: Vec<Currency>,
}
let w: Wrap = self.client.request_public_get("/v1/currencies")?;
Ok(w.currencies)
}
}