Skip to content

Commit

Permalink
feat(core): made code changes to get payment attempt based on attempt id
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhicodes-crypto committed Jan 21, 2023
1 parent cc35353 commit 10ef137
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 45 deletions.
4 changes: 2 additions & 2 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ pub async fn list_payment_methods(
let payment_attempt = payment_intent
.as_ref()
.async_map(|pi| async {
db.find_payment_attempt_by_payment_id_merchant_id(
&pi.payment_id,
db.find_payment_attempt_by_merchant_id_attempt_id(
&pi.merchant_id,
&pi.attempt_id,
merchant_account.storage_scheme,
)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/operations/payment_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsCancelRequest>
})?;

let mut payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/operations/payment_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl<F: Send + Clone> GetTracker<F, payments::PaymentData<F>, api::PaymentsCaptu
helpers::validate_amount_to_capture(payment_intent.amount, request.amount_to_capture)?;

payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
})?;

payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
Expand Down
16 changes: 8 additions & 8 deletions crates/router/src/core/payments/operations/payment_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsSessionRequest>
let merchant_id = &merchant_account.merchant_id;
let storage_scheme = merchant_account.storage_scheme;

let mut payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
merchant_id,
storage_scheme,
)
let mut payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(&payment_id, merchant_id, storage_scheme)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;

let mut payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(&payment_id, merchant_id, storage_scheme)
let mut payment_attempt = db
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/operations/payment_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsStartRequest> f
})?;

payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
Expand Down
91 changes: 73 additions & 18 deletions crates/router/src/core/payments/operations/payment_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,11 @@ async fn get_tracker_for_sync<
)> {
let (payment_intent, payment_attempt, currency, amount);

payment_attempt = match payment_id {
api::PaymentIdType::PaymentIntentId(ref id) => {
db.find_payment_attempt_by_payment_id_merchant_id(id, merchant_id, storage_scheme)
}
api::PaymentIdType::ConnectorTransactionId(ref id) => {
db.find_payment_attempt_by_merchant_id_connector_txn_id(merchant_id, id, storage_scheme)
}
api::PaymentIdType::PaymentAttemptId(ref id) => {
db.find_payment_attempt_by_merchant_id_attempt_id(merchant_id, id, storage_scheme)
}
}
.await
.map_err(|error| error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound))?;
(payment_intent, payment_attempt) =
get_payment_intent_payment_attempt(db, payment_id, merchant_id, storage_scheme).await?;

let payment_id_str = payment_attempt.payment_id.clone();

payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(&payment_id_str, merchant_id, storage_scheme)
.await
.map_err(|error| error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound))?;

let mut connector_response = db
.find_connector_response_by_payment_id_merchant_id_attempt_id(
&payment_intent.payment_id,
Expand Down Expand Up @@ -300,3 +284,74 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRetrieveRequest> for Payme
))
}
}

pub async fn get_payment_intent_payment_attempt(
db: &dyn StorageInterface,
payment_id: &api::PaymentIdType,
merchant_id: &str,
storage_scheme: enums::MerchantStorageScheme,
) -> RouterResult<(storage::PaymentIntent, storage::PaymentAttempt)> {
match payment_id {
api_models::payments::PaymentIdType::PaymentIntentId(ref id) => {
let pi = db
.find_payment_intent_by_payment_id_merchant_id(id, merchant_id, storage_scheme)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
let pa = db
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
pi.attempt_id.as_str(),
storage_scheme,
)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
Ok((pi, pa))
}
api_models::payments::PaymentIdType::ConnectorTransactionId(ref id) => {
let pa = db
.find_payment_attempt_by_merchant_id_connector_txn_id(
merchant_id,
id,
storage_scheme,
)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
let pi = db
.find_payment_intent_by_payment_id_merchant_id(
pa.payment_id.as_str(),
merchant_id,
storage_scheme,
)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
Ok((pi, pa))
}
api_models::payments::PaymentIdType::PaymentAttemptId(ref id) => {
let pa = db
.find_payment_attempt_by_merchant_id_attempt_id(merchant_id, id, storage_scheme)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
let pi = db
.find_payment_intent_by_payment_id_merchant_id(
pa.payment_id.as_str(),
merchant_id,
storage_scheme,
)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;
Ok((pi, pa))
}
}
}
18 changes: 9 additions & 9 deletions crates/router/src/core/payments/operations/payment_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
)
.await?;

payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(&payment_id, merchant_id, storage_scheme)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;

payment_attempt = db
.find_payment_attempt_by_payment_id_merchant_id(
&payment_id,
.find_payment_attempt_by_merchant_id_attempt_id(
merchant_id,
payment_intent.attempt_id.as_str(),
storage_scheme,
)
.await
Expand All @@ -81,13 +88,6 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
.amount
.unwrap_or_else(|| payment_attempt.amount.into());

payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(&payment_id, merchant_id, storage_scheme)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
})?;

helpers::authenticate_client_secret(
request.client_secret.as_ref(),
payment_intent.client_secret.as_ref(),
Expand Down

0 comments on commit 10ef137

Please sign in to comment.