Skip to content

Commit 5cf3510

Browse files
committed
add payment method id support for proxy api
1 parent 6712bf8 commit 5cf3510

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

crates/api_models/src/proxy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ pub enum TokenType {
2727
pub struct ProxyResponse {
2828
/// The response received from the destination
2929
pub response: Value,
30+
/// The status code of the response
3031
pub status_code: u16,
32+
/// The headers of the response
3133
pub response_headers: Value,
3234
}
3335

crates/router/src/core/proxy.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ async fn process_value(
119119
}
120120
}
121121

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)))
127125
} else {
128126
if let Ok((_, token_ref)) = parse_token(&s) {
129127
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
185183
pub async fn proxy_core(
186184
state: SessionState,
187185
merchant_account: domain::MerchantAccount,
186+
key_store: domain::MerchantKeyStore,
188187
req: proxy_api_models::ProxyRequest,
189188
) -> RouterResponse<proxy_api_models::ProxyResponse> {
189+
use api_models::payment_methods::PaymentMethodId;
190+
use common_utils::{ext_traits::OptionExt, id_type};
191+
190192
let token = &req.token;
191-
//TODO: match on token type,
193+
194+
//TODO: match on token type,
192195
//if token_type is tokenization id then fetch vault id from tokenization table
193196
//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+
};
195221

196222
let vault_response = super::payment_methods::vault::retrieve_payment_method_from_vault(
197223
&state,
198224
&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+
)?,
200230
)
201231
.await
202232
.map_err(|_| errors::ApiErrorResponse::InternalServerError)?;
@@ -228,8 +258,7 @@ pub async fn proxy_core(
228258
.change_context(errors::ApiErrorResponse::InternalServerError)
229259
.attach_printable("Error while receiving response")
230260
.and_then(|inner| match inner {
231-
Err(err_res) =>
232-
{
261+
Err(err_res) => {
233262
logger::error!("Response Deserialization Failed: {err_res:?}");
234263
Ok(err_res)
235264
}
@@ -243,9 +272,12 @@ pub async fn proxy_core(
243272
.change_context(errors::ApiErrorResponse::InternalServerError)?;
244273

245274
let status_code = res.status_code;
246-
let response_headers = res.headers.as_ref()
275+
let response_headers = res
276+
.headers
277+
.as_ref()
247278
.map(|h| {
248-
let map: std::collections::BTreeMap<_, _> = h.iter()
279+
let map: std::collections::BTreeMap<_, _> = h
280+
.iter()
249281
.map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
250282
.collect();
251283
serde_json::to_value(map).unwrap_or_else(|_| serde_json::json!({}))

crates/router/src/routes/proxy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub async fn proxy(
2727
proxy::proxy_core(
2828
state,
2929
auth.merchant_account,
30+
auth.key_store,
3031
req,
3132
)
3233
},

0 commit comments

Comments
 (0)