warlocks_cauldron/providers/
payment.rs

1use regex::Regex;
2
3use crate::Person;
4
5use super::dependencies::*;
6
7
8#[derive(Debug)]
9pub struct CreditCard {
10    pub number: String,
11    pub expiration_date: String,
12    pub owner: String,
13    pub cvv: String,
14}
15
16/// Methods collection for generating data related to payments
17pub struct Payment;
18
19impl Payment {
20    /// Generate a random CID
21    pub fn cid() -> String {
22        format!("{:.4}", randint(1, 9999))
23    }
24
25    /// Generate a random PayPal account
26    pub fn paypal() -> String {
27        Person::email(None, false)
28    }
29
30    /// Generate a random bitcoin address
31    pub fn bitcoin_address() -> String {
32        let type_ = get_random_element(vec!["1", "3"].into_iter());
33        let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
34        let address = get_random_elements(characters.chars(), 33);
35        format!("{type_}{}", address.iter().join(""))
36    }
37
38    /// Generate a random Ethereum address
39    pub fn ethereum_address() -> String {
40        format!("0x{}", hex::encode(urandom(20)))
41    }
42
43    /// Generate a random credit card number
44    pub fn credit_card_network() -> &'static str {
45        get_random_element(CREDIT_CARD_NETWORKS.iter())
46    }
47
48    /// Generate a random credit card number
49    pub fn credit_card_number(card_type: Option<CardType>) -> String {
50        let card_type = validate_variant(card_type, None);
51
52        let mut regex = r"(\d{4})(\d{4})(\d{4})(\d{4})";
53        let mut length = 16;
54        let mut groups = 4;
55
56        let start_number = match card_type {
57            CardType::VISA => randint(4000, 4999),
58            CardType::MASTER_CARD => get_random_element(vec![randint(2221, 2720), randint(5100, 5599)].into_iter()),
59            CardType::AMERICAN_EXPRESS => {
60                regex = r"(\d{4})(\d{6})(\d{5})";
61                length = 15;
62                groups = 3;
63                get_random_element(vec![34, 37].into_iter())
64            },
65            _ => panic!("No valid card type!"),
66        };
67
68        let mut str_num = start_number.to_string();
69        while str_num.len() <= length {
70            str_num += &get_random_element("0123456789".chars()).to_string()
71        } 
72
73        str_num.push_str(&luhn::checksum(str_num.as_bytes()).to_string());
74
75        let capt = Regex::new(regex).unwrap().captures_iter(&str_num)
76            .next().unwrap();
77
78        (1..=groups).map(|i| capt.get(i).unwrap().as_str()).join(" ")
79    }
80
81    /// Generate a random expiration date for credit card
82    ///
83    /// Arguments:
84    /// * `minimum` -  Date of issue
85    /// * `maximum` - Maximum of expiration date
86    pub fn credit_card_expiration_date(minimum: i32, maximum: i32) -> String {
87        format!("{:02}/{}", randint(1, 12), randint(minimum, maximum))
88    }
89
90    /// Generate a random CVV
91    pub fn cvv() -> String {
92        format!("{:03}", randint(1, 999))
93    }
94
95    /// Generate full credit card
96    pub fn credit_card() -> CreditCard {
97        CreditCard {
98            number: Self::credit_card_number(None),
99            expiration_date: Self::credit_card_expiration_date(16, 25),
100            owner: Person(&Locale::EN).full_name(None, false).to_uppercase(),
101            cvv: Self::cvv(),
102        }
103    }
104}