refactor: [Checkout] change payment and webhooks API contract (#4023)

This commit is contained in:
Swangi Kumari
2024-03-12 19:04:40 +05:30
committed by GitHub
parent 7513423631
commit 733a560146
2 changed files with 117 additions and 36 deletions

View File

@ -1242,30 +1242,43 @@ impl api::IncomingWebhook for Checkout {
.body .body
.parse_struct("CheckoutWebhookBody") .parse_struct("CheckoutWebhookBody")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?; .change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
let ref_id: api_models::webhooks::ObjectReferenceId =
if checkout::is_chargeback_event(&details.transaction_type) { if checkout::is_chargeback_event(&details.transaction_type) {
return Ok(api_models::webhooks::ObjectReferenceId::PaymentId( let reference = match details.data.reference {
api_models::payments::PaymentIdType::ConnectorTransactionId( Some(reference) => {
api_models::payments::PaymentIdType::PaymentAttemptId(reference)
}
None => api_models::payments::PaymentIdType::ConnectorTransactionId(
details details
.data .data
.payment_id .payment_id
.ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
), ),
)); };
} api_models::webhooks::ObjectReferenceId::PaymentId(reference)
if checkout::is_refund_event(&details.transaction_type) { } else if checkout::is_refund_event(&details.transaction_type) {
return Ok(api_models::webhooks::ObjectReferenceId::RefundId( let refund_reference = match details.data.reference {
api_models::webhooks::RefundIdType::ConnectorRefundId( Some(reference) => api_models::webhooks::RefundIdType::RefundId(reference),
None => api_models::webhooks::RefundIdType::ConnectorRefundId(
details details
.data .data
.action_id .action_id
.ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
), ),
)); };
api_models::webhooks::ObjectReferenceId::RefundId(refund_reference)
} else {
let reference_id = match details.data.reference {
Some(reference) => {
api_models::payments::PaymentIdType::PaymentAttemptId(reference)
} }
Ok(api_models::webhooks::ObjectReferenceId::PaymentId( None => {
api_models::payments::PaymentIdType::ConnectorTransactionId(details.data.id), api_models::payments::PaymentIdType::ConnectorTransactionId(details.data.id)
)) }
};
api_models::webhooks::ObjectReferenceId::PaymentId(reference_id)
};
Ok(ref_id)
} }
fn get_webhook_event_type( fn get_webhook_event_type(

View File

@ -242,6 +242,7 @@ pub struct PaymentsRequest {
pub return_url: ReturnUrl, pub return_url: ReturnUrl,
pub capture: bool, pub capture: bool,
pub reference: String, pub reference: String,
pub metadata: Option<Secret<serde_json::Value>>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -428,6 +429,7 @@ impl TryFrom<&CheckoutRouterData<&types::PaymentsAuthorizeRouterData>> for Payme
let connector_auth = &item.router_data.connector_auth_type; let connector_auth = &item.router_data.connector_auth_type;
let auth_type: CheckoutAuthType = connector_auth.try_into()?; let auth_type: CheckoutAuthType = connector_auth.try_into()?;
let processing_channel_id = auth_type.processing_channel_id; let processing_channel_id = auth_type.processing_channel_id;
let metadata = item.router_data.request.metadata.clone();
Ok(Self { Ok(Self {
source: source_var, source: source_var,
amount: item.amount.to_owned(), amount: item.amount.to_owned(),
@ -437,6 +439,7 @@ impl TryFrom<&CheckoutRouterData<&types::PaymentsAuthorizeRouterData>> for Payme
return_url, return_url,
capture, capture,
reference: item.router_data.connector_request_reference_id.clone(), reference: item.router_data.connector_request_reference_id.clone(),
metadata,
}) })
} }
} }
@ -450,6 +453,16 @@ pub enum CheckoutPaymentStatus {
CardVerified, CardVerified,
Declined, Declined,
Captured, Captured,
#[serde(rename = "Retry Scheduled")]
RetryScheduled,
Voided,
#[serde(rename = "Partially Captured")]
PartiallyCaptured,
#[serde(rename = "Partially Refunded")]
PartiallyRefunded,
Refunded,
Canceled,
Expired,
} }
impl TryFrom<CheckoutWebhookEventType> for CheckoutPaymentStatus { impl TryFrom<CheckoutWebhookEventType> for CheckoutPaymentStatus {
@ -460,7 +473,14 @@ impl TryFrom<CheckoutWebhookEventType> for CheckoutPaymentStatus {
CheckoutWebhookEventType::PaymentCaptured => Ok(Self::Captured), CheckoutWebhookEventType::PaymentCaptured => Ok(Self::Captured),
CheckoutWebhookEventType::PaymentDeclined => Ok(Self::Declined), CheckoutWebhookEventType::PaymentDeclined => Ok(Self::Declined),
CheckoutWebhookEventType::AuthenticationStarted CheckoutWebhookEventType::AuthenticationStarted
| CheckoutWebhookEventType::AuthenticationApproved => Ok(Self::Pending), | CheckoutWebhookEventType::AuthenticationApproved
| CheckoutWebhookEventType::AuthenticationAttempted => Ok(Self::Pending),
CheckoutWebhookEventType::AuthenticationExpired
| CheckoutWebhookEventType::AuthenticationFailed
| CheckoutWebhookEventType::PaymentAuthenticationFailed
| CheckoutWebhookEventType::PaymentCaptureDeclined => Ok(Self::Declined),
CheckoutWebhookEventType::PaymentCanceled => Ok(Self::Canceled),
CheckoutWebhookEventType::PaymentVoided => Ok(Self::Voided),
CheckoutWebhookEventType::PaymentRefunded CheckoutWebhookEventType::PaymentRefunded
| CheckoutWebhookEventType::PaymentRefundDeclined | CheckoutWebhookEventType::PaymentRefundDeclined
| CheckoutWebhookEventType::DisputeReceived | CheckoutWebhookEventType::DisputeReceived
@ -494,10 +514,18 @@ impl ForeignFrom<(CheckoutPaymentStatus, Option<enums::CaptureMethod>)> for enum
Self::Authorized Self::Authorized
} }
} }
CheckoutPaymentStatus::Captured => Self::Charged, CheckoutPaymentStatus::Captured
CheckoutPaymentStatus::Declined => Self::Failure, | CheckoutPaymentStatus::PartiallyRefunded
| CheckoutPaymentStatus::Refunded => Self::Charged,
CheckoutPaymentStatus::PartiallyCaptured => Self::PartialCharged,
CheckoutPaymentStatus::Declined
| CheckoutPaymentStatus::Expired
| CheckoutPaymentStatus::Canceled => Self::Failure,
CheckoutPaymentStatus::Pending => Self::AuthenticationPending, CheckoutPaymentStatus::Pending => Self::AuthenticationPending,
CheckoutPaymentStatus::CardVerified => Self::Pending, CheckoutPaymentStatus::CardVerified | CheckoutPaymentStatus::RetryScheduled => {
Self::Pending
}
CheckoutPaymentStatus::Voided => Self::Voided,
} }
} }
} }
@ -514,10 +542,18 @@ impl ForeignFrom<(CheckoutPaymentStatus, CheckoutPaymentIntent)> for enums::Atte
Self::Authorized Self::Authorized
} }
} }
CheckoutPaymentStatus::Captured => Self::Charged, CheckoutPaymentStatus::Captured
CheckoutPaymentStatus::Declined => Self::Failure, | CheckoutPaymentStatus::PartiallyRefunded
| CheckoutPaymentStatus::Refunded => Self::Charged,
CheckoutPaymentStatus::PartiallyCaptured => Self::PartialCharged,
CheckoutPaymentStatus::Declined
| CheckoutPaymentStatus::Expired
| CheckoutPaymentStatus::Canceled => Self::Failure,
CheckoutPaymentStatus::Pending => Self::AuthenticationPending, CheckoutPaymentStatus::Pending => Self::AuthenticationPending,
CheckoutPaymentStatus::CardVerified => Self::Pending, CheckoutPaymentStatus::CardVerified | CheckoutPaymentStatus::RetryScheduled => {
Self::Pending
}
CheckoutPaymentStatus::Voided => Self::Voided,
} }
} }
} }
@ -537,10 +573,18 @@ impl ForeignFrom<(CheckoutPaymentStatus, Option<Balances>)> for enums::AttemptSt
Self::Authorized Self::Authorized
} }
} }
CheckoutPaymentStatus::Captured => Self::Charged, CheckoutPaymentStatus::Captured
CheckoutPaymentStatus::Declined => Self::Failure, | CheckoutPaymentStatus::PartiallyRefunded
| CheckoutPaymentStatus::Refunded => Self::Charged,
CheckoutPaymentStatus::PartiallyCaptured => Self::PartialCharged,
CheckoutPaymentStatus::Declined
| CheckoutPaymentStatus::Expired
| CheckoutPaymentStatus::Canceled => Self::Failure,
CheckoutPaymentStatus::Pending => Self::AuthenticationPending, CheckoutPaymentStatus::Pending => Self::AuthenticationPending,
CheckoutPaymentStatus::CardVerified => Self::Pending, CheckoutPaymentStatus::CardVerified | CheckoutPaymentStatus::RetryScheduled => {
Self::Pending
}
CheckoutPaymentStatus::Voided => Self::Voided,
} }
} }
} }
@ -559,6 +603,7 @@ pub struct Links {
pub struct PaymentsResponse { pub struct PaymentsResponse {
id: String, id: String,
amount: Option<i32>, amount: Option<i32>,
currency: Option<String>,
action_id: Option<String>, action_id: Option<String>,
status: CheckoutPaymentStatus, status: CheckoutPaymentStatus,
#[serde(rename = "_links")] #[serde(rename = "_links")]
@ -567,6 +612,8 @@ pub struct PaymentsResponse {
reference: Option<String>, reference: Option<String>,
response_code: Option<String>, response_code: Option<String>,
response_summary: Option<String>, response_summary: Option<String>,
approved: Option<bool>,
processed_on: Option<String>,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -1132,11 +1179,18 @@ pub fn is_chargeback_event(event_code: &CheckoutWebhookEventType) -> bool {
pub enum CheckoutWebhookEventType { pub enum CheckoutWebhookEventType {
AuthenticationStarted, AuthenticationStarted,
AuthenticationApproved, AuthenticationApproved,
AuthenticationAttempted,
AuthenticationExpired,
AuthenticationFailed,
PaymentApproved, PaymentApproved,
PaymentCaptured, PaymentCaptured,
PaymentDeclined, PaymentDeclined,
PaymentRefunded, PaymentRefunded,
PaymentRefundDeclined, PaymentRefundDeclined,
PaymentAuthenticationFailed,
PaymentCanceled,
PaymentCaptureDeclined,
PaymentVoided,
DisputeReceived, DisputeReceived,
DisputeExpired, DisputeExpired,
DisputeAccepted, DisputeAccepted,
@ -1169,6 +1223,8 @@ pub struct CheckoutWebhookData {
pub response_code: Option<String>, pub response_code: Option<String>,
pub response_summary: Option<String>, pub response_summary: Option<String>,
pub currency: String, pub currency: String,
pub processed_on: Option<String>,
pub approved: Option<bool>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -1220,13 +1276,22 @@ pub enum CheckoutDisputeTransactionType {
impl From<CheckoutWebhookEventType> for api::IncomingWebhookEvent { impl From<CheckoutWebhookEventType> for api::IncomingWebhookEvent {
fn from(transaction_type: CheckoutWebhookEventType) -> Self { fn from(transaction_type: CheckoutWebhookEventType) -> Self {
match transaction_type { match transaction_type {
CheckoutWebhookEventType::AuthenticationStarted => Self::EventNotSupported, CheckoutWebhookEventType::AuthenticationStarted
CheckoutWebhookEventType::AuthenticationApproved => Self::EventNotSupported, | CheckoutWebhookEventType::AuthenticationApproved
| CheckoutWebhookEventType::AuthenticationAttempted => Self::EventNotSupported,
CheckoutWebhookEventType::AuthenticationExpired
| CheckoutWebhookEventType::AuthenticationFailed
| CheckoutWebhookEventType::PaymentAuthenticationFailed => {
Self::PaymentIntentAuthorizationFailure
}
CheckoutWebhookEventType::PaymentApproved => Self::EventNotSupported, CheckoutWebhookEventType::PaymentApproved => Self::EventNotSupported,
CheckoutWebhookEventType::PaymentCaptured => Self::PaymentIntentSuccess, CheckoutWebhookEventType::PaymentCaptured => Self::PaymentIntentSuccess,
CheckoutWebhookEventType::PaymentDeclined => Self::PaymentIntentFailure, CheckoutWebhookEventType::PaymentDeclined => Self::PaymentIntentFailure,
CheckoutWebhookEventType::PaymentRefunded => Self::RefundSuccess, CheckoutWebhookEventType::PaymentRefunded => Self::RefundSuccess,
CheckoutWebhookEventType::PaymentRefundDeclined => Self::RefundFailure, CheckoutWebhookEventType::PaymentRefundDeclined => Self::RefundFailure,
CheckoutWebhookEventType::PaymentCanceled => Self::PaymentIntentCancelFailure,
CheckoutWebhookEventType::PaymentCaptureDeclined => Self::PaymentIntentCaptureFailure,
CheckoutWebhookEventType::PaymentVoided => Self::PaymentIntentCancelled,
CheckoutWebhookEventType::DisputeReceived CheckoutWebhookEventType::DisputeReceived
| CheckoutWebhookEventType::DisputeEvidenceRequired => Self::DisputeOpened, | CheckoutWebhookEventType::DisputeEvidenceRequired => Self::DisputeOpened,
CheckoutWebhookEventType::DisputeExpired => Self::DisputeExpired, CheckoutWebhookEventType::DisputeExpired => Self::DisputeExpired,
@ -1329,6 +1394,9 @@ impl TryFrom<&api::IncomingWebhookRequestDetails<'_>> for PaymentsResponse {
response_code: data.response_code, response_code: data.response_code,
response_summary: data.response_summary, response_summary: data.response_summary,
action_id: data.action_id, action_id: data.action_id,
currency: Some(data.currency),
processed_on: data.processed_on,
approved: data.approved,
}; };
Ok(psync_struct) Ok(psync_struct)