Skip to content

Commit

Permalink
chore: address pr review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jan 18, 2024
1 parent f4e1512 commit 5df2ef4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 111 deletions.
5 changes: 4 additions & 1 deletion crates/router/src/connector/coinbase/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ fn get_crypto_specific_payment_data(
let name =
billing_address.and_then(|add| add.get_first_name().ok().map(|name| name.to_owned()));
let description = item.get_description().ok();
let connector_meta = CoinbaseConnectorMeta::try_from(&item.connector_meta_data)?;
let connector_meta = CoinbaseConnectorMeta::try_from(&item.connector_meta_data)
.change_context(errors::ConnectorError::InvalidConnectorConfig {
config: "Merchant connector account metadata",
})?;
let pricing_type = connector_meta.pricing_type;
let local_price = get_local_price(item);
let redirect_url = item.request.get_return_url()?;
Expand Down
115 changes: 5 additions & 110 deletions crates/router/tests/connectors/coinbase.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use std::marker::PhantomData;

use api_models::payments::CryptoData;
use common_utils::pii;
use masking::Secret;
use router::{
connector::coinbase::transformers::{CoinbasePaymentsRequest, LocalPrice},
core::errors::ConnectorError,
types::{self, api, storage::enums, PaymentAddress},
};
use router::types::{self, api, storage::enums, PaymentAddress};
use serde_json::json;

use crate::{
Expand Down Expand Up @@ -71,8 +64,8 @@ fn get_default_payment_info() -> Option<utils::PaymentInfo> {
})
}

fn payment_method_details() -> types::PaymentsAuthorizeData {
types::PaymentsAuthorizeData {
fn payment_method_details() -> Option<types::PaymentsAuthorizeData> {
Some(types::PaymentsAuthorizeData {
amount: 1,
currency: enums::Currency::USD,
payment_method_data: types::api::PaymentMethodData::Crypto(CryptoData {
Expand Down Expand Up @@ -103,62 +96,14 @@ fn payment_method_details() -> types::PaymentsAuthorizeData {
surcharge_details: None,
request_incremental_authorization: false,
metadata: None,
}
}

fn construct_payment_router_data(
connector_meta_data: Option<pii::SecretSerdeValue>,
) -> types::PaymentsAuthorizeRouterData {
let connector_auth_type = types::ConnectorAuthType::HeaderKey {
api_key: Secret::new("api_key".to_string()),
};

types::RouterData {
flow: PhantomData,
merchant_id: String::from("Coinbase"),
customer_id: Some(String::from("Coinbase")),
connector: "Coinbase".to_string(),
payment_id: uuid::Uuid::new_v4().to_string(),
attempt_id: uuid::Uuid::new_v4().to_string(),
status: Default::default(),
auth_type: enums::AuthenticationType::NoThreeDs,
payment_method: enums::PaymentMethod::Card,
connector_auth_type,
description: Some("This is a test".to_string()),
return_url: None,
request: payment_method_details(),
response: Err(Default::default()),
payment_method_id: None,
address: Default::default(),
connector_meta_data,
amount_captured: None,
access_token: None,
session_token: None,
reference_id: None,
payment_method_token: None,
connector_customer: None,
recurring_mandate_payment_data: None,
preprocessing_id: None,
connector_request_reference_id: uuid::Uuid::new_v4().to_string(),
#[cfg(feature = "payouts")]
payout_method_data: None,
#[cfg(feature = "payouts")]
quote_id: None,
test_mode: None,
payment_method_balance: None,
connector_api_version: None,
connector_http_status_code: None,
apple_pay_flow: None,
external_latency: None,
frm_metadata: None,
}
})
}

// Creates a payment using the manual capture flow
#[actix_web::test]
async fn should_only_authorize_payment() {
let response = CONNECTOR
.authorize_payment(Some(payment_method_details()), get_default_payment_info())
.authorize_payment(payment_method_details(), get_default_payment_info())
.await
.expect("Authorize payment response");
assert_eq!(response.status, enums::AttemptStatus::AuthenticationPending);
Expand Down Expand Up @@ -247,53 +192,3 @@ async fn should_sync_cancelled_payment() {
.expect("PSync response");
assert_eq!(response.status, enums::AttemptStatus::Voided);
}

#[test]
fn coinbase_payments_request_try_from_works() {
// `connector_meta_data` as `None` - should fail
assert_eq!(
CoinbasePaymentsRequest::try_from(&construct_payment_router_data(None))
.unwrap_err()
.current_context(),
&ConnectorError::InvalidConnectorConfig { config: "metadata" },
);

// `connector_meta_data` as empty json - should fail
assert_eq!(
CoinbasePaymentsRequest::try_from(&construct_payment_router_data(Some(Secret::new(
serde_json::json!({})
))))
.unwrap_err()
.current_context(),
&ConnectorError::InvalidConnectorConfig { config: "metadata" },
);

// `connector_meta_data` as json with missing `pricing_type` - should fail
assert_eq!(
CoinbasePaymentsRequest::try_from(&construct_payment_router_data(Some(Secret::new(
serde_json::json!({ "wrong_type" : "blah" })
))))
.unwrap_err()
.current_context(),
&ConnectorError::InvalidConnectorConfig { config: "metadata" },
);

// `connector_meta_data` as json with correct `pricing_type` - ok
assert_eq!(
CoinbasePaymentsRequest::try_from(&construct_payment_router_data(Some(Secret::new(
serde_json::json!({ "pricing_type" : "fixed_price" })
))))
.unwrap(),
CoinbasePaymentsRequest {
name: None,
description: Some("This is a test".to_string()),
pricing_type: "fixed_price".to_string(),
local_price: LocalPrice {
amount: "1".to_string(),
currency: "USD".to_string()
},
redirect_url: "https://google.com/".to_string(),
cancel_url: "https://google.com/".to_string(),
}
);
}

0 comments on commit 5df2ef4

Please sign in to comment.