Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cards): validate card security code and expiration #874

Merged
merged 11 commits into from
May 3, 2023

Conversation

divinenaman
Copy link
Contributor

@divinenaman divinenaman commented Apr 13, 2023

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates

Description

Add basic validations for card security codes (CVC/CVV/CID code) and card expiration month and year. This logic resides in a new crate called cards. Validations included (as of now):

  • Card security code must be 3 or 4 digits only
  • Expiration month is valid (1-12)
  • Expiration year is a 4 digit number only
  • Card has not expired (expiration month and year correspond to a date in the future)

Additional Changes

  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

This fixes #607

How did you test it?

run cargo test --cards

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed submitted code
  • I added unit tests for my changes where possible
  • I added a CHANGELOG entry if applicable

@divinenaman divinenaman requested review from a team, jarnura and ashokkjag as code owners April 13, 2023 11:14
@SanchithHegde SanchithHegde added C-feature Category: Feature request or enhancement S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Apr 13, 2023
Copy link
Member

@SanchithHegde SanchithHegde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move the logic in the constructors to the TryFrom trait implementations for CardSecurityCode, CardExpirationMonth and CardExpirationYear?

So:

  1. CardSecurityCode::new() would become CardSecurityCode::try_from(u16)
  2. CardExpirationMonth::new() would become CardExpirationMonth::try_from(u8)
  3. CardExpirationYear::new() would become CardExpirationYear::try_from(u16)

And regarding the Serialize and Deserialize implementations, would you want to take them up in this PR or a separate one? Let us know if you have any questions.

crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/Cargo.toml Outdated Show resolved Hide resolved
@SanchithHegde SanchithHegde changed the title feat: cards crate feat(cards): validate card security code and expiration Apr 13, 2023
@SanchithHegde SanchithHegde added S-waiting-on-author Status: This PR is incomplete or needs to address review comments and removed S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Apr 13, 2023
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@vspecky vspecky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also look into adding serde::{Serialize, Deserialize} implementations with appropriate error reporting. Thanks for taking this up!

crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/tests/basic.rs Outdated Show resolved Hide resolved
@jarnura jarnura added this to the April'23 release milestone Apr 14, 2023
@divinenaman
Copy link
Contributor Author

Could you please move the logic in the constructors to the TryFrom trait implementations for CardSecurityCode, CardExpirationMonth and CardExpirationYear?

So:

1. `CardSecurityCode::new()` would become `CardSecurityCode::try_from(u16)`

2. `CardExpirationMonth::new()` would become `CardExpirationMonth::try_from(u8)`

3. `CardExpirationYear::new()` would become `CardExpirationYear::try_from(u16)`

@SanchithHegde should I remove the new implementation and just implement a TryFrom instance or keep both ?

@SanchithHegde
Copy link
Member

@SanchithHegde should I remove the new implementation and just implement a TryFrom instance or keep both ?

Yes, remove the new() method and have only a TryFrom implementation. new() would be suitable when the constructor is not expected to fail (doesn't return a Result), which clearly isn't the case here.

crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/src/lib.rs Outdated Show resolved Hide resolved
crates/cards/tests/basic.rs Outdated Show resolved Hide resolved
crates/cards/Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@SanchithHegde SanchithHegde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that, looks good to me! Thanks again for the PR!

crates/cards/Cargo.toml Outdated Show resolved Hide resolved
crates/cards/tests/basic.rs Outdated Show resolved Hide resolved
crates/cards/tests/basic.rs Outdated Show resolved Hide resolved
Copy link
Member

@SanchithHegde SanchithHegde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that, looks good to me!

crates/cards/tests/basic.rs Outdated Show resolved Hide resolved
SanchithHegde
SanchithHegde previously approved these changes Apr 21, 2023
@SanchithHegde SanchithHegde added S-waiting-on-review Status: This PR has been implemented and needs to be reviewed and removed S-waiting-on-author Status: This PR is incomplete or needs to address review comments labels Apr 24, 2023
Comment on lines +10 to +21
let valid_card_security_code = CardSecurityCode::try_from(1234).unwrap();

// will panic on unwrap
let invalid_card_security_code = CardSecurityCode::try_from(12);

assert_eq!(*valid_card_security_code.peek(), 1234);
assert!(invalid_card_security_code.is_err());

let serialized = serde_json::to_string(&valid_card_security_code).unwrap();
assert_eq!(serialized, "1234");

let derialized = serde_json::from_str::<CardSecurityCode>(&serialized).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker but prefer .expect("what was expected") over .unwrap() since it makes finding the cause of panic easier.

vspecky
vspecky previously approved these changes Apr 25, 2023
Copy link
Member

@vspecky vspecky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SanchithHegde
Copy link
Member

@divinenaman Please address CI checks.

@divinenaman divinenaman dismissed stale reviews from vspecky and SanchithHegde via 7f75e79 April 25, 2023 18:40
@jarnura jarnura added this pull request to the merge queue May 2, 2023
@jarnura jarnura added S-ready-for-merge and removed S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels May 2, 2023
Merged via the queue into juspay:main with commit 0b7bc7b May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: Feature request or enhancement
Projects
No open projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

[FEATURE] Validate card security codes and expiration month/year
5 participants