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
#[cfg(feature = "v2")]
use diesel::PgExpressionMethods;
#[cfg(feature = "v1")]
use diesel::Table;
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods, QueryDsl};
use error_stack::ResultExt;
use super::generics;
#[cfg(feature = "v1")]
use crate::schema::payment_methods::dsl;
#[cfg(feature = "v2")]
use crate::schema_v2::payment_methods::dsl::{self, id as pm_id};
use crate::{
enums as storage_enums, errors, kv,
payment_method::{self, PaymentMethod, PaymentMethodNew},
DatabaseConnectionWithContext, StorageResult,
};
impl PaymentMethodNew {
pub async fn insert(
self,
conn: &DatabaseConnectionWithContext<'_>,
) -> StorageResult<PaymentMethod> {
Box::pin(generics::generic_insert(conn, self)).await
}
pub async fn generate_drainer_insert_query(
self,
conn: &mut DatabaseConnectionWithContext<'_>,
) -> StorageResult<kv::SerializableQuery> {
kv::generate_insert_query(conn, self)
.await
.attach_printable("Failed to generate insert query for payment method")
}
}
#[cfg(feature = "v1")]
impl PaymentMethod {
pub async fn delete_by_payment_method_id(
conn: &DatabaseConnectionWithContext<'_>,
payment_method_id: String,
) -> StorageResult<Self> {
generics::generic_delete_one_with_result::<<Self as HasTable>::Table, _, Self>(
conn,
dsl::payment_method_id.eq(payment_method_id),
)
.await
}
pub async fn delete_by_merchant_id_payment_method_id(
conn: &DatabaseConnectionWithContext<'_>,
merchant_id: &common_utils::id_type::MerchantId,
payment_method_id: &str,
) -> StorageResult<Self> {
generics::generic_delete_one_with_result::<<Self as HasTable>::Table, _, Self>(
conn,
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::payment_method_id.eq(payment_method_id.to_owned())),
)
.await
}
pub async fn find_by_locker_id(
conn: &DatabaseConnectionWithContext<'_>,
locker_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::locker_id.eq(locker_id.to_owned()),
)
.await
}
pub async fn find_by_payment_method_id(
conn: &DatabaseConnectionWithContext<'_>,
payment_method_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::payment_method_id.eq(payment_method_id.to_owned()),
)
.await
}
pub async fn find_by_merchant_id(
conn: &DatabaseConnectionWithContext<'_>,
merchant_id: &common_utils::id_type::MerchantId,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<
<Self as HasTable>::Table,
_,
<<Self as HasTable>::Table as Table>::PrimaryKey,
_,
>(
conn,
dsl::merchant_id.eq(merchant_id.to_owned()),
None,
None,
None,
)
.await
}
pub async fn find_by_customer_id_merchant_id(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::CustomerId,
merchant_id: &common_utils::id_type::MerchantId,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned())),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn find_by_merchant_id_payment_method_ids(
conn: &DatabaseConnectionWithContext<'_>,
merchant_id: &common_utils::id_type::MerchantId,
payment_method_ids: &[String],
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
if payment_method_ids.is_empty() {
return Ok(Vec::new());
}
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::payment_method_id.eq_any(payment_method_ids.to_owned())),
limit,
None,
Some(dsl::payment_method_id.asc()),
)
.await
}
pub async fn get_count_by_customer_id_merchant_id_status(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::CustomerId,
merchant_id: &common_utils::id_type::MerchantId,
status: common_enums::PaymentMethodStatus,
) -> StorageResult<i64> {
let filter = crate::list::into_boxed_list(
<Self as HasTable>::table().count().filter(
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::status.eq(status.to_owned())),
),
);
conn.track_get_result::<<Self as HasTable>::Table, _, i64>(
generics::db_metrics::DatabaseOperation::Count,
filter,
)
.await
.change_context(errors::DatabaseError::Others)
.attach_printable("Failed to get a count of payment methods")
}
pub async fn get_count_by_merchant_id_status(
conn: &DatabaseConnectionWithContext<'_>,
merchant_id: &common_utils::id_type::MerchantId,
status: common_enums::PaymentMethodStatus,
) -> StorageResult<i64> {
let query = <Self as HasTable>::table().count().filter(
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::status.eq(status.to_owned())),
);
conn.track_get_result::<<Self as HasTable>::Table, _, i64>(
generics::db_metrics::DatabaseOperation::Count,
query,
)
.await
.change_context(errors::DatabaseError::Others)
.attach_printable("Failed to get a count of payment methods")
}
pub async fn find_by_customer_id_merchant_id_status(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::CustomerId,
merchant_id: &common_utils::id_type::MerchantId,
status: storage_enums::PaymentMethodStatus,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::status.eq(status)),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn find_by_customer_id_merchant_id_status_pm_type(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::CustomerId,
merchant_id: &common_utils::id_type::MerchantId,
status: storage_enums::PaymentMethodStatus,
payment_method_type: storage_enums::PaymentMethodType,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::status.eq(status))
.and(dsl::payment_method_type.eq(payment_method_type)),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn update_with_payment_method_id(
self,
conn: &DatabaseConnectionWithContext<'_>,
payment_method: payment_method::PaymentMethodUpdateInternal,
) -> StorageResult<Self> {
match Box::pin(generics::generic_update_with_unique_predicate_get_result::<
<Self as HasTable>::Table,
_,
_,
_,
>(
conn,
dsl::payment_method_id.eq(self.payment_method_id.to_owned()),
payment_method,
))
.await
{
Err(error) => match error.current_context() {
errors::DatabaseError::NoFieldsToUpdate => Ok(self),
_ => Err(error),
},
result => result,
}
}
pub async fn find_by_fingerprint_id(
conn: &DatabaseConnectionWithContext<'_>,
fingerprint_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::locker_fingerprint_id.eq(fingerprint_id.to_owned()),
)
.await
}
}
#[cfg(feature = "v2")]
impl PaymentMethod {
pub async fn find_by_id(
conn: &DatabaseConnectionWithContext<'_>,
id: &common_utils::id_type::GlobalPaymentMethodId,
) -> StorageResult<Self> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, Self>(
conn,
pm_id.eq(id.to_owned()),
Some(1),
None,
Some(dsl::created_at.desc()),
)
.await?
.into_iter()
.next()
.ok_or_else(|| error_stack::report!(errors::DatabaseError::NotFound))
.attach_printable("Error finding payment method by id")
}
pub async fn find_by_global_customer_id_merchant_id_statuses(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::GlobalCustomerId,
merchant_id: &common_utils::id_type::MerchantId,
statuses: Vec<storage_enums::PaymentMethodStatus>,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::status.eq_any(statuses)),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn find_by_global_customer_id_merchant_id_status(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::GlobalCustomerId,
merchant_id: &common_utils::id_type::MerchantId,
status: storage_enums::PaymentMethodStatus,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::status.eq(status)),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn find_by_auxiliary_fingerprint_id_and_status(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::GlobalCustomerId,
merchant_id: &common_utils::id_type::MerchantId,
auxiliary_fingerprint_id: &str,
status: storage_enums::PaymentMethodStatus,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id
.eq(customer_id.to_owned())
.and(dsl::merchant_id.eq(merchant_id.to_owned()))
.and(dsl::auxiliary_fingerprint_id.eq(auxiliary_fingerprint_id.to_owned()))
.and(dsl::status.eq(status)),
None,
None,
Some(dsl::created_at.desc()),
)
.await
}
pub async fn find_by_global_customer_id(
conn: &DatabaseConnectionWithContext<'_>,
customer_id: &common_utils::id_type::GlobalCustomerId,
limit: Option<i64>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::customer_id.eq(customer_id.to_owned()),
limit,
None,
Some(dsl::last_used_at.desc()),
)
.await
}
pub async fn update_with_id_and_locker_fingerprint_id(
self,
conn: &DatabaseConnectionWithContext<'_>,
payment_method: payment_method::PaymentMethodUpdateInternal,
) -> StorageResult<Self> {
let locker_fingerprint_id = self.locker_fingerprint_id.clone();
match generics::generic_update_with_unique_predicate_get_result::<
<Self as HasTable>::Table,
_,
_,
_,
>(
conn,
pm_id
.eq(self.id.to_owned())
.and(dsl::locker_fingerprint_id.is_not_distinct_from(locker_fingerprint_id)),
payment_method,
)
.await
{
Err(error) => match error.current_context() {
errors::DatabaseError::NoFieldsToUpdate => Ok(self),
_ => Err(error),
},
result => result,
}
}
pub async fn find_by_fingerprint_id(
conn: &DatabaseConnectionWithContext<'_>,
fingerprint_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::locker_fingerprint_id.eq(fingerprint_id.to_owned()),
)
.await
}
pub async fn get_count_by_merchant_id_status(
conn: &DatabaseConnectionWithContext<'_>,
merchant_id: &common_utils::id_type::MerchantId,
status: common_enums::PaymentMethodStatus,
) -> StorageResult<i64> {
let query = <Self as HasTable>::table().count().filter(
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::status.eq(status.to_owned())),
);
conn.track_get_result::<<Self as HasTable>::Table, _, i64>(
generics::db_metrics::DatabaseOperation::Count,
query,
)
.await
.change_context(errors::DatabaseError::Others)
.attach_printable("Failed to get a count of payment methods")
}
pub async fn find_by_locker_id(
conn: &DatabaseConnectionWithContext<'_>,
locker_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::locker_id.eq(locker_id.to_owned()),
)
.await
}
}
impl payment_method::PaymentMethodUpdateInternal {
#[cfg(feature = "v1")]
pub async fn generate_drainer_update_query(
self,
conn: &mut DatabaseConnectionWithContext<'_>,
payment_method_id: String,
) -> StorageResult<kv::SerializableQuery> {
kv::generate_update_query_with_predicate::<<PaymentMethod as HasTable>::Table, _, _>(
conn,
dsl::payment_method_id.eq(payment_method_id),
self,
)
.await
.attach_printable("Failed to generate update query for payment method")
}
#[cfg(feature = "v2")]
pub async fn generate_drainer_update_query(
self,
conn: &mut DatabaseConnectionWithContext<'_>,
id: common_utils::id_type::GlobalPaymentMethodId,
) -> StorageResult<kv::SerializableQuery> {
kv::generate_update_query_with_predicate::<<PaymentMethod as HasTable>::Table, _, _>(
conn,
pm_id.eq(id),
self,
)
.await
.attach_printable("Failed to generate update query for payment method")
}
}