mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-30 01:27:31 +08:00
feat: Braintree connector integration (#30)
Signed-off-by: Sahebjot Singh <sahebjot94@gmail.com> Co-authored-by: Sahebjot Singh <sahebjot.singh@juspay.in>
This commit is contained in:
@ -49,6 +49,9 @@ base_url = "https://api.sandbox.checkout.com/"
|
|||||||
[connectors.stripe]
|
[connectors.stripe]
|
||||||
base_url = "https://api.stripe.com/"
|
base_url = "https://api.stripe.com/"
|
||||||
|
|
||||||
|
[connectors.braintree]
|
||||||
|
base_url = "https://api.sandbox.braintreegateway.com/"
|
||||||
|
|
||||||
|
|
||||||
[scheduler]
|
[scheduler]
|
||||||
stream = "SCHEDULER_STREAM"
|
stream = "SCHEDULER_STREAM"
|
||||||
|
|||||||
@ -105,6 +105,9 @@ base_url = "https://api.sandbox.checkout.com/"
|
|||||||
[connectors.stripe]
|
[connectors.stripe]
|
||||||
base_url = "https://api.stripe.com/"
|
base_url = "https://api.stripe.com/"
|
||||||
|
|
||||||
|
[connectors.braintree]
|
||||||
|
base_url = "https://api.sandbox.braintreegateway.com/"
|
||||||
|
|
||||||
# Scheduler settings provides a point to modify the behaviour of scheduler flow.
|
# Scheduler settings provides a point to modify the behaviour of scheduler flow.
|
||||||
# It defines the the streams/queues name and configuration as well as event selection variables
|
# It defines the the streams/queues name and configuration as well as event selection variables
|
||||||
[scheduler]
|
[scheduler]
|
||||||
|
|||||||
@ -55,3 +55,6 @@ base_url = "https://api.sandbox.checkout.com/"
|
|||||||
|
|
||||||
[connectors.stripe]
|
[connectors.stripe]
|
||||||
base_url = "https://api.stripe.com/"
|
base_url = "https://api.stripe.com/"
|
||||||
|
|
||||||
|
[connectors.braintree]
|
||||||
|
base_url = "https://api.sandbox.braintreegateway.com/"
|
||||||
@ -100,6 +100,7 @@ pub struct Connectors {
|
|||||||
pub authorizedotnet: ConnectorParams,
|
pub authorizedotnet: ConnectorParams,
|
||||||
pub checkout: ConnectorParams,
|
pub checkout: ConnectorParams,
|
||||||
pub stripe: ConnectorParams,
|
pub stripe: ConnectorParams,
|
||||||
|
pub braintree: ConnectorParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
pub mod aci;
|
pub mod aci;
|
||||||
pub mod adyen;
|
pub mod adyen;
|
||||||
pub mod authorizedotnet;
|
pub mod authorizedotnet;
|
||||||
|
pub mod braintree;
|
||||||
pub mod checkout;
|
pub mod checkout;
|
||||||
pub mod stripe;
|
pub mod stripe;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
aci::Aci, adyen::Adyen, authorizedotnet::Authorizedotnet, checkout::Checkout, stripe::Stripe,
|
aci::Aci, adyen::Adyen, authorizedotnet::Authorizedotnet, braintree::Braintree,
|
||||||
|
checkout::Checkout, stripe::Stripe,
|
||||||
};
|
};
|
||||||
|
|||||||
510
crates/router/src/connector/braintree.rs
Normal file
510
crates/router/src/connector/braintree.rs
Normal file
@ -0,0 +1,510 @@
|
|||||||
|
mod transformers;
|
||||||
|
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
|
use error_stack::ResultExt;
|
||||||
|
|
||||||
|
use self::{braintree::BraintreeAuthType, transformers as braintree};
|
||||||
|
use crate::{
|
||||||
|
configs::settings::Connectors,
|
||||||
|
consts,
|
||||||
|
core::{
|
||||||
|
errors::{self, CustomResult},
|
||||||
|
payments,
|
||||||
|
},
|
||||||
|
headers, logger, services,
|
||||||
|
types::{
|
||||||
|
self,
|
||||||
|
api::{self, ConnectorCommon},
|
||||||
|
ErrorResponse, Response,
|
||||||
|
},
|
||||||
|
utils::{self, BytesExt},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Braintree;
|
||||||
|
|
||||||
|
impl api::ConnectorCommon for Braintree {
|
||||||
|
fn id(&self) -> &'static str {
|
||||||
|
"braintree"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_url(&self, connectors: Connectors) -> String {
|
||||||
|
connectors.braintree.base_url
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_auth_header(
|
||||||
|
&self,
|
||||||
|
auth_type: &types::ConnectorAuthType,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
let auth: braintree::BraintreeAuthType = auth_type
|
||||||
|
.try_into()
|
||||||
|
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
|
||||||
|
Ok(vec![(headers::AUTHORIZATION.to_string(), auth.api_key)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl api::Payment for Braintree {}
|
||||||
|
|
||||||
|
impl api::PaymentAuthorize for Braintree {}
|
||||||
|
impl api::PaymentSync for Braintree {}
|
||||||
|
impl api::PaymentVoid for Braintree {}
|
||||||
|
impl api::PaymentCapture for Braintree {}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl
|
||||||
|
services::ConnectorIntegration<
|
||||||
|
api::Capture,
|
||||||
|
types::PaymentsCaptureData,
|
||||||
|
types::PaymentsResponseData,
|
||||||
|
> for Braintree
|
||||||
|
{
|
||||||
|
// Not Implemented (R)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl
|
||||||
|
services::ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>
|
||||||
|
for Braintree
|
||||||
|
{
|
||||||
|
fn get_headers(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsSyncRouterData,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
let mut headers = vec![
|
||||||
|
(
|
||||||
|
headers::CONTENT_TYPE.to_string(),
|
||||||
|
types::PaymentsSyncType::get_content_type(self).to_string(),
|
||||||
|
),
|
||||||
|
(headers::X_ROUTER.to_string(), "test".to_string()),
|
||||||
|
(headers::X_API_VERSION.to_string(), "6".to_string()),
|
||||||
|
(headers::ACCEPT.to_string(), "application/json".to_string()),
|
||||||
|
];
|
||||||
|
let mut api_key = self.get_auth_header(&req.connector_auth_type)?;
|
||||||
|
headers.append(&mut api_key);
|
||||||
|
Ok(headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_content_type(&self) -> &'static str {
|
||||||
|
"application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsSyncRouterData,
|
||||||
|
connectors: Connectors,
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
let auth_type = braintree::BraintreeAuthType::try_from(&req.connector_auth_type)
|
||||||
|
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
|
||||||
|
let connector_payment_id = req.request.connector_transaction_id.clone();
|
||||||
|
Ok(format!(
|
||||||
|
"{}/merchants/{}/transactions/{}",
|
||||||
|
self.base_url(connectors),
|
||||||
|
auth_type.merchant_account,
|
||||||
|
connector_payment_id
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_request(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsSyncRouterData,
|
||||||
|
connectors: Connectors,
|
||||||
|
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
|
||||||
|
Ok(Some(
|
||||||
|
services::RequestBuilder::new()
|
||||||
|
.method(services::Method::Get)
|
||||||
|
.url(&types::PaymentsSyncType::get_url(self, req, connectors)?)
|
||||||
|
.headers(types::PaymentsSyncType::get_headers(self, req)?)
|
||||||
|
.body(types::PaymentsSyncType::get_request_body(self, req)?)
|
||||||
|
.build(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_error_response(
|
||||||
|
&self,
|
||||||
|
res: Bytes,
|
||||||
|
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
|
||||||
|
let response: braintree::ErrorResponse = res
|
||||||
|
.parse_struct("Error Response")
|
||||||
|
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
|
||||||
|
|
||||||
|
Ok(ErrorResponse {
|
||||||
|
code: consts::NO_ERROR_CODE.to_string(),
|
||||||
|
message: response.api_error_response.message,
|
||||||
|
reason: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(
|
||||||
|
&self,
|
||||||
|
_req: &types::PaymentsSyncRouterData,
|
||||||
|
) -> CustomResult<Option<String>, errors::ConnectorError> {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_response(
|
||||||
|
&self,
|
||||||
|
data: &types::PaymentsSyncRouterData,
|
||||||
|
res: Response,
|
||||||
|
) -> CustomResult<types::PaymentsSyncRouterData, errors::ConnectorError> {
|
||||||
|
logger::debug!(payment_sync_response=?res);
|
||||||
|
let response: braintree::BraintreePaymentsResponse = res
|
||||||
|
.response
|
||||||
|
.parse_struct("braintree PaymentsResponse")
|
||||||
|
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
|
||||||
|
types::RouterData::try_from(types::ResponseRouterData {
|
||||||
|
response,
|
||||||
|
data: data.clone(),
|
||||||
|
http_code: res.status_code,
|
||||||
|
})
|
||||||
|
.change_context(errors::ConnectorError::ResponseHandlingFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl
|
||||||
|
services::ConnectorIntegration<
|
||||||
|
api::Authorize,
|
||||||
|
types::PaymentsAuthorizeData,
|
||||||
|
types::PaymentsResponseData,
|
||||||
|
> for Braintree
|
||||||
|
{
|
||||||
|
fn get_headers(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
let mut headers = vec![
|
||||||
|
(
|
||||||
|
headers::CONTENT_TYPE.to_string(),
|
||||||
|
types::PaymentsAuthorizeType::get_content_type(self).to_string(),
|
||||||
|
),
|
||||||
|
(headers::X_ROUTER.to_string(), "test".to_string()),
|
||||||
|
(headers::X_API_VERSION.to_string(), "6".to_string()),
|
||||||
|
(headers::ACCEPT.to_string(), "application/json".to_string()),
|
||||||
|
];
|
||||||
|
let mut api_key = self.get_auth_header(&req.connector_auth_type)?;
|
||||||
|
headers.append(&mut api_key);
|
||||||
|
Ok(headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
|
connectors: Connectors,
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
let auth_type = BraintreeAuthType::try_from(&req.connector_auth_type)
|
||||||
|
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
|
||||||
|
|
||||||
|
Ok(format!(
|
||||||
|
"{}merchants/{}/transactions",
|
||||||
|
self.base_url(connectors),
|
||||||
|
auth_type.merchant_account
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_request(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
|
connectors: Connectors,
|
||||||
|
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
|
||||||
|
Ok(Some(
|
||||||
|
services::RequestBuilder::new()
|
||||||
|
.method(services::Method::Post)
|
||||||
|
.url(&types::PaymentsAuthorizeType::get_url(
|
||||||
|
self, req, connectors,
|
||||||
|
)?)
|
||||||
|
.headers(types::PaymentsAuthorizeType::get_headers(self, req)?)
|
||||||
|
.body(types::PaymentsAuthorizeType::get_request_body(self, req)?)
|
||||||
|
.build(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(
|
||||||
|
&self,
|
||||||
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
|
) -> CustomResult<Option<String>, errors::ConnectorError> {
|
||||||
|
let braintree_req =
|
||||||
|
utils::Encode::<braintree::BraintreePaymentsRequest>::convert_and_encode(req)
|
||||||
|
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
|
||||||
|
Ok(Some(braintree_req))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_response(
|
||||||
|
&self,
|
||||||
|
data: &types::PaymentsAuthorizeRouterData,
|
||||||
|
res: Response,
|
||||||
|
) -> CustomResult<types::PaymentsAuthorizeRouterData, errors::ConnectorError> {
|
||||||
|
let response: braintree::BraintreePaymentsResponse = res
|
||||||
|
.response
|
||||||
|
.parse_struct("Braintree Payments Response")
|
||||||
|
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
|
||||||
|
logger::debug!(braintreepayments_create_response=?response);
|
||||||
|
types::ResponseRouterData {
|
||||||
|
response,
|
||||||
|
data: data.clone(),
|
||||||
|
http_code: res.status_code,
|
||||||
|
}
|
||||||
|
.try_into()
|
||||||
|
.change_context(errors::ConnectorError::ResponseHandlingFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_error_response(
|
||||||
|
&self,
|
||||||
|
res: Bytes,
|
||||||
|
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
|
||||||
|
logger::debug!(braintreepayments_create_response=?res);
|
||||||
|
|
||||||
|
let response: braintree::ErrorResponse = res
|
||||||
|
.parse_struct("ErrorResponse")
|
||||||
|
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
|
||||||
|
Ok(ErrorResponse {
|
||||||
|
code: consts::NO_ERROR_CODE.to_string(),
|
||||||
|
message: response.api_error_response.message,
|
||||||
|
reason: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl
|
||||||
|
services::ConnectorIntegration<
|
||||||
|
api::Void,
|
||||||
|
types::PaymentsCancelData,
|
||||||
|
types::PaymentsResponseData,
|
||||||
|
> for Braintree
|
||||||
|
{
|
||||||
|
fn get_headers(
|
||||||
|
&self,
|
||||||
|
_req: &types::PaymentsCancelRouterData,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_content_type(&self) -> &'static str {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(
|
||||||
|
&self,
|
||||||
|
_req: &types::PaymentsCancelRouterData,
|
||||||
|
_connectors: Connectors,
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(
|
||||||
|
&self,
|
||||||
|
_req: &types::PaymentsCancelRouterData,
|
||||||
|
) -> CustomResult<Option<String>, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
fn build_request(
|
||||||
|
&self,
|
||||||
|
_req: &types::PaymentsCancelRouterData,
|
||||||
|
_connectors: Connectors,
|
||||||
|
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_response(
|
||||||
|
&self,
|
||||||
|
_data: &types::PaymentsCancelRouterData,
|
||||||
|
_res: Response,
|
||||||
|
) -> CustomResult<types::PaymentsCancelRouterData, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_error_response(
|
||||||
|
&self,
|
||||||
|
_res: Bytes,
|
||||||
|
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl api::Refund for Braintree {}
|
||||||
|
impl api::RefundExecute for Braintree {}
|
||||||
|
impl api::RefundSync for Braintree {}
|
||||||
|
|
||||||
|
impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsResponseData>
|
||||||
|
for Braintree
|
||||||
|
{
|
||||||
|
fn get_headers(
|
||||||
|
&self,
|
||||||
|
req: &types::RefundsRouterData<api::Execute>,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
let mut headers = vec![
|
||||||
|
(
|
||||||
|
headers::CONTENT_TYPE.to_string(),
|
||||||
|
types::RefundExecuteType::get_content_type(self).to_string(),
|
||||||
|
),
|
||||||
|
(headers::X_ROUTER.to_string(), "test".to_string()),
|
||||||
|
(headers::X_API_VERSION.to_string(), "6".to_string()),
|
||||||
|
(headers::ACCEPT.to_string(), "application/json".to_string()),
|
||||||
|
];
|
||||||
|
let mut api_key = self.get_auth_header(&req.connector_auth_type)?;
|
||||||
|
headers.append(&mut api_key);
|
||||||
|
Ok(headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_content_type(&self) -> &'static str {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(
|
||||||
|
&self,
|
||||||
|
_req: &types::RefundsRouterData<api::Execute>,
|
||||||
|
_connectors: Connectors,
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(
|
||||||
|
&self,
|
||||||
|
req: &types::RefundsRouterData<api::Execute>,
|
||||||
|
) -> CustomResult<Option<String>, errors::ConnectorError> {
|
||||||
|
let braintree_req = utils::Encode::<braintree::RefundRequest>::convert_and_url_encode(req)
|
||||||
|
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
|
||||||
|
Ok(Some(braintree_req))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_request(
|
||||||
|
&self,
|
||||||
|
req: &types::RefundsRouterData<api::Execute>,
|
||||||
|
connectors: Connectors,
|
||||||
|
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
|
||||||
|
let request = services::RequestBuilder::new()
|
||||||
|
.method(services::Method::Post)
|
||||||
|
.url(&types::RefundExecuteType::get_url(self, req, connectors)?)
|
||||||
|
.headers(types::RefundExecuteType::get_headers(self, req)?)
|
||||||
|
.body(types::RefundExecuteType::get_request_body(self, req)?)
|
||||||
|
.build();
|
||||||
|
Ok(Some(request))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_response(
|
||||||
|
&self,
|
||||||
|
data: &types::RefundsRouterData<api::Execute>,
|
||||||
|
res: Response,
|
||||||
|
) -> CustomResult<types::RefundsRouterData<api::Execute>, errors::ConnectorError> {
|
||||||
|
logger::debug!(target: "router::connector::braintree", response=?res);
|
||||||
|
let response: braintree::RefundResponse = res
|
||||||
|
.response
|
||||||
|
.parse_struct("braintree RefundResponse")
|
||||||
|
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
|
||||||
|
types::ResponseRouterData {
|
||||||
|
response,
|
||||||
|
data: data.clone(),
|
||||||
|
http_code: res.status_code,
|
||||||
|
}
|
||||||
|
.try_into()
|
||||||
|
.change_context(errors::ConnectorError::ResponseHandlingFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_error_response(
|
||||||
|
&self,
|
||||||
|
_res: Bytes,
|
||||||
|
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl services::ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponseData>
|
||||||
|
for Braintree
|
||||||
|
{
|
||||||
|
fn get_headers(
|
||||||
|
&self,
|
||||||
|
_req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_content_type(&self) -> &'static str {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(
|
||||||
|
&self,
|
||||||
|
_req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
_connectors: Connectors,
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_error_response(
|
||||||
|
&self,
|
||||||
|
_res: Bytes,
|
||||||
|
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(
|
||||||
|
&self,
|
||||||
|
_req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
) -> CustomResult<Option<String>, errors::ConnectorError> {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_request(
|
||||||
|
&self,
|
||||||
|
_req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
_connectors: Connectors,
|
||||||
|
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_response(
|
||||||
|
&self,
|
||||||
|
data: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
res: Response,
|
||||||
|
) -> CustomResult<
|
||||||
|
types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
|
||||||
|
errors::ConnectorError,
|
||||||
|
> {
|
||||||
|
logger::debug!(target: "router::connector::braintree", response=?res);
|
||||||
|
let response: braintree::RefundResponse = res
|
||||||
|
.response
|
||||||
|
.parse_struct("braintree RefundResponse")
|
||||||
|
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
|
||||||
|
types::ResponseRouterData {
|
||||||
|
response,
|
||||||
|
data: data.clone(),
|
||||||
|
http_code: res.status_code,
|
||||||
|
}
|
||||||
|
.try_into()
|
||||||
|
.change_context(errors::ConnectorError::ResponseHandlingFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl api::IncomingWebhook for Braintree {
|
||||||
|
fn get_webhook_object_reference_id(
|
||||||
|
&self,
|
||||||
|
_body: &[u8],
|
||||||
|
) -> CustomResult<String, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_webhook_event_type(
|
||||||
|
&self,
|
||||||
|
_body: &[u8],
|
||||||
|
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_webhook_resource_object(
|
||||||
|
&self,
|
||||||
|
_body: &[u8],
|
||||||
|
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
|
||||||
|
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl services::ConnectorRedirectResponse for Braintree {
|
||||||
|
fn get_flow_type(
|
||||||
|
&self,
|
||||||
|
_query_params: &str,
|
||||||
|
) -> CustomResult<payments::CallConnectorAction, errors::ConnectorError> {
|
||||||
|
Ok(payments::CallConnectorAction::Trigger)
|
||||||
|
}
|
||||||
|
}
|
||||||
253
crates/router/src/connector/braintree/transformers.rs
Normal file
253
crates/router/src/connector/braintree/transformers.rs
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
core::errors,
|
||||||
|
pii::PeekInterface,
|
||||||
|
types::{self, api, storage::enums},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
|
pub struct DeviceData;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
|
pub struct PaymentOptions {
|
||||||
|
submit_for_settlement: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
|
pub struct BraintreePaymentsRequest {
|
||||||
|
transaction: TransactionBody,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct TransactionBody {
|
||||||
|
amount: String,
|
||||||
|
device_data: DeviceData,
|
||||||
|
options: PaymentOptions,
|
||||||
|
credit_card: Card,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
kind: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Card {
|
||||||
|
number: String,
|
||||||
|
expiration_month: String,
|
||||||
|
expiration_year: String,
|
||||||
|
cvv: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&types::PaymentsAuthorizeRouterData> for BraintreePaymentsRequest {
|
||||||
|
type Error = error_stack::Report<errors::ValidateError>;
|
||||||
|
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
||||||
|
match item.request.payment_method_data {
|
||||||
|
api::PaymentMethod::Card(ref ccard) => {
|
||||||
|
let braintree_payment_request = TransactionBody {
|
||||||
|
amount: item.request.amount.to_string(),
|
||||||
|
device_data: DeviceData {},
|
||||||
|
options: PaymentOptions {
|
||||||
|
submit_for_settlement: true,
|
||||||
|
},
|
||||||
|
credit_card: Card {
|
||||||
|
number: ccard.card_number.peek().clone(),
|
||||||
|
expiration_month: ccard.card_exp_month.peek().clone(),
|
||||||
|
expiration_year: ccard.card_exp_year.peek().clone(),
|
||||||
|
cvv: ccard.card_cvc.peek().clone(),
|
||||||
|
},
|
||||||
|
kind: "sale".to_string(),
|
||||||
|
};
|
||||||
|
Ok(BraintreePaymentsRequest {
|
||||||
|
transaction: braintree_payment_request,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => Err(errors::ValidateError.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BraintreeAuthType {
|
||||||
|
pub(super) api_key: String,
|
||||||
|
pub(super) merchant_account: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&types::ConnectorAuthType> for BraintreeAuthType {
|
||||||
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
|
fn try_from(item: &types::ConnectorAuthType) -> Result<Self, Self::Error> {
|
||||||
|
if let types::ConnectorAuthType::BodyKey { api_key, key1 } = item {
|
||||||
|
Ok(Self {
|
||||||
|
api_key: api_key.to_string(),
|
||||||
|
merchant_account: key1.to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err(errors::ConnectorError::FailedToObtainAuthType)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Eq, PartialEq)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum BraintreePaymentStatus {
|
||||||
|
Succeeded,
|
||||||
|
Failed,
|
||||||
|
Authorized,
|
||||||
|
AuthorizedExpired,
|
||||||
|
ProcessorDeclined,
|
||||||
|
GatewayRejected,
|
||||||
|
Voided,
|
||||||
|
SubmittedForSettlement,
|
||||||
|
Settling,
|
||||||
|
Settled,
|
||||||
|
SettlementPending,
|
||||||
|
SettlementDeclined,
|
||||||
|
SettlementConfirmed,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for BraintreePaymentStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
BraintreePaymentStatus::Settling
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<BraintreePaymentStatus> for enums::AttemptStatus {
|
||||||
|
fn from(item: BraintreePaymentStatus) -> Self {
|
||||||
|
match item {
|
||||||
|
BraintreePaymentStatus::Succeeded => enums::AttemptStatus::Charged,
|
||||||
|
BraintreePaymentStatus::AuthorizedExpired => enums::AttemptStatus::AuthorizationFailed,
|
||||||
|
BraintreePaymentStatus::Failed
|
||||||
|
| BraintreePaymentStatus::GatewayRejected
|
||||||
|
| BraintreePaymentStatus::ProcessorDeclined
|
||||||
|
| BraintreePaymentStatus::SettlementDeclined => enums::AttemptStatus::Failure,
|
||||||
|
BraintreePaymentStatus::Authorized => enums::AttemptStatus::Authorized,
|
||||||
|
BraintreePaymentStatus::Voided => enums::AttemptStatus::Voided,
|
||||||
|
_ => enums::AttemptStatus::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, T>
|
||||||
|
TryFrom<types::ResponseRouterData<F, BraintreePaymentsResponse, T, types::PaymentsResponseData>>
|
||||||
|
for types::RouterData<F, T, types::PaymentsResponseData>
|
||||||
|
{
|
||||||
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
|
fn try_from(
|
||||||
|
item: types::ResponseRouterData<
|
||||||
|
F,
|
||||||
|
BraintreePaymentsResponse,
|
||||||
|
T,
|
||||||
|
types::PaymentsResponseData,
|
||||||
|
>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
Ok(types::RouterData {
|
||||||
|
response: Ok(types::PaymentsResponseData {
|
||||||
|
resource_id: types::ResponseId::ConnectorTransactionId(
|
||||||
|
item.response.transaction.id,
|
||||||
|
),
|
||||||
|
redirection_data: None,
|
||||||
|
redirect: false,
|
||||||
|
}),
|
||||||
|
..item.data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Deserialize, Eq, PartialEq)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct BraintreePaymentsResponse {
|
||||||
|
transaction: TransactionResponse,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Deserialize, Eq, PartialEq)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct TransactionResponse {
|
||||||
|
id: String,
|
||||||
|
currency_iso_code: String,
|
||||||
|
amount: String,
|
||||||
|
status: BraintreePaymentStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct ErrorResponse {
|
||||||
|
pub api_error_response: ApiErrorResponse,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Deserialize, Eq, PartialEq)]
|
||||||
|
pub struct ApiErrorResponse {
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Serialize)]
|
||||||
|
pub struct RefundRequest {}
|
||||||
|
|
||||||
|
impl<F> TryFrom<&types::RefundsRouterData<F>> for RefundRequest {
|
||||||
|
type Error = error_stack::Report<errors::ParsingError>;
|
||||||
|
fn try_from(_item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
||||||
|
Ok(RefundRequest {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub enum RefundStatus {
|
||||||
|
Succeeded,
|
||||||
|
Failed,
|
||||||
|
Processing,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RefundStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
RefundStatus::Processing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<self::RefundStatus> for enums::RefundStatus {
|
||||||
|
fn from(item: self::RefundStatus) -> Self {
|
||||||
|
match item {
|
||||||
|
self::RefundStatus::Succeeded => enums::RefundStatus::Success,
|
||||||
|
self::RefundStatus::Failed => enums::RefundStatus::Failure,
|
||||||
|
self::RefundStatus::Processing => enums::RefundStatus::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Deserialize)]
|
||||||
|
pub struct RefundResponse {
|
||||||
|
pub id: String,
|
||||||
|
pub status: RefundStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<types::RefundsResponseRouterData<api::Execute, RefundResponse>>
|
||||||
|
for types::RefundsRouterData<api::Execute>
|
||||||
|
{
|
||||||
|
type Error = error_stack::Report<errors::ParsingError>;
|
||||||
|
fn try_from(
|
||||||
|
item: types::RefundsResponseRouterData<api::Execute, RefundResponse>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
Ok(types::RouterData {
|
||||||
|
response: Ok(types::RefundsResponseData {
|
||||||
|
connector_refund_id: item.response.id,
|
||||||
|
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::ParsingError>;
|
||||||
|
fn try_from(
|
||||||
|
item: types::RefundsResponseRouterData<api::RSync, RefundResponse>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
Ok(types::RouterData {
|
||||||
|
response: Ok(types::RefundsResponseData {
|
||||||
|
connector_refund_id: item.response.id,
|
||||||
|
refund_status: enums::RefundStatus::from(item.response.status),
|
||||||
|
}),
|
||||||
|
..item.data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -57,6 +57,8 @@ pub mod headers {
|
|||||||
pub const CONTENT_TYPE: &str = "Content-Type";
|
pub const CONTENT_TYPE: &str = "Content-Type";
|
||||||
pub const X_ROUTER: &str = "X-router";
|
pub const X_ROUTER: &str = "X-router";
|
||||||
pub const AUTHORIZATION: &str = "Authorization";
|
pub const AUTHORIZATION: &str = "Authorization";
|
||||||
|
pub const ACCEPT: &str = "Accept";
|
||||||
|
pub const X_API_VERSION: &str = "X-ApiVersion";
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_app(
|
pub fn mk_app(
|
||||||
|
|||||||
@ -143,6 +143,7 @@ impl ConnectorData {
|
|||||||
"aci" => Ok(Box::new(&connector::Aci)),
|
"aci" => Ok(Box::new(&connector::Aci)),
|
||||||
"checkout" => Ok(Box::new(&connector::Checkout)),
|
"checkout" => Ok(Box::new(&connector::Checkout)),
|
||||||
"authorizedotnet" => Ok(Box::new(&connector::Authorizedotnet)),
|
"authorizedotnet" => Ok(Box::new(&connector::Authorizedotnet)),
|
||||||
|
"braintree" => Ok(Box::new(&connector::Braintree)),
|
||||||
_ => Err(report!(errors::UnexpectedError)
|
_ => Err(report!(errors::UnexpectedError)
|
||||||
.attach_printable(format!("invalid connector name: {connector_name}")))
|
.attach_printable(format!("invalid connector name: {connector_name}")))
|
||||||
.change_context(errors::ConnectorError::InvalidConnectorName)
|
.change_context(errors::ConnectorError::InvalidConnectorName)
|
||||||
|
|||||||
@ -6,6 +6,7 @@ pub enum Connector {
|
|||||||
Checkout,
|
Checkout,
|
||||||
Aci,
|
Aci,
|
||||||
Authorizedotnet,
|
Authorizedotnet,
|
||||||
|
Braintree,
|
||||||
#[default]
|
#[default]
|
||||||
Dummy,
|
Dummy,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user