Skip to main content

square_rs/
payment.rs

1/*!
2Payment functionality of the [Square API](https://developer.squareup.com).
3*/
4
5use crate::client::SquareClient;
6use crate::endpoint::SquareEndpoint;
7use crate::error::PaymentBuildError;
8use crate::error::SquareError;
9use crate::money::{Currency, Money};
10use crate::response::SquareResponse;
11
12use serde::{Deserialize, Serialize};
13use uuid::Uuid;
14
15impl SquareClient {
16    /// Create a payment with the given [Payment](Payment) to the Square API
17    /// and get the response back
18    ///
19    /// # Arguments
20    /// * `payment` - A [Payment](Payment) created from the [PaymentBuilder](PaymentBuilder)
21    pub async fn create_payment(&self, payment: Payment) -> Result<SquareResponse, SquareError> {
22        self.request(SquareEndpoint::Payments, &payment).await
23    }
24}
25
26/// The representation of a payment to the square API
27/// containing a minimal set of fields for a payment
28/// to be successfully processed.
29#[derive(Serialize, Debug, Deserialize)]
30pub struct Payment {
31    #[serde(rename(serialize = "source_id"))]
32    source_id: String,
33    idempotency_key: String,
34    amount_money: Money,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    verification_token: Option<String>,
37}
38
39/// The [PaymentBuilder](PaymentBuilder)
40pub struct PaymentBuilder {
41    source_id: Option<String>,
42    amount_money: Option<Money>,
43    verification_token: Option<String>,
44}
45
46impl Default for PaymentBuilder {
47    fn default() -> Self {
48        Self {
49            source_id: None,
50            amount_money: None,
51            verification_token: None,
52        }
53    }
54}
55
56impl PaymentBuilder {
57    pub fn new() -> Self {
58        Default::default()
59    }
60
61    pub fn source_id(mut self, source_id: String) -> Self {
62        self.source_id = Some(source_id);
63
64        self
65    }
66
67    pub fn amount(mut self, amount: i64, currency: Currency) -> Self {
68        self.amount_money = Some(Money { amount, currency });
69
70        self
71    }
72
73    pub fn verification_token(mut self, token: String) -> Self {
74        self.verification_token = Some(token);
75
76        self
77    }
78
79    pub async fn build(&self) -> Result<Payment, PaymentBuildError> {
80        let source_id = match &self.source_id {
81            Some(n) => n.clone(),
82            None => return Err(PaymentBuildError),
83        };
84
85        // The idempotency key just needs to be a random string
86        // it is advised to use a v4 uuid by stripe
87        let idempotency_key = Uuid::new_v4().to_string();
88
89        let amount_money = match &self.amount_money {
90            Some(n) => n.clone(),
91            None => return Err(PaymentBuildError),
92        };
93
94        let verification_token = self.verification_token.clone();
95
96        Ok(Payment {
97            source_id,
98            idempotency_key,
99            amount_money,
100            verification_token,
101        })
102    }
103}