mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-27 19:46:48 +08:00
feat(subscription): Add support to call payments microservice from subscription service via payments API client (#9590)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -8,17 +8,18 @@ use crate::{
|
||||
payments::{Address, PaymentMethodDataRequest},
|
||||
};
|
||||
|
||||
// use crate::{
|
||||
// customers::{CustomerRequest, CustomerResponse},
|
||||
// payments::CustomerDetailsResponse,
|
||||
// };
|
||||
|
||||
/// Request payload for creating a subscription.
|
||||
///
|
||||
/// This struct captures details required to create a subscription,
|
||||
/// including plan, profile, merchant connector, and optional customer info.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct CreateSubscriptionRequest {
|
||||
/// Amount to be charged for the invoice.
|
||||
pub amount: MinorUnit,
|
||||
|
||||
/// Currency for the amount.
|
||||
pub currency: api_enums::Currency,
|
||||
|
||||
/// Merchant specific Unique identifier.
|
||||
pub merchant_reference_id: Option<String>,
|
||||
|
||||
@ -30,13 +31,22 @@ pub struct CreateSubscriptionRequest {
|
||||
|
||||
/// customer ID associated with this subscription.
|
||||
pub customer_id: common_utils::id_type::CustomerId,
|
||||
|
||||
/// payment details for the subscription.
|
||||
pub payment_details: CreateSubscriptionPaymentDetails,
|
||||
|
||||
/// billing address for the subscription.
|
||||
pub billing: Option<Address>,
|
||||
|
||||
/// shipping address for the subscription.
|
||||
pub shipping: Option<Address>,
|
||||
}
|
||||
|
||||
/// Response payload returned after successfully creating a subscription.
|
||||
///
|
||||
/// Includes details such as subscription ID, status, plan, merchant, and customer info.
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct CreateSubscriptionResponse {
|
||||
pub struct SubscriptionResponse {
|
||||
/// Unique identifier for the subscription.
|
||||
pub id: common_utils::id_type::SubscriptionId,
|
||||
|
||||
@ -78,6 +88,7 @@ pub struct CreateSubscriptionResponse {
|
||||
/// - `Cancelled`: Subscription has been cancelled.
|
||||
/// - `Failed`: Subscription has failed.
|
||||
#[derive(Debug, Clone, serde::Serialize, strum::EnumString, strum::Display, ToSchema)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SubscriptionStatus {
|
||||
/// Subscription is active.
|
||||
Active,
|
||||
@ -101,7 +112,7 @@ pub enum SubscriptionStatus {
|
||||
Failed,
|
||||
}
|
||||
|
||||
impl CreateSubscriptionResponse {
|
||||
impl SubscriptionResponse {
|
||||
/// Creates a new [`CreateSubscriptionResponse`] with the given identifiers.
|
||||
///
|
||||
/// By default, `client_secret`, `coupon_code`, and `customer` fields are `None`.
|
||||
@ -130,15 +141,77 @@ impl CreateSubscriptionResponse {
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiEventMetric for CreateSubscriptionResponse {}
|
||||
impl ApiEventMetric for SubscriptionResponse {}
|
||||
impl ApiEventMetric for CreateSubscriptionRequest {}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct PaymentDetails {
|
||||
pub struct ConfirmSubscriptionPaymentDetails {
|
||||
pub payment_method: api_enums::PaymentMethod,
|
||||
pub payment_method_type: Option<api_enums::PaymentMethodType>,
|
||||
pub payment_method_data: PaymentMethodDataRequest,
|
||||
pub customer_acceptance: Option<CustomerAcceptance>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct CreateSubscriptionPaymentDetails {
|
||||
pub return_url: common_utils::types::Url,
|
||||
pub setup_future_usage: Option<api_enums::FutureUsage>,
|
||||
pub capture_method: Option<api_enums::CaptureMethod>,
|
||||
pub authentication_type: Option<api_enums::AuthenticationType>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct PaymentDetails {
|
||||
pub payment_method: Option<api_enums::PaymentMethod>,
|
||||
pub payment_method_type: Option<api_enums::PaymentMethodType>,
|
||||
pub payment_method_data: Option<PaymentMethodDataRequest>,
|
||||
pub setup_future_usage: Option<api_enums::FutureUsage>,
|
||||
pub customer_acceptance: Option<CustomerAcceptance>,
|
||||
pub return_url: Option<common_utils::types::Url>,
|
||||
pub capture_method: Option<api_enums::CaptureMethod>,
|
||||
pub authentication_type: Option<api_enums::AuthenticationType>,
|
||||
}
|
||||
|
||||
// Creating new type for PaymentRequest API call as usage of api_models::PaymentsRequest will result in invalid payment request during serialization
|
||||
// Eg: Amount will be serialized as { amount: {Value: 100 }}
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct CreatePaymentsRequestData {
|
||||
pub amount: MinorUnit,
|
||||
pub currency: api_enums::Currency,
|
||||
pub customer_id: Option<common_utils::id_type::CustomerId>,
|
||||
pub billing: Option<Address>,
|
||||
pub shipping: Option<Address>,
|
||||
pub setup_future_usage: Option<api_enums::FutureUsage>,
|
||||
pub return_url: Option<common_utils::types::Url>,
|
||||
pub capture_method: Option<api_enums::CaptureMethod>,
|
||||
pub authentication_type: Option<api_enums::AuthenticationType>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct ConfirmPaymentsRequestData {
|
||||
pub billing: Option<Address>,
|
||||
pub shipping: Option<Address>,
|
||||
pub payment_method: api_enums::PaymentMethod,
|
||||
pub payment_method_type: Option<api_enums::PaymentMethodType>,
|
||||
pub payment_method_data: PaymentMethodDataRequest,
|
||||
pub customer_acceptance: Option<CustomerAcceptance>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct CreateAndConfirmPaymentsRequestData {
|
||||
pub amount: MinorUnit,
|
||||
pub currency: api_enums::Currency,
|
||||
pub customer_id: Option<common_utils::id_type::CustomerId>,
|
||||
pub confirm: bool,
|
||||
pub billing: Option<Address>,
|
||||
pub shipping: Option<Address>,
|
||||
pub setup_future_usage: Option<api_enums::FutureUsage>,
|
||||
pub return_url: Option<common_utils::types::Url>,
|
||||
pub capture_method: Option<api_enums::CaptureMethod>,
|
||||
pub authentication_type: Option<api_enums::AuthenticationType>,
|
||||
pub payment_method: Option<api_enums::PaymentMethod>,
|
||||
pub payment_method_type: Option<api_enums::PaymentMethodType>,
|
||||
pub payment_method_data: Option<PaymentMethodDataRequest>,
|
||||
pub customer_acceptance: Option<CustomerAcceptance>,
|
||||
}
|
||||
|
||||
@ -149,18 +222,16 @@ pub struct PaymentResponseData {
|
||||
pub amount: MinorUnit,
|
||||
pub currency: api_enums::Currency,
|
||||
pub connector: Option<String>,
|
||||
pub payment_method_id: Option<Secret<String>>,
|
||||
pub payment_experience: Option<api_enums::PaymentExperience>,
|
||||
pub error_code: Option<String>,
|
||||
pub error_message: 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>,
|
||||
|
||||
@ -174,10 +245,13 @@ pub struct ConfirmSubscriptionRequest {
|
||||
pub customer_id: common_utils::id_type::CustomerId,
|
||||
|
||||
/// Billing address for the subscription.
|
||||
pub billing_address: Option<Address>,
|
||||
pub billing: Option<Address>,
|
||||
|
||||
/// Shipping address for the subscription.
|
||||
pub shipping: Option<Address>,
|
||||
|
||||
/// Payment details for the invoice.
|
||||
pub payment_details: PaymentDetails,
|
||||
pub payment_details: ConfirmSubscriptionPaymentDetails,
|
||||
}
|
||||
|
||||
impl ConfirmSubscriptionRequest {
|
||||
@ -190,9 +264,9 @@ impl ConfirmSubscriptionRequest {
|
||||
}
|
||||
|
||||
pub fn get_billing_address(&self) -> Result<Address, error_stack::Report<ValidationError>> {
|
||||
self.billing_address.clone().ok_or(error_stack::report!(
|
||||
self.billing.clone().ok_or(error_stack::report!(
|
||||
ValidationError::MissingRequiredField {
|
||||
field_name: "billing_address".to_string()
|
||||
field_name: "billing".to_string()
|
||||
}
|
||||
))
|
||||
}
|
||||
@ -200,6 +274,41 @@ impl ConfirmSubscriptionRequest {
|
||||
|
||||
impl ApiEventMetric for ConfirmSubscriptionRequest {}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
|
||||
pub struct CreateAndConfirmSubscriptionRequest {
|
||||
/// Amount to be charged for the invoice.
|
||||
pub amount: Option<MinorUnit>,
|
||||
|
||||
/// Currency for the amount.
|
||||
pub currency: Option<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: Option<Address>,
|
||||
|
||||
/// Shipping address for the subscription.
|
||||
pub shipping: Option<Address>,
|
||||
|
||||
/// Payment details for the invoice.
|
||||
pub payment_details: PaymentDetails,
|
||||
|
||||
/// Merchant specific Unique identifier.
|
||||
pub merchant_reference_id: Option<String>,
|
||||
}
|
||||
|
||||
impl ApiEventMetric for CreateAndConfirmSubscriptionRequest {}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
pub struct ConfirmSubscriptionResponse {
|
||||
/// Unique identifier for the subscription.
|
||||
@ -231,6 +340,9 @@ pub struct ConfirmSubscriptionResponse {
|
||||
|
||||
/// Invoice Details for the subscription.
|
||||
pub invoice: Option<Invoice>,
|
||||
|
||||
/// Billing Processor subscription ID.
|
||||
pub billing_processor_subscription_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, ToSchema)]
|
||||
|
||||
Reference in New Issue
Block a user