reevit 0.2.0

Official Rust SDK for the Reevit payments API
Documentation
use reqwest::Method;

use crate::{
    Client, Payment, PaymentIntent, PaymentIntentRequest, PaymentListOptions, PaymentStats,
    PaymentStatsOptions, PaymentSummary, Refund, RefundRequest, RequestOptions, Result,
    UpdatePaymentIntentRequest,
};

use super::path_segment;

/// Payment operations.
#[derive(Debug, Clone)]
pub struct Payments {
    client: Client,
}

impl Payments {
    pub(crate) fn new(client: Client) -> Self {
        Self { client }
    }

    /// Creates a payment intent.
    pub async fn create_intent(
        &self,
        input: &PaymentIntentRequest,
        options: RequestOptions,
    ) -> Result<PaymentIntent> {
        let request = self
            .client
            .request(Method::POST, "/v1/payments/intents")?
            .json(input);
        self.client.send(request, options).await
    }

    /// Lists payments for the configured organization.
    pub async fn list(&self, options: &PaymentListOptions) -> Result<Vec<PaymentSummary>> {
        let request = self
            .client
            .request(Method::GET, "/v1/payments")?
            .query(options);
        self.client.send_collection(request, "payments").await
    }

    /// Retrieves a payment by ID.
    pub async fn get(&self, id: &str) -> Result<Payment> {
        let request = self
            .client
            .request(Method::GET, &format!("/v1/payments/{}", path_segment(id)))?;
        self.client.send(request, RequestOptions::default()).await
    }

    /// Updates a pending payment intent.
    pub async fn update_intent(
        &self,
        id: &str,
        input: &UpdatePaymentIntentRequest,
        options: RequestOptions,
    ) -> Result<Payment> {
        let request = self
            .client
            .request(
                Method::PATCH,
                &format!("/v1/payments/intents/{}", path_segment(id)),
            )?
            .json(input);
        self.client.send(request, options).await
    }

    /// Confirms a payment with its provider.
    pub async fn confirm(&self, id: &str, options: RequestOptions) -> Result<Payment> {
        self.client
            .post_empty(
                &format!("/v1/payments/{}/confirm", path_segment(id)),
                options,
            )
            .await
    }

    /// Confirms a payment intent using its client secret.
    pub async fn confirm_intent(
        &self,
        id: &str,
        client_secret: &str,
        options: RequestOptions,
    ) -> Result<Payment> {
        let request = self
            .client
            .request(
                Method::POST,
                &format!("/v1/payments/{}/confirm-intent", path_segment(id)),
            )?
            .query(&[("client_secret", client_secret)])
            .json(&serde_json::json!({}));
        self.client.send(request, options).await
    }

    /// Cancels a pending payment.
    pub async fn cancel(&self, id: &str, options: RequestOptions) -> Result<Payment> {
        self.client
            .post_empty(
                &format!("/v1/payments/{}/cancel", path_segment(id)),
                options,
            )
            .await
    }

    /// Retries a failed payment.
    pub async fn retry(&self, id: &str, options: RequestOptions) -> Result<Payment> {
        self.client
            .post_empty(&format!("/v1/payments/{}/retry", path_segment(id)), options)
            .await
    }

    /// Refunds all or part of a payment.
    pub async fn refund(
        &self,
        id: &str,
        input: &RefundRequest,
        options: RequestOptions,
    ) -> Result<Refund> {
        let request = self
            .client
            .request(
                Method::POST,
                &format!("/v1/payments/{}/refund", path_segment(id)),
            )?
            .json(input);
        self.client.send(request, options).await
    }

    /// Returns aggregate payment statistics.
    pub async fn stats(&self, options: &PaymentStatsOptions) -> Result<PaymentStats> {
        let request = self
            .client
            .request(Method::GET, "/v1/payments/stats")?
            .query(options);
        self.client.send(request, RequestOptions::default()).await
    }
}