Skip to main content

Payments

Struct Payments 

Source
pub struct Payments { /* private fields */ }
Expand description

Payment operations for transaction processing and refunds

Implementations§

Source§

impl Payments

Source

pub fn create(&self, input: CreatePayment) -> Result<Payment>

Create a new payment

§Example
use stateset_embedded::{Commerce, CreatePayment, PaymentMethodType, CardBrand, OrderId, CurrencyCode};
use rust_decimal_macros::dec;

let commerce = Commerce::new("./store.db")?;

let payment = commerce.payments().create(CreatePayment {
    order_id: Some(OrderId::new()),
    payment_method: PaymentMethodType::CreditCard,
    amount: dec!(149.99),
    currency: Some(CurrencyCode::USD),
    card_brand: Some(CardBrand::Visa),
    card_last4: Some("4242".into()),
    billing_email: Some("customer@example.com".into()),
    ..Default::default()
})?;
Source

pub fn get(&self, id: PaymentId) -> Result<Option<Payment>>

Get a payment by ID

Source

pub fn get_by_number(&self, payment_number: &str) -> Result<Option<Payment>>

Get a payment by payment number (e.g., “PAY-20231215123456”)

Source

pub fn get_by_external_id(&self, external_id: &str) -> Result<Option<Payment>>

Get a payment by external ID (e.g., Stripe payment intent ID)

Source

pub fn update(&self, id: PaymentId, input: UpdatePayment) -> Result<Payment>

Update a payment

Source

pub fn list(&self, filter: PaymentFilter) -> Result<Vec<Payment>>

List payments with optional filtering

Source

pub fn for_order(&self, order_id: OrderId) -> Result<Vec<Payment>>

Get all payments for an order

Source

pub fn for_invoice(&self, invoice_id: Uuid) -> Result<Vec<Payment>>

Get all payments for an invoice

Source

pub fn mark_processing(&self, id: PaymentId) -> Result<Payment>

Mark payment as processing

Source

pub fn mark_completed(&self, id: PaymentId) -> Result<Payment>

Mark payment as completed

This records the payment timestamp and marks the transaction as successful.

Source

pub fn mark_failed( &self, id: PaymentId, reason: &str, code: Option<&str>, ) -> Result<Payment>

Mark payment as failed

§Arguments
  • id - Payment ID
  • reason - Human-readable failure reason
  • code - Optional error code from payment processor
Source

pub fn cancel(&self, id: PaymentId) -> Result<Payment>

Cancel a payment

Source

pub fn create_refund(&self, input: CreateRefund) -> Result<Refund>

Create a refund for a payment

§Example
use stateset_embedded::{Commerce, CreateRefund, PaymentId};
use rust_decimal_macros::dec;

let commerce = Commerce::new("./store.db")?;

// Full refund (omit amount for full refund)
let refund = commerce.payments().create_refund(CreateRefund {
    payment_id: PaymentId::new(),
    reason: Some("Customer request".into()),
    ..Default::default()
})?;

// Partial refund
let refund = commerce.payments().create_refund(CreateRefund {
    payment_id: PaymentId::new(),
    amount: Some(dec!(50.00)),
    reason: Some("Partial refund for damaged item".into()),
    ..Default::default()
})?;
Source

pub fn get_refund(&self, id: Uuid) -> Result<Option<Refund>>

Get a refund by ID

Source

pub fn get_refunds(&self, payment_id: PaymentId) -> Result<Vec<Refund>>

Get all refunds for a payment

Source

pub fn complete_refund(&self, id: Uuid) -> Result<Refund>

Complete a refund

This marks the refund as processed and updates the payment’s refunded amount.

Source

pub fn fail_refund(&self, id: Uuid, reason: &str) -> Result<Refund>

Mark a refund as failed

Source

pub fn create_payment_method( &self, input: CreatePaymentMethod, ) -> Result<PaymentMethod>

Create a stored payment method for a customer

§Example
use stateset_embedded::{Commerce, CreatePaymentMethod, PaymentMethodType, CardBrand, CustomerId};

let commerce = Commerce::new("./store.db")?;

let method = commerce.payments().create_payment_method(CreatePaymentMethod {
    customer_id: CustomerId::new(),
    method_type: PaymentMethodType::CreditCard,
    is_default: Some(true),
    card_brand: Some(CardBrand::Visa),
    card_last4: Some("4242".into()),
    card_exp_month: Some(12),
    card_exp_year: Some(2025),
    cardholder_name: Some("Alice Smith".into()),
    ..Default::default()
})?;
Source

pub fn get_payment_methods( &self, customer_id: CustomerId, ) -> Result<Vec<PaymentMethod>>

Get all payment methods for a customer

Source

pub fn delete_payment_method(&self, id: Uuid) -> Result<()>

Delete a payment method

Source

pub fn set_default_payment_method( &self, customer_id: CustomerId, method_id: Uuid, ) -> Result<()>

Set a payment method as the default for a customer

Source

pub fn count(&self, filter: PaymentFilter) -> Result<u64>

Count payments matching a filter

Trait Implementations§

Source§

impl Debug for Payments

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more