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
fix(connector): [fiuu] gracefully handle unknown response enum variants and missing optional fields (#13253)
Akshaya Shankare912a9e46840
diff --git a/crates/hyperswitch_connectors/src/connectors/fiuu.rs b/crates/hyperswitch_connectors/src/connectors/fiuu.rsindex 315b4a1b59..876e15ecb4 100644--- a/crates/hyperswitch_connectors/src/connectors/fiuu.rs+++ b/crates/hyperswitch_connectors/src/connectors/fiuu.rs@@ -38,6 +38,7 @@ use hyperswitch_interfaces::{ConnectorValidation,},configs::Connectors,+ consts::{NO_ERROR_CODE, NO_ERROR_MESSAGE},errors,events::connector_api_logs::ConnectorEvent,types::{self, Response},@@ -219,27 +220,55 @@ impl ConnectorCommon for Fiuu {res: Response,event_builder: Option<&mut ConnectorEvent>,) -> CustomResult<ErrorResponse, errors::ConnectorError> {- let response: fiuu::FiuuErrorResponse = res- .response- .parse_struct("FiuuErrorResponse")- .change_context(errors::ConnectorError::ResponseDeserializationFailed)?;+ if res.response.is_empty() {+ return Ok(ErrorResponse {+ status_code: res.status_code,+ code: NO_ERROR_CODE.to_string(),+ message: NO_ERROR_MESSAGE.to_string(),+ reason: None,+ attempt_status: None,+ connector_transaction_id: None,+ connector_response_reference_id: None,+ network_advice_code: None,+ network_decline_code: None,+ network_error_message: None,+ connector_metadata: None,+ });+ }- event_builder.map(|i| i.set_response_body(&response));- router_env::logger::info!(connector_response=?response);+ let response: Result<fiuu::FiuuErrorResponse, _> =+ res.response.parse_struct("FiuuErrorResponse");- Ok(ErrorResponse {- status_code: res.status_code,- code: response.error_code.clone(),- message: response.error_desc.clone(),- reason: Some(response.error_desc.clone()),- attempt_status: None,- connector_transaction_id: None,- connector_response_reference_id: None,- network_advice_code: None,- network_decline_code: None,- network_error_message: None,- connector_metadata: None,- })+ match response {+ Ok(response) => {+ event_builder.map(|i| i.set_response_body(&response));+ router_env::logger::info!(connector_response=?response);++ Ok(ErrorResponse {+ status_code: res.status_code,+ code: response.error_code.clone(),+ message: response.error_desc.clone(),+ reason: Some(response.error_desc.clone()),+ attempt_status: None,+ connector_transaction_id: None,+ connector_response_reference_id: None,+ network_advice_code: None,+ network_decline_code: None,+ network_error_message: None,+ connector_metadata: None,+ })+ }+ Err(error_msg) => {+ if let Some(event) = event_builder {+ event.set_error(serde_json::json!({+ "error": "Error response parsing failed",+ "status_code": res.status_code+ }));+ }+ router_env::logger::error!(deserialization_error =? error_msg);+ utils::handle_json_response_deserialization_failure(res, "fiuu")+ }+ }}}pub fn build_form_from_struct<T: Serialize + Send + 'static>(@@ -903,6 +932,9 @@ impl webhooks::IncomingWebhook for Fiuu {request: &webhooks::IncomingWebhookRequestDetails<'_>,_context: Option<&webhooks::WebhookContext>,) -> CustomResult<api_models::webhooks::IncomingWebhookEvent, errors::ConnectorError> {+ if request.body.is_empty() {+ return Ok(api_models::webhooks::IncomingWebhookEvent::EndpointVerification);+ }let header = utils::get_header_key_value("content-type", request.headers)?;let resource: FiuuWebhooksResponse = if header == "application/x-www-form-urlencoded" {parse_and_log_keys_in_url_encoded_response::<FiuuWebhooksResponse>(request.body);diff --git a/crates/hyperswitch_connectors/src/connectors/fiuu/transformers.rs b/crates/hyperswitch_connectors/src/connectors/fiuu/transformers.rsindex 0191f9b237..c66493cd72 100644--- a/crates/hyperswitch_connectors/src/connectors/fiuu/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/fiuu/transformers.rs@@ -775,24 +775,24 @@ impl TryFrom<Box<ApplePayPredecryptData>> for FiuuPaymentMethodData {#[derive(Debug, Serialize, Deserialize)]#[serde(rename_all = "PascalCase")]pub struct PaymentsResponse {- pub reference_no: String,+ pub reference_no: Option<String>,#[serde(rename = "TxnID")]pub txn_id: String,- pub txn_type: TxnType,- pub txn_currency: Currency,- pub txn_amount: StringMajorUnit,- pub txn_channel: String,+ pub txn_type: Option<String>,+ pub txn_currency: Option<Currency>,+ pub txn_amount: Option<StringMajorUnit>,+ pub txn_channel: Option<String>,pub txn_data: TxnData,}#[derive(Debug, Serialize, Deserialize)]#[serde(rename_all = "PascalCase")]pub struct DuitNowQrCodeResponse {- pub reference_no: String,- pub txn_type: TxnType,- pub txn_currency: Currency,- pub txn_amount: StringMajorUnit,- pub txn_channel: String,+ pub reference_no: Option<String>,+ pub txn_type: Option<String>,+ pub txn_currency: Option<Currency>,+ pub txn_amount: Option<StringMajorUnit>,+ pub txn_channel: Option<String>,#[serde(rename = "TxnID")]pub txn_id: String,pub txn_data: QrTxnData,@@ -822,7 +822,7 @@ pub enum FiuuPaymentsResponse {pub struct FiuuRecurringResponse {status: FiuuRecurringStautus,#[serde(rename = "orderid")]- order_id: String,+ order_id: Option<String>,#[serde(rename = "tranID")]tran_id: Option<String>,reason: Option<String>,@@ -832,6 +832,8 @@ pub struct FiuuRecurringResponse {pub enum FiuuRecurringStautus {Accepted,Failed,+ #[serde(other)]+ Unknown,}#[derive(Debug, Serialize, Deserialize)]@@ -839,18 +841,11 @@ pub enum FiuuRecurringStautus {pub struct TxnData {#[serde(rename = "RequestURL")]pub request_url: String,- pub request_type: RequestType,+ pub request_type: Option<String>,pub request_data: RequestData,pub request_method: String,}-#[derive(Debug, Serialize, Deserialize)]-#[serde(rename_all = "UPPERCASE")]-pub enum RequestType {- Redirect,- Response,-}-#[derive(Debug, Serialize, Deserialize)]#[serde(untagged)]pub enum RequestData {@@ -886,6 +881,7 @@ impl TryFrom<PaymentsResponseRouterData<FiuuPaymentsResponse>> for PaymentsAuthofn try_from(item: PaymentsResponseRouterData<FiuuPaymentsResponse>,) -> Result<Self, Self::Error> {+ let prev_status = item.data.status;match item.response {FiuuPaymentsResponse::QRPaymentResponse(ref response) => Ok(Self {status: enums::AttemptStatus::AuthenticationPending,@@ -1019,8 +1015,10 @@ impl TryFrom<PaymentsResponseRouterData<FiuuPaymentsResponse>> for PaymentsAutholet recurring_response_item = recurring_response_vec.first();let router_data_response = match recurring_response_item {Some(recurring_response) => {- let status =- common_enums::AttemptStatus::from(recurring_response.status.clone());+ let status = get_recurring_attempt_status(+ recurring_response.status.clone(),+ prev_status,+ );let connector_transaction_id = recurring_response.tran_id.as_ref()@@ -1093,11 +1091,19 @@ impl TryFrom<PaymentsResponseRouterData<FiuuPaymentsResponse>> for PaymentsAutho}}-impl From<FiuuRecurringStautus> for common_enums::AttemptStatus {- fn from(status: FiuuRecurringStautus) -> Self {- match status {- FiuuRecurringStautus::Accepted => Self::Charged,- FiuuRecurringStautus::Failed => Self::Failure,+fn get_recurring_attempt_status(+ status: FiuuRecurringStautus,+ prev_status: common_enums::AttemptStatus,+) -> common_enums::AttemptStatus {+ match status {+ FiuuRecurringStautus::Accepted => common_enums::AttemptStatus::Charged,+ FiuuRecurringStautus::Failed => common_enums::AttemptStatus::Failure,+ FiuuRecurringStautus::Unknown => {+ router_env::logger::warn!(+ "Fiuu returned unknown recurring status; retaining previous status {:?}",+ prev_status+ );+ prev_status}}}@@ -1279,6 +1285,8 @@ pub enum StatCode {Failure,#[serde(rename = "22")]Pending,+ #[serde(other)]+ Unknown,}#[derive(Debug, Serialize, Deserialize, Display, Clone, Copy, PartialEq)]@@ -1300,7 +1308,7 @@ pub enum StatName {ReqChargeback,#[serde(rename = "Pending")]Pending,- #[serde(rename = "Unknown")]+ #[serde(other)]Unknown,}impl TryFrom<&PaymentsSyncRouterData> for FiuuPaymentSyncRequest {@@ -1393,6 +1401,7 @@ impl TryFrom<PaymentsSyncResponseRouterData<FiuuPaymentResponse>> for PaymentsSyfn try_from(item: PaymentsSyncResponseRouterData<FiuuPaymentResponse>,) -> Result<Self, Self::Error> {+ let prev_status = item.data.status;match item.response {FiuuPaymentResponse::FiuuPaymentSyncResponse(response) => {let stat_name = response.stat_name;@@ -1401,6 +1410,7 @@ impl TryFrom<PaymentsSyncResponseRouterData<FiuuPaymentResponse>> for PaymentsSylet status = enums::AttemptStatus::try_from(FiuuSyncStatus {stat_name,stat_code,+ prev_status,})?;let error_response = if status == enums::AttemptStatus::Failure {let error_details = ErrorDetails::try_from(ErrorInputs {@@ -1452,6 +1462,7 @@ impl TryFrom<PaymentsSyncResponseRouterData<FiuuPaymentResponse>> for PaymentsSylet status = enums::AttemptStatus::try_from(FiuuWebhookStatus {capture_method: item.data.request.capture_method,status: response.status,+ prev_status,})?;let txn_id = response.tran_id;let mandate_reference = response.extra_parameters.as_ref().and_then(|extra_p| {@@ -1526,11 +1537,13 @@ impl TryFrom<PaymentsSyncResponseRouterData<FiuuPaymentResponse>> for PaymentsSypub struct FiuuWebhookStatus {pub capture_method: Option<CaptureMethod>,pub status: FiuuPaymentWebhookStatus,+ pub prev_status: enums::AttemptStatus,}impl TryFrom<FiuuWebhookStatus> for enums::AttemptStatus {type Error = Report<errors::ConnectorError>;fn try_from(webhook_status: FiuuWebhookStatus) -> Result<Self, Self::Error> {+ let prev_status = webhook_status.prev_status;match webhook_status.status {FiuuPaymentWebhookStatus::Success => match webhook_status.capture_method {Some(CaptureMethod::Automatic) | Some(CaptureMethod::SequentialAutomatic) => {@@ -1543,6 +1556,13 @@ impl TryFrom<FiuuWebhookStatus> for enums::AttemptStatus {},FiuuPaymentWebhookStatus::Failure => Ok(Self::Failure),FiuuPaymentWebhookStatus::Pending => Ok(Self::AuthenticationPending),+ FiuuPaymentWebhookStatus::Unknown => {+ router_env::logger::warn!(+ "Fiuu returned unknown webhook payment status; retaining previous status {:?}",+ prev_status+ );+ Ok(prev_status)+ }}}}@@ -1569,11 +1589,13 @@ pub struct PaymentCaptureResponse {pub struct FiuuSyncStatus {pub stat_name: StatName,pub stat_code: StatCode,+ pub prev_status: enums::AttemptStatus,}impl TryFrom<FiuuSyncStatus> for enums::AttemptStatus {type Error = errors::ConnectorError;fn try_from(sync_status: FiuuSyncStatus) -> Result<Self, Self::Error> {+ let prev_status = sync_status.prev_status;match (sync_status.stat_code, sync_status.stat_name) {(StatCode::Success, StatName::Captured | StatName::Settled) => Ok(Self::Charged), // For Success as StatCode we can only expect Captured,Settled and Authorized as StatName.(StatCode::Success, StatName::Authorized) => Ok(Self::Authorized),@@ -1583,6 +1605,13 @@ impl TryFrom<FiuuSyncStatus> for enums::AttemptStatus {Ok(Self::Voided)}(StatCode::Failure, _) => Ok(Self::Failure),+ (StatCode::Unknown, _) => {+ router_env::logger::warn!(+ "Fiuu returned unknown sync stat_code; retaining previous status {:?}",+ prev_status+ );+ Ok(prev_status)+ }(other, _) => Err(errors::ConnectorError::UnexpectedResponseError(bytes::Bytes::from(other.to_string()),)),@@ -1877,6 +1906,8 @@ pub enum RefundStatus {Pending,Rejected,Processing,+ #[serde(other)]+ Unknown,}impl TryFrom<RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>>@@ -1887,6 +1918,7 @@ impl TryFrom<RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>>fn try_from(item: RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>,) -> Result<Self, Self::Error> {+ let prev_status = item.data.request.refund_status;match item.response {FiuuRefundSyncResponse::Error(error) => Ok(Self {response: Err(ErrorResponse {@@ -1914,7 +1946,10 @@ impl TryFrom<RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>>Ok(Self {response: Ok(RefundsResponseData {connector_refund_id: refund.refund_id.clone(),- refund_status: enums::RefundStatus::from(refund.status.clone()),+ refund_status: get_refund_status_from_sync(+ refund.status.clone(),+ prev_status,+ ),}),..item.data})@@ -1922,8 +1957,9 @@ impl TryFrom<RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>>FiuuRefundSyncResponse::Webhook(fiuu_webhooks_refund_response) => Ok(Self {response: Ok(RefundsResponseData {connector_refund_id: fiuu_webhooks_refund_response.refund_id,- refund_status: enums::RefundStatus::from(+ refund_status: get_refund_status_from_webhook(fiuu_webhooks_refund_response.status.clone(),+ prev_status,),}),..item.data@@ -1932,13 +1968,21 @@ impl TryFrom<RefundsResponseRouterData<RSync, FiuuRefundSyncResponse>>}}-impl From<RefundStatus> for enums::RefundStatus {- fn from(item: RefundStatus) -> Self {- match item {- RefundStatus::Pending => Self::Pending,- RefundStatus::Success => Self::Success,- RefundStatus::Rejected => Self::Failure,- RefundStatus::Processing => Self::Pending,+fn get_refund_status_from_sync(+ status: RefundStatus,+ prev_status: enums::RefundStatus,+) -> enums::RefundStatus {+ match status {+ RefundStatus::Pending => enums::RefundStatus::Pending,+ RefundStatus::Success => enums::RefundStatus::Success,+ RefundStatus::Rejected => enums::RefundStatus::Failure,+ RefundStatus::Processing => enums::RefundStatus::Pending,+ RefundStatus::Unknown => {+ router_env::logger::warn!(+ "Fiuu returned unknown refund sync status; previous status was {:?}, marking refund as pending",+ prev_status+ );+ prev_status}}}@@ -1986,13 +2030,13 @@ pub struct FiuuWebhooksPaymentResponse {pub order_id: String,#[serde(rename = "tranID")]pub tran_id: String,- pub nbcb: String,+ pub nbcb: Option<String>,pub amount: StringMajorUnit,pub currency: String,pub domain: Secret<String>,pub appcode: Option<Secret<String>>,pub paydate: String,- pub channel: String,+ pub channel: Option<String>,pub error_desc: Option<String>,pub error_code: Option<String>,#[serde(rename = "extraP")]@@ -2061,6 +2105,8 @@ pub enum FiuuRefundsWebhookStatus {#[strum(serialize = "22")]#[serde(rename = "22")]RefundPending,+ #[serde(other)]+ Unknown,}#[derive(Debug, Deserialize, Serialize, Clone, strum::Display)]@@ -2094,6 +2140,8 @@ pub enum FiuuPaymentWebhookStatus {#[strum(serialize = "22")]#[serde(rename = "22")]Pending,+ #[serde(other)]+ Unknown,}impl From<FiuuPaymentWebhookStatus> for StatCode {@@ -2102,6 +2150,7 @@ impl From<FiuuPaymentWebhookStatus> for StatCode {FiuuPaymentWebhookStatus::Success => Self::Success,FiuuPaymentWebhookStatus::Failure => Self::Failure,FiuuPaymentWebhookStatus::Pending => Self::Pending,+ FiuuPaymentWebhookStatus::Unknown => Self::Unknown,}}}@@ -2112,6 +2161,12 @@ impl From<FiuuPaymentWebhookStatus> for api_models::webhooks::IncomingWebhookEveFiuuPaymentWebhookStatus::Success => Self::PaymentIntentSuccess,FiuuPaymentWebhookStatus::Failure => Self::PaymentIntentFailure,FiuuPaymentWebhookStatus::Pending => Self::PaymentIntentProcessing,+ FiuuPaymentWebhookStatus::Unknown => {+ router_env::logger::warn!(+ "Unknown fiuu payment webhook status received; acknowledging without processing"+ );+ Self::EventNotSupported+ }}}}@@ -2122,16 +2177,30 @@ impl From<FiuuRefundsWebhookStatus> for api_models::webhooks::IncomingWebhookEveFiuuRefundsWebhookStatus::RefundSuccess => Self::RefundSuccess,FiuuRefundsWebhookStatus::RefundFailure => Self::RefundFailure,FiuuRefundsWebhookStatus::RefundPending => Self::EventNotSupported,+ FiuuRefundsWebhookStatus::Unknown => {+ router_env::logger::warn!(+ "Unknown fiuu refund webhook status received; acknowledging without processing"+ );+ Self::EventNotSupported+ }}}}-impl From<FiuuRefundsWebhookStatus> for enums::RefundStatus {- fn from(value: FiuuRefundsWebhookStatus) -> Self {- match value {- FiuuRefundsWebhookStatus::RefundFailure => Self::Failure,- FiuuRefundsWebhookStatus::RefundSuccess => Self::Success,- FiuuRefundsWebhookStatus::RefundPending => Self::Pending,+fn get_refund_status_from_webhook(+ status: FiuuRefundsWebhookStatus,+ prev_status: enums::RefundStatus,+) -> enums::RefundStatus {+ match status {+ FiuuRefundsWebhookStatus::RefundFailure => enums::RefundStatus::Failure,+ FiuuRefundsWebhookStatus::RefundSuccess => enums::RefundStatus::Success,+ FiuuRefundsWebhookStatus::RefundPending => enums::RefundStatus::Pending,+ FiuuRefundsWebhookStatus::Unknown => {+ router_env::logger::warn!(+ "Fiuu returned unknown refund webhook status; previous status was {:?}, marking refund as pending",+ prev_status+ );+ prev_status}}}