mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
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:
@ -21,6 +21,7 @@ payouts = ["common_enums/payouts"]
|
||||
v1 = []
|
||||
v2 = []
|
||||
customer_v2 = []
|
||||
payment_methods_v2 = []
|
||||
|
||||
[dependencies]
|
||||
async-trait = { version = "0.1.79", optional = true }
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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};
|
||||
|
||||
@ -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",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
85
crates/common_utils/src/id_type/payment_methods.rs
Normal file
85
crates/common_utils/src/id_type/payment_methods.rs
Normal 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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user