use std::borrow::Cow;
use derive_builder::Builder;
use serde::Serialize;
use crate::{
data::orders::{Order, OrderPayload},
endpoint::Endpoint,
};
#[derive(Debug)]
pub struct CreateOrder {
pub order: OrderPayload,
}
impl CreateOrder {
pub fn new(order: OrderPayload) -> Self {
Self { order }
}
}
impl Endpoint for CreateOrder {
type Query = ();
type Body = OrderPayload;
type Response = Order;
fn relative_path(&self) -> Cow<str> {
Cow::Borrowed("/v2/checkout/orders")
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.order.clone())
}
}
#[derive(Debug)]
pub struct ShowOrderDetails {
pub order_id: String,
}
impl ShowOrderDetails {
pub fn new(order_id: &str) -> Self {
Self {
order_id: order_id.to_string(),
}
}
}
impl Endpoint for ShowOrderDetails {
type Query = ();
type Body = ();
type Response = Order;
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/checkout/orders/{}", self.order_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::GET
}
}
#[derive(Debug, Serialize, Builder, Clone)]
pub struct PaymentSourceToken {
pub id: String,
pub r#type: String,
}
#[derive(Debug, Serialize, Builder, Clone)]
pub struct PaymentSource {
pub token: PaymentSourceToken,
}
#[derive(Debug, Serialize, Clone, Default)]
pub struct PaymentSourceBody {
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_source: Option<PaymentSource>,
}
#[derive(Debug, Clone, Builder)]
pub struct CaptureOrder {
pub order_id: String,
pub body: PaymentSourceBody,
}
impl CaptureOrder {
pub fn new(order_id: &str) -> Self {
Self {
order_id: order_id.to_string(),
body: PaymentSourceBody::default(),
}
}
}
impl Endpoint for CaptureOrder {
type Query = ();
type Body = PaymentSourceBody;
type Response = Order;
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/checkout/orders/{}/capture", self.order_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.body.clone())
}
}
#[derive(Debug)]
pub struct AuthorizeOrder {
order_id: String,
pub body: PaymentSourceBody,
}
impl AuthorizeOrder {
pub fn new(order_id: &str) -> Self {
Self {
order_id: order_id.to_string(),
body: PaymentSourceBody::default(),
}
}
}
impl Endpoint for AuthorizeOrder {
type Query = ();
type Body = PaymentSourceBody;
type Response = Order;
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/checkout/orders/{}/authorize", self.order_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.body.clone())
}
}