mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 10:06:32 +08:00 
			
		
		
		
	fix(connector_customer): create connector_customer on requirement basis (#1097)
This commit is contained in:
		| @ -120,16 +120,9 @@ where | |||||||
|         get_connector_tokenization_action(state, &operation, payment_data, &validate_result) |         get_connector_tokenization_action(state, &operation, payment_data, &validate_result) | ||||||
|             .await?; |             .await?; | ||||||
|  |  | ||||||
|     let connector_string = connector |     let updated_customer = call_create_connector_customer_if_required( | ||||||
|         .as_ref() |  | ||||||
|         .and_then(|connector_type| match connector_type { |  | ||||||
|             api::ConnectorCallType::Single(connector) => Some(connector.connector_name.to_string()), |  | ||||||
|             _ => None, |  | ||||||
|         }); |  | ||||||
|  |  | ||||||
|     let updated_customer = call_create_connector_customer( |  | ||||||
|         state, |         state, | ||||||
|         &connector_string, |         &payment_data.payment_attempt.connector.clone(), | ||||||
|         &customer, |         &customer, | ||||||
|         &merchant_account, |         &merchant_account, | ||||||
|         &mut payment_data, |         &mut payment_data, | ||||||
| @ -612,7 +605,7 @@ where | |||||||
|     Ok(payment_data) |     Ok(payment_data) | ||||||
| } | } | ||||||
|  |  | ||||||
| pub async fn call_create_connector_customer<F, Req>( | pub async fn call_create_connector_customer_if_required<F, Req>( | ||||||
|     state: &AppState, |     state: &AppState, | ||||||
|     connector_name: &Option<String>, |     connector_name: &Option<String>, | ||||||
|     customer: &Option<storage::Customer>, |     customer: &Option<storage::Customer>, | ||||||
| @ -640,14 +633,31 @@ where | |||||||
|                 connector_name, |                 connector_name, | ||||||
|                 api::GetToken::Connector, |                 api::GetToken::Connector, | ||||||
|             )?; |             )?; | ||||||
|  |             let (is_eligible, connector_customer_id, connector_customer_map) = | ||||||
|  |                 customers::should_call_connector_create_customer(state, &connector, customer)?; | ||||||
|  |  | ||||||
|  |             if is_eligible { | ||||||
|  |                 // Create customer at connector and update the customer table to store this data | ||||||
|                 let router_data = payment_data |                 let router_data = payment_data | ||||||
|                 .construct_router_data(state, connector.connector.id(), merchant_account, customer) |                     .construct_router_data( | ||||||
|  |                         state, | ||||||
|  |                         connector.connector.id(), | ||||||
|  |                         merchant_account, | ||||||
|  |                         customer, | ||||||
|  |                     ) | ||||||
|                     .await?; |                     .await?; | ||||||
|  |  | ||||||
|                 let (connector_customer, customer_update) = router_data |                 let (connector_customer, customer_update) = router_data | ||||||
|                 .create_connector_customer(state, &connector, customer) |                     .create_connector_customer(state, &connector, connector_customer_map) | ||||||
|                     .await?; |                     .await?; | ||||||
|  |  | ||||||
|                 payment_data.connector_customer_id = connector_customer; |                 payment_data.connector_customer_id = connector_customer; | ||||||
|                 Ok(customer_update) |                 Ok(customer_update) | ||||||
|  |             } else { | ||||||
|  |                 // Customer already created in previous calls use the same value, no need to update | ||||||
|  |                 payment_data.connector_customer_id = connector_customer_id; | ||||||
|  |                 Ok(None) | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         None => Ok(None), |         None => Ok(None), | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -15,14 +15,10 @@ use crate::{ | |||||||
| pub async fn create_connector_customer<F: Clone, T: Clone>( | pub async fn create_connector_customer<F: Clone, T: Clone>( | ||||||
|     state: &AppState, |     state: &AppState, | ||||||
|     connector: &api::ConnectorData, |     connector: &api::ConnectorData, | ||||||
|     customer: &Option<storage::Customer>, |  | ||||||
|     router_data: &types::RouterData<F, T, types::PaymentsResponseData>, |     router_data: &types::RouterData<F, T, types::PaymentsResponseData>, | ||||||
|     customer_request_data: types::ConnectorCustomerData, |     customer_request_data: types::ConnectorCustomerData, | ||||||
|  |     connector_customer_map: Option<serde_json::Map<String, serde_json::Value>>, | ||||||
| ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { | ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { | ||||||
|     let (is_eligible, connector_customer_id, connector_customer_map) = |  | ||||||
|         should_call_connector_create_customer(state, connector, customer)?; |  | ||||||
|  |  | ||||||
|     if is_eligible { |  | ||||||
|     let connector_integration: services::BoxedConnectorIntegration< |     let connector_integration: services::BoxedConnectorIntegration< | ||||||
|         '_, |         '_, | ||||||
|         api::CreateConnectorCustomer, |         api::CreateConnectorCustomer, | ||||||
| @ -67,6 +63,7 @@ pub async fn create_connector_customer<F: Clone, T: Clone>( | |||||||
|             None |             None | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     let update_customer = update_connector_customer_in_customers( |     let update_customer = update_connector_customer_in_customers( | ||||||
|         connector, |         connector, | ||||||
|         connector_customer_map, |         connector_customer_map, | ||||||
| @ -74,9 +71,6 @@ pub async fn create_connector_customer<F: Clone, T: Clone>( | |||||||
|     ) |     ) | ||||||
|     .await?; |     .await?; | ||||||
|     Ok((connector_customer_id, update_customer)) |     Ok((connector_customer_id, update_customer)) | ||||||
|     } else { |  | ||||||
|         Ok((connector_customer_id, None)) |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| type CreateCustomerCheck = ( | type CreateCustomerCheck = ( | ||||||
| @ -96,6 +90,7 @@ pub fn should_call_connector_create_customer( | |||||||
|         .connector_customer |         .connector_customer | ||||||
|         .connector_list |         .connector_list | ||||||
|         .contains(&connector.connector_name); |         .contains(&connector.connector_name); | ||||||
|  |  | ||||||
|     if connector_customer_filter { |     if connector_customer_filter { | ||||||
|         match customer { |         match customer { | ||||||
|             Some(customer) => match &customer.connector_customer { |             Some(customer) => match &customer.connector_customer { | ||||||
|  | |||||||
| @ -74,7 +74,7 @@ pub trait Feature<F, T> { | |||||||
|         &self, |         &self, | ||||||
|         _state: &AppState, |         _state: &AppState, | ||||||
|         _connector: &api::ConnectorData, |         _connector: &api::ConnectorData, | ||||||
|         _customer: &Option<storage::Customer>, |         _connector_customer_map: Option<serde_json::Map<String, serde_json::Value>>, | ||||||
|     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> |     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> | ||||||
|     where |     where | ||||||
|         F: Clone, |         F: Clone, | ||||||
|  | |||||||
| @ -100,14 +100,14 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu | |||||||
|         &self, |         &self, | ||||||
|         state: &AppState, |         state: &AppState, | ||||||
|         connector: &api::ConnectorData, |         connector: &api::ConnectorData, | ||||||
|         customer: &Option<storage::Customer>, |         connector_customer_map: Option<serde_json::Map<String, serde_json::Value>>, | ||||||
|     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { |     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { | ||||||
|         customers::create_connector_customer( |         customers::create_connector_customer( | ||||||
|             state, |             state, | ||||||
|             connector, |             connector, | ||||||
|             customer, |  | ||||||
|             self, |             self, | ||||||
|             types::ConnectorCustomerData::try_from(self.request.to_owned())?, |             types::ConnectorCustomerData::try_from(self.request.to_owned())?, | ||||||
|  |             connector_customer_map, | ||||||
|         ) |         ) | ||||||
|         .await |         .await | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -84,14 +84,14 @@ impl Feature<api::Verify, types::VerifyRequestData> for types::VerifyRouterData | |||||||
|         &self, |         &self, | ||||||
|         state: &AppState, |         state: &AppState, | ||||||
|         connector: &api::ConnectorData, |         connector: &api::ConnectorData, | ||||||
|         customer: &Option<storage::Customer>, |         connector_customer_map: Option<serde_json::Map<String, serde_json::Value>>, | ||||||
|     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { |     ) -> RouterResult<(Option<String>, Option<storage::CustomerUpdate>)> { | ||||||
|         customers::create_connector_customer( |         customers::create_connector_customer( | ||||||
|             state, |             state, | ||||||
|             connector, |             connector, | ||||||
|             customer, |  | ||||||
|             self, |             self, | ||||||
|             types::ConnectorCustomerData::try_from(self.request.to_owned())?, |             types::ConnectorCustomerData::try_from(self.request.to_owned())?, | ||||||
|  |             connector_customer_map, | ||||||
|         ) |         ) | ||||||
|         .await |         .await | ||||||
|     } |     } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Narayan Bhat
					Narayan Bhat