Skip to content

Commit

Permalink
refactor(connector): changed amount to minor Unit for stripe (#4786)
Browse files Browse the repository at this point in the history
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com>
Co-authored-by: Narayan Bhat <narayan.bhat@juspay.in>
  • Loading branch information
4 people committed Jun 11, 2024
1 parent 4d0c893 commit b705757
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 58 deletions.
6 changes: 4 additions & 2 deletions api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -12824,7 +12824,8 @@
"fees": {
"type": "integer",
"format": "int64",
"description": "Platform fees to be collected on the payment"
"description": "Platform fees to be collected on the payment",
"example": 6540
},
"transfer_account_id": {
"type": "string",
Expand All @@ -12851,7 +12852,8 @@
"application_fees": {
"type": "integer",
"format": "int64",
"description": "Platform fees collected on the payment"
"description": "Platform fees collected on the payment",
"example": 6540
},
"transfer_account_id": {
"type": "string",
Expand Down
6 changes: 4 additions & 2 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ pub struct PaymentChargeRequest {
pub charge_type: api_enums::PaymentChargeType,

/// Platform fees to be collected on the payment
pub fees: i64,
#[schema(value_type = i64, example = 6540)]
pub fees: MinorUnit,

/// Identifier for the reseller's account to send the funds to
pub transfer_account_id: String,
Expand Down Expand Up @@ -3547,7 +3548,8 @@ pub struct PaymentChargeResponse {
pub charge_type: api_enums::PaymentChargeType,

/// Platform fees collected on the payment
pub application_fees: i64,
#[schema(value_type = i64, example = 6540)]
pub application_fees: MinorUnit,

/// Identifier for the reseller's account where the funds were transferred
pub transfer_account_id: String,
Expand Down
24 changes: 24 additions & 0 deletions crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,30 @@ impl AmountConvertor for FloatMajorUnitForConnector {
amount.to_minor_unit_as_i64(currency)
}
}

/// Connector required amount type

#[derive(Default, Debug, serde::Deserialize, serde::Serialize, Clone, Copy, PartialEq)]
pub struct MinorUnitForConnector;

impl AmountConvertor for MinorUnitForConnector {
type Output = MinorUnit;
fn convert(
&self,
amount: MinorUnit,
_currency: enums::Currency,
) -> Result<Self::Output, error_stack::Report<ParsingError>> {
Ok(amount)
}
fn convert_back(
&self,
amount: MinorUnit,
_currency: enums::Currency,
) -> Result<MinorUnit, error_stack::Report<ParsingError>> {
Ok(amount)
}
}

/// This Unit struct represents MinorUnit in which core amount works
#[derive(
Default,
Expand Down
5 changes: 4 additions & 1 deletion crates/hyperswitch_domain_models/src/router_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, marker::PhantomData};

use common_utils::id_type;
use common_utils::{id_type, types::MinorUnit};
use masking::Secret;

use crate::{payment_address::PaymentAddress, payment_method_data};
Expand Down Expand Up @@ -67,6 +67,9 @@ pub struct RouterData<Flow, Request, Response> {
/// This field is used to store various data regarding the response from connector
pub connector_response: Option<ConnectorResponseData>,
pub payment_method_status: Option<common_enums::PaymentMethodStatus>,

// minor amount for amount framework
pub minor_amount_captured: Option<MinorUnit>,
}

// Different patterns of authentication.
Expand Down
7 changes: 6 additions & 1 deletion crates/hyperswitch_domain_models/src/router_request_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct PaymentsAuthorizeData {
#[derive(Debug, serde::Deserialize, Clone)]
pub struct PaymentCharges {
pub charge_type: api_models::enums::PaymentChargeType,
pub fees: i64,
pub fees: MinorUnit,
pub transfer_account_id: String,
}

Expand Down Expand Up @@ -245,6 +245,9 @@ pub struct PaymentsPreProcessingData {
pub mandate_id: Option<api_models::payments::MandateIds>,
pub related_transaction_id: Option<String>,
pub redirect_response: Option<CompleteAuthorizeRedirectResponse>,

// New amount for amount frame work
pub minor_amount: Option<MinorUnit>,
}

impl TryFrom<PaymentsAuthorizeData> for PaymentsPreProcessingData {
Expand All @@ -254,6 +257,7 @@ impl TryFrom<PaymentsAuthorizeData> for PaymentsPreProcessingData {
Ok(Self {
payment_method_data: Some(data.payment_method_data),
amount: Some(data.amount),
minor_amount: Some(data.minor_amount),
email: data.email,
currency: Some(data.currency),
payment_method_type: data.payment_method_type,
Expand Down Expand Up @@ -281,6 +285,7 @@ impl TryFrom<CompleteAuthorizeData> for PaymentsPreProcessingData {
Ok(Self {
payment_method_data: data.payment_method_data,
amount: Some(data.amount),
minor_amount: Some(data.minor_amount),
email: data.email,
currency: Some(data.currency),
payment_method_type: None,
Expand Down
1 change: 0 additions & 1 deletion crates/router/src/connector/bluesnap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ impl ConnectorCommon for Bluesnap {
fn common_get_content_type(&self) -> &'static str {
"application/json"
}

fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str {
connectors.bluesnap.base_url.as_ref()
}
Expand Down
43 changes: 36 additions & 7 deletions crates/router/src/connector/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub mod transformers;

use std::{collections::HashMap, ops::Deref};

use common_utils::request::RequestContent;
use common_utils::{
request::RequestContent,
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
};
use diesel_models::enums;
use error_stack::ResultExt;
use masking::PeekInterface;
Expand All @@ -15,6 +18,7 @@ use super::utils::{self as connector_utils, PaymentMethodDataType, RefundsReques
use super::utils::{PayoutsData, RouterData};
use crate::{
configs::settings,
connector::utils::PaymentsPreProcessingData,
consts,
core::{
errors::{self, CustomResult},
Expand All @@ -35,8 +39,18 @@ use crate::{
utils::{crypto, ByteSliceExt, BytesExt, OptionExt},
};

#[derive(Debug, Clone)]
pub struct Stripe;
#[derive(Clone)]
pub struct Stripe {
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
}

impl Stripe {
pub const fn new() -> &'static Self {
&Self {
amount_converter: &MinorUnitForConnector,
}
}
}

impl<Flow, Request, Response> ConnectorCommonExt<Flow, Request, Response> for Stripe
where
Expand Down Expand Up @@ -223,7 +237,12 @@ impl
req: &types::PaymentsPreProcessingRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_req = stripe::StripeCreditTransferSourceRequest::try_from(req)?;
let req_currency = req.request.get_currency()?;
let req_amount = req.request.get_minor_amount()?;
let amount =
connector_utils::convert_amount(self.amount_converter, req_amount, req_currency)?;
let connector_req =
stripe::StripeCreditTransferSourceRequest::try_from((req, amount, req_currency))?;
Ok(RequestContent::FormUrlEncoded(Box::new(connector_req)))
}

Expand Down Expand Up @@ -618,7 +637,12 @@ impl
req: &types::PaymentsCaptureRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_req = stripe::CaptureRequest::try_from(req)?;
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_amount_to_capture,
req.request.currency,
)?;
let connector_req = stripe::CaptureRequest::try_from(amount)?;
Ok(RequestContent::FormUrlEncoded(Box::new(connector_req)))
}

Expand Down Expand Up @@ -931,12 +955,17 @@ impl
req: &types::PaymentsAuthorizeRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_amount,
req.request.currency,
)?;
match &req.request.payment_method_data {
domain::PaymentMethodData::BankTransfer(bank_transfer_data) => {
stripe::get_bank_transfer_request_data(req, bank_transfer_data.deref())
stripe::get_bank_transfer_request_data(req, bank_transfer_data.deref(), amount)
}
_ => {
let connector_req = stripe::PaymentIntentRequest::try_from(req)?;
let connector_req = stripe::PaymentIntentRequest::try_from((req, amount))?;

Ok(RequestContent::FormUrlEncoded(Box::new(connector_req)))
}
Expand Down
Loading

0 comments on commit b705757

Please sign in to comment.