Skip to content

Commit

Permalink
add incremental authorization api
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-harsha-vardhan committed Dec 2, 2023
1 parent 04e74a7 commit a8df3b3
Show file tree
Hide file tree
Showing 57 changed files with 1,751 additions and 84 deletions.
13 changes: 11 additions & 2 deletions crates/api_models/src/events/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use crate::{
payments::{
PaymentIdType, PaymentListConstraints, PaymentListFilterConstraints, PaymentListFilters,
PaymentListResponse, PaymentListResponseV2, PaymentsApproveRequest, PaymentsCancelRequest,
PaymentsCaptureRequest, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse,
PaymentsRetrieveRequest, PaymentsStartRequest, RedirectionResponse,
PaymentsCaptureRequest, PaymentsIncrementalAuthorizationRequest, PaymentsRejectRequest,
PaymentsRequest, PaymentsResponse, PaymentsRetrieveRequest, PaymentsStartRequest,
RedirectionResponse,
},
};
impl ApiEventMetric for PaymentsRetrieveRequest {
Expand Down Expand Up @@ -149,3 +150,11 @@ impl ApiEventMetric for PaymentListResponseV2 {
}

impl ApiEventMetric for RedirectionResponse {}

impl ApiEventMetric for PaymentsIncrementalAuthorizationRequest {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Payment {
payment_id: self.payment_id.clone(),
})
}
}
28 changes: 28 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,12 @@ pub struct PaymentsResponse {

/// If true incremental authorization can be performed on this payment
pub incremental_authorization_allowed: Option<bool>,

/// Total number of authorizations happened in an incremental_authorization payment
pub authorization_count: Option<i32>,

/// List of authorizations happened to the payment
pub authorizations: Option<Vec<AuthorizationResponse>>,
}

#[derive(Clone, Debug, serde::Deserialize, ToSchema, serde::Serialize)]
Expand Down Expand Up @@ -2284,6 +2290,16 @@ pub struct PaymentListResponse {
// The list of payments response objects
pub data: Vec<PaymentsResponse>,
}

#[derive(Setter, Clone, Default, Debug, PartialEq, serde::Serialize, ToSchema)]
pub struct AuthorizationResponse {
pub authorization_id: String,
pub amount: i64,
pub status: common_enums::AuthorizationStatus,
pub code: Option<String>,
pub message: Option<String>,
}

#[derive(Clone, Debug, serde::Serialize)]
pub struct PaymentListResponseV2 {
/// The number of payments included in the list for given constraints
Expand Down Expand Up @@ -2990,6 +3006,18 @@ pub struct PaymentsCancelRequest {
pub merchant_connector_details: Option<admin::MerchantConnectorDetailsWrap>,
}

#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
pub struct PaymentsIncrementalAuthorizationRequest {
/// The identifier for the payment
#[serde(skip)]
pub payment_id: String,
/// The total amount including previously authorized amount and additional amount
#[schema(value_type = i64, example = 6540)]
pub amount: i64,
/// Reason for incremental authorization
pub reason: Option<String>,
}

#[derive(Default, Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
pub struct PaymentsApproveRequest {
/// The identifier for the payment
Expand Down
26 changes: 26 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ pub enum CaptureStatus {
Failed,
}

#[derive(
Default,
Clone,
Debug,
Eq,
PartialEq,
serde::Deserialize,
serde::Serialize,
strum::Display,
strum::EnumString,
ToSchema,
Hash,
)]
#[router_derive::diesel_enum(storage_type = "db_enum")]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum AuthorizationStatus {
Success,
Failure,
// Created state is before calling connector
#[default]
Created,
// Requires merchant action
Unresolved,
}

#[derive(
Clone,
Copy,
Expand Down
1 change: 1 addition & 0 deletions crates/common_utils/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Method {
Post,
Put,
Delete,
Patch,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
1 change: 1 addition & 0 deletions crates/data_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ pub struct PaymentIntent {
pub surcharge_applicable: Option<bool>,
pub request_incremental_authorization: storage_enums::RequestIncrementalAuthorization,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}
3 changes: 3 additions & 0 deletions crates/data_models/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ pub enum PaymentAttemptUpdate {
connector: Option<String>,
updated_by: String,
},
AmountUpdate {
amount: i64,
},
}

impl ForeignIDRef for PaymentAttempt {
Expand Down
18 changes: 18 additions & 0 deletions crates/data_models/src/payments/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub struct PaymentIntentNew {
pub surcharge_applicable: Option<bool>,
pub request_incremental_authorization: storage_enums::RequestIncrementalAuthorization,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -186,6 +187,12 @@ pub enum PaymentIntentUpdate {
surcharge_applicable: bool,
updated_by: String,
},
AmountUpdate {
amount: i64,
},
AuthorizationCountUpdate {
authorization_count: i32,
},
}

#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -218,6 +225,7 @@ pub struct PaymentIntentUpdateInternal {
pub updated_by: String,
pub surcharge_applicable: Option<bool>,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}

impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
Expand Down Expand Up @@ -381,6 +389,16 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
updated_by,
..Default::default()
},
PaymentIntentUpdate::AmountUpdate { amount } => Self {
amount: Some(amount),
..Default::default()
},
PaymentIntentUpdate::AuthorizationCountUpdate {
authorization_count,
} => Self {
authorization_count: Some(authorization_count),
..Default::default()
},
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions crates/diesel_models/src/authorization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;

use crate::{enums as storage_enums, schema::authorization};

#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize, Hash)]
#[diesel(table_name = authorization)]
#[diesel(primary_key(authorization_id, merchant_id))]
pub struct Authorization {
pub authorization_id: String,
pub merchant_id: String,
pub payment_id: String,
pub amount: i64,
#[serde(with = "common_utils::custom_serde::iso8601")]
pub created_at: PrimitiveDateTime,
#[serde(with = "common_utils::custom_serde::iso8601")]
pub modified_at: PrimitiveDateTime,
pub status: storage_enums::AuthorizationStatus,
pub code: Option<String>,
pub message: Option<String>,
pub connector_authorization_id: Option<String>,
}

#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay, Serialize, Deserialize)]
#[diesel(table_name = authorization)]
pub struct AuthorizationNew {
pub authorization_id: String,
pub merchant_id: String,
pub payment_id: String,
pub amount: i64,
pub status: storage_enums::AuthorizationStatus,
pub code: Option<String>,
pub message: Option<String>,
pub connector_authorization_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AuthorizationUpdate {
StatusUpdate {
status: storage_enums::AuthorizationStatus,
code: Option<String>,
message: Option<String>,
connector_authorization_id: Option<String>,
},
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = authorization)]
pub struct AuthorizationUpdateInternal {
pub status: Option<storage_enums::AuthorizationStatus>,
pub code: Option<String>,
pub message: Option<String>,
pub modified_at: Option<PrimitiveDateTime>,
pub connector_authorization_id: Option<String>,
}

impl From<AuthorizationUpdate> for AuthorizationUpdateInternal {
fn from(authorization_child_update: AuthorizationUpdate) -> Self {
let now = Some(common_utils::date_time::now());
match authorization_child_update {
AuthorizationUpdate::StatusUpdate {
status,
code,
message,
connector_authorization_id,
} => Self {
status: Some(status),
code,
message,
connector_authorization_id,
modified_at: now,
},
}
}
}
10 changes: 5 additions & 5 deletions crates/diesel_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pub mod diesel_exports {
pub use super::{
DbAttemptStatus as AttemptStatus, DbAuthenticationType as AuthenticationType,
DbCaptureMethod as CaptureMethod, DbCaptureStatus as CaptureStatus,
DbConnectorStatus as ConnectorStatus, DbConnectorType as ConnectorType,
DbCountryAlpha2 as CountryAlpha2, DbCurrency as Currency, DbDisputeStage as DisputeStage,
DbDisputeStatus as DisputeStatus, DbEventClass as EventClass,
DbEventObjectType as EventObjectType, DbEventType as EventType,
DbAuthorizationStatus as AuthorizationStatus, DbCaptureMethod as CaptureMethod,
DbCaptureStatus as CaptureStatus, DbConnectorStatus as ConnectorStatus,
DbConnectorType as ConnectorType, DbCountryAlpha2 as CountryAlpha2, DbCurrency as Currency,
DbDisputeStage as DisputeStage, DbDisputeStatus as DisputeStatus,
DbEventClass as EventClass, DbEventObjectType as EventObjectType, DbEventType as EventType,
DbFraudCheckStatus as FraudCheckStatus, DbFraudCheckType as FraudCheckType,
DbFutureUsage as FutureUsage, DbIntentStatus as IntentStatus,
DbMandateStatus as MandateStatus, DbMandateType as MandateType,
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod capture;
pub mod cards_info;
pub mod configs;

pub mod authorization;
pub mod customers;
pub mod dispute;
pub mod encryption;
Expand Down
7 changes: 7 additions & 0 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ pub enum PaymentAttemptUpdate {
connector: Option<String>,
updated_by: String,
},
AmountUpdate {
amount: i64,
},
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
Expand Down Expand Up @@ -679,6 +682,10 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
updated_by,
..Default::default()
},
PaymentAttemptUpdate::AmountUpdate { amount } => Self {
amount: Some(amount),
..Default::default()
},
}
}
}
21 changes: 21 additions & 0 deletions crates/diesel_models/src/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct PaymentIntent {
pub surcharge_applicable: Option<bool>,
pub request_incremental_authorization: RequestIncrementalAuthorization,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}

#[derive(
Expand Down Expand Up @@ -111,6 +112,7 @@ pub struct PaymentIntentNew {
pub surcharge_applicable: Option<bool>,
pub request_incremental_authorization: RequestIncrementalAuthorization,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -188,6 +190,12 @@ pub enum PaymentIntentUpdate {
surcharge_applicable: Option<bool>,
updated_by: String,
},
AmountUpdate {
amount: i64,
},
AuthorizationCountUpdate {
authorization_count: i32,
},
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
Expand Down Expand Up @@ -221,6 +229,7 @@ pub struct PaymentIntentUpdateInternal {
pub updated_by: String,
pub surcharge_applicable: Option<bool>,
pub incremental_authorization_allowed: Option<bool>,
pub authorization_count: Option<i32>,
}

impl PaymentIntentUpdate {
Expand Down Expand Up @@ -252,6 +261,7 @@ impl PaymentIntentUpdate {
updated_by,
surcharge_applicable,
incremental_authorization_allowed,
authorization_count,
} = self.into();
PaymentIntent {
amount: amount.unwrap_or(source.amount),
Expand Down Expand Up @@ -283,6 +293,7 @@ impl PaymentIntentUpdate {
surcharge_applicable: surcharge_applicable.or(source.surcharge_applicable),

incremental_authorization_allowed,
authorization_count,
..source
}
}
Expand Down Expand Up @@ -449,6 +460,16 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
updated_by,
..Default::default()
},
PaymentIntentUpdate::AmountUpdate { amount } => Self {
amount: Some(amount),
..Default::default()
},
PaymentIntentUpdate::AuthorizationCountUpdate {
authorization_count,
} => Self {
authorization_count: Some(authorization_count),
..Default::default()
},
}
}
}
1 change: 1 addition & 0 deletions crates/diesel_models/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod capture;
pub mod cards_info;
pub mod configs;

pub mod authorization;
pub mod customers;
pub mod dispute;
pub mod events;
Expand Down
Loading

0 comments on commit a8df3b3

Please sign in to comment.