Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor: make NextAction as enum (#1234)
Sangamesh Kulkarnia359b76d09ff
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex d71c3ae4c7..a90fa8313f 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -1067,21 +1067,20 @@ pub enum NextActionType {TriggerApi,DisplayBankTransferInformation,}+#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, ToSchema)]-pub struct NextAction {- /// Specifying the action type to be performed next- #[serde(rename = "type")]- pub next_action_type: NextActionType,- //TODO: Make an enum having redirect_to_url and bank_transfer_steps_and_charges_details and use here+#[serde(tag = "type", rename_all = "snake_case")]+pub enum NextActionData {/// Contains the url for redirection flow- #[schema(example = "https://router.juspay.io/redirect/fakushdfjlksdfasklhdfj")]- pub redirect_to_url: Option<String>,+ RedirectToUrl { redirect_to_url: String },/// Informs the next steps for bank transfer and also contains the charges details (ex: amount received, amount charged etc)- pub bank_transfer_steps_and_charges_details: Option<NextStepsRequirements>,+ DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details: BankTransferNextStepsData,+ },}#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]-pub struct NextStepsRequirements {+pub struct BankTransferNextStepsData {#[serde(flatten)]pub bank_transfer_instructions: BankTransferInstructions,pub receiver: ReceiverDetails,@@ -1268,7 +1267,7 @@ pub struct PaymentsResponse {pub statement_descriptor_suffix: Option<String>,/// Additional information required for redirection- pub next_action: Option<NextAction>,+ pub next_action: Option<NextActionData>,/// If the payment was cancelled the reason provided herepub cancellation_reason: Option<String>,diff --git a/crates/router/src/compatibility/stripe/payment_intents/types.rs b/crates/router/src/compatibility/stripe/payment_intents/types.rsindex 752244440f..befd17c965 100644--- a/crates/router/src/compatibility/stripe/payment_intents/types.rs+++ b/crates/router/src/compatibility/stripe/payment_intents/types.rs@@ -610,22 +610,34 @@ pub struct RedirectUrl {pub url: Option<String>,}-#[derive(Eq, PartialEq, Serialize, Debug)]-pub struct StripeNextAction {- #[serde(rename = "type")]- stype: payments::NextActionType,- redirect_to_url: RedirectUrl,+#[derive(Eq, PartialEq, serde::Serialize, Debug)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum StripeNextAction {+ RedirectToUrl {+ redirect_to_url: RedirectUrl,+ },+ DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details: payments::BankTransferNextStepsData,+ },}-fn into_stripe_next_action(- next_action: Option<payments::NextAction>,+pub(crate) fn into_stripe_next_action(+ next_action: Option<payments::NextActionData>,return_url: Option<String>,) -> Option<StripeNextAction> {- next_action.map(|n| StripeNextAction {- stype: n.next_action_type,- redirect_to_url: RedirectUrl {- return_url,- url: n.redirect_to_url,+ next_action.map(|next_action_data| match next_action_data {+ payments::NextActionData::RedirectToUrl { redirect_to_url } => {+ StripeNextAction::RedirectToUrl {+ redirect_to_url: RedirectUrl {+ return_url,+ url: Some(redirect_to_url),+ },+ }+ }+ payments::NextActionData::DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details,+ } => StripeNextAction::DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details,},})}diff --git a/crates/router/src/compatibility/stripe/setup_intents/types.rs b/crates/router/src/compatibility/stripe/setup_intents/types.rsindex 1bc7f3fb85..9a90f044e2 100644--- a/crates/router/src/compatibility/stripe/setup_intents/types.rs+++ b/crates/router/src/compatibility/stripe/setup_intents/types.rs@@ -284,22 +284,34 @@ pub struct RedirectUrl {pub url: Option<String>,}-#[derive(Eq, PartialEq, Serialize)]-pub struct StripeNextAction {- #[serde(rename = "type")]- stype: payments::NextActionType,- redirect_to_url: RedirectUrl,+#[derive(Eq, PartialEq, serde::Serialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum StripeNextAction {+ RedirectToUrl {+ redirect_to_url: RedirectUrl,+ },+ DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details: payments::BankTransferNextStepsData,+ },}pub(crate) fn into_stripe_next_action(- next_action: Option<payments::NextAction>,+ next_action: Option<payments::NextActionData>,return_url: Option<String>,) -> Option<StripeNextAction> {- next_action.map(|n| StripeNextAction {- stype: n.next_action_type,- redirect_to_url: RedirectUrl {- return_url,- url: n.redirect_to_url,+ next_action.map(|next_action_data| match next_action_data {+ payments::NextActionData::RedirectToUrl { redirect_to_url } => {+ StripeNextAction::RedirectToUrl {+ redirect_to_url: RedirectUrl {+ return_url,+ url: Some(redirect_to_url),+ },+ }+ }+ payments::NextActionData::DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details,+ } => StripeNextAction::DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details,},})}diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rsindex 228427c29a..9064d725fe 100644--- a/crates/router/src/connector/stripe/transformers.rs+++ b/crates/router/src/connector/stripe/transformers.rs@@ -1603,6 +1603,9 @@ impl<F, T>// Or we identify the mandate txns before hand and always call SetupIntent in case of mandate payment calllet network_txn_id = Option::foreign_from(item.response.latest_attempt);+ let connector_metadata =+ get_connector_metadata(item.response.next_action.as_ref(), item.response.amount)?;+Ok(Self {status: enums::AttemptStatus::from(item.response.status),// client_secret: Some(item.response.client_secret.clone().as_str()),@@ -1613,7 +1616,7 @@ impl<F, T>resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),redirection_data,mandate_reference,- connector_metadata: None,+ connector_metadata,network_txn_id,}),amount_captured: Some(item.response.amount_received),diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex b53a2bd5b7..2f82ca08c5 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -380,7 +380,10 @@ impl PaymentRedirectFlow for PaymentRedirectCompleteAuthorize {api_models::enums::IntentStatus::RequiresCustomerAction => {let startpay_url = payments_response.next_action- .and_then(|next_action| next_action.redirect_to_url)+ .and_then(|next_action_data| match next_action_data {+ api_models::payments::NextActionData::RedirectToUrl { redirect_to_url } => Some(redirect_to_url),+ api_models::payments::NextActionData::DisplayBankTransferInformation { .. } => None,+ }).ok_or(errors::ApiErrorResponse::InternalServerError).into_report().attach_printable(diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rsindex 9a81a6ef6b..878df61a31 100644--- a/crates/router/src/core/payments/transformers.rs+++ b/crates/router/src/core/payments/transformers.rs@@ -309,20 +309,19 @@ whereif payment_intent.status == enums::IntentStatus::RequiresCustomerAction|| bank_transfer_next_steps.is_some(){- let next_action_type = if bank_transfer_next_steps.is_some() {- api::NextActionType::DisplayBankTransferInformation- } else {- api::NextActionType::RedirectToUrl- };- next_action_response = Some(api::NextAction {- next_action_type,- redirect_to_url: Some(helpers::create_startpay_url(- server,- &payment_attempt,- &payment_intent,- )),- bank_transfer_steps_and_charges_details: bank_transfer_next_steps,- });+ next_action_response = bank_transfer_next_steps+ .map(|bank_transfer| {+ api_models::payments::NextActionData::DisplayBankTransferInformation {+ bank_transfer_steps_and_charges_details: bank_transfer,+ }+ })+ .or(Some(api_models::payments::NextActionData::RedirectToUrl {+ redirect_to_url: helpers::create_startpay_url(+ server,+ &payment_attempt,+ &payment_intent,+ ),+ }));};let mut response: api::PaymentsResponse = Default::default();@@ -520,11 +519,11 @@ impl ForeignFrom<ephemeral_key::EphemeralKey> for api::ephemeral_key::EphemeralKpub fn bank_transfer_next_steps_check(payment_attempt: storage::PaymentAttempt,-) -> RouterResult<Option<api_models::payments::NextStepsRequirements>> {+) -> RouterResult<Option<api_models::payments::BankTransferNextStepsData>> {let bank_transfer_next_step = if let Some(storage_models::enums::PaymentMethod::BankTransfer) =payment_attempt.payment_method{- let bank_transfer_next_steps: Option<api_models::payments::NextStepsRequirements> =+ let bank_transfer_next_steps: Option<api_models::payments::BankTransferNextStepsData> =payment_attempt.connector_metadata.map(|metadata| {diff --git a/crates/router/src/openapi.rs b/crates/router/src/openapi.rsindex baba9f3fd0..03316ae05a 100644--- a/crates/router/src/openapi.rs+++ b/crates/router/src/openapi.rs@@ -177,7 +177,7 @@ Never share your secret api keys. Keep them guarded and secure.api_models::payments::NextActionType,api_models::payments::Metadata,api_models::payments::WalletData,- api_models::payments::NextAction,+ api_models::payments::NextActionData,api_models::payments::PayLaterData,api_models::payments::MandateData,api_models::payments::PhoneDetails,diff --git a/crates/router/src/types/api/payments.rs b/crates/router/src/types/api/payments.rsindex a1f6e1396b..58509f9397 100644--- a/crates/router/src/types/api/payments.rs+++ b/crates/router/src/types/api/payments.rs@@ -1,7 +1,7 @@pub use api_models::payments::{AcceptanceType, Address, AddressDetails, Amount, AuthenticationForStartResponse, Card,CustomerAcceptance, MandateData, MandateTxnType, MandateType, MandateValidationFields,- NextAction, NextActionType, OnlineMandate, PayLaterData, PaymentIdType, PaymentListConstraints,+ NextActionType, OnlineMandate, PayLaterData, PaymentIdType, PaymentListConstraints,PaymentListResponse, PaymentMethodData, PaymentMethodDataResponse, PaymentOp,PaymentRetrieveBody, PaymentRetrieveBodyWithCredentials, PaymentsCancelRequest,PaymentsCaptureRequest, PaymentsRedirectRequest, PaymentsRedirectionResponse, PaymentsRequest,