Skip to content

Commit

Permalink
refactor(connector): [Mifinity] fix redirection after payment complet…
Browse files Browse the repository at this point in the history
…ion and handle 5xx error (#5250)
  • Loading branch information
swangi-kumari authored Jul 10, 2024
1 parent 545a6db commit 38d5137
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
91 changes: 56 additions & 35 deletions crates/router/src/connector/mifinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ pub mod transformers;

use std::fmt::Debug;

use error_stack::{report, ResultExt};
use error_stack::{report, Report, ResultExt};
use masking::ExposeInterface;
use transformers as mifinity;

use self::transformers::auth_headers;
use crate::{
configs::settings,
consts,
core::errors::{self, CustomResult},
events::connector_api_logs::ConnectorEvent,
headers,
headers, logger,
services::{
self,
request::{self, Mask},
Expand Down Expand Up @@ -112,39 +113,59 @@ impl ConnectorCommon for Mifinity {
res: Response,
event_builder: Option<&mut ConnectorEvent>,
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
let response: mifinity::MifinityErrorResponse = res
.response
.parse_struct("MifinityErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;

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
.errors
.iter()
.map(|error| error.error_code.clone())
.collect::<Vec<String>>()
.join(" & "),
message: response
.errors
.iter()
.map(|error| error.message.clone())
.collect::<Vec<String>>()
.join(" & "),
reason: Some(
response
.errors
.iter()
.map(|error| error.message.clone())
.collect::<Vec<String>>()
.join(" & "),
),
attempt_status: None,
connector_transaction_id: None,
})
if res.response.is_empty() {
Ok(ErrorResponse {
status_code: res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: consts::NO_ERROR_MESSAGE.to_string(),
reason: Some(consts::CONNECTOR_UNAUTHORIZED_ERROR.to_string()),
attempt_status: None,
connector_transaction_id: None,
})
} else {
let response: Result<
mifinity::MifinityErrorResponse,
Report<common_utils::errors::ParsingError>,
> = res.response.parse_struct("MifinityErrorResponse");

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
.errors
.iter()
.map(|error| error.error_code.clone())
.collect::<Vec<String>>()
.join(" & "),
message: response
.errors
.iter()
.map(|error| error.message.clone())
.collect::<Vec<String>>()
.join(" & "),
reason: Some(
response
.errors
.iter()
.map(|error| error.message.clone())
.collect::<Vec<String>>()
.join(" & "),
),
attempt_status: None,
connector_transaction_id: None,
})
}

Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
logger::error!(deserialization_error =? error_msg);
crate::utils::handle_json_response_deserialization_failure(res, "mifinity")
}
}
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions crates/router/src/services/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,20 +1877,23 @@ pub fn build_redirection_form(
RedirectForm::Mifinity {
initialization_token,
} => {
let mifinity_base_url = config.connectors.mifinity.base_url;
maud::html! {
(maud::DOCTYPE)
head {
(PreEscaped(r#"<script src='https://demo.mifinity.com/widgets/sgpg.js?58190a411dc3'></script>"#))
(PreEscaped(format!(r#"<script src='{mifinity_base_url}widgets/sgpg.js?58190a411dc3'></script>"#)))
}

(PreEscaped(format!("<div id='widget-container'></div>
<script>
var widget = showPaymentIframe('widget-container', {{
token: '{initialization_token}',
complete: function() {{
setTimeout(function() {{
widget.close();
}}, 5000);
var f = document.createElement('form');
f.action=window.location.pathname.replace(/payments\\/redirect\\/(\\w+)\\/(\\w+)\\/\\w+/, \"payments/$1/$2/redirect/response/mifinity\");
f.method='GET';
document.body.appendChild(f);
f.submit();
}}
}});
</script>")))
Expand Down

0 comments on commit 38d5137

Please sign in to comment.