mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 00:49:42 +08:00
452 lines
15 KiB
Rust
452 lines
15 KiB
Rust
use api_models::payments::PaymentMethodData;
|
|
use masking::Secret;
|
|
use router::types::{self, api, storage::enums};
|
|
|
|
use crate::{
|
|
connector_auth,
|
|
utils::{self, ConnectorActions},
|
|
};
|
|
|
|
#[derive(Clone, Copy)]
|
|
struct BamboraTest;
|
|
impl ConnectorActions for BamboraTest {}
|
|
impl utils::Connector for BamboraTest {
|
|
fn get_data(&self) -> types::api::ConnectorData {
|
|
use router::connector::Bambora;
|
|
types::api::ConnectorData {
|
|
connector: Box::new(&Bambora),
|
|
connector_name: types::Connector::Bambora,
|
|
get_token: types::api::GetToken::Connector,
|
|
}
|
|
}
|
|
|
|
fn get_auth_token(&self) -> types::ConnectorAuthType {
|
|
types::ConnectorAuthType::from(
|
|
connector_auth::ConnectorAuthentication::new()
|
|
.bambora
|
|
.expect("Missing connector authentication configuration"),
|
|
)
|
|
}
|
|
|
|
fn get_name(&self) -> String {
|
|
"bambora".to_string()
|
|
}
|
|
}
|
|
|
|
static CONNECTOR: BamboraTest = BamboraTest {};
|
|
|
|
fn get_default_payment_authorize_data() -> Option<types::PaymentsAuthorizeData> {
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: types::api::PaymentMethodData::Card(api::Card {
|
|
card_number: Secret::new("4030000010001234".to_string()),
|
|
card_exp_year: Secret::new("25".to_string()),
|
|
card_cvc: Secret::new("123".to_string()),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
})
|
|
}
|
|
|
|
// Cards Positive Tests
|
|
// Creates a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_only_authorize_payment() {
|
|
let response = CONNECTOR
|
|
.authorize_payment(get_default_payment_authorize_data(), None)
|
|
.await
|
|
.expect("Authorize payment response");
|
|
assert_eq!(response.status, enums::AttemptStatus::Authorized);
|
|
}
|
|
|
|
// Captures a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_capture_authorized_payment() {
|
|
let response = CONNECTOR
|
|
.authorize_and_capture_payment(get_default_payment_authorize_data(), None, None)
|
|
.await
|
|
.expect("Capture payment response");
|
|
assert_eq!(response.status, enums::AttemptStatus::Charged);
|
|
}
|
|
|
|
// Partially captures a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_partially_capture_authorized_payment() {
|
|
let response = CONNECTOR
|
|
.authorize_and_capture_payment(
|
|
get_default_payment_authorize_data(),
|
|
Some(types::PaymentsCaptureData {
|
|
amount_to_capture: 50,
|
|
..utils::PaymentCaptureType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.expect("Capture payment response");
|
|
assert_eq!(response.status, enums::AttemptStatus::Charged);
|
|
}
|
|
|
|
// Synchronizes a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_sync_authorized_payment() {
|
|
let authorize_response = CONNECTOR
|
|
.authorize_payment(get_default_payment_authorize_data(), None)
|
|
.await
|
|
.expect("Authorize payment response");
|
|
let txn_id = utils::get_connector_transaction_id(authorize_response.response);
|
|
let response = CONNECTOR
|
|
.psync_retry_till_status_matches(
|
|
enums::AttemptStatus::Authorized,
|
|
Some(types::PaymentsSyncData {
|
|
connector_transaction_id: router::types::ResponseId::ConnectorTransactionId(
|
|
txn_id.unwrap(),
|
|
),
|
|
encoded_data: None,
|
|
capture_method: Some(storage_models::enums::CaptureMethod::Manual),
|
|
connector_meta: None,
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.expect("PSync response");
|
|
assert_eq!(response.status, enums::AttemptStatus::Authorized,);
|
|
}
|
|
|
|
// Voids a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_void_authorized_payment() {
|
|
let response = CONNECTOR
|
|
.authorize_and_void_payment(
|
|
get_default_payment_authorize_data(),
|
|
Some(types::PaymentsCancelData {
|
|
connector_transaction_id: String::from(""),
|
|
cancellation_reason: Some("requested_by_customer".to_string()),
|
|
..Default::default()
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.expect("Void payment response");
|
|
assert_eq!(response.status, enums::AttemptStatus::Voided);
|
|
}
|
|
|
|
// Refunds a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_refund_manually_captured_payment() {
|
|
let response = CONNECTOR
|
|
.capture_payment_and_refund(get_default_payment_authorize_data(), None, None, None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Partially refunds a payment using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_partially_refund_manually_captured_payment() {
|
|
let response = CONNECTOR
|
|
.capture_payment_and_refund(
|
|
get_default_payment_authorize_data(),
|
|
None,
|
|
Some(types::RefundsData {
|
|
refund_amount: 50,
|
|
..utils::PaymentRefundType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Synchronizes a refund using the manual capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_sync_manually_captured_refund() {
|
|
let refund_response = CONNECTOR
|
|
.capture_payment_and_refund(get_default_payment_authorize_data(), None, None, None)
|
|
.await
|
|
.unwrap();
|
|
let response = CONNECTOR
|
|
.rsync_retry_till_status_matches(
|
|
enums::RefundStatus::Success,
|
|
refund_response.response.unwrap().connector_refund_id,
|
|
None,
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Creates a payment using the automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_make_payment() {
|
|
let authorize_response = CONNECTOR
|
|
.make_payment(get_default_payment_authorize_data(), None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(authorize_response.status, enums::AttemptStatus::Charged);
|
|
}
|
|
|
|
// Synchronizes a payment using the automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_sync_auto_captured_payment() {
|
|
let authorize_response = CONNECTOR
|
|
.make_payment(get_default_payment_authorize_data(), None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(authorize_response.status, enums::AttemptStatus::Charged);
|
|
let txn_id = utils::get_connector_transaction_id(authorize_response.response);
|
|
assert_ne!(txn_id, None, "Empty connector transaction id");
|
|
let response = CONNECTOR
|
|
.psync_retry_till_status_matches(
|
|
enums::AttemptStatus::Charged,
|
|
Some(types::PaymentsSyncData {
|
|
connector_transaction_id: router::types::ResponseId::ConnectorTransactionId(
|
|
txn_id.unwrap(),
|
|
),
|
|
encoded_data: None,
|
|
capture_method: Some(enums::CaptureMethod::Automatic),
|
|
connector_meta: None,
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status, enums::AttemptStatus::Charged,);
|
|
}
|
|
|
|
// Refunds a payment using the automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_refund_auto_captured_payment() {
|
|
let response = CONNECTOR
|
|
.make_payment_and_refund(get_default_payment_authorize_data(), None, None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Partially refunds a payment using the automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_partially_refund_succeeded_payment() {
|
|
let refund_response = CONNECTOR
|
|
.make_payment_and_refund(
|
|
get_default_payment_authorize_data(),
|
|
Some(types::RefundsData {
|
|
refund_amount: 50,
|
|
..utils::PaymentRefundType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
refund_response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Synchronizes a refund using the automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_sync_refund() {
|
|
let refund_response = CONNECTOR
|
|
.make_payment_and_refund(get_default_payment_authorize_data(), None, None)
|
|
.await
|
|
.unwrap();
|
|
let response = CONNECTOR
|
|
.rsync_retry_till_status_matches(
|
|
enums::RefundStatus::Success,
|
|
refund_response.response.unwrap().connector_refund_id,
|
|
None,
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success,
|
|
);
|
|
}
|
|
|
|
// Cards Negative scenerios
|
|
// Creates a payment with incorrect card number.
|
|
#[actix_web::test]
|
|
async fn should_fail_payment_for_incorrect_card_number() {
|
|
let response = CONNECTOR
|
|
.make_payment(
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: PaymentMethodData::Card(api::Card {
|
|
card_number: Secret::new("1234567891011".to_string()),
|
|
card_exp_year: Secret::new("25".to_string()),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap_err().message,
|
|
"Invalid Card Number".to_string(),
|
|
);
|
|
}
|
|
|
|
// Creates a payment with empty card number.
|
|
#[actix_web::test]
|
|
async fn should_fail_payment_for_empty_card_number() {
|
|
let response = CONNECTOR
|
|
.make_payment(
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: PaymentMethodData::Card(api::Card {
|
|
card_number: Secret::new(String::from("")),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let x = response.response.unwrap_err();
|
|
assert_eq!(
|
|
x.reason,
|
|
Some(r#"[{"field":"card:number","message":"Invalid Card Number"},{"field":"card:expiry_year","message":"Invalid expiration year"}]"#.to_string()),
|
|
);
|
|
}
|
|
|
|
// Creates a payment with incorrect CVC.
|
|
#[actix_web::test]
|
|
async fn should_fail_payment_for_incorrect_cvc() {
|
|
let response = CONNECTOR
|
|
.make_payment(
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: PaymentMethodData::Card(api::Card {
|
|
card_exp_year: Secret::new("25".to_string()),
|
|
card_cvc: Secret::new("12345".to_string()),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap_err().reason,
|
|
Some(r#"[{"field":"card:cvd","message":"Invalid card CVD"},{"field":"card:cvd","message":"Invalid card CVD"}]"#.to_string())
|
|
);
|
|
}
|
|
|
|
// Creates a payment with incorrect expiry month.
|
|
#[actix_web::test]
|
|
async fn should_fail_payment_for_invalid_exp_month() {
|
|
let response = CONNECTOR
|
|
.make_payment(
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: PaymentMethodData::Card(api::Card {
|
|
card_exp_month: Secret::new("20".to_string()),
|
|
card_number: Secret::new("4030000010001234".to_string()),
|
|
card_exp_year: Secret::new("25".to_string()),
|
|
card_cvc: Secret::new("123".to_string()),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap_err().reason,
|
|
Some(r#"[{"field":"card:expiry_month","message":"Invalid expiry date"}]"#.to_string())
|
|
);
|
|
}
|
|
|
|
// Creates a payment with incorrect expiry year.
|
|
#[actix_web::test]
|
|
async fn should_fail_payment_for_incorrect_expiry_year() {
|
|
let response = CONNECTOR
|
|
.make_payment(
|
|
Some(types::PaymentsAuthorizeData {
|
|
payment_method_data: PaymentMethodData::Card(api::Card {
|
|
card_exp_year: Secret::new("2000".to_string()),
|
|
card_number: Secret::new("4030000010001234".to_string()),
|
|
card_cvc: Secret::new("123".to_string()),
|
|
..utils::CCardType::default().0
|
|
}),
|
|
..utils::PaymentAuthorizeType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap_err().reason,
|
|
Some(r#"[{"field":"card:expiry_year","message":"Invalid expiration year"}]"#.to_string())
|
|
);
|
|
}
|
|
|
|
// Voids a payment using automatic capture flow (Non 3DS).
|
|
#[actix_web::test]
|
|
async fn should_fail_void_payment_for_auto_capture() {
|
|
let authorize_response = CONNECTOR
|
|
.make_payment(get_default_payment_authorize_data(), None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(authorize_response.status, enums::AttemptStatus::Charged);
|
|
let txn_id = utils::get_connector_transaction_id(authorize_response.response);
|
|
assert_ne!(txn_id, None, "Empty connector transaction id");
|
|
let void_response = CONNECTOR
|
|
.void_payment(txn_id.unwrap(), None, None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
void_response.response.unwrap_err().message,
|
|
"Transaction cannot be adjusted"
|
|
);
|
|
}
|
|
|
|
// Captures a payment using invalid connector payment id.
|
|
#[actix_web::test]
|
|
async fn should_fail_capture_for_invalid_payment() {
|
|
let capture_response = CONNECTOR
|
|
.capture_payment("123456789".to_string(), None, None)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
capture_response.response.unwrap_err().message,
|
|
String::from("Missing or invalid payment information - Please validate all required payment information.")
|
|
);
|
|
}
|
|
|
|
// Refunds a payment with refund amount higher than payment amount.
|
|
#[actix_web::test]
|
|
async fn should_succeed_for_refund_amount_higher_than_payment_amount() {
|
|
let response = CONNECTOR
|
|
.make_payment_and_refund(
|
|
get_default_payment_authorize_data(),
|
|
Some(types::RefundsData {
|
|
refund_amount: 150,
|
|
..utils::PaymentRefundType::default().0
|
|
}),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
response.response.unwrap().refund_status,
|
|
enums::RefundStatus::Success
|
|
);
|
|
}
|