mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 01:57:45 +08:00 
			
		
		
		
	fix(core): store payment token in payment attempt (#146)
This commit is contained in:
		| @ -349,11 +349,12 @@ impl | |||||||
|         data: &types::PaymentsAuthorizeRouterData, |         data: &types::PaymentsAuthorizeRouterData, | ||||||
|         res: Response, |         res: Response, | ||||||
|     ) -> CustomResult<types::PaymentsAuthorizeRouterData, errors::ConnectorError> { |     ) -> CustomResult<types::PaymentsAuthorizeRouterData, errors::ConnectorError> { | ||||||
|  |         logger::debug!(stripe_payments_create_response=?res); | ||||||
|         let response: stripe::PaymentIntentResponse = res |         let response: stripe::PaymentIntentResponse = res | ||||||
|             .response |             .response | ||||||
|             .parse_struct("PaymentIntentResponse") |             .parse_struct("PaymentIntentResponse") | ||||||
|             .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; |             .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; | ||||||
|         logger::debug!(payments_create_response=?response); |  | ||||||
|         types::RouterData::try_from(types::ResponseRouterData { |         types::RouterData::try_from(types::ResponseRouterData { | ||||||
|             response, |             response, | ||||||
|             data: data.clone(), |             data: data.clone(), | ||||||
|  | |||||||
| @ -373,6 +373,7 @@ impl<F, T> | |||||||
|                     StripePaymentMethodOptions::Card { |                     StripePaymentMethodOptions::Card { | ||||||
|                         mandate_options, .. |                         mandate_options, .. | ||||||
|                     } => mandate_options.map(|mandate_options| mandate_options.reference), |                     } => mandate_options.map(|mandate_options| mandate_options.reference), | ||||||
|  |                     StripePaymentMethodOptions::Klarna {} => None, | ||||||
|                 }); |                 }); | ||||||
|  |  | ||||||
|         Ok(types::RouterData { |         Ok(types::RouterData { | ||||||
| @ -424,6 +425,7 @@ impl<F, T> | |||||||
|                     StripePaymentMethodOptions::Card { |                     StripePaymentMethodOptions::Card { | ||||||
|                         mandate_options, .. |                         mandate_options, .. | ||||||
|                     } => mandate_options.map(|mandate_option| mandate_option.reference), |                     } => mandate_options.map(|mandate_option| mandate_option.reference), | ||||||
|  |                     StripePaymentMethodOptions::Klarna {} => None, | ||||||
|                 }); |                 }); | ||||||
|  |  | ||||||
|         Ok(types::RouterData { |         Ok(types::RouterData { | ||||||
| @ -662,6 +664,7 @@ pub enum StripePaymentMethodOptions { | |||||||
|     Card { |     Card { | ||||||
|         mandate_options: Option<StripeMandateOptions>, |         mandate_options: Option<StripeMandateOptions>, | ||||||
|     }, |     }, | ||||||
|  |     Klarna {}, | ||||||
| } | } | ||||||
| // #[derive(Deserialize, Debug, Clone, Eq, PartialEq)] | // #[derive(Deserialize, Debug, Clone, Eq, PartialEq)] | ||||||
| // pub struct Card | // pub struct Card | ||||||
|  | |||||||
| @ -356,7 +356,6 @@ where | |||||||
|     Ok(response) |     Ok(response) | ||||||
| } | } | ||||||
|  |  | ||||||
| #[allow(dead_code)] |  | ||||||
| async fn call_multiple_connectors_service<F, Op, Req>( | async fn call_multiple_connectors_service<F, Op, Req>( | ||||||
|     state: &AppState, |     state: &AppState, | ||||||
|     merchant_account: &storage::MerchantAccount, |     merchant_account: &storage::MerchantAccount, | ||||||
| @ -521,15 +520,7 @@ pub fn should_call_connector<Op: Debug, F: Clone>( | |||||||
|     payment_data: &PaymentData<F>, |     payment_data: &PaymentData<F>, | ||||||
| ) -> bool { | ) -> bool { | ||||||
|     match format!("{:?}", operation).as_str() { |     match format!("{:?}", operation).as_str() { | ||||||
|         "PaymentConfirm" => { |         "PaymentConfirm" => true, | ||||||
|             payment_data |  | ||||||
|                 .payment_attempt |  | ||||||
|                 .authentication_type |  | ||||||
|                 .unwrap_or_default() |  | ||||||
|                 == enums::AuthenticationType::NoThreeDs |  | ||||||
|                 || payment_data.payment_attempt.payment_method |  | ||||||
|                     == Some(enums::PaymentMethodType::PayLater) |  | ||||||
|         } |  | ||||||
|         "PaymentStart" => { |         "PaymentStart" => { | ||||||
|             !matches!( |             !matches!( | ||||||
|                 payment_data.payment_intent.status, |                 payment_data.payment_intent.status, | ||||||
|  | |||||||
| @ -197,6 +197,7 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         let connector = payment_data.payment_attempt.connector.clone(); |         let connector = payment_data.payment_attempt.connector.clone(); | ||||||
|  |         let payment_token = payment_data.token.clone(); | ||||||
|  |  | ||||||
|         payment_data.payment_attempt = db |         payment_data.payment_attempt = db | ||||||
|             .update_payment_attempt( |             .update_payment_attempt( | ||||||
| @ -206,6 +207,7 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|                     payment_method, |                     payment_method, | ||||||
|                     browser_info, |                     browser_info, | ||||||
|                     connector, |                     connector, | ||||||
|  |                     payment_token, | ||||||
|                 }, |                 }, | ||||||
|                 storage_scheme, |                 storage_scheme, | ||||||
|             ) |             ) | ||||||
|  | |||||||
| @ -250,6 +250,23 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|             _ => None, |             _ => None, | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|  |         let payment_token = payment_data.token.clone(); | ||||||
|  |         let connector = payment_data.payment_attempt.connector.clone(); | ||||||
|  |  | ||||||
|  |         payment_data.payment_attempt = db | ||||||
|  |             .update_payment_attempt( | ||||||
|  |                 payment_data.payment_attempt, | ||||||
|  |                 storage::PaymentAttemptUpdate::UpdateTrackers { | ||||||
|  |                     payment_token, | ||||||
|  |                     connector, | ||||||
|  |                 }, | ||||||
|  |                 storage_scheme, | ||||||
|  |             ) | ||||||
|  |             .await | ||||||
|  |             .map_err(|error| { | ||||||
|  |                 error.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound) | ||||||
|  |             })?; | ||||||
|  |  | ||||||
|         let customer_id = payment_data.payment_intent.customer_id.clone(); |         let customer_id = payment_data.payment_intent.customer_id.clone(); | ||||||
|         payment_data.payment_intent = db |         payment_data.payment_intent = db | ||||||
|             .update_payment_intent( |             .update_payment_intent( | ||||||
|  | |||||||
| @ -240,6 +240,7 @@ impl PaymentAttemptInterface for MockDb { | |||||||
|             amount_to_capture: payment_attempt.amount_to_capture, |             amount_to_capture: payment_attempt.amount_to_capture, | ||||||
|             mandate_id: None, |             mandate_id: None, | ||||||
|             browser_info: None, |             browser_info: None, | ||||||
|  |             payment_token: None, | ||||||
|             error_code: payment_attempt.error_code, |             error_code: payment_attempt.error_code, | ||||||
|         }; |         }; | ||||||
|         payment_attempts.push(payment_attempt.clone()); |         payment_attempts.push(payment_attempt.clone()); | ||||||
| @ -371,6 +372,7 @@ mod storage { | |||||||
|                         cancellation_reason: payment_attempt.cancellation_reason.clone(), |                         cancellation_reason: payment_attempt.cancellation_reason.clone(), | ||||||
|                         mandate_id: payment_attempt.mandate_id.clone(), |                         mandate_id: payment_attempt.mandate_id.clone(), | ||||||
|                         browser_info: payment_attempt.browser_info.clone(), |                         browser_info: payment_attempt.browser_info.clone(), | ||||||
|  |                         payment_token: payment_attempt.payment_token.clone(), | ||||||
|                         error_code: payment_attempt.error_code.clone(), |                         error_code: payment_attempt.error_code.clone(), | ||||||
|                     }; |                     }; | ||||||
|  |  | ||||||
|  | |||||||
| @ -36,6 +36,7 @@ pub struct PaymentAttempt { | |||||||
|     pub amount_to_capture: Option<i64>, |     pub amount_to_capture: Option<i64>, | ||||||
|     pub mandate_id: Option<String>, |     pub mandate_id: Option<String>, | ||||||
|     pub browser_info: Option<serde_json::Value>, |     pub browser_info: Option<serde_json::Value>, | ||||||
|  |     pub payment_token: Option<String>, | ||||||
|     pub error_code: Option<String>, |     pub error_code: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -71,6 +72,7 @@ pub struct PaymentAttemptNew { | |||||||
|     pub amount_to_capture: Option<i64>, |     pub amount_to_capture: Option<i64>, | ||||||
|     pub mandate_id: Option<String>, |     pub mandate_id: Option<String>, | ||||||
|     pub browser_info: Option<serde_json::Value>, |     pub browser_info: Option<serde_json::Value>, | ||||||
|  |     pub payment_token: Option<String>, | ||||||
|     pub error_code: Option<String>, |     pub error_code: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -83,6 +85,10 @@ pub enum PaymentAttemptUpdate { | |||||||
|         authentication_type: Option<storage_enums::AuthenticationType>, |         authentication_type: Option<storage_enums::AuthenticationType>, | ||||||
|         payment_method: Option<storage_enums::PaymentMethodType>, |         payment_method: Option<storage_enums::PaymentMethodType>, | ||||||
|     }, |     }, | ||||||
|  |     UpdateTrackers { | ||||||
|  |         payment_token: Option<String>, | ||||||
|  |         connector: Option<String>, | ||||||
|  |     }, | ||||||
|     AuthenticationTypeUpdate { |     AuthenticationTypeUpdate { | ||||||
|         authentication_type: storage_enums::AuthenticationType, |         authentication_type: storage_enums::AuthenticationType, | ||||||
|     }, |     }, | ||||||
| @ -91,6 +97,7 @@ pub enum PaymentAttemptUpdate { | |||||||
|         payment_method: Option<storage_enums::PaymentMethodType>, |         payment_method: Option<storage_enums::PaymentMethodType>, | ||||||
|         browser_info: Option<serde_json::Value>, |         browser_info: Option<serde_json::Value>, | ||||||
|         connector: Option<String>, |         connector: Option<String>, | ||||||
|  |         payment_token: Option<String>, | ||||||
|     }, |     }, | ||||||
|     VoidUpdate { |     VoidUpdate { | ||||||
|         status: storage_enums::AttemptStatus, |         status: storage_enums::AttemptStatus, | ||||||
| @ -133,6 +140,7 @@ pub struct PaymentAttemptUpdateInternal { | |||||||
|     redirect: Option<bool>, |     redirect: Option<bool>, | ||||||
|     mandate_id: Option<String>, |     mandate_id: Option<String>, | ||||||
|     browser_info: Option<serde_json::Value>, |     browser_info: Option<serde_json::Value>, | ||||||
|  |     payment_token: Option<String>, | ||||||
|     error_code: Option<String>, |     error_code: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -155,6 +163,7 @@ impl PaymentAttemptUpdate { | |||||||
|                 .unwrap_or(source.payment_method_id), |                 .unwrap_or(source.payment_method_id), | ||||||
|             browser_info: pa_update.browser_info, |             browser_info: pa_update.browser_info, | ||||||
|             modified_at: common_utils::date_time::now(), |             modified_at: common_utils::date_time::now(), | ||||||
|  |             payment_token: pa_update.payment_token, | ||||||
|             ..source |             ..source | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -192,12 +201,14 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal { | |||||||
|                 payment_method, |                 payment_method, | ||||||
|                 browser_info, |                 browser_info, | ||||||
|                 connector, |                 connector, | ||||||
|  |                 payment_token, | ||||||
|             } => Self { |             } => Self { | ||||||
|                 status: Some(status), |                 status: Some(status), | ||||||
|                 payment_method, |                 payment_method, | ||||||
|                 modified_at: Some(common_utils::date_time::now()), |                 modified_at: Some(common_utils::date_time::now()), | ||||||
|                 browser_info, |                 browser_info, | ||||||
|                 connector, |                 connector, | ||||||
|  |                 payment_token, | ||||||
|                 ..Default::default() |                 ..Default::default() | ||||||
|             }, |             }, | ||||||
|             PaymentAttemptUpdate::VoidUpdate { |             PaymentAttemptUpdate::VoidUpdate { | ||||||
| @ -244,6 +255,14 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal { | |||||||
|                 status: Some(status), |                 status: Some(status), | ||||||
|                 ..Default::default() |                 ..Default::default() | ||||||
|             }, |             }, | ||||||
|  |             PaymentAttemptUpdate::UpdateTrackers { | ||||||
|  |                 payment_token, | ||||||
|  |                 connector, | ||||||
|  |             } => Self { | ||||||
|  |                 payment_token, | ||||||
|  |                 connector, | ||||||
|  |                 ..Default::default() | ||||||
|  |             }, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -214,6 +214,7 @@ diesel::table! { | |||||||
|         mandate_id -> Nullable<Varchar>, |         mandate_id -> Nullable<Varchar>, | ||||||
|         browser_info -> Nullable<Jsonb>, |         browser_info -> Nullable<Jsonb>, | ||||||
|         error_code -> Nullable<Varchar>, |         error_code -> Nullable<Varchar>, | ||||||
|  |         payment_token -> Nullable<Varchar>, | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -0,0 +1 @@ | |||||||
|  | ALTER TABLE payment_attempt DROP COLUMN payment_token; | ||||||
| @ -0,0 +1 @@ | |||||||
|  | ALTER TABLE payment_attempt ADD COLUMN payment_token VARCHAR(255); | ||||||
		Reference in New Issue
	
	Block a user
	 Narayan Bhat
					Narayan Bhat