reevit 0.2.0

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

use crate::{
    Client, CreatePaymentLinkRequest, PaginationOptions, PaymentLink, PaymentLinkListOptions,
    PaymentLinkStats, PaymentSummary, RequestOptions, Result, UpdatePaymentLinkRequest,
};

use super::path_segment;

/// Hosted payment link operations.
#[derive(Debug, Clone)]
pub struct PaymentLinks {
    client: Client,
}

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

    /// Lists payment links.
    pub async fn list(&self, options: &PaymentLinkListOptions) -> Result<Vec<PaymentLink>> {
        let request = self
            .client
            .request(Method::GET, "/v1/payment-links")?
            .query(options);
        self.client.send_collection(request, "payment_links").await
    }

    /// Creates a payment link.
    pub async fn create(
        &self,
        input: &CreatePaymentLinkRequest,
        options: RequestOptions,
    ) -> Result<PaymentLink> {
        let request = self
            .client
            .request(Method::POST, "/v1/payment-links")?
            .json(input);
        self.client.send(request, options).await
    }

    /// Retrieves a payment link by ID.
    pub async fn get(&self, id: &str) -> Result<PaymentLink> {
        self.get_path(&format!("/v1/payment-links/{}", path_segment(id)))
            .await
    }

    /// Updates a payment link.
    pub async fn update(
        &self,
        id: &str,
        input: &UpdatePaymentLinkRequest,
        options: RequestOptions,
    ) -> Result<PaymentLink> {
        let request = self
            .client
            .request(
                Method::PATCH,
                &format!("/v1/payment-links/{}", path_segment(id)),
            )?
            .json(input);
        self.client.send(request, options).await
    }

    /// Deletes a payment link.
    pub async fn delete(&self, id: &str, options: RequestOptions) -> Result<()> {
        let request = self.client.request(
            Method::DELETE,
            &format!("/v1/payment-links/{}", path_segment(id)),
        )?;
        self.client.send(request, options).await
    }

    /// Returns aggregate statistics for a payment link.
    pub async fn stats(&self, id: &str) -> Result<PaymentLinkStats> {
        let request = self.client.request(
            Method::GET,
            &format!("/v1/payment-links/{}/stats", path_segment(id)),
        )?;
        self.client.send(request, RequestOptions::default()).await
    }

    /// Lists payments created from a payment link.
    pub async fn list_payments(
        &self,
        id: &str,
        options: &PaginationOptions,
    ) -> Result<Vec<PaymentSummary>> {
        let request = self
            .client
            .request(
                Method::GET,
                &format!("/v1/payment-links/{}/payments", path_segment(id)),
            )?
            .query(options);
        self.client.send_collection(request, "payments").await
    }

    /// Resolves a public payment link by code.
    pub async fn get_by_code(&self, code: &str) -> Result<PaymentLink> {
        self.get_path(&format!("/v1/pay/{}", path_segment(code)))
            .await
    }

    async fn get_path(&self, path: &str) -> Result<PaymentLink> {
        let request = self.client.request(Method::GET, path)?;
        self.client.send(request, RequestOptions::default()).await
    }
}