mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 10:06:32 +08:00 
			
		
		
		
	feat(connector): add payment, refund urls for dummy connector (#1084)
This commit is contained in:
		| @ -2,9 +2,11 @@ mod transformers; | ||||
|  | ||||
| use std::fmt::Debug; | ||||
|  | ||||
| use api_models::payments::PaymentMethodData; | ||||
| use error_stack::{IntoReport, ResultExt}; | ||||
| use transformers as dummyconnector; | ||||
|  | ||||
| use super::utils::{PaymentsAuthorizeRequestData, RefundsRequestData}; | ||||
| use crate::{ | ||||
|     configs::settings, | ||||
|     core::errors::{self, CustomResult}, | ||||
| @ -96,9 +98,9 @@ impl ConnectorCommon for DummyConnector { | ||||
|  | ||||
|         Ok(ErrorResponse { | ||||
|             status_code: res.status_code, | ||||
|             code: response.code, | ||||
|             message: response.message, | ||||
|             reason: response.reason, | ||||
|             code: response.error.code, | ||||
|             message: response.error.message, | ||||
|             reason: response.error.reason, | ||||
|         }) | ||||
|     } | ||||
| } | ||||
| @ -136,10 +138,22 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P | ||||
|  | ||||
|     fn get_url( | ||||
|         &self, | ||||
|         _req: &types::PaymentsAuthorizeRouterData, | ||||
|         _connectors: &settings::Connectors, | ||||
|         req: &types::PaymentsAuthorizeRouterData, | ||||
|         connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<String, errors::ConnectorError> { | ||||
|         Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into()) | ||||
|         let payment_method_data = req.request.payment_method_data.to_owned(); | ||||
|         let payment_method_type = req.request.get_payment_method_type()?; | ||||
|         match payment_method_data { | ||||
|             PaymentMethodData::Card(_) => Ok(format!("{}/payment", self.base_url(connectors))), | ||||
|             _ => Err(error_stack::report!(errors::ConnectorError::NotSupported { | ||||
|                 message: format!( | ||||
|                     "The payment method {} is not supported", | ||||
|                     payment_method_type | ||||
|                 ), | ||||
|                 connector: "dummyconnector", | ||||
|                 payment_experience: api::enums::PaymentExperience::RedirectToUrl.to_string(), | ||||
|             })), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn get_request_body( | ||||
| @ -180,7 +194,7 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P | ||||
|         data: &types::PaymentsAuthorizeRouterData, | ||||
|         res: Response, | ||||
|     ) -> CustomResult<types::PaymentsAuthorizeRouterData, errors::ConnectorError> { | ||||
|         let response: dummyconnector::DummyConnectorPaymentsResponse = res | ||||
|         let response: dummyconnector::PaymentsResponse = res | ||||
|             .response | ||||
|             .parse_struct("DummyConnector PaymentsAuthorizeResponse") | ||||
|             .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; | ||||
| @ -217,10 +231,23 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe | ||||
|  | ||||
|     fn get_url( | ||||
|         &self, | ||||
|         _req: &types::PaymentsSyncRouterData, | ||||
|         _connectors: &settings::Connectors, | ||||
|         req: &types::PaymentsSyncRouterData, | ||||
|         connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<String, errors::ConnectorError> { | ||||
|         Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into()) | ||||
|         match req | ||||
|             .request | ||||
|             .connector_transaction_id | ||||
|             .get_connector_transaction_id() | ||||
|         { | ||||
|             Ok(transaction_id) => Ok(format!( | ||||
|                 "{}/payments/{}", | ||||
|                 self.base_url(connectors), | ||||
|                 transaction_id | ||||
|             )), | ||||
|             Err(_) => Err(error_stack::report!( | ||||
|                 errors::ConnectorError::MissingConnectorTransactionID | ||||
|             )), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn build_request( | ||||
| @ -243,7 +270,7 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe | ||||
|         data: &types::PaymentsSyncRouterData, | ||||
|         res: Response, | ||||
|     ) -> CustomResult<types::PaymentsSyncRouterData, errors::ConnectorError> { | ||||
|         let response: dummyconnector::DummyConnectorPaymentsResponse = res | ||||
|         let response: dummyconnector::PaymentsResponse = res | ||||
|             .response | ||||
|             .parse_struct("dummyconnector PaymentsSyncResponse") | ||||
|             .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; | ||||
| @ -315,7 +342,7 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme | ||||
|         data: &types::PaymentsCaptureRouterData, | ||||
|         res: Response, | ||||
|     ) -> CustomResult<types::PaymentsCaptureRouterData, errors::ConnectorError> { | ||||
|         let response: dummyconnector::DummyConnectorPaymentsResponse = res | ||||
|         let response: dummyconnector::PaymentsResponse = res | ||||
|             .response | ||||
|             .parse_struct("DummyConnector PaymentsCaptureResponse") | ||||
|             .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; | ||||
| @ -357,10 +384,14 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon | ||||
|  | ||||
|     fn get_url( | ||||
|         &self, | ||||
|         _req: &types::RefundsRouterData<api::Execute>, | ||||
|         _connectors: &settings::Connectors, | ||||
|         req: &types::RefundsRouterData<api::Execute>, | ||||
|         connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<String, errors::ConnectorError> { | ||||
|         Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into()) | ||||
|         Ok(format!( | ||||
|             "{}/{}/refund", | ||||
|             self.base_url(connectors), | ||||
|             req.request.connector_transaction_id | ||||
|         )) | ||||
|     } | ||||
|  | ||||
|     fn get_request_body( | ||||
| @ -435,10 +466,15 @@ impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponse | ||||
|  | ||||
|     fn get_url( | ||||
|         &self, | ||||
|         _req: &types::RefundSyncRouterData, | ||||
|         _connectors: &settings::Connectors, | ||||
|         req: &types::RefundSyncRouterData, | ||||
|         connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<String, errors::ConnectorError> { | ||||
|         Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into()) | ||||
|         let refund_id = req.request.get_connector_refund_id()?; | ||||
|         Ok(format!( | ||||
|             "{}/refunds/{}", | ||||
|             self.base_url(connectors), | ||||
|             refund_id | ||||
|         )) | ||||
|     } | ||||
|  | ||||
|     fn build_request( | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| use masking::Secret; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use storage_models::enums::Currency; | ||||
|  | ||||
| use crate::{ | ||||
|     connector::utils::PaymentsAuthorizeRequestData, | ||||
| @ -7,14 +8,19 @@ use crate::{ | ||||
|     types::{self, api, storage::enums}, | ||||
| }; | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| #[derive(Default, Debug, Serialize, Eq, PartialEq)] | ||||
| #[derive(Debug, Serialize, Eq, PartialEq)] | ||||
| pub struct DummyConnectorPaymentsRequest { | ||||
|     amount: i64, | ||||
|     card: DummyConnectorCard, | ||||
|     currency: Currency, | ||||
|     payment_method_data: PaymentMethodData, | ||||
| } | ||||
|  | ||||
| #[derive(Default, Debug, Serialize, Eq, PartialEq)] | ||||
| #[derive(Debug, serde::Serialize, Eq, PartialEq)] | ||||
| pub enum PaymentMethodData { | ||||
|     Card(DummyConnectorCard), | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Serialize, Eq, PartialEq)] | ||||
| pub struct DummyConnectorCard { | ||||
|     name: Secret<String>, | ||||
|     number: cards::CardNumber, | ||||
| @ -39,7 +45,8 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for DummyConnectorPaymentsRequ | ||||
|                 }; | ||||
|                 Ok(Self { | ||||
|                     amount: item.request.amount, | ||||
|                     card, | ||||
|                     currency: item.request.currency, | ||||
|                     payment_method_data: PaymentMethodData::Card(card), | ||||
|                 }) | ||||
|             } | ||||
|             _ => Err(errors::ConnectorError::NotImplemented("Payment methods".to_string()).into()), | ||||
| @ -47,7 +54,6 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for DummyConnectorPaymentsRequ | ||||
|     } | ||||
| } | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| // Auth Struct | ||||
| pub struct DummyConnectorAuthType { | ||||
|     pub(super) api_key: String, | ||||
| @ -85,31 +91,22 @@ impl From<DummyConnectorPaymentStatus> for enums::AttemptStatus { | ||||
|     } | ||||
| } | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| #[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)] | ||||
| pub struct DummyConnectorPaymentsResponse { | ||||
| pub struct PaymentsResponse { | ||||
|     status: DummyConnectorPaymentStatus, | ||||
|     id: String, | ||||
|     amount: i64, | ||||
|     currency: Currency, | ||||
|     created: String, | ||||
|     payment_method_type: String, | ||||
| } | ||||
|  | ||||
| impl<F, T> | ||||
|     TryFrom< | ||||
|         types::ResponseRouterData< | ||||
|             F, | ||||
|             DummyConnectorPaymentsResponse, | ||||
|             T, | ||||
|             types::PaymentsResponseData, | ||||
|         >, | ||||
|     > for types::RouterData<F, T, types::PaymentsResponseData> | ||||
| impl<F, T> TryFrom<types::ResponseRouterData<F, 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, | ||||
|             DummyConnectorPaymentsResponse, | ||||
|             T, | ||||
|             types::PaymentsResponseData, | ||||
|         >, | ||||
|         item: types::ResponseRouterData<F, PaymentsResponse, T, types::PaymentsResponseData>, | ||||
|     ) -> Result<Self, Self::Error> { | ||||
|         Ok(Self { | ||||
|             status: enums::AttemptStatus::from(item.response.status), | ||||
| @ -125,7 +122,6 @@ impl<F, T> | ||||
|     } | ||||
| } | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| // REFUND : | ||||
| // Type definition for RefundRequest | ||||
| #[derive(Default, Debug, Serialize)] | ||||
| @ -137,7 +133,7 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for DummyConnectorRefundRequest { | ||||
|     type Error = error_stack::Report<errors::ConnectorError>; | ||||
|     fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> { | ||||
|         Ok(Self { | ||||
|             amount: item.request.amount, | ||||
|             amount: item.request.refund_amount, | ||||
|         }) | ||||
|     } | ||||
| } | ||||
| @ -146,6 +142,7 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for DummyConnectorRefundRequest { | ||||
|  | ||||
| #[allow(dead_code)] | ||||
| #[derive(Debug, Serialize, Default, Deserialize, Clone)] | ||||
| #[serde(rename_all = "lowercase")] | ||||
| pub enum RefundStatus { | ||||
|     Succeeded, | ||||
|     Failed, | ||||
| @ -164,11 +161,14 @@ impl From<RefundStatus> for enums::RefundStatus { | ||||
|     } | ||||
| } | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| #[derive(Default, Debug, Clone, Serialize, Deserialize)] | ||||
| pub struct RefundResponse { | ||||
|     id: String, | ||||
|     status: RefundStatus, | ||||
|     currency: Currency, | ||||
|     created: String, | ||||
|     payment_amount: i64, | ||||
|     refund_amount: i64, | ||||
| } | ||||
|  | ||||
| impl TryFrom<types::RefundsResponseRouterData<api::Execute, RefundResponse>> | ||||
| @ -205,10 +205,13 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, RefundResponse>> | ||||
|     } | ||||
| } | ||||
|  | ||||
| //TODO: Fill the struct with respective fields | ||||
| #[derive(Default, Debug, Serialize, Deserialize, PartialEq)] | ||||
| pub struct DummyConnectorErrorResponse { | ||||
|     pub status_code: u16, | ||||
|     pub error: ErrorData, | ||||
| } | ||||
|  | ||||
| #[derive(Default, Debug, Serialize, Deserialize, PartialEq)] | ||||
| pub struct ErrorData { | ||||
|     pub code: String, | ||||
|     pub message: String, | ||||
|     pub reason: Option<String>, | ||||
|  | ||||
| @ -170,6 +170,7 @@ pub trait PaymentsAuthorizeRequestData { | ||||
|     fn is_mandate_payment(&self) -> bool; | ||||
|     fn get_webhook_url(&self) -> Result<String, Error>; | ||||
|     fn get_router_return_url(&self) -> Result<String, Error>; | ||||
|     fn get_payment_method_type(&self) -> Result<storage_models::enums::PaymentMethodType, Error>; | ||||
| } | ||||
|  | ||||
| impl PaymentsAuthorizeRequestData for types::PaymentsAuthorizeData { | ||||
| @ -233,6 +234,11 @@ impl PaymentsAuthorizeRequestData for types::PaymentsAuthorizeData { | ||||
|             .clone() | ||||
|             .ok_or_else(missing_field_err("webhook_url")) | ||||
|     } | ||||
|     fn get_payment_method_type(&self) -> Result<storage_models::enums::PaymentMethodType, Error> { | ||||
|         self.payment_method_type | ||||
|             .to_owned() | ||||
|             .ok_or_else(missing_field_err("payment_method_type")) | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub trait BrowserInformationData { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 ThisIsMani
					ThisIsMani