Skip to content

Commit

Permalink
feat(connector): add payment, refund urls for dummy connector (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsMani authored May 11, 2023
1 parent 822fc69 commit fee0e9d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 46 deletions.
72 changes: 54 additions & 18 deletions crates/router/src/connector/dummyconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
})
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(
Expand All @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
59 changes: 31 additions & 28 deletions crates/router/src/connector/dummyconnector/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use masking::Secret;
use serde::{Deserialize, Serialize};
use storage_models::enums::Currency;

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)]
#[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,
Expand All @@ -39,15 +45,15 @@ 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()),
}
}
}

//TODO: Fill the struct with respective fields
// Auth Struct
pub struct DummyConnectorAuthType {
pub(super) api_key: String,
Expand Down Expand Up @@ -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),
Expand All @@ -125,7 +122,6 @@ impl<F, T>
}
}

//TODO: Fill the struct with respective fields
// REFUND :
// Type definition for RefundRequest
#[derive(Default, Debug, Serialize)]
Expand All @@ -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,
})
}
}
Expand All @@ -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,
Expand All @@ -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>>
Expand Down Expand Up @@ -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>,
Expand Down
6 changes: 6 additions & 0 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fee0e9d

Please sign in to comment.