Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(connector): [Iatapay] refactor authorize flow and fix payment status mapping #2409

Merged
merged 16 commits into from
Jan 25, 2024
Merged
56 changes: 28 additions & 28 deletions crates/router/src/connector/iatapay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,13 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe
req: &types::PaymentsSyncRouterData,
connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
let connector_id = req
.request
.connector_transaction_id
.get_connector_transaction_id()
.change_context(errors::ConnectorError::MissingConnectorTransactionID)?;
let auth: iatapay::IatapayAuthType =
iatapay::IatapayAuthType::try_from(&req.connector_auth_type)?;
let merchant_id = auth.merchant_id.peek();
Ok(format!(
"{}/payments/{}",
"{}/merchants/{merchant_id}/payments/{}",
self.base_url(connectors),
connector_id
req.connector_request_reference_id.clone()
))
}

Expand Down Expand Up @@ -634,11 +632,10 @@ impl api::IncomingWebhook for Iatapay {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
let notif: IatapayPaymentsResponse =
request
.body
.parse_struct("IatapayPaymentsResponse")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
let notif: iatapay::IatapayWebhookResponse = request
.body
.parse_struct("IatapayWebhookResponse")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
if notif.iata_payment_id.is_some() {
Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(
Expand All @@ -658,29 +655,32 @@ impl api::IncomingWebhook for Iatapay {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
let notif: IatapayPaymentsResponse =
request
.body
.parse_struct("IatapayPaymentsResponse")
.change_context(errors::ConnectorError::WebhookEventTypeNotFound)?;
let notif: iatapay::IatapayWebhookResponse = request
.body
.parse_struct("IatapayWebhookResponse")
.change_context(errors::ConnectorError::WebhookEventTypeNotFound)?;
match notif.status {
iatapay::IatapayPaymentStatus::Authorized => match notif.iata_payment_id.is_some() {
iatapay::IatapayWebhookStatus::Authorized => match notif.iata_payment_id.is_some() {
true => Ok(api::IncomingWebhookEvent::PaymentIntentSuccess),
false => Ok(api::IncomingWebhookEvent::RefundSuccess),
},
iatapay::IatapayPaymentStatus::Failed => match notif.iata_payment_id.is_some() {
iatapay::IatapayWebhookStatus::Failed => match notif.iata_payment_id.is_some() {
true => Ok(api::IncomingWebhookEvent::PaymentIntentFailure),
false => Ok(api::IncomingWebhookEvent::RefundFailure),
},
iatapay::IatapayPaymentStatus::Unknown
| iatapay::IatapayPaymentStatus::Created
| iatapay::IatapayPaymentStatus::Initiated
| iatapay::IatapayPaymentStatus::Cleared
| iatapay::IatapayPaymentStatus::Settled
| iatapay::IatapayPaymentStatus::Tobeinvestigated
| iatapay::IatapayPaymentStatus::Blocked
| iatapay::IatapayPaymentStatus::Locked
| iatapay::IatapayPaymentStatus::UnexpectedSettled => {
iatapay::IatapayWebhookStatus::Settled => {
Ok(api::IncomingWebhookEvent::PaymentIntentSuccess)
}
iatapay::IatapayWebhookStatus::Initiated => {
Ok(api::IncomingWebhookEvent::PaymentIntentProcessing)
}
iatapay::IatapayWebhookStatus::Created
| iatapay::IatapayWebhookStatus::Cleared
| iatapay::IatapayWebhookStatus::Tobeinvestigated
| iatapay::IatapayWebhookStatus::Blocked
| iatapay::IatapayWebhookStatus::Locked
| iatapay::IatapayWebhookStatus::UnexpectedSettled
| iatapay::IatapayWebhookStatus::Unknown => {
Ok(api::IncomingWebhookEvent::EventNotSupported)
}
}
Expand Down
49 changes: 40 additions & 9 deletions crates/router/src/connector/iatapay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl
let payment_method = item.router_data.payment_method;
let country = match payment_method {
PaymentMethod::Upi => "IN".to_string(),

PaymentMethod::Card
| PaymentMethod::CardRedirect
| PaymentMethod::PayLater
Expand All @@ -154,7 +153,19 @@ impl
api::PaymentMethodData::Upi(upi_data) => upi_data.vpa_id.map(|id| PayerInfo {
token_id: id.switch_strategy(),
}),
_ => None,
api::PaymentMethodData::Card(_)
| api::PaymentMethodData::CardRedirect(_)
| api::PaymentMethodData::Wallet(_)
| api::PaymentMethodData::PayLater(_)
| api::PaymentMethodData::BankRedirect(_)
| api::PaymentMethodData::BankDebit(_)
| api::PaymentMethodData::BankTransfer(_)
| api::PaymentMethodData::Crypto(_)
| api::PaymentMethodData::MandatePayment
| api::PaymentMethodData::Reward
| api::PaymentMethodData::Voucher(_)
| api::PaymentMethodData::GiftCard(_)
| api::PaymentMethodData::CardToken(_) => None,
};
let payload = Self {
merchant_id: IatapayAuthType::try_from(&item.router_data.connector_auth_type)?
Expand Down Expand Up @@ -212,15 +223,9 @@ pub enum IatapayPaymentStatus {
Initiated,
Authorized,
Settled,
Tobeinvestigated,
Blocked,
Cleared,
Failed,
Locked,
#[serde(rename = "UNEXPECTED SETTLED")]
UnexpectedSettled,
#[serde(other)]
Unknown,
}

impl From<IatapayPaymentStatus> for enums::AttemptStatus {
Expand All @@ -230,7 +235,6 @@ impl From<IatapayPaymentStatus> for enums::AttemptStatus {
IatapayPaymentStatus::Failed | IatapayPaymentStatus::UnexpectedSettled => Self::Failure,
IatapayPaymentStatus::Created => Self::AuthenticationPending,
IatapayPaymentStatus::Initiated => Self::Pending,
_ => Self::Voided,
}
}
}
Expand Down Expand Up @@ -473,3 +477,30 @@ pub struct IatapayAccessTokenErrorResponse {
pub error: String,
pub path: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IatapayWebhookResponse {
pub status: IatapayWebhookStatus,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incase of payment failure error_message, error_code and error_reason should be captured

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are already doing this for payments!

pub iata_payment_id: Option<String>,
pub iata_refund_id: Option<String>,
}
prasunna09 marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum IatapayWebhookStatus {
#[default]
Created,
Initiated,
Authorized,
Settled,
Cleared,
Failed,
Tobeinvestigated,
Blocked,
Locked,
#[serde(rename = "UNEXPECTED SETTLED")]
UnexpectedSettled,
#[serde(other)]
Unknown,
}
Loading