Skip to content

Commit

Permalink
refactor NexixpayPaymentsResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrudul Vajpayee authored and Mrudul Vajpayee committed Nov 10, 2024
1 parent a0528c5 commit e9826fe
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 102 deletions.
36 changes: 11 additions & 25 deletions crates/hyperswitch_connectors/src/connectors/nexixpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,31 +480,17 @@ impl ConnectorIntegration<Authorize, PaymentsAuthorizeData, PaymentsResponseData
event_builder: Option<&mut ConnectorEvent>,
res: Response,
) -> CustomResult<PaymentsAuthorizeRouterData, errors::ConnectorError> {
if data.request.connector_mandate_id().is_none() {
let response: nexixpay::NexixpayPaymentsResponse = res
.response
.parse_struct("NexixpayPaymentsResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);
RouterData::try_from(ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
} else {
let response: nexixpay::NexixpayMandateResponse = res
.response
.parse_struct("NexixpayMandateResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);
RouterData::try_from(ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}
let response: nexixpay::NexixpayPaymentsResponse = res
.response
.parse_struct("NexixpayPaymentsResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);
RouterData::try_from(ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}

fn get_error_response(
Expand Down
160 changes: 83 additions & 77 deletions crates/hyperswitch_connectors/src/connectors/nexixpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ struct Recurrence {

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NexixpayPaymentsResponse {
pub struct PaymentsResponse {
operation: Operation,
three_d_s_auth_request: String,
three_d_s_auth_url: Secret<url::Url>,
Expand All @@ -199,6 +199,14 @@ pub struct NexixpayMandateResponse {
operation: Operation,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum NexixpayPaymentsResponse {
PaymentResponse(Box<PaymentsResponse>),
MandateResponse(Box<NexixpayMandateResponse>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreeDSAuthResult {
Expand Down Expand Up @@ -719,83 +727,81 @@ impl<F>
PaymentsResponseData,
>,
) -> Result<Self, Self::Error> {
let complete_authorize_url = item.data.request.get_complete_authorize_url()?;
let operation_id: String = item.response.operation.operation_id;
let redirection_form = nexixpay_threeds_link(NexixpayRedirectionRequest {
three_d_s_auth_url: item.response.three_d_s_auth_url.expose().to_string(),
three_ds_request: item.response.three_d_s_auth_request.clone(),
return_url: complete_authorize_url.clone(),
transaction_id: operation_id.clone(),
})?;
let is_auto_capture = item.data.request.is_auto_capture()?;
let connector_metadata = Some(serde_json::json!(NexixpayConnectorMetaData {
three_d_s_auth_result: None,
three_d_s_auth_response: None,
authorization_operation_id: Some(operation_id.clone()),
cancel_operation_id: None,
capture_operation_id: {
if is_auto_capture {
Some(operation_id)
} else {
None
}
},
psync_flow: NexixpayPaymentIntent::Authorize
}));
Ok(Self {
status: AttemptStatus::from(item.response.operation.operation_result),
response: Ok(PaymentsResponseData::TransactionResponse {
resource_id: ResponseId::ConnectorTransactionId(
item.response.operation.order_id.clone(),
),
redirection_data: Box::new(Some(redirection_form.clone())),
mandate_reference: Box::new(Some(MandateReference {
connector_mandate_id: item.data.connector_mandate_request_reference_id.clone(),
payment_method_id: None,
mandate_metadata: None,
connector_mandate_request_reference_id: None,
})),
connector_metadata,
network_txn_id: None,
connector_response_reference_id: Some(item.response.operation.order_id),
incremental_authorization_allowed: None,
charge_id: None,
}),
..item.data
})
}
}

impl<F>
TryFrom<
ResponseRouterData<F, NexixpayMandateResponse, PaymentsAuthorizeData, PaymentsResponseData>,
> for RouterData<F, PaymentsAuthorizeData, PaymentsResponseData>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
item: ResponseRouterData<
F,
NexixpayMandateResponse,
PaymentsAuthorizeData,
PaymentsResponseData,
>,
) -> Result<Self, Self::Error> {
Ok(Self {
status: AttemptStatus::from(item.response.operation.operation_result),
response: Ok(PaymentsResponseData::TransactionResponse {
resource_id: ResponseId::ConnectorTransactionId(
item.response.operation.order_id.clone(),
),
redirection_data: Box::new(None),
mandate_reference: Box::new(None),
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: Some(item.response.operation.order_id),
incremental_authorization_allowed: None,
charge_id: None,
match item.response {
NexixpayPaymentsResponse::PaymentResponse(ref response_body) => {
let complete_authorize_url = item.data.request.get_complete_authorize_url()?;
let operation_id: String = response_body.operation.operation_id.clone();
let redirection_form = nexixpay_threeds_link(NexixpayRedirectionRequest {
three_d_s_auth_url: response_body
.three_d_s_auth_url
.clone()
.expose()
.to_string(),
three_ds_request: response_body.three_d_s_auth_request.clone(),
return_url: complete_authorize_url.clone(),
transaction_id: operation_id.clone(),
})?;
let is_auto_capture = item.data.request.is_auto_capture()?;
let connector_metadata = Some(serde_json::json!(NexixpayConnectorMetaData {
three_d_s_auth_result: None,
three_d_s_auth_response: None,
authorization_operation_id: Some(operation_id.clone()),
cancel_operation_id: None,
capture_operation_id: {
if is_auto_capture {
Some(operation_id)
} else {
None
}
},
psync_flow: NexixpayPaymentIntent::Authorize
}));
Ok(Self {
status: AttemptStatus::from(response_body.operation.operation_result.clone()),
response: Ok(PaymentsResponseData::TransactionResponse {
resource_id: ResponseId::ConnectorTransactionId(
response_body.operation.order_id.clone(),
),
redirection_data: Box::new(Some(redirection_form.clone())),
mandate_reference: Box::new(Some(MandateReference {
connector_mandate_id: item
.data
.connector_mandate_request_reference_id
.clone(),
payment_method_id: None,
mandate_metadata: None,
connector_mandate_request_reference_id: None,
})),
connector_metadata,
network_txn_id: None,
connector_response_reference_id: Some(
response_body.operation.order_id.clone(),
),
incremental_authorization_allowed: None,
charge_id: None,
}),
..item.data
})
}
NexixpayPaymentsResponse::MandateResponse(ref mandate_response) => Ok(Self {
status: AttemptStatus::from(mandate_response.operation.operation_result.clone()),
response: Ok(PaymentsResponseData::TransactionResponse {
resource_id: ResponseId::ConnectorTransactionId(
mandate_response.operation.order_id.clone(),
),
redirection_data: Box::new(None),
mandate_reference: Box::new(None),
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: Some(
mandate_response.operation.order_id.clone(),
),
incremental_authorization_allowed: None,
charge_id: None,
}),
..item.data
}),
..item.data
})
}
}
}

Expand Down

0 comments on commit e9826fe

Please sign in to comment.