feat: populate fields from payment attempt in payment list (#736)

This commit is contained in:
Kartikeya Hegde
2023-03-15 14:23:04 +05:30
committed by GitHub
parent ec2b1b18fd
commit b5b3d57c4c
3 changed files with 49 additions and 30 deletions

View File

@ -684,9 +684,13 @@ pub async fn list_payments(
merchant: storage::MerchantAccount, merchant: storage::MerchantAccount,
constraints: api::PaymentListConstraints, constraints: api::PaymentListConstraints,
) -> RouterResponse<api::PaymentListResponse> { ) -> RouterResponse<api::PaymentListResponse> {
use futures::stream::StreamExt;
use crate::types::transformers::ForeignFrom;
helpers::validate_payment_list_request(&constraints)?; helpers::validate_payment_list_request(&constraints)?;
let merchant_id = &merchant.merchant_id; let merchant_id = &merchant.merchant_id;
let payment_intent = let payment_intents =
helpers::filter_by_constraints(db, &constraints, merchant_id, merchant.storage_scheme) helpers::filter_by_constraints(db, &constraints, merchant_id, merchant.storage_scheme)
.await .await
.map_err(|err| { .map_err(|err| {
@ -696,10 +700,24 @@ pub async fn list_payments(
) )
})?; })?;
let data: Vec<api::PaymentsResponse> = payment_intent let pi = futures::stream::iter(payment_intents)
.into_iter() .filter_map(|pi| async {
.map(types::transformers::ForeignInto::foreign_into) let pa = db
.collect(); .find_payment_attempt_by_payment_id_merchant_id(
&pi.payment_id,
merchant_id,
// since OLAP doesn't have KV. Force to get the data from PSQL.
storage_enums::MerchantStorageScheme::PostgresOnly,
)
.await
.ok()?;
Some((pi, pa))
})
.collect::<Vec<(storage::PaymentIntent, storage::PaymentAttempt)>>()
.await;
let data: Vec<api::PaymentsResponse> = pi.into_iter().map(ForeignFrom::foreign_from).collect();
Ok(services::ApplicationResponse::Json( Ok(services::ApplicationResponse::Json(
api::PaymentListResponse { api::PaymentListResponse {
size: data.len(), size: data.len(),

View File

@ -15,7 +15,7 @@ use crate::{
types::{ types::{
self, api, self, api,
storage::{self, enums}, storage::{self, enums},
transformers::ForeignInto, transformers::{ForeignFrom, ForeignInto},
}, },
utils::{OptionExt, ValueExt}, utils::{OptionExt, ValueExt},
}; };
@ -399,6 +399,30 @@ where
}) })
} }
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;
Self {
payment_id: Some(pi.payment_id),
merchant_id: Some(pi.merchant_id),
status: pi.status.foreign_into(),
amount: pi.amount,
amount_capturable: pi.amount_captured,
client_secret: pi.client_secret.map(|s| s.into()),
created: Some(pi.created_at),
currency: pi.currency.map(|c| c.to_string()).unwrap_or_default(),
description: pi.description,
metadata: pi.metadata,
customer_id: pi.customer_id,
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()
}
}
}
impl<F: Clone> TryFrom<PaymentData<F>> for types::PaymentsAuthorizeData { impl<F: Clone> TryFrom<PaymentData<F>> for types::PaymentsAuthorizeData {
type Error = error_stack::Report<errors::ApiErrorResponse>; type Error = error_stack::Report<errors::ApiErrorResponse>;

View File

@ -16,10 +16,7 @@ use time::PrimitiveDateTime;
use crate::{ use crate::{
core::errors, core::errors,
services::api, services::api,
types::{ types::{self, api as api_types},
self, api as api_types, storage,
transformers::{ForeignFrom, ForeignInto},
},
}; };
pub(crate) trait PaymentsRequestExt { pub(crate) trait PaymentsRequestExt {
@ -119,26 +116,6 @@ impl MandateValidationFieldsExt for MandateValidationFields {
} }
} }
impl ForeignFrom<storage::PaymentIntent> for PaymentsResponse {
fn foreign_from(item: storage::PaymentIntent) -> Self {
let item = item;
Self {
payment_id: Some(item.payment_id),
merchant_id: Some(item.merchant_id),
status: item.status.foreign_into(),
amount: item.amount,
amount_capturable: item.amount_captured,
client_secret: item.client_secret.map(|s| s.into()),
created: Some(item.created_at),
currency: item.currency.map(|c| c.to_string()).unwrap_or_default(),
description: item.description,
metadata: item.metadata,
customer_id: item.customer_id,
..Default::default()
}
}
}
// Extract only the last 4 digits of card // Extract only the last 4 digits of card
pub trait PaymentAuthorize: pub trait PaymentAuthorize: