mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 09:07:09 +08:00
feat(router): adding generic tokenization endpoint (#7905)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -179,6 +179,10 @@ pub const DEFAULT_CARD_TESTING_GUARD_EXPIRY_IN_SECS: i32 = 3600;
|
||||
/// SOAP 1.1 Envelope Namespace
|
||||
pub const SOAP_ENV_NAMESPACE: &str = "http://schemas.xmlsoap.org/soap/envelope/";
|
||||
|
||||
#[cfg(all(feature = "v2", feature = "tokenization_v2"))]
|
||||
/// Length of generated tokens
|
||||
pub const TOKEN_LENGTH: usize = 32;
|
||||
|
||||
/// The tag name used for identifying the host in metrics.
|
||||
pub const METRICS_HOST_TAG_NAME: &str = "host";
|
||||
|
||||
|
||||
@ -121,6 +121,10 @@ pub enum ApiEventsType {
|
||||
PaymentMethodSession {
|
||||
payment_method_session_id: id_type::GlobalPaymentMethodSessionId,
|
||||
},
|
||||
#[cfg(feature = "v2")]
|
||||
Token {
|
||||
token_id: Option<id_type::GlobalTokenId>,
|
||||
},
|
||||
ProcessTracker,
|
||||
}
|
||||
|
||||
|
||||
@ -34,6 +34,7 @@ pub use self::global_id::{
|
||||
payment::{GlobalAttemptId, GlobalPaymentId},
|
||||
payment_methods::{GlobalPaymentMethodId, GlobalPaymentMethodSessionId},
|
||||
refunds::GlobalRefundId,
|
||||
token::GlobalTokenId,
|
||||
CellId,
|
||||
};
|
||||
pub use self::{
|
||||
|
||||
@ -2,6 +2,7 @@ pub(super) mod customer;
|
||||
pub(super) mod payment;
|
||||
pub(super) mod payment_methods;
|
||||
pub(super) mod refunds;
|
||||
pub(super) mod token;
|
||||
|
||||
use diesel::{backend::Backend, deserialize::FromSql, serialize::ToSql, sql_types};
|
||||
use error_stack::ResultExt;
|
||||
@ -27,6 +28,7 @@ pub(crate) enum GlobalEntity {
|
||||
PaymentMethod,
|
||||
Refund,
|
||||
PaymentMethodSession,
|
||||
Token,
|
||||
}
|
||||
|
||||
impl GlobalEntity {
|
||||
@ -38,6 +40,7 @@ impl GlobalEntity {
|
||||
Self::Attempt => "att",
|
||||
Self::Refund => "ref",
|
||||
Self::PaymentMethodSession => "pms",
|
||||
Self::Token => "tok",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
crates/common_utils/src/id_type/global_id/token.rs
Normal file
37
crates/common_utils/src/id_type/global_id/token.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use error_stack::ResultExt;
|
||||
|
||||
use crate::{errors, generate_id_with_default_len, generate_time_ordered_id_without_prefix, types};
|
||||
|
||||
crate::global_id_type!(
|
||||
GlobalTokenId,
|
||||
"A global id that can be used to identify a token.
|
||||
|
||||
The format will be `<cell_id>_<entity_prefix>_<time_ordered_id>`.
|
||||
|
||||
Example: `cell1_tok_uu1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p`"
|
||||
);
|
||||
|
||||
// Database related implementations so that this field can be used directly in the database tables
|
||||
crate::impl_queryable_id_type!(GlobalTokenId);
|
||||
crate::impl_to_sql_from_sql_global_id_type!(GlobalTokenId);
|
||||
|
||||
impl GlobalTokenId {
|
||||
/// Get string representation of the id
|
||||
pub fn get_string_repr(&self) -> &str {
|
||||
self.0.get_string_repr()
|
||||
}
|
||||
|
||||
/// Generate a new GlobalTokenId from a cell id
|
||||
pub fn generate(cell_id: &crate::id_type::CellId) -> Self {
|
||||
let global_id = super::GlobalId::generate(cell_id, super::GlobalEntity::Token);
|
||||
Self(global_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::events::ApiEventMetric for GlobalTokenId {
|
||||
fn get_api_event_type(&self) -> Option<crate::events::ApiEventsType> {
|
||||
Some(crate::events::ApiEventsType::Token {
|
||||
token_id: Some(self.clone()),
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -333,3 +333,10 @@ mod nanoid_tests {
|
||||
assert!(ref_id.is_ok())
|
||||
}
|
||||
}
|
||||
|
||||
/// Module for tokenization-related functionality
|
||||
///
|
||||
/// This module provides types and functions for handling tokenized payment data,
|
||||
/// including response structures and token generation utilities.
|
||||
#[cfg(all(feature = "v2", feature = "tokenization_v2"))]
|
||||
pub mod tokenization;
|
||||
|
||||
21
crates/common_utils/src/tokenization.rs
Normal file
21
crates/common_utils/src/tokenization.rs
Normal file
@ -0,0 +1,21 @@
|
||||
//! Module for tokenization-related functionality
|
||||
//!
|
||||
//! This module provides types and functions for handling tokenized payment data,
|
||||
//! including response structures and token generation utilities.
|
||||
|
||||
use common_enums::ApiVersion;
|
||||
use diesel;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{consts::TOKEN_LENGTH, id_type::GlobalTokenId};
|
||||
|
||||
#[cfg(all(feature = "v2", feature = "tokenization_v2"))]
|
||||
/// Generates a new token string
|
||||
///
|
||||
/// # Returns
|
||||
/// A randomly generated token string of length `TOKEN_LENGTH`
|
||||
pub fn generate_token() -> String {
|
||||
use nanoid::nanoid;
|
||||
nanoid!(TOKEN_LENGTH)
|
||||
}
|
||||
Reference in New Issue
Block a user