refactor: make NextAction as enum (#1234)

This commit is contained in:
Sangamesh Kulkarni
2023-05-24 10:46:00 +05:30
committed by GitHub
parent 48e537568d
commit a359b76d09
8 changed files with 81 additions and 53 deletions

View File

@ -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 here
pub cancellation_reason: Option<String>,

View File

@ -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,
#[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,
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: n.redirect_to_url,
url: Some(redirect_to_url),
},
}
}
payments::NextActionData::DisplayBankTransferInformation {
bank_transfer_steps_and_charges_details,
} => StripeNextAction::DisplayBankTransferInformation {
bank_transfer_steps_and_charges_details,
},
})
}

View File

@ -284,22 +284,34 @@ pub struct RedirectUrl {
pub url: Option<String>,
}
#[derive(Eq, PartialEq, Serialize)]
pub struct StripeNextAction {
#[serde(rename = "type")]
stype: payments::NextActionType,
#[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,
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: n.redirect_to_url,
url: Some(redirect_to_url),
},
}
}
payments::NextActionData::DisplayBankTransferInformation {
bank_transfer_steps_and_charges_details,
} => StripeNextAction::DisplayBankTransferInformation {
bank_transfer_steps_and_charges_details,
},
})
}

View File

@ -1603,6 +1603,9 @@ impl<F, T>
// Or we identify the mandate txns before hand and always call SetupIntent in case of mandate payment call
let 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),

View File

@ -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(

View File

@ -309,20 +309,19 @@ where
if 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(
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,
)),
bank_transfer_steps_and_charges_details: bank_transfer_next_steps,
});
),
}));
};
let mut response: api::PaymentsResponse = Default::default();
@ -520,11 +519,11 @@ impl ForeignFrom<ephemeral_key::EphemeralKey> for api::ephemeral_key::EphemeralK
pub 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| {

View File

@ -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,

View File

@ -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,