mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +08:00 
			
		
		
		
	feat: update surcharge_amount and tax_amount in update_trackers of payment_confirm (#2603)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
		| @ -216,6 +216,10 @@ impl ConnectorValidation for Paypal { | ||||
|             ), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn validate_if_surcharge_implemented(&self) -> CustomResult<(), errors::ConnectorError> { | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl | ||||
|  | ||||
| @ -148,7 +148,11 @@ impl ConnectorCommon for Trustpay { | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl ConnectorValidation for Trustpay {} | ||||
| impl ConnectorValidation for Trustpay { | ||||
|     fn validate_if_surcharge_implemented(&self) -> CustomResult<(), errors::ConnectorError> { | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl api::Payment for Trustpay {} | ||||
|  | ||||
|  | ||||
| @ -73,6 +73,12 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu | ||||
|             .connector | ||||
|             .validate_capture_method(self.request.capture_method) | ||||
|             .to_payment_failed_response()?; | ||||
|         if self.request.surcharge_details.is_some() { | ||||
|             connector | ||||
|                 .connector | ||||
|                 .validate_if_surcharge_implemented() | ||||
|                 .to_payment_failed_response()?; | ||||
|         } | ||||
|  | ||||
|         if self.should_proceed_with_authorize() { | ||||
|             self.decide_authentication_type(); | ||||
|  | ||||
| @ -82,7 +82,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> | ||||
|  | ||||
|         helpers::validate_status_with_capture_method(payment_intent.status, capture_method)?; | ||||
|  | ||||
|         helpers::validate_amount_to_capture(payment_intent.amount, request.amount_to_capture)?; | ||||
|         helpers::validate_amount_to_capture( | ||||
|             payment_attempt.amount_capturable, | ||||
|             request.amount_to_capture, | ||||
|         )?; | ||||
|  | ||||
|         helpers::validate_capture_method(capture_method)?; | ||||
|  | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| use std::marker::PhantomData; | ||||
|  | ||||
| use api_models::enums::FrmSuggestion; | ||||
| use api_models::{enums::FrmSuggestion, payment_methods}; | ||||
| use async_trait::async_trait; | ||||
| use common_utils::ext_traits::{AsyncExt, Encode}; | ||||
| use error_stack::ResultExt; | ||||
| @ -335,6 +335,19 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> | ||||
|             sm | ||||
|         }); | ||||
|  | ||||
|         // populate payment_data.surcharge_details from request | ||||
|         let surcharge_details = request.surcharge_details.map(|surcharge_details| { | ||||
|             payment_methods::SurchargeDetailsResponse { | ||||
|                 surcharge: payment_methods::Surcharge::Fixed(surcharge_details.surcharge_amount), | ||||
|                 tax_on_surcharge: None, | ||||
|                 surcharge_amount: surcharge_details.surcharge_amount, | ||||
|                 tax_on_surcharge_amount: surcharge_details.tax_amount.unwrap_or(0), | ||||
|                 final_amount: payment_attempt.amount | ||||
|                     + surcharge_details.surcharge_amount | ||||
|                     + surcharge_details.tax_amount.unwrap_or(0), | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         Ok(( | ||||
|             Box::new(self), | ||||
|             PaymentData { | ||||
| @ -368,7 +381,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> | ||||
|                 ephemeral_key: None, | ||||
|                 multiple_capture_data: None, | ||||
|                 redirect_response: None, | ||||
|                 surcharge_details: None, | ||||
|                 surcharge_details, | ||||
|                 frm_message: None, | ||||
|                 payment_link_data: None, | ||||
|             }, | ||||
| @ -543,7 +556,19 @@ impl<F: Clone, Ctx: PaymentMethodRetrieve> | ||||
|             .take(); | ||||
|         let order_details = payment_data.payment_intent.order_details.clone(); | ||||
|         let metadata = payment_data.payment_intent.metadata.clone(); | ||||
|         let authorized_amount = payment_data.payment_attempt.amount; | ||||
|         let surcharge_amount = payment_data | ||||
|             .surcharge_details | ||||
|             .as_ref() | ||||
|             .map(|surcharge_details| surcharge_details.surcharge_amount); | ||||
|         let tax_amount = payment_data | ||||
|             .surcharge_details | ||||
|             .as_ref() | ||||
|             .map(|surcharge_details| surcharge_details.tax_on_surcharge_amount); | ||||
|         let authorized_amount = payment_data | ||||
|             .surcharge_details | ||||
|             .as_ref() | ||||
|             .map(|surcharge_details| surcharge_details.final_amount) | ||||
|             .unwrap_or(payment_data.payment_attempt.amount); | ||||
|         let payment_attempt_fut = db | ||||
|             .update_payment_attempt_with_attempt_id( | ||||
|                 payment_data.payment_attempt, | ||||
| @ -564,6 +589,8 @@ impl<F: Clone, Ctx: PaymentMethodRetrieve> | ||||
|                     error_code, | ||||
|                     error_message, | ||||
|                     amount_capturable: Some(authorized_amount), | ||||
|                     surcharge_amount, | ||||
|                     tax_amount, | ||||
|                     updated_by: storage_scheme.to_string(), | ||||
|                 }, | ||||
|                 storage_scheme, | ||||
|  | ||||
| @ -559,7 +559,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>( | ||||
|  | ||||
|             payment_attempt_update = Some(storage::PaymentAttemptUpdate::AmountToCaptureUpdate { | ||||
|                 status: multiple_capture_data.get_attempt_status(authorized_amount), | ||||
|                 amount_capturable: payment_data.payment_attempt.amount | ||||
|                 amount_capturable: authorized_amount | ||||
|                     - multiple_capture_data.get_total_blocked_amount(), | ||||
|                 updated_by: storage_scheme.to_string(), | ||||
|             }); | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| use std::{fmt::Debug, marker::PhantomData, str::FromStr}; | ||||
|  | ||||
| use api_models::payments::FrmMessage; | ||||
| use api_models::payments::{FrmMessage, RequestSurchargeDetails}; | ||||
| use common_utils::{consts::X_HS_LATENCY, fp_utils}; | ||||
| use diesel_models::ephemeral_key; | ||||
| use error_stack::{IntoReport, ResultExt}; | ||||
| @ -424,7 +424,13 @@ where | ||||
|             .change_context(errors::ApiErrorResponse::InvalidDataValue { | ||||
|                 field_name: "payment_method_data", | ||||
|             })?; | ||||
|  | ||||
|     let surcharge_details = | ||||
|         payment_attempt | ||||
|             .surcharge_amount | ||||
|             .map(|surcharge_amount| RequestSurchargeDetails { | ||||
|                 surcharge_amount, | ||||
|                 tax_amount: payment_attempt.tax_amount, | ||||
|             }); | ||||
|     let merchant_decision = payment_intent.merchant_decision.to_owned(); | ||||
|     let frm_message = payment_data.frm_message.map(FrmMessage::foreign_from); | ||||
|  | ||||
| @ -557,6 +563,7 @@ where | ||||
|                         .set_amount(payment_attempt.amount) | ||||
|                         .set_amount_capturable(Some(payment_attempt.amount_capturable)) | ||||
|                         .set_amount_received(payment_intent.amount_captured) | ||||
|                         .set_surcharge_details(surcharge_details) | ||||
|                         .set_connector(routed_through) | ||||
|                         .set_client_secret(payment_intent.client_secret.map(masking::Secret::new)) | ||||
|                         .set_created(Some(payment_intent.created_at)) | ||||
| @ -743,6 +750,7 @@ where | ||||
|                 reference_id: payment_attempt.connector_response_reference_id, | ||||
|                 attempt_count: payment_intent.attempt_count, | ||||
|                 payment_link: payment_link_data, | ||||
|                 surcharge_details, | ||||
|                 ..Default::default() | ||||
|             }, | ||||
|             headers, | ||||
|  | ||||
| @ -96,6 +96,14 @@ pub trait ConnectorValidation: ConnectorCommon { | ||||
|     fn is_webhook_source_verification_mandatory(&self) -> bool { | ||||
|         false | ||||
|     } | ||||
|  | ||||
|     fn validate_if_surcharge_implemented(&self) -> CustomResult<(), errors::ConnectorError> { | ||||
|         Err(errors::ConnectorError::NotImplemented(format!( | ||||
|             "Surcharge not implemented for {}", | ||||
|             self.id() | ||||
|         )) | ||||
|         .into()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[async_trait::async_trait] | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Hrithikesh
					Hrithikesh