1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use reqwest::IntoUrl;
use serde::Serialize;
use validator::Validate;

use crate::{InvalidError, SDKError, YapayProduct};

#[derive(Validate, Default, Debug, Clone, PartialEq, Serialize)]
pub struct CheckoutPreferences {
    order_number: String,

    transaction_products: Vec<YapayProduct>,

    /// URL para redirecionamento caso concluída a transação com sucesso
    url_success: Option<String>,
    /// URL para redirecionamento caso pedido esteja aguardando a confirmação de pagamento
    url_process: Option<String>,
    /// URL para redirecionamento caso concluída a transação mas ocorreu falha no pagamento
    url_cancel: Option<String>,

    /// URL para onde deve notificado após mudanças no status de pagamento.
    notification_url: Option<String>,
}

impl CheckoutPreferences {
    pub fn new(order_number: String, products: Vec<YapayProduct>) -> Result<Self, SDKError> {
        let builder = Self {
            order_number,
            transaction_products: products,
            url_success: None,
            url_process: None,
            url_cancel: None,
            notification_url: None,
        };

        if let Err(err) = builder.validate() {
            return Err(InvalidError::ValidatorLibError(err).into());
        }

        Ok(builder)
    }

    pub fn set_notification_url<U>(mut self, url: U) -> Result<Self, SDKError>
    where
        U: IntoUrl,
    {
        let res = url
            .into_url()
            .map(|a| a.as_str().to_string())
            .map_err::<SDKError, _>(|e| InvalidError::URLError(e).into())?;

        self.notification_url = Some(res);
        Ok(self)
    }

    pub fn to_form(self, token: &str) -> String {
        let mut base_vec = vec![
            ("token_account", token.to_string()),
            ("order_number", self.order_number),
        ];

        for item in self.transaction_products {
            base_vec.push(("transaction_product[][description]", item.description));
            base_vec.push(("transaction_product[][quantity]", item.quantity));
            base_vec.push(("transaction_product[][price_unit]", item.price_unit));
        }

        if let Some(url) = self.notification_url {
            base_vec.push(("notification_url", url));
        }

        let mut querystring = String::new();
        base_vec
            .into_iter()
            .enumerate()
            .for_each(|(idx, (key, val))| {
                if idx == 0 {
                    querystring.push_str(&*format!("{}={}", key, val));
                } else {
                    querystring.push_str(&*format!("&{}={}", key, val));
                }
            });

        querystring
    }
}