payu_india/
bin.rs

1use crate::PayuApiClient;
2use anyhow::{anyhow, Error};
3use reqwest::Url;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Serialize, Deserialize)]
8pub struct BinInfo {
9    status: i32, //0 = Failed,1 = Success
10    data: Data,
11}
12
13#[derive(Serialize, Deserialize)]
14pub struct Data {
15    total_count: i32,
16    last: i32,
17    next_start: i32,
18    bins_data: BinsData,
19}
20
21#[derive(Serialize, Deserialize)]
22pub struct BinsData {
23    // The issuing bank of the card used for the transaction
24    issuing_bank: String,
25    // The BIN number of the card is displayed in the response.
26    bin: String,
27    // Response value can contain any of the following:
28    //  creditcard signifies that the particular bin is a credit card BIN
29    //  debitcard signifies that the particular bin is a debit card BIN
30    category: String,
31    // Response value can contain any of the following:
32    // MAST
33    // VISA
34    // MAES
35    // AMEX
36    // DINR
37    // Unknown
38    card_type: String,
39    // Response value can contain any of the following:
40    // 0 signifies that the particular BIN is domestic.
41    // 1 signifies that the particular BIN is International.
42    is_domestic: i32,
43    //Response value can contain any of the following:
44    // 0 signifies that the card is not an ATM card.
45    // 1 signifies that the card is an ATM card.
46    is_atmpin_card: i32,
47    // Response value can contain any of the following:
48    // 0 signifies that the card does not have OTP on the fly facility.
49    // 1 signifies that the card have OTP on the fly facility.
50    is_otp_on_the_fly: i32,
51}
52
53#[derive(Serialize, Deserialize)]
54pub struct BinDetails {
55    #[serde(rename = "isDomestic")]
56    pub is_domestic: String,
57    #[serde(rename = "issuingBank")]
58    pub issuing_bank: String,
59    #[serde(rename = "cardType")]
60    pub card_type: String,
61    #[serde(rename = "cardCategory")]
62    pub card_category: String,
63}
64
65pub struct Bin {
66    client: PayuApiClient,
67}
68
69impl Bin {
70    pub fn new(client: PayuApiClient) -> Self {
71        Self { client }
72    }
73
74    pub async fn get_bin_info(self, bin: i64) -> Result<BinInfo, Error> {
75        let input_vars = HashMap::from([
76            ("command".to_string(), "getBINInfo".to_string()),
77            ("var1".to_string(), "1".to_string()),
78            ("var2".to_string(), bin.to_string()),
79            ("var3".to_string(), "0".to_string()),
80            ("var5".to_string(), "1".to_string()),
81        ]);
82        let vars = self.client.generate_hash(input_vars).unwrap();
83        let client = reqwest::Client::new();
84        let req = client
85            .post(
86                Url::parse(
87                    format!("{}/merchant/postservice?form=2", self.client.base_url).as_str(),
88                )
89                .unwrap(),
90            )
91            .form(&vars)
92            .send()
93            .await;
94        return match req {
95            Ok(r) => Ok(r.json::<BinInfo>().await.unwrap()),
96            Err(e) => Err(anyhow!(e)),
97        };
98    }
99
100    pub async fn check_is_domestic(self, bin: i64) -> Result<BinDetails, Error> {
101        let input_vars = HashMap::from([
102            ("command".to_string(), "check_isDomestic".to_string()),
103            ("var1".to_string(), bin.to_string()),
104        ]);
105        let vars = self.client.generate_hash(input_vars).unwrap();
106
107        let client = reqwest::Client::new();
108        let req = client
109            .post(
110                Url::parse(
111                    format!("{}/merchant/postservice?form=2", self.client.base_url).as_str(),
112                )
113                .unwrap(),
114            )
115            .form(&vars)
116            .send()
117            .await;
118        return match req {
119            Ok(r) => Ok(r.json::<BinDetails>().await.unwrap()),
120            Err(e) => Err(anyhow!(e)),
121        };
122    }
123}