use super::common::*;
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Checkout {
#[serde(skip_serializing_if = "Option::is_none")]
pub checkout_reference: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub return_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<CheckoutStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub date: Option<crate::datetime::DateTime>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::nullable::deserialize"
)]
pub valid_until: Option<crate::Nullable<crate::datetime::DateTime>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mandate: Option<MandateResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hosted_checkout_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transactions: Option<Vec<CheckoutTransactionsItem>>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CheckoutSuccess {
#[serde(skip_serializing_if = "Option::is_none")]
pub checkout_reference: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub return_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<CheckoutSuccessStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub date: Option<crate::datetime::DateTime>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::nullable::deserialize"
)]
pub valid_until: Option<crate::Nullable<crate::datetime::DateTime>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mandate: Option<MandateResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hosted_checkout_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transactions: Option<Vec<CheckoutSuccessTransactionsItem>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub redirect_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_instrument: Option<CheckoutSuccessPaymentInstrument>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DetailsError {
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub failed_constraints: Option<Vec<DetailsErrorFailedConstraintsItem>>,
}
impl std::fmt::Display for DetailsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match (&self.title, &self.details) {
(Some(title), Some(details)) => write!(f, "{}: {}", title, details),
(Some(title), None) => write!(f, "{}", title),
(None, Some(details)) => write!(f, "{}", details),
(None, None) => write!(f, "{:?}", self),
}
}
}
impl std::error::Error for DetailsError {}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct HostedCheckout {
pub enabled: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum CheckoutStatus {
#[serde(rename = "PENDING")]
Pending,
#[serde(rename = "FAILED")]
Failed,
#[serde(rename = "PAID")]
Paid,
#[serde(rename = "EXPIRED")]
Expired,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CheckoutTransactionsItem {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<crate::datetime::DateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<TransactionStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_type: Option<PaymentType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub installments_count: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vat_amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tip_amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entry_mode: Option<EntryMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth_code: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum CheckoutSuccessStatus {
#[serde(rename = "PENDING")]
Pending,
#[serde(rename = "FAILED")]
Failed,
#[serde(rename = "PAID")]
Paid,
#[serde(rename = "EXPIRED")]
Expired,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CheckoutSuccessTransactionsItem {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<crate::datetime::DateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<TransactionStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_type: Option<PaymentType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub installments_count: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vat_amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tip_amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entry_mode: Option<EntryMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth_code: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CheckoutSuccessPaymentInstrument {
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DetailsErrorFailedConstraintsItem {
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reference: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum CreateRequestPurpose {
#[serde(rename = "CHECKOUT")]
Checkout,
#[serde(rename = "SETUP_RECURRING_PAYMENT")]
SetupRecurringPayment,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ListAvailablePaymentMethodsResponseAvailablePaymentMethodsItem {
pub id: String,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ListParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub checkout_reference: Option<String>,
}
pub type ListResponse = Vec<CheckoutSuccess>;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CreateRequest {
pub checkout_reference: String,
pub amount: f32,
pub currency: Currency,
pub merchant_code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub return_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub purpose: Option<CreateRequestPurpose>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::nullable::deserialize"
)]
pub valid_until: Option<crate::Nullable<crate::datetime::DateTime>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub redirect_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hosted_checkout: Option<HostedCheckout>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct UpdateRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub checkout_reference: Option<String>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::nullable::deserialize"
)]
pub valid_until: Option<crate::Nullable<crate::datetime::DateTime>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer_id: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ListAvailablePaymentMethodsParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ListAvailablePaymentMethodsResponse {
#[serde(skip_serializing_if = "Option::is_none")]
pub available_payment_methods:
Option<Vec<ListAvailablePaymentMethodsResponseAvailablePaymentMethodsItem>>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CreateApplePaySessionRequest {
pub context: String,
pub target: String,
}
pub type CreateApplePaySessionResponse = serde_json::Value;
use crate::client::Client;
#[derive(Debug, PartialEq)]
pub enum ListErrorBody {
Unauthorized(Problem),
}
#[derive(Debug, PartialEq)]
pub enum CreateErrorBody {
BadRequest(ErrorExtended),
Unauthorized(Problem),
Forbidden(ErrorForbidden),
Conflict(Error),
}
#[derive(Debug, PartialEq)]
pub enum DeactivateErrorBody {
Unauthorized(Problem),
NotFound(Error),
Conflict(Error),
}
#[derive(Debug, PartialEq)]
pub enum GetErrorBody {
Unauthorized(Problem),
NotFound(Error),
}
#[derive(Debug, PartialEq)]
pub enum UpdateErrorBody {
Unauthorized(Problem),
NotFound(Error),
}
#[derive(Debug, PartialEq)]
pub enum ListAvailablePaymentMethodsErrorBody {
BadRequest(DetailsError),
}
#[derive(Debug, PartialEq)]
pub enum CreateApplePaySessionErrorBody {
BadRequest(crate::error::UnknownApiBody),
NotFound(Error),
}
#[derive(Debug)]
pub struct CheckoutsClient<'a> {
client: &'a Client,
}
impl<'a> CheckoutsClient<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub fn client(&self) -> &Client {
self.client
}
pub async fn list(
&self,
params: ListParams,
) -> crate::error::SdkResult<ListResponse, ListErrorBody> {
let path = "/v0.1/checkouts";
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
if let Some(ref value) = params.checkout_reference {
request = request.query(&[("checkout_reference", value)]);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: ListResponse = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
let body: Problem = response.json().await?;
Err(crate::error::SdkError::api(ListErrorBody::Unauthorized(
body,
)))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn create(
&self,
body: CreateRequest,
) -> crate::error::SdkResult<Checkout, CreateErrorBody> {
let path = "/v0.1/checkouts";
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.post(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout())
.json(&body);
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::CREATED => {
let data: Checkout = response.json().await?;
Ok(data)
}
reqwest::StatusCode::BAD_REQUEST => {
let body: ErrorExtended = response.json().await?;
Err(crate::error::SdkError::api(CreateErrorBody::BadRequest(
body,
)))
}
reqwest::StatusCode::UNAUTHORIZED => {
let body: Problem = response.json().await?;
Err(crate::error::SdkError::api(CreateErrorBody::Unauthorized(
body,
)))
}
reqwest::StatusCode::FORBIDDEN => {
let body: ErrorForbidden = response.json().await?;
Err(crate::error::SdkError::api(CreateErrorBody::Forbidden(
body,
)))
}
reqwest::StatusCode::CONFLICT => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(CreateErrorBody::Conflict(body)))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn deactivate(
&self,
checkout_id: impl Into<String>,
) -> crate::error::SdkResult<Checkout, DeactivateErrorBody> {
let path = format!("/v0.1/checkouts/{}", checkout_id.into());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.delete(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: Checkout = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
let body: Problem = response.json().await?;
Err(crate::error::SdkError::api(
DeactivateErrorBody::Unauthorized(body),
))
}
reqwest::StatusCode::NOT_FOUND => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(DeactivateErrorBody::NotFound(
body,
)))
}
reqwest::StatusCode::CONFLICT => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(DeactivateErrorBody::Conflict(
body,
)))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn get(
&self,
checkout_id: impl Into<String>,
) -> crate::error::SdkResult<CheckoutSuccess, GetErrorBody> {
let path = format!("/v0.1/checkouts/{}", checkout_id.into());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: CheckoutSuccess = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
let body: Problem = response.json().await?;
Err(crate::error::SdkError::api(GetErrorBody::Unauthorized(
body,
)))
}
reqwest::StatusCode::NOT_FOUND => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(GetErrorBody::NotFound(body)))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn update(
&self,
checkout_id: impl Into<String>,
body: UpdateRequest,
) -> crate::error::SdkResult<Checkout, UpdateErrorBody> {
let path = format!("/v0.1/checkouts/{}", checkout_id.into());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.patch(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout())
.json(&body);
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: Checkout = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
let body: Problem = response.json().await?;
Err(crate::error::SdkError::api(UpdateErrorBody::Unauthorized(
body,
)))
}
reqwest::StatusCode::NOT_FOUND => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(UpdateErrorBody::NotFound(body)))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn list_available_payment_methods(
&self,
merchant_code: impl Into<String>,
params: ListAvailablePaymentMethodsParams,
) -> crate::error::SdkResult<
ListAvailablePaymentMethodsResponse,
ListAvailablePaymentMethodsErrorBody,
> {
let path = format!("/v0.1/merchants/{}/payment-methods", merchant_code.into());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
if let Some(ref value) = params.amount {
request = request.query(&[("amount", value)]);
}
if let Some(ref value) = params.currency {
request = request.query(&[("currency", value)]);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: ListAvailablePaymentMethodsResponse = response.json().await?;
Ok(data)
}
reqwest::StatusCode::BAD_REQUEST => {
let body: DetailsError = response.json().await?;
Err(crate::error::SdkError::api(
ListAvailablePaymentMethodsErrorBody::BadRequest(body),
))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
pub async fn create_apple_pay_session(
&self,
checkout_id: impl Into<String>,
body: Option<CreateApplePaySessionRequest>,
) -> crate::error::SdkResult<CreateApplePaySessionResponse, CreateApplePaySessionErrorBody>
{
let path = format!("/v0.2/checkouts/{}/apple-pay-session", checkout_id.into());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.put(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
if let Some(body) = body {
request = request.json(&body);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: CreateApplePaySessionResponse = response.json().await?;
Ok(data)
}
reqwest::StatusCode::BAD_REQUEST => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::api(
CreateApplePaySessionErrorBody::BadRequest(body),
))
}
reqwest::StatusCode::NOT_FOUND => {
let body: Error = response.json().await?;
Err(crate::error::SdkError::api(
CreateApplePaySessionErrorBody::NotFound(body),
))
}
_ => {
let body_bytes = response.bytes().await?;
let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
Err(crate::error::SdkError::unexpected(status, body))
}
}
}
}