mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 18:17:13 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			191 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			191 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use serde::{Deserialize, Serialize};
 | |
| use masking::Secret;
 | |
| use crate::{connector::utils::PaymentsAuthorizeRequestData,core::errors,types::{self,api, storage::enums}};
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| #[derive(Default, Debug, Serialize, Eq, PartialEq)]
 | |
| pub struct {{project-name | downcase | pascal_case}}PaymentsRequest {
 | |
|     amount: i64,
 | |
|     card: {{project-name | downcase | pascal_case}}Card
 | |
| }
 | |
| 
 | |
| #[derive(Default, Debug, Serialize, Eq, PartialEq)]
 | |
| pub struct {{project-name | downcase | pascal_case}}Card {
 | |
|     name: Secret<String>,
 | |
|     number: cards::CardNumber,
 | |
|     expiry_month: Secret<String>,
 | |
|     expiry_year: Secret<String>,
 | |
|     cvc: Secret<String>,
 | |
|     complete: bool,
 | |
| }
 | |
| 
 | |
| impl TryFrom<&types::PaymentsAuthorizeRouterData> for {{project-name | downcase | pascal_case}}PaymentsRequest  {
 | |
|     type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self,Self::Error> {
 | |
|         match item.request.payment_method_data.clone() {
 | |
|             api::PaymentMethodData::Card(req_card) => {
 | |
|                 let card = {{project-name | downcase | pascal_case}}Card {
 | |
|                     name: req_card.card_holder_name,
 | |
|                     number: req_card.card_number,
 | |
|                     expiry_month: req_card.card_exp_month,
 | |
|                     expiry_year: req_card.card_exp_year,
 | |
|                     cvc: req_card.card_cvc,
 | |
|                     complete: item.request.is_auto_capture()?,
 | |
|                 };
 | |
|                 Ok(Self {
 | |
|                     amount: item.request.amount,
 | |
|                     card,
 | |
|                 })
 | |
|             }
 | |
|             _ => Err(errors::ConnectorError::NotImplemented("Payment methods".to_string()).into()),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| // Auth Struct
 | |
| pub struct {{project-name | downcase | pascal_case}}AuthType {
 | |
|     pub(super) api_key: String
 | |
| }
 | |
| 
 | |
| impl TryFrom<&types::ConnectorAuthType> for {{project-name | downcase | pascal_case}}AuthType  {
 | |
|     type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(auth_type: &types::ConnectorAuthType) -> Result<Self, Self::Error> {
 | |
|         match auth_type {
 | |
|             types::ConnectorAuthType::HeaderKey { api_key } => Ok(Self {
 | |
|                 api_key: api_key.to_string(),
 | |
|             }),
 | |
|             _ => Err(errors::ConnectorError::FailedToObtainAuthType.into()),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| // PaymentsResponse
 | |
| //TODO: Append the remaining status flags
 | |
| #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
 | |
| #[serde(rename_all = "lowercase")]
 | |
| pub enum {{project-name | downcase | pascal_case}}PaymentStatus {
 | |
|     Succeeded,
 | |
|     Failed,
 | |
|     #[default]
 | |
|     Processing,
 | |
| }
 | |
| 
 | |
| impl From<{{project-name | downcase | pascal_case}}PaymentStatus> for enums::AttemptStatus {
 | |
|     fn from(item: {{project-name | downcase | pascal_case}}PaymentStatus) -> Self {
 | |
|         match item {
 | |
|             {{project-name | downcase | pascal_case}}PaymentStatus::Succeeded => Self::Charged,
 | |
|             {{project-name | downcase | pascal_case}}PaymentStatus::Failed => Self::Failure,
 | |
|             {{project-name | downcase | pascal_case}}PaymentStatus::Processing => Self::Authorizing,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| #[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
 | |
| pub struct {{project-name | downcase | pascal_case}}PaymentsResponse {
 | |
|     status: {{project-name | downcase | pascal_case}}PaymentStatus,
 | |
|     id: String,
 | |
| }
 | |
| 
 | |
| impl<F,T> TryFrom<types::ResponseRouterData<F, {{project-name | downcase | pascal_case}}PaymentsResponse, T, types::PaymentsResponseData>> for types::RouterData<F, T, types::PaymentsResponseData> {
 | |
|     type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(item: types::ResponseRouterData<F, {{project-name | downcase | pascal_case}}PaymentsResponse, T, types::PaymentsResponseData>) -> Result<Self,Self::Error> {
 | |
|         Ok(Self {
 | |
|             status: enums::AttemptStatus::from(item.response.status),
 | |
|             response: Ok(types::PaymentsResponseData::TransactionResponse {
 | |
|                 resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),
 | |
|                 redirection_data: None,
 | |
|                 mandate_reference: None,
 | |
|                 connector_metadata: None,
 | |
|             }),
 | |
|             ..item.data
 | |
|         })
 | |
|     }
 | |
| }
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| // REFUND :
 | |
| // Type definition for RefundRequest
 | |
| #[derive(Default, Debug, Serialize)]
 | |
| pub struct {{project-name | downcase | pascal_case}}RefundRequest {
 | |
|     pub amount: i64
 | |
| }
 | |
| 
 | |
| impl<F> TryFrom<&types::RefundsRouterData<F>> for {{project-name | downcase | pascal_case}}RefundRequest {
 | |
|     type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self,Self::Error> {
 | |
|         Ok(Self {
 | |
|             amount: item.request.refund_amount,
 | |
|         })
 | |
|     }
 | |
| }
 | |
| 
 | |
| // Type definition for Refund Response
 | |
| 
 | |
| #[allow(dead_code)]
 | |
| #[derive(Debug, Serialize, Default, Deserialize, Clone)]
 | |
| pub enum RefundStatus {
 | |
|     Succeeded,
 | |
|     Failed,
 | |
|     #[default]
 | |
|     Processing,
 | |
| }
 | |
| 
 | |
| impl From<RefundStatus> for enums::RefundStatus {
 | |
|     fn from(item: RefundStatus) -> Self {
 | |
|         match item {
 | |
|             RefundStatus::Succeeded => Self::Success,
 | |
|             RefundStatus::Failed => Self::Failure,
 | |
|             RefundStatus::Processing => Self::Pending,
 | |
|             //TODO: Review mapping
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| #[derive(Default, Debug, Clone, Serialize, Deserialize)]
 | |
| pub struct RefundResponse {
 | |
|     id: String,
 | |
|     status: RefundStatus
 | |
| }
 | |
| 
 | |
| impl TryFrom<types::RefundsResponseRouterData<api::Execute, RefundResponse>>
 | |
|     for types::RefundsRouterData<api::Execute>
 | |
| {
 | |
|     type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(
 | |
|         item: types::RefundsResponseRouterData<api::Execute, RefundResponse>,
 | |
|     ) -> Result<Self, Self::Error> {
 | |
|         Ok(Self {
 | |
|             response: Ok(types::RefundsResponseData {
 | |
|                 connector_refund_id: item.response.id.to_string(),
 | |
|                 refund_status: enums::RefundStatus::from(item.response.status),
 | |
|             }),
 | |
|             ..item.data
 | |
|         })
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl TryFrom<types::RefundsResponseRouterData<api::RSync, RefundResponse>> for types::RefundsRouterData<api::RSync>
 | |
| {
 | |
|      type Error = error_stack::Report<errors::ConnectorError>;
 | |
|     fn try_from(item: types::RefundsResponseRouterData<api::RSync, RefundResponse>) -> Result<Self,Self::Error> {
 | |
|         Ok(Self {
 | |
|             response: Ok(types::RefundsResponseData {
 | |
|                 connector_refund_id: item.response.id.to_string(),
 | |
|                 refund_status: enums::RefundStatus::from(item.response.status),
 | |
|             }),
 | |
|             ..item.data
 | |
|         })
 | |
|      }
 | |
|  }
 | |
| 
 | |
| //TODO: Fill the struct with respective fields
 | |
| #[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
 | |
| pub struct {{project-name | downcase | pascal_case}}ErrorResponse {
 | |
|     pub status_code: u16,
 | |
|     pub code: String,
 | |
|     pub message: String,
 | |
|     pub reason: Option<String>,
 | |
| }
 | 
