@@ -119,11 +119,9 @@ async fn process_value(
119
119
}
120
120
}
121
121
122
- if tokens_processed {
123
- Ok ( Value :: String ( result) )
124
- } else {
125
- Ok ( Value :: String ( s) )
126
- }
122
+ Ok ( tokens_processed
123
+ . then ( || Value :: String ( result) )
124
+ . unwrap_or ( Value :: String ( s) ) )
127
125
} else {
128
126
if let Ok ( ( _, token_ref) ) = parse_token ( & s) {
129
127
extract_field_from_vault_data ( vault_data, & token_ref. field )
@@ -185,18 +183,50 @@ fn extract_field_from_vault_data(vault_data: &Value, field_name: &str) -> Router
185
183
pub async fn proxy_core (
186
184
state : SessionState ,
187
185
merchant_account : domain:: MerchantAccount ,
186
+ key_store : domain:: MerchantKeyStore ,
188
187
req : proxy_api_models:: ProxyRequest ,
189
188
) -> RouterResponse < proxy_api_models:: ProxyResponse > {
189
+ use api_models:: payment_methods:: PaymentMethodId ;
190
+ use common_utils:: { ext_traits:: OptionExt , id_type} ;
191
+
190
192
let token = & req. token ;
191
- //TODO: match on token type,
193
+
194
+ //TODO: match on token type,
192
195
//if token_type is tokenization id then fetch vault id from tokenization table
193
196
//else if token_type is payment method id then fetch vault id from payment method table
194
- let vault_id = domain:: VaultId :: generate ( token. clone ( ) ) ;
197
+
198
+ let db = state. store . as_ref ( ) ;
199
+ let vault_id = match req. token_type {
200
+ proxy_api_models:: TokenType :: PaymentMethodId => {
201
+ let pm_id = PaymentMethodId {
202
+ payment_method_id : token. clone ( ) ,
203
+ } ;
204
+ let pm_id =
205
+ id_type:: GlobalPaymentMethodId :: generate_from_string ( pm_id. payment_method_id )
206
+ . change_context ( errors:: ApiErrorResponse :: InternalServerError )
207
+ . attach_printable ( "Unable to generate GlobalPaymentMethodId" ) ?;
208
+
209
+ db. find_payment_method (
210
+ & ( ( & state) . into ( ) ) ,
211
+ & key_store,
212
+ & pm_id,
213
+ merchant_account. storage_scheme ,
214
+ )
215
+ . await
216
+ . change_context ( errors:: ApiErrorResponse :: PaymentMethodNotFound ) ?
217
+ . locker_id
218
+ }
219
+ proxy_api_models:: TokenType :: TokenizationId => None , //TODO: Fetch locker id from tokenization table.
220
+ } ;
195
221
196
222
let vault_response = super :: payment_methods:: vault:: retrieve_payment_method_from_vault (
197
223
& state,
198
224
& merchant_account,
199
- & vault_id,
225
+ & vault_id. get_required_value ( "vault_id" ) . change_context (
226
+ errors:: ApiErrorResponse :: MissingRequiredField {
227
+ field_name : "vault id" ,
228
+ } ,
229
+ ) ?,
200
230
)
201
231
. await
202
232
. map_err ( |_| errors:: ApiErrorResponse :: InternalServerError ) ?;
@@ -228,8 +258,7 @@ pub async fn proxy_core(
228
258
. change_context ( errors:: ApiErrorResponse :: InternalServerError )
229
259
. attach_printable ( "Error while receiving response" )
230
260
. and_then ( |inner| match inner {
231
- Err ( err_res) =>
232
- {
261
+ Err ( err_res) => {
233
262
logger:: error!( "Response Deserialization Failed: {err_res:?}" ) ;
234
263
Ok ( err_res)
235
264
}
@@ -243,9 +272,12 @@ pub async fn proxy_core(
243
272
. change_context ( errors:: ApiErrorResponse :: InternalServerError ) ?;
244
273
245
274
let status_code = res. status_code ;
246
- let response_headers = res. headers . as_ref ( )
275
+ let response_headers = res
276
+ . headers
277
+ . as_ref ( )
247
278
. map ( |h| {
248
- let map: std:: collections:: BTreeMap < _ , _ > = h. iter ( )
279
+ let map: std:: collections:: BTreeMap < _ , _ > = h
280
+ . iter ( )
249
281
. map ( |( k, v) | ( k. to_string ( ) , v. to_str ( ) . unwrap_or ( "" ) . to_string ( ) ) )
250
282
. collect ( ) ;
251
283
serde_json:: to_value ( map) . unwrap_or_else ( |_| serde_json:: json!( { } ) )
0 commit comments