Skip to content

Commit

Permalink
refactor(payment_methods): update connector_mandate_details for car…
Browse files Browse the repository at this point in the history
…d metadata changes (#6848)
  • Loading branch information
Aprabhat19 authored Dec 27, 2024
1 parent 906d754 commit d19c1a1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthor
payment_method_billing_address,
business_profile,
connector_mandate_reference_id.clone(),
merchant_connector_id.clone(),
));

let is_connector_mandate = resp.request.customer_acceptance.is_some()
Expand Down Expand Up @@ -315,6 +316,7 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthor
payment_method_billing_address.as_ref(),
&business_profile,
connector_mandate_reference_id,
merchant_connector_id.clone(),
))
.await;

Expand Down Expand Up @@ -1099,6 +1101,7 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::SetupMandateRequestDa
payment_method_billing_address,
business_profile,
connector_mandate_reference_id,
merchant_connector_id.clone(),
))
.await?;

Expand Down
71 changes: 70 additions & 1 deletion crates/router/src/core/payments/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub async fn save_payment_method<FData>(
payment_method_billing_address: Option<&hyperswitch_domain_models::address::Address>,
business_profile: &domain::Profile,
mut original_connector_mandate_reference_id: Option<ConnectorMandateReferenceId>,
merchant_connector_id: Option<id_type::MerchantConnectorAccountId>,
) -> RouterResult<SavePaymentMethodDataResponse>
where
FData: mandate::MandateBehaviour + Clone,
Expand Down Expand Up @@ -458,7 +459,41 @@ where
resp.payment_method_id = payment_method_id;

let existing_pm = match payment_method {
Ok(pm) => Ok(pm),
Ok(pm) => {
let mandate_details = pm
.connector_mandate_details
.clone()
.map(|val| {
val.parse_value::<diesel_models::PaymentsMandateReference>(
"PaymentsMandateReference",
)
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to deserialize to Payment Mandate Reference ")?;
if let Some((mandate_details, merchant_connector_id)) =
mandate_details.zip(merchant_connector_id)
{
let connector_mandate_details =
update_connector_mandate_details_status(
merchant_connector_id,
mandate_details,
ConnectorMandateStatus::Inactive,
)?;
payment_methods::cards::update_payment_method_connector_mandate_details(
state,
key_store,
db,
pm.clone(),
connector_mandate_details,
merchant_account.storage_scheme,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to add payment method in db")?;
}
Ok(pm)
}
Err(err) => {
if err.current_context().is_db_not_found() {
payment_methods::cards::create_payment_method(
Expand Down Expand Up @@ -1260,3 +1295,37 @@ pub fn update_connector_mandate_details(

Ok(connector_mandate_details)
}

#[cfg(feature = "v1")]
pub fn update_connector_mandate_details_status(
merchant_connector_id: id_type::MerchantConnectorAccountId,
mut payment_mandate_reference: diesel_models::PaymentsMandateReference,
status: ConnectorMandateStatus,
) -> RouterResult<Option<serde_json::Value>> {
let mandate_reference = {
payment_mandate_reference
.entry(merchant_connector_id)
.and_modify(|pm| {
let update_rec = diesel_models::PaymentsMandateReferenceRecord {
connector_mandate_id: pm.connector_mandate_id.clone(),
payment_method_type: pm.payment_method_type,
original_payment_authorized_amount: pm.original_payment_authorized_amount,
original_payment_authorized_currency: pm.original_payment_authorized_currency,
mandate_metadata: pm.mandate_metadata.clone(),
connector_mandate_status: Some(status),
connector_mandate_request_reference_id: pm
.connector_mandate_request_reference_id
.clone(),
};
*pm = update_rec
});
Some(payment_mandate_reference)
};
let connector_mandate_details = mandate_reference
.map(|mandate| mandate.encode_to_value())
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to serialize customer acceptance to value")?;

Ok(connector_mandate_details)
}

0 comments on commit d19c1a1

Please sign in to comment.