Skip to content

Commit

Permalink
feat(payments_v2): implement payments capture v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Narayanbhat166 committed Dec 2, 2024
1 parent 83e8bc0 commit b9ff053
Show file tree
Hide file tree
Showing 20 changed files with 1,799 additions and 166 deletions.
10 changes: 10 additions & 0 deletions crates/api_models/src/events/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::payment_methods::CustomerPaymentMethodsListResponse;
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
use crate::payment_methods::CustomerPaymentMethodsListResponse;
use crate::{
events,
payment_methods::{
CustomerDefaultPaymentMethodResponse, DefaultPaymentMethod, ListCountriesCurrenciesRequest,
ListCountriesCurrenciesResponse, PaymentMethodCollectLinkRenderRequest,
Expand Down Expand Up @@ -419,3 +420,12 @@ impl ApiEventMetric for PaymentStartRedirectionRequest {
})
}
}

#[cfg(feature = "v2")]
impl ApiEventMetric for events::PaymentsCaptureResponse {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Payment {
payment_id: self.id.clone(),
})
}
}
31 changes: 27 additions & 4 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ pub struct AmountDetailsResponse {

#[cfg(feature = "v2")]
#[derive(Clone, Debug, PartialEq, serde::Serialize, ToSchema)]
pub struct ConfirmIntentAmountDetailsResponse {
pub struct PaymentAmountDetailsResponse {
/// The payment amount. Amount for the payment in the lowest denomination of the currency, (i.e) in cents for USD denomination, in yen for JPY denomination etc. E.g., Pass 100 to charge $1.00 and 1 for 1¥ since ¥ is a zero-decimal currency. Read more about [the Decimal and Non-Decimal Currencies](https://github.com/juspay/hyperswitch/wiki/Decimal-and-Non%E2%80%90Decimal-Currencies)
#[schema(value_type = u64, example = 6540)]
#[serde(default, deserialize_with = "amount::deserialize")]
Expand Down Expand Up @@ -3890,6 +3890,7 @@ pub struct PhoneDetails {
pub country_code: Option<String>,
}

#[cfg(feature = "v1")]
#[derive(Debug, Clone, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct PaymentsCaptureRequest {
/// The unique identifier for the payment
Expand All @@ -3898,7 +3899,7 @@ pub struct PaymentsCaptureRequest {
/// The unique identifier for the merchant
#[schema(value_type = Option<String>)]
pub merchant_id: Option<id_type::MerchantId>,
/// The Amount to be captured/ debited from the user's payment method.
/// The Amount to be captured/ debited from the user's payment method. If not passed the full amount will be captured.
#[schema(value_type = i64, example = 6540)]
pub amount_to_capture: Option<MinorUnit>,
/// Decider to refund the uncaptured amount
Expand All @@ -3912,6 +3913,28 @@ pub struct PaymentsCaptureRequest {
pub merchant_connector_details: Option<admin::MerchantConnectorDetailsWrap>,
}

#[cfg(feature = "v2")]
#[derive(Debug, Clone, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct PaymentsCaptureRequest {
/// The Amount to be captured/ debited from the user's payment method. If not passed the full amount will be captured.
#[schema(value_type = Option<i64>, example = 6540)]
pub amount_to_capture: Option<MinorUnit>,
}

#[cfg(feature = "v2")]
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
pub struct PaymentsCaptureResponse {
/// The unique identifier for the payment
pub id: id_type::GlobalPaymentId,

/// Amount details related to the payment
pub amount: PaymentAmountDetailsResponse,

/// Status of the payment
#[schema(value_type = IntentStatus, example = "succeeded")]
pub status: common_enums::IntentStatus,
}

#[derive(Default, Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct UrlDetails {
pub url: String,
Expand Down Expand Up @@ -4615,7 +4638,7 @@ pub struct PaymentsConfirmIntentResponse {
pub status: api_enums::IntentStatus,

/// Amount related information for this payment and attempt
pub amount: ConfirmIntentAmountDetailsResponse,
pub amount: PaymentAmountDetailsResponse,

/// The connector used for the payment
#[schema(example = "stripe")]
Expand Down Expand Up @@ -4684,7 +4707,7 @@ pub struct PaymentsRetrieveResponse {
pub status: api_enums::IntentStatus,

/// Amount related information for this payment and attempt
pub amount: ConfirmIntentAmountDetailsResponse,
pub amount: PaymentAmountDetailsResponse,

/// The connector used for the payment
#[schema(example = "stripe")]
Expand Down
4 changes: 2 additions & 2 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ pub enum PaymentAttemptUpdate {
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = payment_attempt)]
pub struct PaymentAttemptUpdateInternal {
// net_amount: Option<MinorUnit>,
pub status: Option<storage_enums::AttemptStatus>,
// authentication_type: Option<storage_enums::AuthenticationType>,
pub error_message: Option<String>,
Expand All @@ -774,7 +773,8 @@ pub struct PaymentAttemptUpdateInternal {
// multiple_capture_count: Option<i16>,
// pub surcharge_amount: Option<MinorUnit>,
// tax_on_surcharge: Option<MinorUnit>,
// amount_capturable: Option<MinorUnit>,
pub amount_capturable: Option<MinorUnit>,
pub amount_to_capture: Option<MinorUnit>,
pub updated_by: String,
pub merchant_connector_id: Option<id_type::MerchantConnectorAccountId>,
pub connector: Option<String>,
Expand Down
15 changes: 11 additions & 4 deletions crates/diesel_models/src/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub enum PaymentIntentUpdate {
/// Update the payment intent details on payment intent confirmation, after calling the connector
ConfirmIntentPostUpdate {
status: storage_enums::IntentStatus,
amount_captured: Option<MinorUnit>,
updated_by: String,
},
}
Expand Down Expand Up @@ -517,7 +518,7 @@ pub struct PaymentIntentUpdateInternal {
// pub amount: Option<MinorUnit>,
// pub currency: Option<storage_enums::Currency>,
pub status: Option<storage_enums::IntentStatus>,
// pub amount_captured: Option<MinorUnit>,
pub amount_captured: Option<MinorUnit>,
// pub customer_id: Option<common_utils::id_type::CustomerId>,
// pub return_url: Option<>,
// pub setup_future_usage: Option<storage_enums::FutureUsage>,
Expand Down Expand Up @@ -591,7 +592,7 @@ impl PaymentIntentUpdate {
// amount,
// currency,
status,
// amount_captured,
amount_captured,
// customer_id,
// return_url,
// setup_future_usage,
Expand All @@ -617,7 +618,7 @@ impl PaymentIntentUpdate {
// amount: amount.unwrap_or(source.amount),
// currency: currency.unwrap_or(source.currency),
status: status.unwrap_or(source.status),
// amount_captured: amount_captured.or(source.amount_captured),
amount_captured: amount_captured.or(source.amount_captured),
// customer_id: customer_id.or(source.customer_id),
// return_url: return_url.or(source.return_url),
// setup_future_usage: setup_future_usage.or(source.setup_future_usage),
Expand Down Expand Up @@ -747,9 +748,15 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
active_attempt_id: Some(active_attempt_id),
modified_at: common_utils::date_time::now(),
updated_by,
amount_captured: None,
},
PaymentIntentUpdate::ConfirmIntentPostUpdate { status, updated_by } => Self {
PaymentIntentUpdate::ConfirmIntentPostUpdate {
status,
amount_captured,
updated_by,
} => Self {
status: Some(status),
amount_captured,
active_attempt_id: None,
modified_at: common_utils::date_time::now(),
updated_by,
Expand Down
15 changes: 13 additions & 2 deletions crates/hyperswitch_domain_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl AmountDetails {
TaxCalculationOverride::Calculate => None,
};

payment_attempt::AttemptAmountDetails {
payment_attempt::AttemptAmountDetails::from(payment_attempt::AttemptAmountDetailsSetter {
net_amount,
amount_to_capture: None,
surcharge_amount,
Expand All @@ -270,7 +270,7 @@ impl AmountDetails {
amount_capturable: MinorUnit::zero(),
shipping_cost: self.shipping_cost,
order_tax_amount,
}
})
}
}

Expand Down Expand Up @@ -575,6 +575,17 @@ where
pub should_sync_with_connector: bool,
}

#[cfg(feature = "v2")]
#[derive(Clone)]
pub struct PaymentCaptureData<F>
where
F: Clone,
{
pub flow: PhantomData<F>,
pub payment_intent: PaymentIntent,
pub payment_attempt: PaymentAttempt,
}

#[cfg(feature = "v2")]
impl<F> PaymentStatusData<F>
where
Expand Down
Loading

0 comments on commit b9ff053

Please sign in to comment.