Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(revenue_recovery): make connector_transaction_id and transaction_created_at mandatory and update recovery API docs (#14688)
CHALLA NISHANTH BABU853e39ecae48
diff --git a/api-reference/docs.json b/api-reference/docs.jsonindex 3e474cfe64..81797bc560 100644--- a/api-reference/docs.json+++ b/api-reference/docs.json@@ -340,7 +340,7 @@]},{- "version": "2.0.0 [BETA]",+ "version": "2.0.0","groups": [{"group": "Get Started [BETA]",@@ -503,13 +503,6 @@"v2/tokenization/tokenization--create"]},- {- "group": "Revenue Recovery",- "pages": [- "v2/revenue-recovery/revenue-recovery--create",- "v2/revenue-recovery/revenue-recovery--retrieve"- ]- },{"group": "Schemas","pages": [@@ -517,6 +510,12 @@]}]+ },+ {+ "group": "Revenue Recovery",+ "pages": [+ "v2/revenue-recovery/revenue-recovery--create"+ ]}]}diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.jsonindex a4e336f569..7bfd63e5e2 100644--- a/api-reference/v2/openapi_spec_v2.json+++ b/api-reference/v2/openapi_spec_v2.json@@ -4872,8 +4872,20 @@"Revenue Recovery"],"summary": "Revenue Recovery - Create",- "description": "Record a payment attempt made outside of Hyperswitch against a billing connector invoice,\nand take the requested recovery action on it, such as scheduling a retry for a failed payment\nor cancelling the invoice.",+ "description": "Record a transaction made outside of Hyperswitch against a billing connector invoice,\nand take the requested recovery action on it, such as scheduling a retry for a failed payment\nor recording an externally successful payment.","operationId": "Create a Revenue Recovery Payment",+ "parameters": [+ {+ "name": "X-Profile-Id",+ "in": "header",+ "description": "Unique identifier of the business profile under which the invoice is processed. The billing and payment merchant connector accounts in the request must belong to this profile.",+ "required": true,+ "schema": {+ "type": "string"+ },+ "example": "pro_abcdefghijklmnop"+ }+ ],"requestBody": {"content": {"application/json": {@@ -4881,7 +4893,7 @@"$ref": "#/components/schemas/RecoveryPaymentsCreate"},"examples": {- "Record a failed payment attempt and schedule a retry": {+ "Record a failed transaction and schedule a retry": {"value": {"action": "schedule_failed_payment","amount_details": {@@ -4889,16 +4901,25 @@"order_amount": 6540},"billing_merchant_connector_id": "mca_billing_1234567890",+ "billing_started_at": "2022-09-10T10:11:12Z","connector_customer_id": "cust_12345","connector_transaction_id": "993672945374576J","error": {"code": "card_declined",- "message": "The card was declined."+ "message": "The card was declined.",+ "network_advice_code": "01",+ "network_decline_code": "51",+ "network_error_message": "Retry after some time",+ "reason": "Insufficient funds"},"merchant_reference_id": "invoice_mbabizu24mvu3mela5njyh","payment_merchant_connector_id": "mca_payment_1234567890","payment_method_data": {"payment_method_metadata": {+ "card_exp_month": "01",+ "card_exp_year": "2026",+ "card_extended_bin": "42424242",+ "card_isin": "424242","card_network": "Visa","card_type": "credit","last4": "4242"@@ -4918,17 +4939,22 @@},"responses": {"200": {- "description": "Revenue Recovery Payment Recorded Successfully",+ "description": "Revenue Recovery Transaction Recorded Successfully","content": {"application/json": {"schema": {"$ref": "#/components/schemas/RecoveryPaymentsResponse"+ },+ "example": {+ "id": "12_pay_01a3868d94a471219b5c4bc71638e2zc",+ "intent_status": "failed",+ "merchant_reference_id": "invoice_mbabizu24mvu3mela5njyh"}}}},"400": {- "description": "Missing mandatory fields",+ "description": "Missing or invalid mandatory fields","content": {"application/json": {"schema": {@@ -31048,27 +31074,28 @@}}},- "RecoveryAction": {+ "RecoveryPaymentsAction": {"type": "string",- "description": "type of action that needs to taken after consuming recovery payload",+ "description": "Action to be taken on the transaction reported through the revenue recovery API","enum": [- "cancel_invoice","schedule_failed_payment",- "success_payment_external",- "pending_payment",- "no_action",- "invalid_action"+ "success_payment_external"]},"RecoveryPaymentsCreate": {"type": "object","required": ["amount_details",+ "merchant_reference_id","billing_merchant_connector_id","payment_merchant_connector_id","transaction_status","payment_method_sub_type",+ "transaction_created_at",+ "payment_method_type","connector_customer_id",+ "billing_started_at",+ "connector_transaction_id","payment_method_data","action"],@@ -31078,9 +31105,8 @@},"merchant_reference_id": {"type": "string",- "description": "The invoice identifier from the merchant's billing system that this payment attempt is\nbeing recorded against. This ensures idempotency when the same invoice is reported\nmore than once.",+ "description": "Unique identifier of the invoice in the merchant's billing system against which the\ntransaction is being recorded.","example": "invoice_mbabizu24mvu3mela5njyh",- "nullable": true,"maxLength": 30,"minLength": 30},@@ -31094,16 +31120,16 @@},"billing_merchant_connector_id": {"type": "string",- "description": "Billing connector id to update the invoices.",+ "description": "Unique identifier of the billing connector account, such as Chargebee, that manages the\ninvoice. The account must belong to the profile passed in the `X-Profile-Id` header.","example": "mca_1234567890"},"payment_merchant_connector_id": {"type": "string",- "description": "Payments connector id to update the invoices.",+ "description": "Unique identifier of the payment connector account, such as Stripe, on which the\ntransaction was processed. The account must belong to the profile passed in the\n`X-Profile-Id` header.","example": "mca_1234567890"},"transaction_status": {- "$ref": "#/components/schemas/AttemptStatus"+ "$ref": "#/components/schemas/RecoveryTransactionStatus"},"billing": {"allOf": [@@ -31119,41 +31145,33 @@"transaction_created_at": {"type": "string","format": "date-time",- "description": "The time at which payment attempt was created.",- "example": "2022-09-10T10:11:12Z",- "nullable": true+ "description": "The time at which the transaction was created at the payment connector.",+ "example": "2022-09-10T10:11:12Z"},"payment_method_type": {- "allOf": [- {- "$ref": "#/components/schemas/PaymentMethod"- }- ],- "nullable": true+ "$ref": "#/components/schemas/PaymentMethod"},"connector_customer_id": {"type": "string",- "description": "customer id at payment connector for which mandate is attached.",+ "description": "Unique identifier of the customer at the payment connector, with which the payment\nmethod token used for the transaction is associated.","example": "cust_12345"},"billing_started_at": {"type": "string","format": "date-time",- "description": "Invoice billing started at billing connector end.",- "example": "2022-09-10T10:11:12Z",- "nullable": true+ "description": "Invoice billing started at billing connector's end.",+ "example": "2022-09-10T10:11:12Z"},"connector_transaction_id": {"type": "string",- "description": "A unique identifier for a payment provided by the payment connector",- "example": "993672945374576J",- "nullable": true+ "description": "Unique identifier of the transaction at the payment connector. It is used to identify\nthe transaction among the attempts recorded for the invoice.",+ "example": "993672945374576J"},"payment_method_data": {"$ref": "#/components/schemas/CustomRecoveryPaymentMethodData"},"action": {- "$ref": "#/components/schemas/RecoveryAction"+ "$ref": "#/components/schemas/RecoveryPaymentsAction"},"enable_partial_authorization": {"type": "boolean",@@ -31178,7 +31196,7 @@"properties": {"id": {"type": "string",- "description": "Unique identifier for the payment.",+ "description": "Unique identifier of the invoice in Hyperswitch against which the transaction has been recorded.","example": "pay_mbabizu24mvu3mela5njyhpit4","maxLength": 30,"minLength": 30@@ -31193,14 +31211,24 @@},"merchant_reference_id": {"type": "string",- "description": "Unique identifier for the payment. This ensures idempotency for multiple payments\nthat have been done by a single merchant.",- "example": "pay_mbabizu24mvu3mela5njyhpit4",+ "description": "Unique identifier of the invoice in the merchant's billing system against which the\ntransaction is being recorded.",+ "example": "invoice_mbabizu24mvu3mela5njyh","nullable": true,"maxLength": 30,"minLength": 30}}},+ "RecoveryTransactionStatus": {+ "type": "string",+ "description": "Status of the transaction reported.",+ "enum": [+ "charged",+ "failure",+ "partial_charged",+ "partial_charged_and_chargeable"+ ]+ },"RecurringDetails": {"oneOf": [{diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex d01d506e41..c0ea3db493 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -2312,7 +2312,7 @@ pub struct PaymentAttemptRecordResponse {#[cfg(feature = "v2")]#[derive(Debug, serde::Serialize, Clone, ToSchema)]pub struct RecoveryPaymentsResponse {- /// Unique identifier for the payment.+ /// Unique identifier of the invoice in Hyperswitch against which the transaction has been recorded.#[schema(min_length = 30,max_length = 30,@@ -2321,16 +2321,17 @@ pub struct RecoveryPaymentsResponse {)]pub id: id_type::GlobalPaymentId,+ /// Current status of the payment after the transaction has been recorded.#[schema(value_type = IntentStatus, example = "failed", default = "requires_confirmation")]pub intent_status: api_enums::IntentStatus,- /// Unique identifier for the payment. This ensures idempotency for multiple payments- /// that have been done by a single merchant.+ /// Unique identifier of the invoice in the merchant's billing system against which the+ /// transaction is being recorded.#[schema(value_type = Option<String>,min_length = 30,max_length = 30,- example = "pay_mbabizu24mvu3mela5njyhpit4"+ example = "invoice_mbabizu24mvu3mela5njyh")]pub merchant_reference_id: Option<id_type::PaymentReferenceId>,}@@ -14697,11 +14698,10 @@ pub struct RecoveryPaymentsCreate {/// The amount details for the paymentpub amount_details: AmountDetails,- /// The invoice identifier from the merchant's billing system that this payment attempt is- /// being recorded against. This ensures idempotency when the same invoice is reported- /// more than once.+ /// Unique identifier of the invoice in the merchant's billing system against which the+ /// transaction is being recorded.#[schema(- value_type = Option<String>,+ value_type = String,min_length = 30,max_length = 30,example = "invoice_mbabizu24mvu3mela5njyh"@@ -14711,53 +14711,58 @@ pub struct RecoveryPaymentsCreate {/// Error details for the payment if anypub error: Option<ErrorDetails>,- /// Billing connector id to update the invoices.+ /// Unique identifier of the billing connector account, such as Chargebee, that manages the+ /// invoice. The account must belong to the profile passed in the `X-Profile-Id` header.#[schema(value_type = String, example = "mca_1234567890")]pub billing_merchant_connector_id: id_type::MerchantConnectorAccountId,- /// Payments connector id to update the invoices.+ /// Unique identifier of the payment connector account, such as Stripe, on which the+ /// transaction was processed. The account must belong to the profile passed in the+ /// `X-Profile-Id` header.#[schema(value_type = String, example = "mca_1234567890")]pub payment_merchant_connector_id: id_type::MerchantConnectorAccountId,- /// The status of the transaction at the payment connector.- #[schema(value_type = AttemptStatus, example = "charged")]- pub transaction_status: enums::AttemptStatus,+ /// Status of the transaction reported.+ #[schema(value_type = RecoveryTransactionStatus, example = "failure")]+ pub transaction_status: common_payments_types::RecoveryTransactionStatus,- /// The billing details of the payment attempt.+ /// The billing details of the transaction.pub billing: Option<Address>,- /// The payment method subtype to be used for the payment. This should match with the `payment_method_data` provided+ /// The payment method subtype used for the transaction.#[schema(value_type = PaymentMethodType, example = "apple_pay")]pub payment_method_sub_type: api_enums::PaymentMethodType,- /// The time at which payment attempt was created.+ /// The time at which the transaction was created at the payment connector.#[schema(example = "2022-09-10T10:11:12Z")]- #[serde(default, with = "common_utils::custom_serde::iso8601::option")]- pub transaction_created_at: Option<PrimitiveDateTime>,+ #[serde(with = "common_utils::custom_serde::iso8601")]+ pub transaction_created_at: PrimitiveDateTime,- /// Payment method type for the payment attempt- #[schema(value_type = Option<PaymentMethod>, example = "wallet")]+ /// Payment method type used for the transaction.+ #[schema(value_type = PaymentMethod, example = "wallet")]pub payment_method_type: common_enums::PaymentMethod,- /// customer id at payment connector for which mandate is attached.+ /// Unique identifier of the customer at the payment connector, with which the payment+ /// method token used for the transaction is associated.#[schema(value_type = String, example = "cust_12345")]pub connector_customer_id: Secret<String>,- /// Invoice billing started at billing connector end.+ /// Invoice billing started at billing connector's end.#[schema(example = "2022-09-10T10:11:12Z")]- #[serde(default, with = "common_utils::custom_serde::iso8601::option")]- pub billing_started_at: Option<PrimitiveDateTime>,+ #[serde(with = "common_utils::custom_serde::iso8601")]+ pub billing_started_at: PrimitiveDateTime,- /// A unique identifier for a payment provided by the payment connector- #[schema(value_type = Option<String>, example = "993672945374576J")]- pub connector_transaction_id: Option<Secret<String>>,+ /// Unique identifier of the transaction at the payment connector. It is used to identify+ /// the transaction among the attempts recorded for the invoice.+ #[schema(value_type = String, example = "993672945374576J")]+ pub connector_transaction_id: String,/// payment method token units at payment processor end.pub payment_method_data: CustomRecoveryPaymentMethodData,/// Type of action that needs to be taken after consuming the recovery payload. For example: scheduling a failed payment or stopping the invoice.- #[schema(value_type = RecoveryAction, example = "schedule_failed_payment")]- pub action: common_payments_types::RecoveryAction,+ #[schema(value_type = RecoveryPaymentsAction, example = "schedule_failed_payment")]+ pub action: common_payments_types::RecoveryPaymentsAction,/// Allow partial authorization for this payment#[schema(value_type = Option<bool>, default = false)]diff --git a/crates/common_types/src/payments.rs b/crates/common_types/src/payments.rsindex 1f7a97fecb..71c95c6d23 100644--- a/crates/common_types/src/payments.rs+++ b/crates/common_types/src/payments.rs@@ -1046,6 +1046,52 @@ pub enum RecoveryAction {InvalidAction,}+/// Action to be taken on the transaction reported through the revenue recovery API+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]+#[serde(rename_all = "snake_case")]+pub enum RecoveryPaymentsAction {+ /// Records the external transaction against payment intent and schedules a retry.+ ScheduleFailedPayment,+ /// Records the external payment and stops the internal process tracker.+ SuccessPaymentExternal,+}++impl From<RecoveryPaymentsAction> for RecoveryAction {+ fn from(action: RecoveryPaymentsAction) -> Self {+ match action {+ RecoveryPaymentsAction::ScheduleFailedPayment => Self::ScheduleFailedPayment,+ RecoveryPaymentsAction::SuccessPaymentExternal => Self::SuccessPaymentExternal,+ }+ }+}++/// Status of the transaction reported.+#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]+#[serde(rename_all = "snake_case")]+pub enum RecoveryTransactionStatus {+ /// The transaction was charged in full.+ Charged,+ /// The transaction failed.+ Failure,+ /// The transaction was charged for a part of the amount.+ PartialCharged,+ /// The transaction was charged for a part of the amount and the remaining amount can still be captured.+ PartialChargedAndChargeable,+}++impl From<RecoveryTransactionStatus> for common_enums::AttemptStatus {+ fn from(status: RecoveryTransactionStatus) -> Self {+ match status {+ RecoveryTransactionStatus::Charged => Self::Charged,+ RecoveryTransactionStatus::Failure => Self::Failure,+ RecoveryTransactionStatus::PartialCharged => Self::PartialCharged,+ RecoveryTransactionStatus::PartialChargedAndChargeable => {+ Self::PartialChargedAndChargeable+ }+ }+ }+}+/// Billing Descriptor information to be sent to the payment gateway#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, AsExpression, FromSqlRow, ToSchema,diff --git a/crates/openapi/src/openapi_v2.rs b/crates/openapi/src/openapi_v2.rsindex b3071521ad..c403e763e8 100644--- a/crates/openapi/src/openapi_v2.rs+++ b/crates/openapi/src/openapi_v2.rs@@ -1008,7 +1008,8 @@ Never share your secret api keys. Keep them guarded and secure.api_models::payments::RecoveryPaymentsResponse,api_models::payments::CustomRecoveryPaymentMethodData,api_models::payments::AdditionalCardInfo,- common_types::payments::RecoveryAction,+ common_types::payments::RecoveryPaymentsAction,+ common_types::payments::RecoveryTransactionStatus,api_models::enums::RevenueRecoveryAlgorithmType,api_models::enums::RevenueRecoveryABAlgorithm,api_models::enums::ProcessTrackerStatus,diff --git a/crates/openapi/src/routes/revenue_recovery.rs b/crates/openapi/src/routes/revenue_recovery.rsindex 617e13b296..f4327d1f50 100644--- a/crates/openapi/src/routes/revenue_recovery.rs+++ b/crates/openapi/src/routes/revenue_recovery.rs@@ -24,17 +24,24 @@ pub async fn revenue_recovery_pt_retrieve_api() {}#[cfg(feature = "v2")]/// Revenue Recovery - Create///-/// Record a payment attempt made outside of Hyperswitch against a billing connector invoice,+/// Record a transaction made outside of Hyperswitch against a billing connector invoice,/// and take the requested recovery action on it, such as scheduling a retry for a failed payment-/// or cancelling the invoice.+/// or recording an externally successful payment.#[utoipa::path(post,path = "/v2/payments/recovery",+ params(+ (+ "X-Profile-Id" = String, Header,+ description = "Unique identifier of the business profile under which the invoice is processed. The billing and payment merchant connector accounts in the request must belong to this profile.",+ example = "pro_abcdefghijklmnop"+ ),+ ),request_body(content = RecoveryPaymentsCreate,examples((- "Record a failed payment attempt and schedule a retry" = (+ "Record a failed transaction and schedule a retry" = (value = json!({"amount_details": {"order_amount": 6540,@@ -49,16 +56,25 @@ pub async fn revenue_recovery_pt_retrieve_api() {}"connector_customer_id": "cust_12345","connector_transaction_id": "993672945374576J","transaction_created_at": "2022-09-10T10:11:12Z",+ "billing_started_at": "2022-09-10T10:11:12Z","error": {"code": "card_declined",- "message": "The card was declined."+ "message": "The card was declined.",+ "reason": "Insufficient funds",+ "network_advice_code": "01",+ "network_decline_code": "51",+ "network_error_message": "Retry after some time"},"payment_method_data": {"primary_processor_payment_method_token": "token_1234","payment_method_metadata": {"card_network": "Visa","card_type": "credit",- "last4": "4242"+ "last4": "4242",+ "card_isin": "424242",+ "card_extended_bin": "42424242",+ "card_exp_month": "01",+ "card_exp_year": "2026"}},"action": "schedule_failed_payment"@@ -68,8 +84,17 @@ pub async fn revenue_recovery_pt_retrieve_api() {}),),responses(- (status = 200, description = "Revenue Recovery Payment Recorded Successfully", body = RecoveryPaymentsResponse),- (status = 400, description = "Missing mandatory fields", body = GenericErrorResponseOpenApi),+ (+ status = 200,+ description = "Revenue Recovery Transaction Recorded Successfully",+ body = RecoveryPaymentsResponse,+ example = json!({+ "id": "12_pay_01a3868d94a471219b5c4bc71638e2zc",+ "intent_status": "failed",+ "merchant_reference_id": "invoice_mbabizu24mvu3mela5njyh"+ })+ ),+ (status = 400, description = "Missing or invalid mandatory fields", body = GenericErrorResponseOpenApi),(status = 404, description = "Resource missing"),(status = 500, description = "Internal server error"),),diff --git a/crates/router/src/core/revenue_recovery/api.rs b/crates/router/src/core/revenue_recovery/api.rsindex aa44e47735..b8c1bc5559 100644--- a/crates/router/src/core/revenue_recovery/api.rs+++ b/crates/router/src/core/revenue_recovery/api.rs@@ -360,7 +360,7 @@ pub async fn custom_revenue_recovery_core(router_env::logger::info!("Intent retry count: {:?}", intent_retry_count);let recovery_action = recovery_incoming::RecoveryAction {- action: request.action.to_owned(),+ action: request.action.to_owned().into(),};let mca_retry_threshold = billing_connector_account.get_retry_threshold()diff --git a/crates/router/src/core/revenue_recovery/transformers.rs b/crates/router/src/core/revenue_recovery/transformers.rsindex 34ebd1095c..0debf7b0e7 100644--- a/crates/router/src/core/revenue_recovery/transformers.rs+++ b/crates/router/src/core/revenue_recovery/transformers.rs@@ -55,7 +55,7 @@ impl ForeignFrom<api_models::payments::RecoveryPaymentsCreate>billing_address: data.billing,retry_count: None,next_billing_at: None,- billing_started_at: data.billing_started_at,+ billing_started_at: Some(data.billing_started_at),metadata: data.metadata,enable_partial_authorization: data.enable_partial_authorization,}@@ -70,9 +70,9 @@ impl ForeignFrom<&api_models::payments::RecoveryPaymentsCreate>amount: data.amount_details.order_amount().into(),currency: data.amount_details.currency(),merchant_reference_id: data.merchant_reference_id.to_owned(),- connector_transaction_id: data.connector_transaction_id.as_ref().map(|txn_id| {- common_utils::types::ConnectorTransactionId::TxnId(txn_id.peek().to_string())- }),+ connector_transaction_id: Some(common_utils::types::ConnectorTransactionId::TxnId(+ data.connector_transaction_id.to_owned(),+ )),error_code: data.error.as_ref().map(|error| error.code.clone()),error_message: data.error.as_ref().map(|error| error.message.clone()),processor_payment_method_token: data@@ -85,8 +85,8 @@ impl ForeignFrom<&api_models::payments::RecoveryPaymentsCreate>.payment_merchant_connector_id.get_string_repr().to_string(),- transaction_created_at: data.transaction_created_at.to_owned(),- status: data.transaction_status,+ transaction_created_at: Some(data.transaction_created_at),+ status: data.transaction_status.into(),payment_method_type: data.payment_method_type,payment_method_sub_type: data.payment_method_sub_type,network_advice_code: data@@ -104,7 +104,7 @@ impl ForeignFrom<&api_models::payments::RecoveryPaymentsCreate>// retry count will be updated whenever there is new attempt is created.retry_count: None,invoice_next_billing_time: None,- invoice_billing_started_at_time: data.billing_started_at,+ invoice_billing_started_at_time: Some(data.billing_started_at),card_info: data.payment_method_data.payment_method_metadata.clone(),charge_id: None,}