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: certificate decode failed when creating the session token for applepay (#1385)
Narayan Bhat8497c55283d5
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex c53acb6d85..7df67b13a5 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -1711,6 +1711,12 @@ pub struct ApplepaySessionRequest {pub initiative_context: String,}+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]+pub struct ConnectorMetadata {+ pub apple_pay: Option<ApplePayMetadata>,+ pub google_pay: Option<GpayMetaData>,+}+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]pub struct ApplepaySessionTokenData {#[serde(rename = "apple_pay")]diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rsindex 72f2e9eaa9..c36ba7c734 100644--- a/crates/router/src/core/admin.rs+++ b/crates/router/src/core/admin.rs@@ -380,6 +380,36 @@ fn get_business_details_wrapper(}}+fn validate_certificate_in_mca_metadata(+ connector_metadata: Secret<serde_json::Value>,+) -> RouterResult<()> {+ let parsed_connector_metadata = connector_metadata+ .parse_value::<api_models::payments::ConnectorMetadata>("ApplepaySessionTokenData")+ .change_context(errors::ParsingError::StructParseFailure("Metadata"))+ .change_context(errors::ApiErrorResponse::InvalidDataFormat {+ field_name: "metadata".to_string(),+ expected_format: "connector metadata".to_string(),+ })?;++ parsed_connector_metadata+ .apple_pay+ .map(|applepay_metadata| {+ let api_models::payments::SessionTokenInfo {+ certificate,+ certificate_keys,+ ..+ } = applepay_metadata.session_token_data;+ helpers::create_identity_from_certificate_and_key(certificate, certificate_keys)+ .change_context(errors::ApiErrorResponse::InvalidDataValue {+ field_name: "certificate/certificate key",+ })+ .map(|_identity_result| ())+ })+ .transpose()?;++ Ok(())+}+pub async fn create_payment_connector(store: &dyn StorageInterface,req: api::MerchantConnectorCreate,@@ -390,6 +420,11 @@ pub async fn create_payment_connector(.change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Unable to get key from merchant key store")?;+ req.metadata+ .clone()+ .map(validate_certificate_in_mca_metadata)+ .transpose()?;+let merchant_account = store.find_merchant_account_by_merchant_id(merchant_id).awaitdiff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rsindex 3147ef8026..3723799e23 100644--- a/crates/router/src/core/payments/helpers.rs+++ b/crates/router/src/core/payments/helpers.rs@@ -1,5 +1,6 @@use std::borrow::Cow;+use base64::Engine;use common_utils::{ext_traits::{AsyncExt, ByteSliceExt, ValueExt},fp_utils, generate_id, pii,@@ -46,6 +47,33 @@ use crate::{},};+pub fn create_identity_from_certificate_and_key(+ encoded_certificate: String,+ encoded_certificate_key: String,+) -> Result<reqwest::Identity, error_stack::Report<errors::ApiClientError>> {+ let decoded_certificate = consts::BASE64_ENGINE+ .decode(encoded_certificate)+ .into_report()+ .change_context(errors::ApiClientError::CertificateDecodeFailed)?;++ let decoded_certificate_key = consts::BASE64_ENGINE+ .decode(encoded_certificate_key)+ .into_report()+ .change_context(errors::ApiClientError::CertificateDecodeFailed)?;++ let certificate = String::from_utf8(decoded_certificate)+ .into_report()+ .change_context(errors::ApiClientError::CertificateDecodeFailed)?;++ let certificate_key = String::from_utf8(decoded_certificate_key)+ .into_report()+ .change_context(errors::ApiClientError::CertificateDecodeFailed)?;++ reqwest::Identity::from_pkcs8_pem(certificate.as_bytes(), certificate_key.as_bytes())+ .into_report()+ .change_context(errors::ApiClientError::CertificateDecodeFailed)+}+pub fn filter_mca_based_on_business_details(merchant_connector_accounts: Vec<domain::MerchantConnectorAccount>,payment_intent: Option<&storage_models::payment_intent::PaymentIntent>,diff --git a/crates/router/src/services/api/client.rs b/crates/router/src/services/api/client.rsindex 3d6ba2afcb..d37f22f750 100644--- a/crates/router/src/services/api/client.rs+++ b/crates/router/src/services/api/client.rs@@ -1,11 +1,12 @@-use base64::Engine;use error_stack::{IntoReport, ResultExt};use once_cell::sync::OnceCell;use crate::{configs::settings::{Locker, Proxy},- consts,- core::errors::{self, CustomResult},+ core::{+ errors::{self, CustomResult},+ payments,+ },};static NON_PROXIED_CLIENT: OnceCell<reqwest::Client> = OnceCell::new();@@ -74,30 +75,13 @@ pub(super) fn create_client(client_certificate_key: Option<String>,) -> CustomResult<reqwest::Client, errors::ApiClientError> {match (client_certificate, client_certificate_key) {- (Some(encoded_cert), Some(encoded_cert_key)) => {+ (Some(encoded_certificate), Some(encoded_certificate_key)) => {let client_builder = get_client_builder(proxy_config, should_bypass_proxy)?;- let decoded_cert = consts::BASE64_ENGINE- .decode(encoded_cert)- .into_report()- .change_context(errors::ApiClientError::CertificateDecodeFailed)?;- let decoded_cert_key = consts::BASE64_ENGINE- .decode(encoded_cert_key)- .into_report()- .change_context(errors::ApiClientError::CertificateDecodeFailed)?;-- let certificate = String::from_utf8(decoded_cert)- .into_report()- .change_context(errors::ApiClientError::CertificateDecodeFailed)?;- let certificate_key = String::from_utf8(decoded_cert_key)- .into_report()- .change_context(errors::ApiClientError::CertificateDecodeFailed)?;- let identity = reqwest::Identity::from_pkcs8_pem(- certificate.as_bytes(),- certificate_key.as_bytes(),- )- .into_report()- .change_context(errors::ApiClientError::CertificateDecodeFailed)?;+ let identity = payments::helpers::create_identity_from_certificate_and_key(+ encoded_certificate,+ encoded_certificate_key,+ )?;client_builder.identity(identity)