feat(payment_methods_v2): Payment method Create API (#5812)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sarthak Soni
2024-09-18 12:22:09 +05:30
committed by GitHub
parent 0442004024
commit be902ffa53
35 changed files with 2168 additions and 798 deletions

View File

@ -27,6 +27,8 @@ pub enum ApiEventsType {
payment_method: Option<PaymentMethod>,
payment_method_type: Option<PaymentMethodType>,
},
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
PaymentMethodCreate,
#[cfg(all(feature = "v2", feature = "customer_v2"))]
Customer {
id: String,

View File

@ -12,6 +12,8 @@ mod profile;
mod routing;
mod global_id;
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
mod payment_methods;
pub use customer::CustomerId;
use diesel::{
@ -25,6 +27,8 @@ pub use merchant::MerchantId;
pub use merchant_connector_account::MerchantConnectorAccountId;
pub use organization::OrganizationId;
pub use payment::PaymentId;
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
pub use payment_methods::GlobalPaymentMethodId;
pub use profile::ProfileId;
pub use routing::RoutingId;
use serde::{Deserialize, Serialize};

View File

@ -21,6 +21,7 @@ pub(crate) struct GlobalId(LengthId<MAX_GLOBAL_ID_LENGTH, MIN_GLOBAL_ID_LENGTH>)
pub(crate) enum GlobalEntity {
Customer,
Payment,
PaymentMethod,
}
impl GlobalEntity {
@ -28,6 +29,7 @@ impl GlobalEntity {
match self {
Self::Customer => "cus",
Self::Payment => "pay",
Self::PaymentMethod => "pm",
}
}
}

View File

@ -0,0 +1,85 @@
use diesel::{backend::Backend, deserialize::FromSql, serialize::ToSql, sql_types};
use error_stack::ResultExt;
use crate::{
errors,
errors::CustomResult,
id_type::global_id::{CellId, GlobalEntity, GlobalId, GlobalIdError},
};
#[derive(
Debug,
Clone,
Hash,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
diesel::expression::AsExpression,
)]
#[diesel(sql_type = diesel::sql_types::Text)]
pub struct GlobalPaymentMethodId(GlobalId);
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
pub enum GlobalPaymentMethodIdError {
#[error("Failed to construct GlobalPaymentMethodId")]
ConstructionError,
}
impl GlobalPaymentMethodId {
fn get_global_id(&self) -> &GlobalId {
&self.0
}
/// Create a new GlobalPaymentMethodId from celll id information
pub fn generate(cell_id: &str) -> error_stack::Result<Self, errors::ValidationError> {
let cell_id = CellId::from_string(cell_id.to_string())?;
let global_id = GlobalId::generate(cell_id, GlobalEntity::PaymentMethod);
Ok(Self(global_id))
}
pub fn get_string_repr(&self) -> String {
todo!()
}
pub fn generate_from_string(value: String) -> CustomResult<Self, GlobalPaymentMethodIdError> {
let id = GlobalId::from_string(value)
.change_context(GlobalPaymentMethodIdError::ConstructionError)?;
Ok(Self(id))
}
}
impl<DB> diesel::Queryable<diesel::sql_types::Text, DB> for GlobalPaymentMethodId
where
DB: diesel::backend::Backend,
Self: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,
{
type Row = Self;
fn build(row: Self::Row) -> diesel::deserialize::Result<Self> {
Ok(row)
}
}
impl<DB> ToSql<sql_types::Text, DB> for GlobalPaymentMethodId
where
DB: Backend,
GlobalId: ToSql<sql_types::Text, DB>,
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, DB>,
) -> diesel::serialize::Result {
let id = self.get_global_id();
id.to_sql(out)
}
}
impl<DB> FromSql<sql_types::Text, DB> for GlobalPaymentMethodId
where
DB: Backend,
GlobalId: FromSql<sql_types::Text, DB>,
{
fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let global_id = GlobalId::from_sql(value)?;
Ok(Self(global_id))
}
}