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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Provider part of the Payment API
use chrono::{DateTime, TimeZone};
use std::fmt::Display;
use std::sync::Arc;

use crate::{web::WebClient, web::WebInterface, Result};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use ya_client_model::payment::*;

#[derive(Default, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ProviderApiConfig {
    // All timeouts are given in seconds.
    // None is interpreted by server as default timeout (60 seconds).
    pub send_debit_note_timeout: Option<f64>,
    pub cancel_debit_note_timeout: Option<f64>,
    pub send_invoice_timeout: Option<f64>,
    pub cancel_invoice_timeout: Option<f64>,
}

impl ProviderApiConfig {
    pub fn from_env() -> envy::Result<Self> {
        envy::from_env()
    }
}

#[derive(Clone)]
pub struct PaymentProviderApi {
    client: Arc<WebClient>,
    config: ProviderApiConfig,
}

impl WebInterface for PaymentProviderApi {
    const API_URL_ENV_VAR: &'static str = crate::payment::PAYMENT_URL_ENV_VAR;
    const API_SUFFIX: &'static str = PAYMENT_API_PATH;

    fn from_client(client: WebClient) -> Self {
        let config = ProviderApiConfig::default();
        PaymentProviderApi::new(&Arc::new(client), config)
    }
}

impl PaymentProviderApi {
    pub fn new(client: &Arc<WebClient>, config: ProviderApiConfig) -> Self {
        Self {
            client: client.clone(),
            config,
        }
    }

    pub async fn issue_debit_note(&self, debit_note: &NewDebitNote) -> Result<DebitNote> {
        self.client
            .post("provider/debitNotes")
            .send_json(debit_note)
            .json()
            .await
    }

    pub async fn get_debit_notes(&self) -> Result<Vec<DebitNote>> {
        self.client.get("provider/debitNotes").send().json().await
    }

    pub async fn get_debit_note(&self, debit_note_id: &str) -> Result<DebitNote> {
        let url = url_format!("provider/debitNotes/{debit_note_id}", debit_note_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_payments_for_debit_note(&self, debit_note_id: &str) -> Result<Vec<Payment>> {
        let url = url_format!(
            "provider/debitNotes/{debit_note_id}/payments",
            debit_note_id
        );
        self.client.get(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn send_debit_note(&self, debit_note_id: &str) -> Result<()> {
        let timeout = self.config.send_debit_note_timeout;
        let url = url_format!(
            "provider/debitNotes/{debit_note_id}/send",
            debit_note_id,
            #[query] timeout
        );
        self.client.post(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn cancel_debit_note(&self, debit_note_id: &str) -> Result<()> {
        let timeout = self.config.cancel_debit_note_timeout;
        let url = url_format!(
            "provider/debitNotes/{debit_note_id}/cancel",
            debit_note_id,
            #[query] timeout
        );
        self.client.post(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_debit_note_events<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<DebitNoteEvent>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "provider/debitNoteEvents",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await
    }

    pub async fn issue_invoice(&self, invoice: &NewInvoice) -> Result<Invoice> {
        self.client
            .post("provider/invoices")
            .send_json(invoice)
            .json()
            .await
    }

    pub async fn get_invoices(&self) -> Result<Vec<Invoice>> {
        self.client.get("provider/invoices").send().json().await
    }

    pub async fn get_invoice(&self, invoice_id: &str) -> Result<Invoice> {
        let url = url_format!("provider/invoices/{invoice_id}", invoice_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_payments_for_invoice(&self, invoice_id: &str) -> Result<Vec<Payment>> {
        let url = url_format!("provider/invoices/{invoice_id}/payments", invoice_id);
        self.client.get(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn send_invoice(&self, invoice_id: &str) -> Result<()> {
        let timeout = self.config.send_invoice_timeout;
        let url = url_format!(
            "provider/invoices/{invoice_id}/send",
            invoice_id,
            #[query] timeout
        );
        self.client.post(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn cancel_invoice(&self, invoice_id: &str) -> Result<()> {
        let timeout = self.config.cancel_invoice_timeout;
        let url = url_format!(
            "provider/invoices/{invoice_id}/cancel",
            invoice_id,
            #[query] timeout
        );
        self.client.post(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_invoice_events<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<InvoiceEvent>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "provider/invoiceEvents",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_payments<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<Payment>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "provider/payments",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await
    }

    pub async fn get_payment(&self, payment_id: &str) -> Result<Payment> {
        let url = url_format!("provider/payments/{payment_id}", payment_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_accounts(&self) -> Result<Vec<Account>> {
        self.client.get("provider/accounts").send().json().await
    }
}