Skip to content

Commit

Permalink
refactor: remove charge_id from refund request
Browse files Browse the repository at this point in the history
  • Loading branch information
swangi-kumari committed Dec 2, 2024
1 parent 570c056 commit f5930fe
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 65 deletions.
6 changes: 5 additions & 1 deletion crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ crate::impl_to_sql_from_sql_json!(ChargeRefunds);
)]
#[diesel(sql_type = Jsonb)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
/// Charge specific fields for controlling the revert of funds from either platform or connected account. Check sub-fields for more details.
pub enum SplitRefundRequest {
/// StripeSplitRefundRequest
Expand All @@ -1090,10 +1091,11 @@ crate::impl_to_sql_from_sql_json!(SplitRefundRequest);
Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, FromSqlRow, AsExpression, ToSchema,
)]
#[diesel(sql_type = Jsonb)]
#[serde(deny_unknown_fields)]
/// Charge specific fields for controlling the revert of funds from either platform or connected account for Stripe. Check sub-fields for more details.
pub struct StripeSplitRefundRequest {
/// Identifier for charge created for the payment
pub charge_id: String,
// pub charge_id: String,

/// Toggle for reverting the application fee that was collected for the payment.
/// If set to false, the funds are pulled from the destination account.
Expand All @@ -1110,6 +1112,7 @@ crate::impl_to_sql_from_sql_json!(StripeSplitRefundRequest);
)]
#[diesel(sql_type = Jsonb)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
/// Charge specific
pub enum SplitPaymentsRequest {
/// StripeSplitPayment
Expand All @@ -1121,6 +1124,7 @@ crate::impl_to_sql_from_sql_json!(SplitPaymentsRequest);
Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, FromSqlRow, AsExpression, ToSchema,
)]
#[diesel(sql_type = Jsonb)]
#[serde(deny_unknown_fields)]
/// Stripe Split Payment
pub struct StripeSplitPayment {
/// Payment charge type
Expand Down
1 change: 1 addition & 0 deletions crates/openapi/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ Never share your secret api keys. Keep them guarded and secure.
common_utils::payout_method_utils::VenmoAdditionalData,
common_utils::types::SplitPaymentsRequest,
common_utils::types::StripeSplitPayment,
common_utils::types::StripeSplitRefundRequest,
api_models::refunds::RefundRequest,
api_models::refunds::RefundType,
api_models::refunds::RefundResponse,
Expand Down
1 change: 1 addition & 0 deletions crates/openapi/src/openapi_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Never share your secret api keys. Keep them guarded and secure.
common_utils::payout_method_utils::VenmoAdditionalData,
common_utils::types::SplitPaymentsRequest,
common_utils::types::StripeSplitPayment,
common_utils::types::StripeSplitRefundRequest,
api_models::refunds::RefundRequest,
api_models::refunds::RefundsCreateRequest,
api_models::refunds::RefundErrorDetails,
Expand Down
186 changes: 127 additions & 59 deletions crates/router/src/core/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::HashMap;
use api_models::admin::MerchantConnectorInfo;
use common_utils::{
ext_traits::AsyncExt,
types::{ConnectorTransactionId, MinorUnit, SplitPaymentsRequest, SplitRefundRequest},
types::{ConnectorTransactionId, MinorUnit, SplitPaymentsRequest},
};
use diesel_models::process_tracker::business_status;
use error_stack::{report, ResultExt};
Expand Down Expand Up @@ -473,7 +473,11 @@ pub async fn refund_retrieve_core(
.clone()
.zip(refund.split_refunds.clone())
.map(|(split_payments, split_refunds)| {
ForeignTryFrom::foreign_try_from((split_refunds, split_payments))
ForeignTryFrom::foreign_try_from((
split_refunds,
split_payments,
payment_attempt.charge_id.clone(),
))
})
.transpose()?;

Expand Down Expand Up @@ -745,39 +749,24 @@ pub async fn validate_and_create_refund(
) -> RouterResult<refunds::RefundResponse> {
let db = &*state.store;

let split_refunds = match (
payment_intent.split_payments.as_ref(),
payment_attempt.charge_id.as_ref(),
) {
(Some(split_payments), Some(charge_id)) => {
let split_refunds = match payment_intent.split_payments.as_ref() {
Some(SplitPaymentsRequest::StripeSplitPayment(stripe_payment)) => {
let refund_request = req
.split_refunds
.clone()
.get_required_value("split_refunds")?;

// let stripe_refund = match refund_request {
// SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) => stripe_refund,
// };
let SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) = refund_request;

utils::when(*charge_id != stripe_refund.charge_id, || {
Err(report!(errors::ApiErrorResponse::InvalidDataValue {
field_name: "split_refunds.charge_id",
}))
.attach_printable("charge_id sent in request mismatches with original charge_id")
})?;

// let stripe_payment = match split_payments {
// SplitPaymentsRequest::StripeSplitPayment(payment) => payment,
// };
let SplitPaymentsRequest::StripeSplitPayment(stripe_payment) = split_payments;

let options =
validator::validate_charge_refund(&refund_request, &stripe_payment.charge_type)?;

// Construct SplitRefundsRequest
let charge_id = payment_attempt.charge_id.clone().ok_or_else(|| {
report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
"Transaction in invalid. Missing field \"charge_id\" in payment_attempt.",
)
})?;

Some(SplitRefundsRequest::StripeSplitRefund(StripeSplitRefund {
charge_id: stripe_refund.charge_id.clone(),
charge_id,
charge_type: stripe_payment.charge_type.clone(),
transfer_account_id: stripe_payment.transfer_account_id.clone(),
options,
Expand All @@ -786,6 +775,53 @@ pub async fn validate_and_create_refund(
_ => None,
};

// let split_refunds = match (
// payment_intent.split_payments.as_ref(),
// payment_attempt.charge_id.as_ref(),
// ) {
// (Some(split_payments), Some(charge_id)) => {
// let refund_request = req
// .split_refunds
// .clone()
// .get_required_value("split_refunds")?;

// // let stripe_refund = match refund_request {
// // SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) => stripe_refund,
// // };
// let SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) = refund_request;

// // utils::when(*charge_id != stripe_refund.charge_id, || {
// // Err(report!(errors::ApiErrorResponse::InvalidDataValue {
// // field_name: "split_refunds.charge_id",
// // }))
// // .attach_printable("charge_id sent in request mismatches with original charge_id")
// // })?;

// // let stripe_payment = match split_payments {
// // SplitPaymentsRequest::StripeSplitPayment(payment) => payment,
// // };
// let SplitPaymentsRequest::StripeSplitPayment(stripe_payment) = split_payments;

// let options =
// validator::validate_charge_refund(&refund_request, &stripe_payment.charge_type)?;

// let charge_id = payment_attempt.charge_id.clone().ok_or_else(|| {
// report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
// "Transaction in invalid. Missing field \"charge_id\" in payment_attempt.",
// )
// })?;

// // Construct SplitRefundsRequest
// Some(SplitRefundsRequest::StripeSplitRefund(StripeSplitRefund {
// charge_id,
// charge_type: stripe_payment.charge_type.clone(),
// transfer_account_id: stripe_payment.transfer_account_id.clone(),
// options,
// }))
// }
// _ => None,
// };

// Only for initial dev and testing
let refund_type = req.refund_type.unwrap_or_default();

Expand Down Expand Up @@ -1480,57 +1516,89 @@ pub async fn trigger_refund_execute_workflow(
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;

let split_refunds = match (
payment_intent.split_payments.as_ref(),
payment_attempt.charge_id.as_ref(),
) {
(Some(split_payments), Some(charge_id)) => {
let split_refunds = match payment_intent.split_payments.as_ref() {
Some(SplitPaymentsRequest::StripeSplitPayment(stripe_payment)) => {
let refund_request = refund
.split_refunds
.clone()
.get_required_value("split_refunds")?;

// Match the enum variant of SplitRefundRequest
// let stripe_refund = match refund_request {
// SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) => {
// stripe_refund
// }
// };
let SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) =
refund_request;

// Validate charge_id
utils::when(*charge_id != stripe_refund.charge_id, || {
Err(report!(errors::ApiErrorResponse::InvalidDataValue {
field_name: "split_refunds.charge_id",
}))
.attach_printable(
"charge_id sent in request mismatches with original charge_id",
)
})?;

// Match the variant of SplitPaymentsRequest
// let stripe_payment = match split_payments {
// SplitPaymentsRequest::StripeSplitPayment(payment) => payment,
// };
let SplitPaymentsRequest::StripeSplitPayment(stripe_payment) = split_payments;

// Validate refund options with correct `charge_type`
let options = validator::validate_charge_refund(
&refund_request,
&stripe_payment.charge_type,
)?;

// Construct SplitRefundsRequest
let charge_id = payment_attempt.charge_id.clone().ok_or_else(|| {
report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
"Transaction in invalid. Missing field \"charge_id\" in payment_attempt.",
)
})?;

Some(SplitRefundsRequest::StripeSplitRefund(StripeSplitRefund {
charge_id: stripe_refund.charge_id.clone(),
charge_id,
charge_type: stripe_payment.charge_type.clone(),
transfer_account_id: stripe_payment.transfer_account_id.clone(),
options,
}))
}
_ => None,
};

// let split_refunds = match (
// payment_intent.split_payments.as_ref(),
// payment_attempt.charge_id.as_ref(),
// ) {
// (Some(split_payments), Some(charge_id)) => {
// let refund_request = refund
// .split_refunds
// .clone()
// .get_required_value("split_refunds")?;

// // Match the enum variant of SplitRefundRequest
// // let stripe_refund = match refund_request {
// // SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) => {
// // stripe_refund
// // }
// // };
// let SplitRefundRequest::StripeSplitRefundRequest(ref stripe_refund) =
// refund_request;

// // Validate charge_id
// // utils::when(*charge_id != stripe_refund.charge_id, || {
// // Err(report!(errors::ApiErrorResponse::InvalidDataValue {
// // field_name: "split_refunds.charge_id",
// // }))
// // .attach_printable(
// // "charge_id sent in request mismatches with original charge_id",
// // )
// // })?;

// // Match the variant of SplitPaymentsRequest
// // let stripe_payment = match split_payments {
// // SplitPaymentsRequest::StripeSplitPayment(payment) => payment,
// // };
// let SplitPaymentsRequest::StripeSplitPayment(stripe_payment) = split_payments;

// // Validate refund options with correct `charge_type`
// let options = validator::validate_charge_refund(
// &refund_request,
// &stripe_payment.charge_type,
// )?;
// let charge_id = payment_attempt.charge_id.clone().ok_or_else(|| {
// report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
// "Transaction in invalid. Missing field \"charge_id\" in payment_attempt.",
// )
// })?;
// // Construct SplitRefundsRequest
// Some(SplitRefundsRequest::StripeSplitRefund(StripeSplitRefund {
// charge_id,
// charge_type: stripe_payment.charge_type.clone(),
// transfer_account_id: stripe_payment.transfer_account_id.clone(),
// options,
// }))
// }
// _ => None,
// };
//trigger refund request to gateway
let updated_refund = Box::pin(trigger_refund_to_gateway(
state,
Expand Down
15 changes: 10 additions & 5 deletions crates/router/src/core/refunds/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
use common_utils::types::{SplitPaymentsRequest, SplitRefundRequest};
use error_stack::Report;
use error_stack::{report, Report};
use hyperswitch_domain_models::router_request_types;

use super::validator;
use crate::{core::errors, types::transformers::ForeignTryFrom};

impl ForeignTryFrom<(SplitRefundRequest, SplitPaymentsRequest)>
impl ForeignTryFrom<(SplitRefundRequest, SplitPaymentsRequest, Option<String>)>
for router_request_types::SplitRefundsRequest
{
type Error = Report<errors::ApiErrorResponse>;

fn foreign_try_from(
item: (SplitRefundRequest, SplitPaymentsRequest),
item: (SplitRefundRequest, SplitPaymentsRequest, Option<String>),
) -> Result<Self, Self::Error> {
let (refund_request, payment_charges) = item;
let (refund_request, payment_charges, charge_id) = item;

match refund_request {
SplitRefundRequest::StripeSplitRefundRequest(stripe_refund) => match payment_charges {
SplitPaymentsRequest::StripeSplitPayment(stripe_payment) => {
let charge_id = charge_id.ok_or_else(|| {
report!(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Missing `charge_id` in PaymentAttempt.")
})?;

let options = validator::validate_charge_refund(
&SplitRefundRequest::StripeSplitRefundRequest(stripe_refund.clone()),
&stripe_payment.charge_type,
)?;

Ok(Self::StripeSplitRefund(
router_request_types::StripeSplitRefund {
charge_id: stripe_refund.charge_id,
charge_id, // Use `charge_id` from `PaymentAttempt`
transfer_account_id: stripe_payment.transfer_account_id,
charge_type: stripe_payment.charge_type,
options,
Expand Down

0 comments on commit f5930fe

Please sign in to comment.