Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunna09 committed Nov 13, 2023
1 parent a998443 commit de60989
Show file tree
Hide file tree
Showing 14 changed files with 19 additions and 55 deletions.
4 changes: 0 additions & 4 deletions crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,10 +1424,6 @@ impl api::IncomingWebhook for Braintree {

let response = decode_webhook_payload(notif.bt_payload.replace('\n', "").as_bytes())?;

// let res_json = serde_json::to_value(response)
// .into_report()
// .change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;

Ok(Box::new(response))
}

Expand Down
3 changes: 0 additions & 3 deletions crates/router/src/connector/cashtocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,6 @@ impl api::IncomingWebhook for Cashtocode {
.body
.parse_struct("CashtocodeIncomingWebhook")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
// let res_json =
// utils::Encode::<transformers::CashtocodeIncomingWebhook>::encode_to_value(&webhook)
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;

Ok(Box::new(webhook))
}
Expand Down
3 changes: 1 addition & 2 deletions crates/router/src/connector/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,7 @@ impl api::IncomingWebhook for Coinbase {
.body
.parse_struct("CoinbaseWebhookDetails")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
// Encode::<CoinbaseWebhookDetails>::encode_to_value(&notif.event)
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)

Ok(Box::new(notif.event))
}
}
3 changes: 1 addition & 2 deletions crates/router/src/connector/cryptopay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ impl api::IncomingWebhook for Cryptopay {
.body
.parse_struct("CryptopayWebhookDetails")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
// Encode::<CryptopayWebhookDetails>::encode_to_value(&notif)
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)

Ok(Box::new(notif))
}
}
9 changes: 5 additions & 4 deletions crates/router/src/connector/globalpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,11 @@ impl api::IncomingWebhook for Globalpay {
let details = std::str::from_utf8(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let res_json = serde_json::from_str(details)
.into_report()
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
Ok(Box::new(res_json))
Ok(Box::new(
serde_json::from_str(details)
.into_report()
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?,
))
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/router/src/connector/iatapay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,7 @@ impl api::IncomingWebhook for Iatapay {
.body
.parse_struct("IatapayPaymentsResponse")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
// Encode::<IatapayPaymentsResponse>::encode_to_value(&notif)
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)

Ok(Box::new(notif))
}
}
4 changes: 0 additions & 4 deletions crates/router/src/connector/noon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,6 @@ impl api::IncomingWebhook for Noon {
.parse_struct("NoonWebhookObject")
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;

// let res_json = serde_json::to_value(noon::NoonPaymentsResponse::from(resource))
// .into_report()
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;

Ok(Box::new(noon::NoonPaymentsResponse::from(resource)))
}
}
2 changes: 1 addition & 1 deletion crates/router/src/connector/nuvei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ impl api::IncomingWebhook for Nuvei {
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let payment_response = nuvei::NuveiPaymentsResponse::from(body);
// Encode::<nuvei::NuveiPaymentsResponse>::encode_to_value(&payment_response).switch()

Ok(Box::new(payment_response))
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/router/src/connector/opennode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ impl api::IncomingWebhook for Opennode {
let notif = serde_urlencoded::from_bytes::<OpennodeWebhookDetails>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
// Encode::<OpennodeWebhookDetails>::encode_to_value(&notif.status)
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)

Ok(Box::new(notif.status))
}
}
22 changes: 4 additions & 18 deletions crates/router/src/connector/payme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,27 +1087,13 @@ impl api::IncomingWebhook for Payme {
transformers::NotifyType::SaleComplete
| transformers::NotifyType::SaleAuthorized
| transformers::NotifyType::SaleFailure => {
// serde_json::to_value(payme::PaymePaySaleResponse::from(resource))
// .into_report()
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)
Ok(Box::new(payme::PaymePaySaleResponse::from(resource)))
}
transformers::NotifyType::Refund => {
// serde_json::to_value(payme::PaymeQueryTransactionResponse::from(resource))
// .into_report()
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)
Ok(Box::new(payme::PaymeQueryTransactionResponse::from(
resource,
)))
}
transformers::NotifyType::Refund => Ok(Box::new(
payme::PaymeQueryTransactionResponse::from(resource),
)),
transformers::NotifyType::SaleChargeback
| transformers::NotifyType::SaleChargebackRefund =>
// serde_json::to_value(resource)
// .into_report()
// .change_context(errors::ConnectorError::WebhookBodyDecodingFailed),
{
Ok(Box::new(resource))
}
| transformers::NotifyType::SaleChargebackRefund => Ok(Box::new(resource)),
}
}

Expand Down
4 changes: 0 additions & 4 deletions crates/router/src/connector/trustpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,6 @@ impl api::IncomingWebhook for Trustpay {
.body
.parse_struct("TrustpayWebhookResponse")
.switch()?;
// let res_json = utils::Encode::<trustpay::WebhookPaymentInformation>::encode_to_value(
// &details.payment_information,
// )
// .change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
Ok(Box::new(details.payment_information))
}

Expand Down
3 changes: 0 additions & 3 deletions crates/router/src/connector/worldpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,6 @@ impl api::IncomingWebhook for Worldpay {
.parse_struct("WorldpayWebhookEventType")
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
let psync_body = WorldpayEventResponse::try_from(body)?;
// let res_json = serde_json::to_value(psync_body)
// .into_report()
// .change_context(errors::ConnectorError::WebhookResponseEncodingFailed)?;
Ok(Box::new(psync_body))
}
}
10 changes: 4 additions & 6 deletions crates/router/src/core/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,7 @@ pub async fn webhooks_wrapper<W: types::OutgoingWebhookType, Ctx: PaymentMethodR
.saturating_duration_since(start_instant)
.as_millis();

// let body_vec: Vec<u8> = body.to_vec();
// let serialized_request: serde_json::Value = serde_json::from_slice(&body_vec)
// .into_report()
// .change_context(errors::ApiErrorResponse::WebhookProcessingFailure)?;
let serialized_request = serde_json::to_value(&serialized_req)
let serialized_request = masking::masked_serialize(&serialized_req)
.into_report()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Could not convert webhook effect to string")?;
Expand Down Expand Up @@ -1127,7 +1123,9 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType, Ctx: PaymentMethodRetr
resource_object: serde_json::to_vec(&event_object)
.into_report()
.change_context(errors::ParsingError::EncodeError("byte-vec"))
.attach_printable_lazy(|| format!("Unable to convert webhook paylaod to a value"))
.attach_printable_lazy(|| {
"Unable to convert webhook paylaod to a value".to_string()
})
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable(
"There was an issue when encoding the incoming webhook body to bytes",
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/events/event_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::services::logger;
pub struct EventLogger {}

impl EventHandler for EventLogger {
#[track_caller]
fn log_event(&self, event: RawEvent) {
logger::info!(event = ?serde_json::to_string(&event.payload).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? event.event_type, event_id =? event.key, log_type = "event");
}
Expand Down

0 comments on commit de60989

Please sign in to comment.