refactor(connector): airwallex convert init payment to preprocessing (#4842)

This commit is contained in:
Hrithikesh
2024-06-03 16:21:49 +05:30
committed by GitHub
parent 67f017f6f0
commit e5da133fe0
22 changed files with 851 additions and 55 deletions

View File

@ -18,7 +18,7 @@ use crate::{
payments,
},
events::connector_api_logs::ConnectorEvent,
headers, logger, routes,
headers, logger,
services::{
self,
request::{self, Mask},
@ -27,7 +27,6 @@ use crate::{
types::{
self,
api::{self, ConnectorCommon, ConnectorCommonExt},
transformers::ForeignFrom,
ErrorResponse, Response, RouterData,
},
utils::{crypto, BytesExt},
@ -123,6 +122,7 @@ impl ConnectorValidation for Airwallex {
}
impl api::Payment for Airwallex {}
impl api::PaymentsPreProcessing for Airwallex {}
impl api::PaymentsCompleteAuthorize for Airwallex {}
impl api::MandateSetup for Airwallex {}
impl
@ -242,14 +242,14 @@ impl ConnectorIntegration<api::AccessTokenAuth, types::AccessTokenRequestData, t
impl
ConnectorIntegration<
api::InitPayment,
types::PaymentsAuthorizeData,
api::PreProcessing,
types::PaymentsPreProcessingData,
types::PaymentsResponseData,
> for Airwallex
{
fn get_headers(
&self,
req: &types::PaymentsInitRouterData,
req: &types::PaymentsPreProcessingRouterData,
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
self.build_headers(req, connectors)
@ -261,7 +261,7 @@ impl
fn get_url(
&self,
_req: &types::PaymentsInitRouterData,
_req: &types::PaymentsPreProcessingRouterData,
connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
Ok(format!(
@ -273,7 +273,7 @@ impl
fn get_request_body(
&self,
req: &types::PaymentsInitRouterData,
req: &types::PaymentsPreProcessingRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let req_obj = airwallex::AirwallexIntentRequest::try_from(req)?;
@ -282,16 +282,20 @@ impl
fn build_request(
&self,
req: &types::PaymentsInitRouterData,
req: &types::PaymentsPreProcessingRouterData,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
Ok(Some(
services::RequestBuilder::new()
.method(services::Method::Post)
.url(&types::PaymentsInitType::get_url(self, req, connectors)?)
.url(&types::PaymentsPreProcessingType::get_url(
self, req, connectors,
)?)
.attach_default_headers()
.headers(types::PaymentsInitType::get_headers(self, req, connectors)?)
.set_body(types::PaymentsInitType::get_request_body(
.headers(types::PaymentsPreProcessingType::get_headers(
self, req, connectors,
)?)
.set_body(types::PaymentsPreProcessingType::get_request_body(
self, req, connectors,
)?)
.build(),
@ -300,10 +304,10 @@ impl
fn handle_response(
&self,
data: &types::PaymentsInitRouterData,
data: &types::PaymentsPreProcessingRouterData,
event_builder: Option<&mut ConnectorEvent>,
res: Response,
) -> CustomResult<types::PaymentsInitRouterData, errors::ConnectorError> {
) -> CustomResult<types::PaymentsPreProcessingRouterData, errors::ConnectorError> {
let response: airwallex::AirwallexPaymentsResponse = res
.response
.parse_struct("airwallex AirwallexPaymentsResponse")
@ -335,36 +339,6 @@ impl api::PaymentAuthorize for Airwallex {}
impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::PaymentsResponseData>
for Airwallex
{
async fn execute_pretasks(
&self,
router_data: &mut types::PaymentsAuthorizeRouterData,
app_state: &routes::AppState,
) -> CustomResult<(), errors::ConnectorError> {
let integ: Box<
&(dyn ConnectorIntegration<
api::InitPayment,
types::PaymentsAuthorizeData,
types::PaymentsResponseData,
> + Send
+ Sync
+ 'static),
> = Box::new(&Self);
let authorize_data = &types::PaymentsInitRouterData::foreign_from((
&router_data.to_owned(),
router_data.request.clone(),
));
let resp = services::execute_connector_processing_step(
app_state,
integ,
authorize_data,
payments::CallConnectorAction::Trigger,
None,
)
.await?;
router_data.reference_id = resp.reference_id;
Ok(())
}
fn get_headers(
&self,
req: &types::PaymentsAuthorizeRouterData,

View File

@ -41,13 +41,26 @@ pub struct AirwallexIntentRequest {
//ID created in merchant's order system that corresponds to this PaymentIntent.
merchant_order_id: String,
}
impl TryFrom<&types::PaymentsInitRouterData> for AirwallexIntentRequest {
impl TryFrom<&types::PaymentsPreProcessingRouterData> for AirwallexIntentRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsInitRouterData) -> Result<Self, Self::Error> {
fn try_from(item: &types::PaymentsPreProcessingRouterData) -> Result<Self, Self::Error> {
// amount and currency will always be Some since PaymentsPreProcessingData is constructed using PaymentsAuthorizeData
let amount = item
.request
.amount
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "amount",
})?;
let currency =
item.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?;
Ok(Self {
request_id: Uuid::new_v4().to_string(),
amount: utils::to_currency_base_unit(item.request.amount, item.request.currency)?,
currency: item.request.currency,
amount: utils::to_currency_base_unit(amount, currency)?,
currency,
merchant_order_id: item.connector_request_reference_id.clone(),
})
}

View File

@ -1885,6 +1885,14 @@ where
dyn api::Connector:
services::api::ConnectorIntegration<F, Req, router_types::PaymentsResponseData>,
{
if !is_operation_complete_authorize(&operation)
&& connector
.connector_name
.is_pre_processing_required_before_authorize()
{
router_data = router_data.preprocessing_steps(state, connector).await?;
return Ok((router_data, should_continue_payment));
}
//TODO: For ACH transfers, if preprocessing_step is not required for connectors encountered in future, add the check
let router_data_and_should_continue_payment = match payment_data.payment_method_data.clone() {
Some(api_models::payments::PaymentMethodData::BankTransfer(data)) => match data.deref() {

View File

@ -928,7 +928,6 @@ impl<const T: u8>
default_imp_for_pre_processing_steps!(
connector::Aci,
connector::Airwallex,
connector::Authorizedotnet,
connector::Bambora,
connector::Billwerk,

View File

@ -322,13 +322,14 @@ pub async fn authorize_preprocessing_steps<F: Clone>(
),
],
);
let authorize_router_data = helpers::router_data_type_conversion::<_, F, _, _, _, _>(
let mut authorize_router_data = helpers::router_data_type_conversion::<_, F, _, _, _, _>(
resp.clone(),
router_data.request.to_owned(),
resp.response,
);
if connector.connector_name == api_models::enums::Connector::Airwallex {
authorize_router_data.reference_id = resp.reference_id;
}
Ok(authorize_router_data)
} else {
Ok(router_data.clone())