Skip to content

Commit

Permalink
Accept bank cards only for specific connectors
Browse files Browse the repository at this point in the history
Signed-off-by: Wojciech Kula <[email protected]>
  • Loading branch information
wku12 committed Dec 18, 2024
1 parent 60f5889 commit 299f485
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
5 changes: 5 additions & 0 deletions modules/RsPaymentTerminal/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ config:
password:
description: Password for the Feig terminal.
type: integer
accept_credit_cards_for_connectors:
description: Comma separated string with the connectors that should accept credit cards
type: string
default: "1,2"

requires:
session:
interface: session_cost
Expand Down
82 changes: 62 additions & 20 deletions modules/RsPaymentTerminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,21 @@ mod sync_feig {
use sync_feig::SyncFeig;

impl ProvidedIdToken {
fn new(id_token: String, authorization_type: AuthorizationType) -> Self {
fn new(
id_token: String,
authorization_type: AuthorizationType,
connectors: Option<Vec<i64>>,
) -> Self {
Self {
parent_id_token: None,
id_token: IdToken {
value: id_token,
r#type: IdTokenType::Local,
additional_info: None
additional_info: None,
},
authorization_type,
certificate: None,
connectors: None,
connectors: connectors,
iso_15118_certificate_hash_data: None,
prevalidated: None,
request_id: None,
Expand All @@ -154,6 +158,9 @@ pub struct PaymentTerminalModule {

/// The Feig interface.
feig: SyncFeig,

/// For which connectors credit cards should be accepted
accept_credit_cards_for_connectors: Vec<i64>,
}

impl PaymentTerminalModule {
Expand Down Expand Up @@ -188,26 +195,39 @@ impl PaymentTerminalModule {
};
let card_info = read_card_loop()?;

let provided_token = match card_info {
if let Some(provided_token) = match card_info {
CardInfo::Bank => {
self.feig.begin_transaction(&token)?;

// Reuse the bank token as invoice token so we can use the
// invoice token later on to commit our transactions.
ProvidedIdToken::new(token, AuthorizationType::BankCard)
}
CardInfo::MembershipCard(id_token) => {
ProvidedIdToken::new(id_token, AuthorizationType::RFID)
if !self.accept_credit_cards_for_connectors.is_empty() {
self.feig.begin_transaction(&token)?;

// Reuse the bank token as invoice token so we can use the
// invoice token later on to commit our transactions.
Some(ProvidedIdToken::new(
token,
AuthorizationType::BankCard,
Some(self.accept_credit_cards_for_connectors.clone()),
))
} else {
None
}
}
};
publishers.token_provider.provided_token(provided_token)?;
CardInfo::MembershipCard(id_token) => Some(ProvidedIdToken::new(
id_token,
AuthorizationType::RFID,
None,
)),
} {
publishers.token_provider.provided_token(provided_token)?;
}
Ok(())
}

/// The implementation of the `SessionCostClientSubscriber::on_session_cost`,
/// but here we can return errors.
fn on_session_cost_impl(&self, context: &Context, value: SessionCost) -> Result<()> {
let Some(id_tag) = value.id_tag else { return Ok(()) };
let Some(id_tag) = value.id_tag else {
return Ok(());
};

// We only care about bank cards.
match id_tag.authorization_type {
Expand Down Expand Up @@ -289,9 +309,16 @@ fn main() -> Result<()> {

let (tx, rx) = channel();

let accept_credit_cards_for_connectors = config
.accept_credit_cards_for_connectors
.split(',')
.filter_map(|s| s.trim().parse::<i64>().ok())
.collect();

let pt_module = Arc::new(PaymentTerminalModule {
tx,
feig: SyncFeig::new(pt_config),
accept_credit_cards_for_connectors,
});

let _module = Module::new(
Expand Down Expand Up @@ -447,7 +474,10 @@ mod tests {
code: Some(CurrencyCode::EUR),
decimals: None,
},
id_tag: Some(ProvidedIdToken::new(String::new(), AuthorizationType::BankCard)),
id_tag: Some(ProvidedIdToken::new(
String::new(),
AuthorizationType::BankCard,
)),
status: SessionStatus::Running,
session_id: String::new(),
idle_price: None,
Expand Down Expand Up @@ -487,7 +517,10 @@ mod tests {
code: Some(CurrencyCode::EUR),
decimals: None,
},
id_tag: Some(ProvidedIdToken::new("token".to_string(), AuthorizationType::BankCard)),
id_tag: Some(ProvidedIdToken::new(
"token".to_string(),
AuthorizationType::BankCard,
)),
status: SessionStatus::Finished,
session_id: String::new(),
idle_price: None,
Expand All @@ -505,7 +538,10 @@ mod tests {
code: Some(CurrencyCode::EUR),
decimals: None,
},
id_tag: Some(ProvidedIdToken::new("token".to_string(), AuthorizationType::BankCard)),
id_tag: Some(ProvidedIdToken::new(
"token".to_string(),
AuthorizationType::BankCard,
)),
status: SessionStatus::Finished,
session_id: String::new(),
idle_price: None,
Expand All @@ -530,7 +566,10 @@ mod tests {
code: Some(CurrencyCode::EUR),
decimals: None,
},
id_tag: Some(ProvidedIdToken::new("token".to_string(), AuthorizationType::BankCard)),
id_tag: Some(ProvidedIdToken::new(
"token".to_string(),
AuthorizationType::BankCard,
)),
status: SessionStatus::Finished,
session_id: String::new(),
idle_price: None,
Expand Down Expand Up @@ -565,7 +604,10 @@ mod tests {
code: Some(CurrencyCode::EUR),
decimals: None,
},
id_tag: Some(ProvidedIdToken::new("token".to_string(), AuthorizationType::BankCard)),
id_tag: Some(ProvidedIdToken::new(
"token".to_string(),
AuthorizationType::BankCard,
)),
status: SessionStatus::Finished,
session_id: String::new(),
idle_price: None,
Expand Down

0 comments on commit 299f485

Please sign in to comment.