fix(connector): update amount captured after webhook call and parse error responses from connector properly (#1680)

This commit is contained in:
BallaNitesh
2023-07-19 14:46:04 +05:30
committed by GitHub
parent f06e5dcd63
commit cac9f5049e
2 changed files with 59 additions and 30 deletions

View File

@ -262,8 +262,12 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe
data: &types::PaymentsSyncRouterData, data: &types::PaymentsSyncRouterData,
res: Response, res: Response,
) -> CustomResult<types::PaymentsSyncRouterData, errors::ConnectorError> { ) -> CustomResult<types::PaymentsSyncRouterData, errors::ConnectorError> {
let response: transformers::CashtocodePaymentsSyncResponse = res
.response
.parse_struct("CashtocodePaymentsSyncResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from(types::ResponseRouterData { types::RouterData::try_from(types::ResponseRouterData {
response: cashtocode::CashtocodePaymentsSyncResponse {}, response,
data: data.clone(), data: data.clone(),
http_code: res.status_code, http_code: res.status_code,
}) })
@ -359,9 +363,9 @@ impl api::IncomingWebhook for Cashtocode {
&self, &self,
request: &api::IncomingWebhookRequestDetails<'_>, request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> { ) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
let webhook: transformers::CashtocodeObjectId = request let webhook: transformers::CashtocodePaymentsSyncResponse = request
.body .body
.parse_struct("CashtocodeObjectId") .parse_struct("CashtocodePaymentsSyncResponse")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?; .change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
Ok(api_models::webhooks::ObjectReferenceId::PaymentId( Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
@ -397,9 +401,9 @@ impl api::IncomingWebhook for Cashtocode {
) -> CustomResult<services::api::ApplicationResponse<serde_json::Value>, errors::ConnectorError> ) -> CustomResult<services::api::ApplicationResponse<serde_json::Value>, errors::ConnectorError>
{ {
let status = "EXECUTED".to_string(); let status = "EXECUTED".to_string();
let obj: transformers::CashtocodeObjectId = request let obj: transformers::CashtocodePaymentsSyncResponse = request
.body .body
.parse_struct("CashtocodeObjectId") .parse_struct("CashtocodePaymentsSyncResponse")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?; .change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
let response: serde_json::Value = let response: serde_json::Value =
serde_json::json!({ "status": status, "transactionId" : obj.transaction_id}); serde_json::json!({ "status": status, "transactionId" : obj.transaction_id});

View File

@ -123,13 +123,25 @@ pub struct CashtocodeErrors {
pub event_type: String, pub event_type: String,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum CashtocodePaymentsResponse {
CashtoCodeError(CashtocodeErrorResponse),
CashtoCodeData(CashtocodePaymentsResponseData),
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CashtocodePaymentsResponse { pub struct CashtocodePaymentsResponseData {
pub pay_url: String, pub pay_url: String,
} }
pub struct CashtocodePaymentsSyncResponse {} #[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CashtocodePaymentsSyncResponse {
pub transaction_id: String,
pub amount: i64,
}
impl<F, T> impl<F, T>
TryFrom< TryFrom<
@ -145,14 +157,25 @@ impl<F, T>
types::PaymentsResponseData, types::PaymentsResponseData,
>, >,
) -> Result<Self, Self::Error> { ) -> Result<Self, Self::Error> {
let (status, response) = match item.response {
CashtocodePaymentsResponse::CashtoCodeError(error_data) => (
enums::AttemptStatus::Failure,
Err(types::ErrorResponse {
code: error_data.error.to_string(),
status_code: item.http_code,
message: error_data.error_description,
reason: None,
}),
),
CashtocodePaymentsResponse::CashtoCodeData(response_data) => {
let redirection_data = services::RedirectForm::Form { let redirection_data = services::RedirectForm::Form {
endpoint: item.response.pay_url.clone(), endpoint: response_data.pay_url,
method: services::Method::Post, method: services::Method::Post,
form_fields: Default::default(), form_fields: Default::default(),
}; };
Ok(Self { (
status: enums::AttemptStatus::AuthenticationPending, enums::AttemptStatus::AuthenticationPending,
response: Ok(types::PaymentsResponseData::TransactionResponse { Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId( resource_id: types::ResponseId::ConnectorTransactionId(
item.data.attempt_id.clone(), item.data.attempt_id.clone(),
), ),
@ -162,6 +185,13 @@ impl<F, T>
network_txn_id: None, network_txn_id: None,
connector_response_reference_id: None, connector_response_reference_id: None,
}), }),
)
}
};
Ok(Self {
status,
response,
..item.data ..item.data
}) })
} }
@ -198,6 +228,7 @@ impl<F, T>
network_txn_id: None, network_txn_id: None,
connector_response_reference_id: None, connector_response_reference_id: None,
}), }),
amount_captured: Some(item.response.amount),
..item.data ..item.data
}) })
} }
@ -205,7 +236,7 @@ impl<F, T>
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CashtocodeErrorResponse { pub struct CashtocodeErrorResponse {
pub error: String, pub error: u32,
pub error_description: String, pub error_description: String,
pub errors: Option<Vec<CashtocodeErrors>>, pub errors: Option<Vec<CashtocodeErrors>>,
} }
@ -220,9 +251,3 @@ pub struct CashtocodeIncomingWebhook {
pub event_type: String, pub event_type: String,
pub transaction_id: String, pub transaction_id: String,
} }
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CashtocodeObjectId {
pub transaction_id: String,
}