mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-27 19:46:48 +08:00
feat(subscriptions): Add Subscription confirm handler (#9353)
Co-authored-by: Prajjwal kumar <write2prajjwal@gmail.com> Co-authored-by: Prajjwal Kumar <prajjwal.kumar@juspay.in> Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Co-authored-by: Jagan Elavarasan <jaganelavarasan@gmail.com>
This commit is contained in:
@ -1,7 +1,13 @@
|
||||
use common_utils::events::ApiEventMetric;
|
||||
use common_types::payments::CustomerAcceptance;
|
||||
use common_utils::{errors::ValidationError, events::ApiEventMetric, types::MinorUnit};
|
||||
use masking::Secret;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::{
|
||||
enums as api_enums,
|
||||
payments::{Address, PaymentMethodDataRequest},
|
||||
};
|
||||
|
||||
// use crate::{
|
||||
// customers::{CustomerRequest, CustomerResponse},
|
||||
// payments::CustomerDetailsResponse,
|
||||
@ -63,7 +69,14 @@ pub struct CreateSubscriptionResponse {
|
||||
///
|
||||
/// - `Created`: Subscription was created but not yet activated.
|
||||
/// - `Active`: Subscription is currently active.
|
||||
/// - `InActive`: Subscription is inactive (e.g., cancelled or expired).
|
||||
/// - `InActive`: Subscription is inactive.
|
||||
/// - `Pending`: Subscription is pending activation.
|
||||
/// - `Trial`: Subscription is in a trial period.
|
||||
/// - `Paused`: Subscription is paused.
|
||||
/// - `Unpaid`: Subscription is unpaid.
|
||||
/// - `Onetime`: Subscription is a one-time payment.
|
||||
/// - `Cancelled`: Subscription has been cancelled.
|
||||
/// - `Failed`: Subscription has failed.
|
||||
#[derive(Debug, Clone, serde::Serialize, strum::EnumString, strum::Display, ToSchema)]
|
||||
pub enum SubscriptionStatus {
|
||||
/// Subscription is active.
|
||||
@ -74,6 +87,18 @@ pub enum SubscriptionStatus {
|
||||
InActive,
|
||||
/// Subscription is in pending state.
|
||||
Pending,
|
||||
/// Subscription is in trial state.
|
||||
Trial,
|
||||
/// Subscription is paused.
|
||||
Paused,
|
||||
/// Subscription is unpaid.
|
||||
Unpaid,
|
||||
/// Subscription is a one-time payment.
|
||||
Onetime,
|
||||
/// Subscription is cancelled.
|
||||
Cancelled,
|
||||
/// Subscription has failed.
|
||||
Failed,
|
||||
}
|
||||
|
||||
impl CreateSubscriptionResponse {
|
||||
@ -107,3 +132,141 @@ impl CreateSubscriptionResponse {
|
||||
|
||||
impl ApiEventMetric for CreateSubscriptionResponse {}
|
||||
impl ApiEventMetric for CreateSubscriptionRequest {}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct PaymentDetails {
|
||||
pub payment_method: api_enums::PaymentMethod,
|
||||
pub payment_method_type: Option<api_enums::PaymentMethodType>,
|
||||
pub payment_method_data: PaymentMethodDataRequest,
|
||||
pub setup_future_usage: Option<api_enums::FutureUsage>,
|
||||
pub customer_acceptance: Option<CustomerAcceptance>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct PaymentResponseData {
|
||||
pub payment_id: common_utils::id_type::PaymentId,
|
||||
pub status: api_enums::IntentStatus,
|
||||
pub amount: MinorUnit,
|
||||
pub currency: api_enums::Currency,
|
||||
pub connector: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct ConfirmSubscriptionRequest {
|
||||
/// Client secret for SDK based interaction.
|
||||
pub client_secret: Option<String>,
|
||||
|
||||
/// Amount to be charged for the invoice.
|
||||
pub amount: MinorUnit,
|
||||
|
||||
/// Currency for the amount.
|
||||
pub currency: api_enums::Currency,
|
||||
|
||||
/// Identifier for the associated plan_id.
|
||||
pub plan_id: Option<String>,
|
||||
|
||||
/// Identifier for the associated item_price_id for the subscription.
|
||||
pub item_price_id: Option<String>,
|
||||
|
||||
/// Idenctifier for the coupon code for the subscription.
|
||||
pub coupon_code: Option<String>,
|
||||
|
||||
/// Identifier for customer.
|
||||
pub customer_id: common_utils::id_type::CustomerId,
|
||||
|
||||
/// Billing address for the subscription.
|
||||
pub billing_address: Option<Address>,
|
||||
|
||||
/// Payment details for the invoice.
|
||||
pub payment_details: PaymentDetails,
|
||||
}
|
||||
|
||||
impl ConfirmSubscriptionRequest {
|
||||
pub fn get_item_price_id(&self) -> Result<String, error_stack::Report<ValidationError>> {
|
||||
self.item_price_id.clone().ok_or(error_stack::report!(
|
||||
ValidationError::MissingRequiredField {
|
||||
field_name: "item_price_id".to_string()
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
pub fn get_billing_address(&self) -> Result<Address, error_stack::Report<ValidationError>> {
|
||||
self.billing_address.clone().ok_or(error_stack::report!(
|
||||
ValidationError::MissingRequiredField {
|
||||
field_name: "billing_address".to_string()
|
||||
}
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiEventMetric for ConfirmSubscriptionRequest {}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct ConfirmSubscriptionResponse {
|
||||
/// Unique identifier for the subscription.
|
||||
pub id: common_utils::id_type::SubscriptionId,
|
||||
|
||||
/// Merchant specific Unique identifier.
|
||||
pub merchant_reference_id: Option<String>,
|
||||
|
||||
/// Current status of the subscription.
|
||||
pub status: SubscriptionStatus,
|
||||
|
||||
/// Identifier for the associated subscription plan.
|
||||
pub plan_id: Option<String>,
|
||||
|
||||
/// Identifier for the associated item_price_id for the subscription.
|
||||
pub price_id: Option<String>,
|
||||
|
||||
/// Optional coupon code applied to this subscription.
|
||||
pub coupon: Option<String>,
|
||||
|
||||
/// Associated profile ID.
|
||||
pub profile_id: common_utils::id_type::ProfileId,
|
||||
|
||||
/// Payment details for the invoice.
|
||||
pub payment: Option<PaymentResponseData>,
|
||||
|
||||
/// Customer ID associated with this subscription.
|
||||
pub customer_id: Option<common_utils::id_type::CustomerId>,
|
||||
|
||||
/// Invoice Details for the subscription.
|
||||
pub invoice: Option<Invoice>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct Invoice {
|
||||
/// Unique identifier for the invoice.
|
||||
pub id: common_utils::id_type::InvoiceId,
|
||||
|
||||
/// Unique identifier for the subscription.
|
||||
pub subscription_id: common_utils::id_type::SubscriptionId,
|
||||
|
||||
/// Identifier for the merchant.
|
||||
pub merchant_id: common_utils::id_type::MerchantId,
|
||||
|
||||
/// Identifier for the profile.
|
||||
pub profile_id: common_utils::id_type::ProfileId,
|
||||
|
||||
/// Identifier for the merchant connector account.
|
||||
pub merchant_connector_id: common_utils::id_type::MerchantConnectorAccountId,
|
||||
|
||||
/// Identifier for the Payment.
|
||||
pub payment_intent_id: Option<common_utils::id_type::PaymentId>,
|
||||
|
||||
/// Identifier for the Payment method.
|
||||
pub payment_method_id: Option<String>,
|
||||
|
||||
/// Identifier for the Customer.
|
||||
pub customer_id: common_utils::id_type::CustomerId,
|
||||
|
||||
/// Invoice amount.
|
||||
pub amount: MinorUnit,
|
||||
|
||||
/// Currency for the invoice payment.
|
||||
pub currency: api_enums::Currency,
|
||||
|
||||
/// Status of the invoice.
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
impl ApiEventMetric for ConfirmSubscriptionResponse {}
|
||||
|
||||
Reference in New Issue
Block a user