Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): add db interface for /relay #6879

Merged
merged 22 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
171b23b
feat(router): add /relay endpoint
ShankarSinghC Dec 18, 2024
53afd05
docs(openapi): re-generate OpenAPI specification
hyperswitch-bot[bot] Dec 18, 2024
f2e1642
fix fmt
ShankarSinghC Dec 18, 2024
05c9174
Merge branch 'main' into relay/api-contract
ShankarSinghC Dec 18, 2024
cbd83af
add db interface for /relay
ShankarSinghC Dec 18, 2024
bee0022
Merge branch 'main' of https://github.com/juspay/hyperswitch into rel…
ShankarSinghC Dec 18, 2024
9642a52
Merge branch 'main' into relay/api-contract
ShankarSinghC Dec 18, 2024
eaa65ca
chore: run formatter
hyperswitch-bot[bot] Dec 18, 2024
0b5a2e9
Merge branch 'main' into relay/api-contract
ShankarSinghC Dec 19, 2024
7194393
Merge branch 'relay/api-contract' into relay/db-interface
ShankarSinghC Dec 19, 2024
3f27d09
add /rely endpoint to suppport proxy refund usecase
ShankarSinghC Dec 21, 2024
6d697b1
Merge branch 'main' of https://github.com/juspay/hyperswitch into rel…
ShankarSinghC Dec 21, 2024
ca56a8f
Merge branch 'relay/db-interface' of https://github.com/juspay/hypers…
ShankarSinghC Dec 21, 2024
047c75f
chore: run formatter
hyperswitch-bot[bot] Dec 21, 2024
3e8b3e5
address pr comments
ShankarSinghC Dec 22, 2024
1ed3a7c
chore: run formatter
hyperswitch-bot[bot] Dec 22, 2024
38f12a4
address pr comments
ShankarSinghC Dec 22, 2024
cb635df
Merge branch 'relay/db-interface' of https://github.com/juspay/hypers…
ShankarSinghC Dec 22, 2024
79759b1
address pr comments
ShankarSinghC Dec 22, 2024
86b1e39
address pr comments
ShankarSinghC Dec 22, 2024
0cd4143
update v2 schema
ShankarSinghC Dec 22, 2024
2251e52
address pr comments
ShankarSinghC Dec 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -23213,7 +23213,15 @@
"RelayData": {
"oneOf": [
{
"$ref": "#/components/schemas/RelayRefundRequest"
"type": "object",
"required": [
"refund"
],
"properties": {
"refund": {
"$ref": "#/components/schemas/RelayRefundRequest"
}
}
}
]
},
Expand Down Expand Up @@ -23269,12 +23277,12 @@
"properties": {
"connector_resource_id": {
"type": "string",
"description": "The identifier that is associated to a resource at the connector to which the relay request is being made",
"description": "The identifier that is associated to a resource at the connector reference to which the relay request is being made",
"example": "7256228702616471803954"
},
"connector_id": {
"type": "string",
"description": "Identifier of the connector ( merchant connector account ) to which relay request is being made",
"description": "Identifier of the connector ( merchant connector account ) which was chosen to make the payment",
"example": "mca_5apGeP94tMts6rg3U3kR"
},
"type": {
Expand Down Expand Up @@ -23309,11 +23317,10 @@
"status": {
"$ref": "#/components/schemas/RelayStatus"
},
"connector_reference_id": {
"connector_resource_id": {
"type": "string",
"description": "The reference identifier provided by the connector for the relay request",
"example": "pi_3MKEivSFNglxLpam0ZaL98q9",
"nullable": true
"description": "The identifier that is associated to a resource at the connector reference to which the relay request is being made",
"example": "pi_3MKEivSFNglxLpam0ZaL98q9"
},
"error": {
"allOf": [
Expand All @@ -23323,19 +23330,20 @@
],
"nullable": true
},
"connector_resource_id": {
"connector_reference_id": {
"type": "string",
"description": "The identifier that is associated to a resource at the connector to which the relay request is being made",
"example": "7256228702616471803954"
"example": "re_3QY4TnEOqOywnAIx1Mm1p7GQ",
"nullable": true
},
"connector_id": {
"type": "string",
"description": "Identifier of the connector ( merchant connector account ) to which relay request is being made",
"description": "Identifier of the connector ( merchant connector account ) which was chosen to make the payment",
"example": "mca_5apGeP94tMts6rg3U3kR"
},
"profile_id": {
"type": "string",
"description": "The business profile that is associated with this relay request",
"description": "The business profile that is associated with this relay request.",
"example": "pro_abcdefghijklmnopqrstuvwxyz"
},
"type": {
Expand All @@ -23354,8 +23362,9 @@
"RelayStatus": {
"type": "string",
"enum": [
"created",
"pending",
"success",
"processing",
"failure"
]
},
Expand Down
53 changes: 21 additions & 32 deletions crates/api_models/src/relay.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
pub use common_utils::types::MinorUnit;
use common_utils::types::MinorUnit;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::enums;
use crate::enums as api_enums;

#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
pub struct RelayRequest {
/// The identifier that is associated to a resource at the connector to which the relay request is being made
/// The identifier that is associated to a resource at the connector reference to which the relay request is being made
#[schema(example = "7256228702616471803954")]
pub connector_resource_id: String,
/// Identifier of the connector ( merchant connector account ) to which relay request is being made
/// Identifier of the connector ( merchant connector account ) which was chosen to make the payment
#[schema(example = "mca_5apGeP94tMts6rg3U3kR", value_type = String)]
pub connector_id: common_utils::id_type::MerchantConnectorAccountId,
/// The type of relay request
#[serde(rename = "type")]
pub relay_type: RelayType,
#[schema(value_type = RelayType)]
pub relay_type: api_enums::RelayType,
/// The data that is associated with the relay request
pub data: Option<RelayData>,
}

#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RelayType {
/// The relay request is for a refund
Refund,
}

#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", untagged)]
pub enum RelayData {
/// The data that is associated with a refund relay request
Refund(RelayRefundRequest),
Expand All @@ -40,7 +34,7 @@ pub struct RelayRefundRequest {
pub amount: MinorUnit,
/// The currency in which the amount is being refunded
#[schema(value_type = Currency)]
pub currency: enums::Currency,
pub currency: api_enums::Currency,
/// The reason for the refund
#[schema(max_length = 255, example = "Customer returned the product")]
pub reason: Option<String>,
Expand All @@ -52,39 +46,30 @@ pub struct RelayResponse {
#[schema(example = "relay_mbabizu24mvu3mela5njyhpit4", value_type = String)]
pub id: common_utils::id_type::RelayId,
/// The status of the relay request
pub status: RelayStatus,
/// The reference identifier provided by the connector for the relay request
#[schema(value_type = RelayStatus)]
pub status: api_enums::RelayStatus,
/// The identifier that is associated to a resource at the connector reference to which the relay request is being made
#[schema(example = "pi_3MKEivSFNglxLpam0ZaL98q9")]
pub connector_reference_id: Option<String>,
pub connector_resource_id: String,
/// The error details if the relay request failed
pub error: Option<RelayError>,
/// The identifier that is associated to a resource at the connector to which the relay request is being made
#[schema(example = "7256228702616471803954")]
pub connector_resource_id: String,
/// Identifier of the connector ( merchant connector account ) to which relay request is being made
#[schema(example = "re_3QY4TnEOqOywnAIx1Mm1p7GQ")]
pub connector_reference_id: Option<String>,
/// Identifier of the connector ( merchant connector account ) which was chosen to make the payment
#[schema(example = "mca_5apGeP94tMts6rg3U3kR", value_type = String)]
pub connector_id: common_utils::id_type::MerchantConnectorAccountId,
/// The business profile that is associated with this relay request
/// The business profile that is associated with this relay request.
#[schema(example = "pro_abcdefghijklmnopqrstuvwxyz", value_type = String)]
pub profile_id: common_utils::id_type::ProfileId,
/// The type of relay request
#[serde(rename = "type")]
pub relay_type: RelayType,
#[schema(value_type = RelayType)]
pub relay_type: api_enums::RelayType,
/// The data that is associated with the relay request
pub data: Option<RelayData>,
}

#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RelayStatus {
/// The relay request is successful
Success,
/// The relay request is being processed
Processing,
/// The relay request has failed
Failure,
}

#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
pub struct RelayError {
/// The error code
Expand All @@ -101,3 +86,7 @@ pub struct RelayRetrieveRequest {
/// The unique identifier for the Relay
pub id: String,
}

impl common_utils::events::ApiEventMetric for RelayRequest {}

impl common_utils::events::ApiEventMetric for RelayResponse {}
67 changes: 67 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,53 @@ pub enum RefundStatus {
TransactionFailure,
}

#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
Hash,
PartialEq,
strum::Display,
strum::EnumString,
strum::EnumIter,
serde::Serialize,
serde::Deserialize,
ToSchema,
)]
#[router_derive::diesel_enum(storage_type = "db_enum")]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum RelayStatus {
Created,
#[default]
Pending,
Success,
Failure,
}

#[derive(
Clone,
Copy,
Debug,
Eq,
Hash,
PartialEq,
strum::Display,
strum::EnumString,
strum::EnumIter,
serde::Serialize,
serde::Deserialize,
ToSchema,
)]
#[router_derive::diesel_enum(storage_type = "db_enum")]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum RelayType {
Refund,
}

#[derive(
Clone,
Copy,
Expand Down Expand Up @@ -3372,6 +3419,26 @@ impl From<ConnectorType> for TransactionType {
}
}

impl From<RefundStatus> for RelayStatus {
fn from(refund_status: RefundStatus) -> Self {
match refund_status {
RefundStatus::Failure | RefundStatus::TransactionFailure => Self::Failure,
RefundStatus::ManualReview | RefundStatus::Pending => Self::Pending,
RefundStatus::Success => Self::Success,
}
}
}

impl From<RelayStatus> for RefundStatus {
fn from(relay_status: RelayStatus) -> Self {
match relay_status {
RelayStatus::Failure => Self::Failure,
RelayStatus::Pending | RelayStatus::Created => Self::Pending,
RelayStatus::Success => Self::Success,
}
}
}

#[derive(
Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize, Default, ToSchema,
)]
Expand Down
6 changes: 6 additions & 0 deletions crates/common_utils/src/id_type/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ crate::id_type!(
);
crate::impl_id_type_methods!(RelayId, "relay_id");

crate::impl_try_from_cow_str_id_type!(RelayId, "relay_id");
crate::impl_generate_id_id_type!(RelayId, "relay");
crate::impl_serializable_secret_id_type!(RelayId);
crate::impl_queryable_id_type!(RelayId);
crate::impl_to_sql_from_sql_id_type!(RelayId);

crate::impl_debug_id_type!(RelayId);
3 changes: 2 additions & 1 deletion crates/diesel_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub mod diesel_exports {
DbPaymentMethodIssuerCode as PaymentMethodIssuerCode, DbPaymentSource as PaymentSource,
DbPaymentType as PaymentType, DbPayoutStatus as PayoutStatus, DbPayoutType as PayoutType,
DbProcessTrackerStatus as ProcessTrackerStatus, DbReconStatus as ReconStatus,
DbRefundStatus as RefundStatus, DbRefundType as RefundType,
DbRefundStatus as RefundStatus, DbRefundType as RefundType, DbRelayStatus as RelayStatus,
DbRelayType as RelayType,
DbRequestIncrementalAuthorization as RequestIncrementalAuthorization,
DbRoleScope as RoleScope, DbRoutingAlgorithmKind as RoutingAlgorithmKind,
DbScaExemptionType as ScaExemptionType,
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod payouts;
pub mod process_tracker;
pub mod query;
pub mod refund;
pub mod relay;
pub mod reverse_lookup;
pub mod role;
pub mod routing_algorithm;
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod payout_attempt;
pub mod payouts;
pub mod process_tracker;
pub mod refund;
pub mod relay;
pub mod reverse_lookup;
pub mod role;
pub mod routing_algorithm;
Expand Down
38 changes: 38 additions & 0 deletions crates/diesel_models/src/query/relay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use diesel::{associations::HasTable, ExpressionMethods};

use super::generics;
use crate::{
errors,
relay::{Relay, RelayNew, RelayUpdateInternal},
schema::relay::dsl,
PgPooledConn, StorageResult,
};

impl RelayNew {
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<Relay> {
generics::generic_insert(conn, self).await
}
}

impl Relay {
pub async fn update(
self,
conn: &PgPooledConn,
relay: RelayUpdateInternal,
) -> StorageResult<Self> {
match generics::generic_update_with_unique_predicate_get_result::<
<Self as HasTable>::Table,
_,
_,
_,
>(conn, dsl::id.eq(self.id.to_owned()), relay)
.await
{
Err(error) => match error.current_context() {
errors::DatabaseError::NoFieldsToUpdate => Ok(self),
_ => Err(error),
},
result => result,
}
}
}
Loading
Loading