Skip to content

Commit

Permalink
feat(connector): add support for surcharge in trustpay (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrithikesh026 authored Oct 16, 2023
1 parent f43ed3c commit 2d5d3b8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 4 deletions.
10 changes: 10 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,21 @@ pub struct PaymentsRequest {
/// associated with the merchant account will be used.
pub profile_id: Option<String>,

/// surcharge_details for this payment
#[schema(value_type = Option<RequestSurchargeDetails>)]
pub surcharge_details: Option<RequestSurchargeDetails>,

/// The type of the payment that differentiates between normal and various types of mandate payments
#[schema(value_type = Option<PaymentType>)]
pub payment_type: Option<api_enums::PaymentType>,
}

#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, Copy, ToSchema)]
pub struct RequestSurchargeDetails {
pub surcharge_amount: i64,
pub tax_amount: Option<i64>,
}

#[derive(Default, Debug, Clone, Copy)]
pub struct HeaderPayload {
pub payment_confirm_source: Option<api_enums::PaymentSource>,
Expand Down
15 changes: 13 additions & 2 deletions crates/router/src/connector/trustpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,13 @@ impl
&self,
req: &types::PaymentsPreProcessingRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let amount = req.request.get_amount()?;
let currency = req.request.get_currency()?;
let amount = req
.request
.surcharge_details
.as_ref()
.map(|surcharge_details| surcharge_details.final_amount)
.unwrap_or(req.request.get_amount()?);
let connector_router_data = trustpay::TrustpayRouterData::try_from((
&self.get_currency_unit(),
currency,
Expand Down Expand Up @@ -542,10 +547,16 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let amount = req
.request
.surcharge_details
.as_ref()
.map(|surcharge_details| surcharge_details.final_amount)
.unwrap_or(req.request.amount);
let connector_router_data = trustpay::TrustpayRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
amount,
req,
))?;
let connector_req = trustpay::TrustpayPaymentsRequest::try_from(&connector_router_data)?;
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/authorize_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ impl TryFrom<types::PaymentsAuthorizeData> for types::PaymentsPreProcessingData
webhook_url: data.webhook_url,
complete_authorize_url: data.complete_authorize_url,
browser_info: data.browser_info,
surcharge_details: data.surcharge_details,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
multiple_capture_data.update_capture(updated_capture);
}

let authorized_amount = payment_data.payment_attempt.amount;
let authorized_amount = payment_data.payment_attempt.get_total_amount();

payment_attempt_update = Some(storage::PaymentAttemptUpdate::AmountToCaptureUpdate {
status: multiple_capture_data.get_attempt_status(authorized_amount),
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,7 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::PaymentsPreProce
webhook_url,
complete_authorize_url,
browser_info,
surcharge_details: payment_data.surcharge_details,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod compatibility;
pub mod configs;
pub mod connection;
pub mod connector;
pub(crate) mod consts;
pub mod consts;
pub mod core;
pub mod cors;
pub mod db;
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::payments::SepaBankTransferInstructions,
api_models::payments::BacsBankTransferInstructions,
api_models::payments::RedirectResponse,
api_models::payments::RequestSurchargeDetails,
api_models::payments::PaymentAttemptResponse,
api_models::payments::CaptureResponse,
api_models::payment_methods::RequiredFieldInfo,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ pub struct PaymentsPreProcessingData {
pub router_return_url: Option<String>,
pub webhook_url: Option<String>,
pub complete_authorize_url: Option<String>,
pub surcharge_details: Option<api_models::payment_methods::SurchargeDetailsResponse>,
pub browser_info: Option<BrowserInformation>,
}

Expand Down
5 changes: 5 additions & 0 deletions crates/router/src/types/storage/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait PaymentAttemptExt {

fn get_next_capture_id(&self) -> String;
fn get_intent_status(&self, amount_captured: Option<i64>) -> enums::IntentStatus;
fn get_total_amount(&self) -> i64;
}

impl PaymentAttemptExt for PaymentAttempt {
Expand Down Expand Up @@ -67,6 +68,10 @@ impl PaymentAttemptExt for PaymentAttempt {
intent_status
}
}

fn get_total_amount(&self) -> i64 {
self.amount + self.surcharge_amount.unwrap_or(0) + self.tax_amount.unwrap_or(0)
}
}

pub trait AttemptStatusExt {
Expand Down
33 changes: 33 additions & 0 deletions openapi/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -8900,6 +8900,14 @@
"description": "The business profile to use for this payment, if not passed the default business profile\nassociated with the merchant account will be used.",
"nullable": true
},
"surcharge_details": {
"allOf": [
{
"$ref": "#/components/schemas/RequestSurchargeDetails"
}
],
"nullable": true
},
"payment_type": {
"allOf": [
{
Expand Down Expand Up @@ -9256,6 +9264,14 @@
"description": "The business profile to use for this payment, if not passed the default business profile\nassociated with the merchant account will be used.",
"nullable": true
},
"surcharge_details": {
"allOf": [
{
"$ref": "#/components/schemas/RequestSurchargeDetails"
}
],
"nullable": true
},
"payment_type": {
"allOf": [
{
Expand Down Expand Up @@ -10644,6 +10660,23 @@
}
}
},
"RequestSurchargeDetails": {
"type": "object",
"required": [
"surcharge_amount"
],
"properties": {
"surcharge_amount": {
"type": "integer",
"format": "int64"
},
"tax_amount": {
"type": "integer",
"format": "int64",
"nullable": true
}
}
},
"RequiredFieldInfo": {
"type": "object",
"description": "Required fields info used while listing the payment_method_data",
Expand Down

0 comments on commit 2d5d3b8

Please sign in to comment.