Skip to content

Commit

Permalink
feat(router): separate straight through algorithm in separate column …
Browse files Browse the repository at this point in the history
…in payment attempt (#863)
  • Loading branch information
vspecky authored Apr 12, 2023
1 parent 1b94d25 commit 01f86c4
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 121 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 22 additions & 31 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ pub async fn list_payments(
) -> RouterResponse<api::PaymentListResponse> {
use futures::stream::StreamExt;

use crate::types::transformers::ForeignTryFrom;
use crate::types::transformers::ForeignFrom;

helpers::validate_payment_list_request(&constraints)?;
let merchant_id = &merchant.merchant_id;
Expand Down Expand Up @@ -859,11 +859,7 @@ pub async fn list_payments(
.collect::<Vec<(storage::PaymentIntent, storage::PaymentAttempt)>>()
.await;

let data: Vec<api::PaymentsResponse> = pi
.into_iter()
.map(ForeignTryFrom::foreign_try_from)
.collect::<Result<_, _>>()
.change_context(errors::ApiErrorResponse::InternalServerError)?;
let data: Vec<api::PaymentsResponse> = pi.into_iter().map(ForeignFrom::foreign_from).collect();

Ok(services::ApplicationResponse::Json(
api::PaymentListResponse {
Expand Down Expand Up @@ -912,24 +908,12 @@ pub fn update_straight_through_routing<F>(
where
F: Send + Clone,
{
let mut routing_data: storage::RoutingData = payment_data
.payment_attempt
.connector
let _: api::RoutingAlgorithm = request_straight_through
.clone()
.unwrap_or_else(|| serde_json::json!({}))
.parse_value("RoutingData")
.attach_printable("Invalid routing data format in payment attempt")?;

let request_straight_through: api::RoutingAlgorithm = request_straight_through
.parse_value("RoutingAlgorithm")
.attach_printable("Invalid straight through routing rules format")?;

routing_data.algorithm = Some(request_straight_through);

let encoded_routing_data = Encode::<storage::RoutingData>::encode_to_value(&routing_data)
.attach_printable("Unable to serialize routing data to serde value")?;

payment_data.payment_attempt.connector = Some(encoded_routing_data);
payment_data.payment_attempt.straight_through_algorithm = Some(request_straight_through);

Ok(())
}
Expand Down Expand Up @@ -987,14 +971,17 @@ pub fn connector_selection<F>(
where
F: Send + Clone,
{
let mut routing_data: storage::RoutingData = payment_data
.payment_attempt
.connector
.clone()
.unwrap_or_else(|| serde_json::json!({}))
.parse_value("RoutingData")
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Invalid routing data format in payment attempt")?;
let mut routing_data = storage::RoutingData {
routed_through: payment_data.payment_attempt.connector.clone(),
algorithm: payment_data
.payment_attempt
.straight_through_algorithm
.clone()
.map(|val| val.parse_value("RoutingAlgorithm"))
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Invalid straight through algorithm format in payment attempt")?,
};

let request_straight_through: Option<api::RoutingAlgorithm> = request_straight_through
.map(|val| val.parse_value("RoutingAlgorithm"))
Expand All @@ -1009,11 +996,15 @@ where
&mut routing_data,
)?;

let encoded_routing_data = Encode::<storage::RoutingData>::encode_to_value(&routing_data)
let encoded_algorithm = routing_data
.algorithm
.map(|algo| Encode::<api::RoutingAlgorithm>::encode_to_value(&algo))
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to serialize routing data to serde value")?;
.attach_printable("Unable to serialize routing algorithm to serde value")?;

payment_data.payment_attempt.connector = Some(encoded_routing_data);
payment_data.payment_attempt.connector = routing_data.routed_through;
payment_data.payment_attempt.straight_through_algorithm = encoded_algorithm;

Ok(decided_connector)
}
Expand Down
7 changes: 1 addition & 6 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,9 @@ where
Op: std::fmt::Debug,
{
if check_if_operation_confirm(operation) {
let routed_through: storage::RoutedThroughData = payment_attempt
let connector_name = payment_attempt
.connector
.clone()
.parse_value("RoutedThroughData")
.change_context(errors::ApiErrorResponse::InternalServerError)?;

let connector_name = routed_through
.routed_through
.ok_or(errors::ApiErrorResponse::InternalServerError)?;

let schedule_time = payment_sync::get_sync_process_schedule_time(
Expand Down
5 changes: 5 additions & 0 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen
};

let connector = payment_data.payment_attempt.connector.clone();
let straight_through_algorithm = payment_data
.payment_attempt
.straight_through_algorithm
.clone();
let payment_token = payment_data.token.clone();
let payment_method_type = payment_data.payment_attempt.payment_method_type.clone();
let payment_experience = payment_data.payment_attempt.payment_experience.clone();
Expand Down Expand Up @@ -362,6 +366,7 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen
payment_method_type,
payment_experience,
business_sub_label,
straight_through_algorithm,
},
storage_scheme,
)
Expand Down
17 changes: 10 additions & 7 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
storage::{
self,
enums::{self, IntentStatus},
PaymentAttemptExt,
},
transformers::ForeignInto,
},
Expand Down Expand Up @@ -139,8 +138,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
})?;
connector_response = db
.insert_connector_response(
Self::make_connector_response(&payment_attempt)
.change_context(errors::ApiErrorResponse::InternalServerError)?,
Self::make_connector_response(&payment_attempt),
storage_scheme,
)
.await
Expand Down Expand Up @@ -316,13 +314,18 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen

let payment_token = payment_data.token.clone();
let connector = payment_data.payment_attempt.connector.clone();
let straight_through_algorithm = payment_data
.payment_attempt
.straight_through_algorithm
.clone();

payment_data.payment_attempt = db
.update_payment_attempt_with_attempt_id(
payment_data.payment_attempt,
storage::PaymentAttemptUpdate::UpdateTrackers {
payment_token,
connector,
straight_through_algorithm,
},
storage_scheme,
)
Expand Down Expand Up @@ -532,18 +535,18 @@ impl PaymentCreate {
#[instrument(skip_all)]
pub fn make_connector_response(
payment_attempt: &storage::PaymentAttempt,
) -> CustomResult<storage::ConnectorResponseNew, errors::ParsingError> {
Ok(storage::ConnectorResponseNew {
) -> storage::ConnectorResponseNew {
storage::ConnectorResponseNew {
payment_id: payment_attempt.payment_id.clone(),
merchant_id: payment_attempt.merchant_id.clone(),
attempt_id: payment_attempt.attempt_id.clone(),
created_at: payment_attempt.created_at,
modified_at: payment_attempt.modified_at,
connector_name: payment_attempt.get_routed_through_connector()?,
connector_name: payment_attempt.connector.clone(),
connector_transaction_id: None,
authentication_data: None,
encoded_data: None,
})
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::VerifyRequest> for Paym

connector_response = match db
.insert_connector_response(
PaymentCreate::make_connector_response(&payment_attempt)
.change_context(errors::ApiErrorResponse::InternalServerError)?,
PaymentCreate::make_connector_response(&payment_attempt),
storage_scheme,
)
.await
Expand Down
22 changes: 8 additions & 14 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
services::{self, RedirectForm},
types::{
self, api,
storage::{self, enums, PaymentAttemptExt},
transformers::{ForeignInto, ForeignTryFrom},
storage::{self, enums},
transformers::{ForeignFrom, ForeignInto},
},
utils::{OptionExt, ValueExt},
};
Expand Down Expand Up @@ -278,9 +278,7 @@ where
})
}
let mut response: api::PaymentsResponse = Default::default();
let routed_through = payment_attempt
.get_routed_through_connector()
.change_context(errors::ApiErrorResponse::InternalServerError)?;
let routed_through = payment_attempt.connector.clone();

let connector_label = routed_through.as_ref().map(|connector_name| {
helpers::get_connector_label(
Expand Down Expand Up @@ -417,15 +415,11 @@ where
})
}

impl ForeignTryFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::PaymentsResponse {
type Error = error_stack::Report<errors::ParsingError>;

fn foreign_try_from(
item: (storage::PaymentIntent, storage::PaymentAttempt),
) -> Result<Self, Self::Error> {
impl ForeignFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::PaymentsResponse {
fn foreign_from(item: (storage::PaymentIntent, storage::PaymentAttempt)) -> Self {
let pi = item.0;
let pa = item.1;
Ok(Self {
Self {
payment_id: Some(pi.payment_id),
merchant_id: Some(pi.merchant_id),
status: pi.status.foreign_into(),
Expand All @@ -437,11 +431,11 @@ impl ForeignTryFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::
description: pi.description,
metadata: pi.metadata,
customer_id: pi.customer_id,
connector: pa.get_routed_through_connector()?,
connector: pa.connector,
payment_method: pa.payment_method.map(ForeignInto::foreign_into),
payment_method_type: pa.payment_method_type.map(ForeignInto::foreign_into),
..Default::default()
})
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/router/src/core/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
types::{
self,
api::{self, refunds},
storage::{self, enums, PaymentAttemptExt, ProcessTrackerExt},
storage::{self, enums, ProcessTrackerExt},
transformers::{ForeignFrom, ForeignInto},
},
utils::{self, OptionExt},
Expand Down Expand Up @@ -120,8 +120,8 @@ pub async fn trigger_refund_to_gateway(
creds_identifier: Option<String>,
) -> RouterResult<storage::Refund> {
let routed_through = payment_attempt
.get_routed_through_connector()
.change_context(errors::ApiErrorResponse::InternalServerError)?
.connector
.clone()
.ok_or(errors::ApiErrorResponse::InternalServerError)
.into_report()
.attach_printable("Failed to retrieve connector from payment attempt")?;
Expand Down Expand Up @@ -552,8 +552,8 @@ pub async fn validate_and_create_refund(
.change_context(errors::ApiErrorResponse::MaximumRefundCount)?;

let connector = payment_attempt
.get_routed_through_connector()
.change_context(errors::ApiErrorResponse::InternalServerError)?
.connector
.clone()
.ok_or(errors::ApiErrorResponse::InternalServerError)
.into_report()
.attach_printable("No connector populated in payment attempt")?;
Expand Down
4 changes: 4 additions & 0 deletions crates/router/src/db/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl PaymentAttemptInterface for MockDb {
payment_method_type: payment_attempt.payment_method_type,
payment_method_data: payment_attempt.payment_method_data,
business_sub_label: payment_attempt.business_sub_label,
straight_through_algorithm: payment_attempt.straight_through_algorithm,
};
payment_attempts.push(payment_attempt.clone());
Ok(payment_attempt)
Expand Down Expand Up @@ -396,6 +397,9 @@ mod storage {
payment_method_type: payment_attempt.payment_method_type.clone(),
payment_method_data: payment_attempt.payment_method_data.clone(),
business_sub_label: payment_attempt.business_sub_label.clone(),
straight_through_algorithm: payment_attempt
.straight_through_algorithm
.clone(),
};

let field = format!("pa_{}", created_attempt.attempt_id);
Expand Down
5 changes: 2 additions & 3 deletions crates/router/src/scheduler/workflows/payment_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
scheduler::{consumer, process_data, utils},
types::{
api,
storage::{self, enums, PaymentAttemptExt, ProcessTrackerExt},
storage::{self, enums, ProcessTrackerExt},
},
utils::{OptionExt, ValueExt},
};
Expand Down Expand Up @@ -64,8 +64,7 @@ impl ProcessTrackerWorkflow for PaymentsSyncWorkflow {
_ => {
let connector = payment_data
.payment_attempt
.get_routed_through_connector()
.map_err(errors::ProcessTrackerError::EParsingError)?
.connector
.ok_or(errors::ProcessTrackerError::MissingRequiredField)?;

retry_sync_task(
Expand Down
Loading

0 comments on commit 01f86c4

Please sign in to comment.