{ "commit_sha": "2e7e89dc88a55f622c3338c253c5e53238f6b530", "pr_number": 10074, "rust_files": [ "crates/router/src/core/webhooks/incoming.rs" ], "diffs": [ { "id": "crates/router/src/core/webhooks/incoming.rs::function::update_connector_mandate_details", "file": "crates/router/src/core/webhooks/incoming.rs", "kind": "function_item", "status": "modified", "code_changed": true, "imports_changed": false, "before_code": "async fn update_connector_mandate_details(\n state: &SessionState,\n merchant_context: &domain::MerchantContext,\n object_ref_id: api::ObjectReferenceId,\n connector: &ConnectorEnum,\n request_details: &IncomingWebhookRequestDetails<'_>,\n) -> CustomResult<(), errors::ApiErrorResponse> {\n let webhook_connector_mandate_details = connector\n .get_mandate_details(request_details)\n .switch()\n .attach_printable(\"Could not find connector mandate details in incoming webhook body\")?;\n\n let webhook_connector_network_transaction_id = connector\n .get_network_txn_id(request_details)\n .switch()\n .attach_printable(\n \"Could not find connector network transaction id in incoming webhook body\",\n )?;\n\n // Either one OR both of the fields are present\n if webhook_connector_mandate_details.is_some()\n || webhook_connector_network_transaction_id.is_some()\n {\n let payment_attempt =\n get_payment_attempt_from_object_reference_id(state, object_ref_id, merchant_context)\n .await?;\n if let Some(ref payment_method_id) = payment_attempt.payment_method_id {\n let key_manager_state = &state.into();\n let payment_method_info = state\n .store\n .find_payment_method(\n key_manager_state,\n merchant_context.get_merchant_key_store(),\n payment_method_id,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;\n\n // Update connector's mandate details\n let updated_connector_mandate_details =\n if let Some(webhook_mandate_details) = webhook_connector_mandate_details {\n let mandate_details = payment_method_info\n .get_common_mandate_reference()\n .change_context(errors::ApiErrorResponse::InternalServerError)\n .attach_printable(\"Failed to deserialize to Payment Mandate Reference\")?;\n\n let merchant_connector_account_id = payment_attempt\n .merchant_connector_id\n .clone()\n .get_required_value(\"merchant_connector_id\")?;\n\n if mandate_details.payments.as_ref().is_none_or(|payments| {\n !payments.0.contains_key(&merchant_connector_account_id)\n }) {\n // Update the payment attempt to maintain consistency across tables.\n let (mandate_metadata, connector_mandate_request_reference_id) =\n payment_attempt\n .connector_mandate_detail\n .as_ref()\n .map(|details| {\n (\n details.mandate_metadata.clone(),\n details.connector_mandate_request_reference_id.clone(),\n )\n })\n .unwrap_or((None, None));\n\n let connector_mandate_reference_id = ConnectorMandateReferenceId {\n connector_mandate_id: Some(\n webhook_mandate_details\n .connector_mandate_id\n .peek()\n .to_string(),\n ),\n payment_method_id: Some(payment_method_id.to_string()),\n mandate_metadata,\n connector_mandate_request_reference_id,\n };\n\n let attempt_update =\n storage::PaymentAttemptUpdate::ConnectorMandateDetailUpdate {\n connector_mandate_detail: Some(connector_mandate_reference_id),\n tokenization: None, // We need to handle tokenization field update in webhooks as well\n updated_by: merchant_context\n .get_merchant_account()\n .storage_scheme\n .to_string(),\n };\n\n state\n .store\n .update_payment_attempt_with_attempt_id(\n payment_attempt.clone(),\n attempt_update,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;\n\n insert_mandate_details(\n &payment_attempt,\n &webhook_mandate_details,\n Some(mandate_details),\n )?\n } else {\n logger::info!(\n \"Skipping connector mandate details update since they are already present.\"\n );\n None\n }\n } else {\n None\n };\n\n let connector_mandate_details_value = updated_connector_mandate_details\n .map(|common_mandate| {\n common_mandate.get_mandate_details_value().map_err(|err| {\n router_env::logger::error!(\n \"Failed to get get_mandate_details_value : {:?}\",\n err\n );\n errors::ApiErrorResponse::MandateUpdateFailed\n })\n })\n .transpose()?;\n\n let pm_update = diesel_models::PaymentMethodUpdate::ConnectorNetworkTransactionIdAndMandateDetailsUpdate {\n connector_mandate_details: connector_mandate_details_value.map(masking::Secret::new),\n network_transaction_id: webhook_connector_network_transaction_id\n .map(|webhook_network_transaction_id| webhook_network_transaction_id.get_id().clone()),\n };\n\n state\n .store\n .update_payment_method(\n key_manager_state,\n merchant_context.get_merchant_key_store(),\n payment_method_info,\n pm_update,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .change_context(errors::ApiErrorResponse::InternalServerError)\n .attach_printable(\"Failed to update payment method in db\")?;\n }\n }\n Ok(())\n}", "after_code": "async fn update_connector_mandate_details(\n state: &SessionState,\n merchant_context: &domain::MerchantContext,\n object_ref_id: api::ObjectReferenceId,\n connector: &ConnectorEnum,\n request_details: &IncomingWebhookRequestDetails<'_>,\n) -> CustomResult<(), errors::ApiErrorResponse> {\n let webhook_connector_mandate_details = connector\n .get_mandate_details(request_details)\n .switch()\n .attach_printable(\"Could not find connector mandate details in incoming webhook body\")?;\n\n let webhook_connector_network_transaction_id = connector\n .get_network_txn_id(request_details)\n .switch()\n .attach_printable(\n \"Could not find connector network transaction id in incoming webhook body\",\n )?;\n\n // Either one OR both of the fields are present\n if webhook_connector_mandate_details.is_some()\n || webhook_connector_network_transaction_id.is_some()\n {\n let payment_attempt =\n get_payment_attempt_from_object_reference_id(state, object_ref_id, merchant_context)\n .await?;\n if let Some(ref payment_method_id) = payment_attempt.payment_method_id {\n let key_manager_state = &state.into();\n let payment_method_info = state\n .store\n .find_payment_method(\n key_manager_state,\n merchant_context.get_merchant_key_store(),\n payment_method_id,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;\n\n // Update connector's mandate details\n let updated_connector_mandate_details =\n if let Some(webhook_mandate_details) = webhook_connector_mandate_details {\n let mandate_details = payment_method_info\n .get_common_mandate_reference()\n .change_context(errors::ApiErrorResponse::InternalServerError)\n .attach_printable(\"Failed to deserialize to Payment Mandate Reference\")?;\n\n let merchant_connector_account_id = payment_attempt\n .merchant_connector_id\n .clone()\n .get_required_value(\"merchant_connector_id\")?;\n\n if mandate_details.payments.as_ref().is_none_or(|payments| {\n !payments.0.contains_key(&merchant_connector_account_id)\n }) {\n // Update the payment attempt to maintain consistency across tables.\n let (mandate_metadata, connector_mandate_request_reference_id) =\n payment_attempt\n .connector_mandate_detail\n .as_ref()\n .map(|details| {\n (\n details.mandate_metadata.clone(),\n details.connector_mandate_request_reference_id.clone(),\n )\n })\n .unwrap_or((None, None));\n\n let connector_mandate_reference_id = ConnectorMandateReferenceId {\n connector_mandate_id: Some(\n webhook_mandate_details\n .connector_mandate_id\n .peek()\n .to_string(),\n ),\n payment_method_id: Some(payment_method_id.to_string()),\n mandate_metadata,\n connector_mandate_request_reference_id,\n };\n\n let attempt_update =\n storage::PaymentAttemptUpdate::ConnectorMandateDetailUpdate {\n connector_mandate_detail: Some(connector_mandate_reference_id),\n tokenization: None, // We need to handle tokenization field update in webhooks as well\n updated_by: merchant_context\n .get_merchant_account()\n .storage_scheme\n .to_string(),\n };\n\n state\n .store\n .update_payment_attempt_with_attempt_id(\n payment_attempt.clone(),\n attempt_update,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;\n\n insert_mandate_details(\n &payment_attempt,\n &webhook_mandate_details,\n Some(mandate_details),\n )?\n } else {\n logger::info!(\n \"Skipping connector mandate details update since they are already present.\"\n );\n None\n }\n } else {\n None\n };\n\n let connector_mandate_details_value = updated_connector_mandate_details\n .map(|common_mandate| {\n common_mandate.get_mandate_details_value().map_err(|err| {\n router_env::logger::error!(\n \"Failed to get get_mandate_details_value : {:?}\",\n err\n );\n errors::ApiErrorResponse::MandateUpdateFailed\n })\n })\n .transpose()?;\n\n let pm_update = diesel_models::PaymentMethodUpdate::ConnectorNetworkTransactionIdAndMandateDetailsUpdate {\n connector_mandate_details: connector_mandate_details_value.map(masking::Secret::new),\n network_transaction_id: webhook_connector_network_transaction_id\n .map(|webhook_network_transaction_id| webhook_network_transaction_id.get_id().clone()),\n };\n\n state\n .store\n .update_payment_method(\n key_manager_state,\n merchant_context.get_merchant_key_store(),\n payment_method_info,\n pm_update,\n merchant_context.get_merchant_account().storage_scheme,\n )\n .await\n .change_context(errors::ApiErrorResponse::InternalServerError)\n .attach_printable(\"Failed to update payment method in db\")?;\n }\n }\n Ok(())\n}", "diff_span": { "before": " storage::PaymentAttemptUpdate::ConnectorMandateDetailUpdate {\n connector_mandate_detail: Some(connector_mandate_reference_id),\n tokenization: None, // We need to handle tokenization field update in webhooks as well\n updated_by: merchant_context\n .get_merchant_account()", "after": " storage::PaymentAttemptUpdate::ConnectorMandateDetailUpdate {\n connector_mandate_detail: Some(connector_mandate_reference_id),\n tokenization: None, // We need to handle tokenization field update in webhooks as well\n updated_by: merchant_context\n .get_merchant_account()" }, "commit_sha": "2e7e89dc88a55f622c3338c253c5e53238f6b530" } ] }