swish-api 0.1.0

API bindings for the Swish Payment API
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! # The SwishClient
//!
//! This is the client that's used to make calls to the Swish API.
//!
use error::{RequestError, SwishClientError};
use futures::stream::Stream;
use futures::{future, Future};
use hyper::client::HttpConnector;
use hyper::header::{self, HeaderValue, CONTENT_TYPE, LOCATION};
use hyper::Client as HttpClient;
use hyper::StatusCode;
use hyper::{self, Body, Request, Uri};
use hyper_tls::HttpsConnector;
use native_tls::{Identity, TlsConnector};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json;
use std::error;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
use std::str;
use tokio_core::reactor::Handle;

/// The client used to make call to the Swish API.
#[derive(Debug)]
pub struct SwishClient {
    merchant_swish_number: String,
    swish_api_url: String,
    passphrase: String,
    cert_path: String,
    handle: Handle,
}

/// This is what will be returned when a payment is
/// successfully created at Swish.
#[derive(Debug, Serialize, Deserialize)]
pub struct CreatedPayment {
    pub id: String,
    pub location: String,
    pub request_token: Option<String>,
}

/// This is all the data that's returned from the
/// Swish API when fetching a payment.
#[derive(Debug, Deserialize, Clone)]
pub struct Payment {
    pub id: String,
    pub amount: f64,
    #[serde(rename = "payeePaymentReference")]
    pub payee_payment_reference: Option<String>,
    #[serde(rename = "paymentReference")]
    pub payment_reference: Option<String>,
    #[serde(rename = "payerAlias")]
    pub payer_alias: Option<String>,
    #[serde(rename = "payeeAlias")]
    pub payee_alias: Option<String>,

    pub message: Option<String>,
    pub status: Option<Status>,
    #[serde(rename = "dateCreated")]
    pub date_created: String,
    pub currency: Currency,
    #[serde(rename = "datePaid")]
    pub date_paid: Option<String>,

    // Errors can occur
    #[serde(rename = "errorCode")]
    pub error_code: Option<String>,
    #[serde(rename = "errorMessage")]
    pub error_message: Option<String>,
}

/// The status of an operation.
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub enum Status {
    #[serde(rename = "CREATED")]
    Created,
    #[serde(rename = "PAID")]
    Paid,
    #[serde(rename = "ERROR")]
    Error,
    #[serde(rename = "VALIDATED")]
    Validated,
    #[serde(rename = "INITIATED")]
    Initiated,
}

/// Params used to create a new payment.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PaymentParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payee_payment_reference: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payer_alias: Option<&'a str>,
    pub payee_alias: &'a str,

    pub amount: f64,
    currency: Currency,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<&'a str>,
    pub callback_url: &'a str,
}

/// Params used to create a new refund.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RefundParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payer_payment_reference: Option<&'a str>,
    pub original_payment_reference: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payment_reference: Option<&'a str>,
    pub payer_alias: &'a str,
    pub payee_alias: &'a str,
    pub amount: f64,
    currency: Currency,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<&'a str>,
    pub callback_url: &'a str,
}

/// The currency the Swish API supports.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum Currency {
    /// SEK is currently the only currency supported at Swish.
    SEK,
}

impl Default for Currency {
    fn default() -> Self {
        Currency::SEK
    }
}

/// This will be returned when a refund
/// is successfully created.
#[derive(Debug, Serialize, Deserialize)]
pub struct CreatedRefund {
    pub id: String,
    pub location: String,
}

/// This is all the data that's returned
/// from the Swish API when fetching a refund.
#[derive(Debug, Deserialize, Clone)]
pub struct Refund {
    pub id: String,
    pub amount: f64,
    #[serde(rename = "payerPaymentReference")]
    pub payer_payment_reference: Option<String>,
    #[serde(rename = "originalpaymentReference")]
    pub original_payment_reference: Option<String>,
    #[serde(rename = "payerAlias")]
    pub payer_alias: Option<String>,
    #[serde(rename = "payeeAlias")]
    pub payee_alias: Option<String>,

    pub message: Option<String>,
    pub status: Option<Status>,
    #[serde(rename = "dateCreated")]
    pub date_created: String,
    pub currency: Currency,
    #[serde(rename = "datePaid")]
    pub date_paid: Option<String>,

    // Errors can occur
    #[serde(rename = "errorCode")]
    pub error_code: Option<String>,
    #[serde(rename = "errorMessage")]
    pub error_message: Option<String>,
    #[serde(rename = "additionalInformation")]
    pub additional_information: Option<String>,
}

/// Custom Header returned by the Swish API.
const PAYMENT_REQUEST_TOKEN: &str = "paymentrequesttoken";

/// Type alias for Future used within the SwishClient
type SwishBoxFuture<'a, T> = Box<Future<Item = T, Error = SwishClientError> + 'a>;

impl SwishClient {
    /// [`SwishClient`]: struct.SwishClient.html
    ///
    /// Creates a new SwishClient
    ///
    /// # Arguments
    ///
    /// * `merchant_swish_number` - The merchants swish number which will receive the payments.
    /// * `cert_path` - The path to the certificate.
    /// * `passphrase` - The passphrase to the certificate.
    /// * `handle` - A tokio reactor handle.
    ///
    /// # Returns
    /// A configured [`SwishClient`].
    ///
    /// # Example
    ///
    /// ```
    /// extern crate tokio_core;
    /// extern crate swish_api;
    ///
    /// use swish_api::client::SwishClient;
    /// use tokio_core::reactor::Core;
    /// use std::env;
    ///
    /// let core = Core::new().unwrap();
    /// let handle = core.handle();
    /// let current_dir = env::current_dir().unwrap();
    /// let cert_path = current_dir.join("./certs/test_cert.p12");
    /// let swish_client = cert_path
    ///    .into_os_string()
    ///    .to_str()
    ///    .map(|cert_path_string| {
    ///        SwishClient::new("1231181189", cert_path_string, "swish", handle)
    ///    }).unwrap();
    /// ```
    pub fn new(
        merchant_swish_number: &str,
        cert_path: &str,
        passphrase: &str,
        handle: Handle,
    ) -> Self {
        SwishClient {
            merchant_swish_number: merchant_swish_number.to_owned(),
            swish_api_url: "https://mss.cpc.getswish.net/swish-cpcapi/api/v1/".to_owned(),
            passphrase: passphrase.to_owned(),
            cert_path: cert_path.to_owned(),
            handle,
        }
    }

    /// [`PaymentParams`]: struct.PaymentParams.html
    /// [`CreatedPayment`]: struct.CreatedPayment.html
    ///
    /// Creates a payment with the provided [`PaymentParams`].
    ///
    /// # Returns
    /// A Future with a [`CreatedPayment`].
    ///
    /// # Arguments
    ///
    /// * `params` - [`PaymentParams`].
    ///
    /// # Example
    ///
    /// ```
    /// extern crate tokio_core;
    /// extern crate swish_api;
    ///
    /// use tokio_core::reactor::Core;
    /// use std::env;
    /// use swish_api::client::{PaymentParams, SwishClient};
    ///
    /// let core = Core::new().unwrap();
    /// let handle = core.handle();
    /// let current_dir = env::current_dir().unwrap();
    /// let cert_path = current_dir.join("./tests/test_cert.p12");
    /// let root_cert_path = current_dir.join("./tests/root_cert.der");
    /// let swish_client = cert_path
    ///    .into_os_string()
    ///    .to_str()
    ///    .map(|cert_path_string| {
    ///        SwishClient::new("1231181189", cert_path_string, "swish", handle)
    ///    }).unwrap();
    ///
    /// let mut payment_params = PaymentParams::default();
    /// payment_params.amount = 100.00;
    /// payment_params.payee_alias = "1231181189";
    /// payment_params.payee_payment_reference = Some("0123456789");
    /// payment_params.callback_url = "https://example.com/api/swishcb/paymentrequests";
    /// payment_params.message = Some("Kingston USB Flash Drive 8 GB");
    ///
    /// let payment = swish_client.create_payment(payment_params);
    ///
    /// ```
    pub fn create_payment<'a>(
        &'a self,
        params: PaymentParams,
    ) -> SwishBoxFuture<'a, CreatedPayment> {
        let payment_params = PaymentParams {
            payee_alias: self.merchant_swish_number.as_str(),
            ..params
        };

        let response: SwishBoxFuture<'a, (String, header::HeaderMap)> =
            self.post::<CreatedPayment, PaymentParams>("paymentrequests", payment_params);

        let payment_future = response.and_then(move |(_, headers)| {
            let location = get_header_as_string(&headers, LOCATION);
            let request_token = get_header_as_string(
                &headers,
                header::HeaderName::from_static(PAYMENT_REQUEST_TOKEN),
            );

            let payment = location.and_then(|location| {
                self.get_payment_id_from_location(&location)
                    .map(|payment_id| CreatedPayment {
                        id: payment_id,
                        request_token,
                        location,
                    })
            });

            future::result(serde_json::from_value(json!(payment)).map_err(SwishClientError::from))
        });
        Box::new(payment_future)
    }

    /// [`Payment`]: struct.Payment.html
    ///
    /// Gets a payment for a given `payment_id`.
    ///
    /// # Returns
    /// A Future with a [`Payment`].
    ///
    /// # Arguments
    ///
    /// * `payment_id` - A string id for a payment
    ///
    /// # Example
    ///
    /// ```
    /// extern crate tokio_core;
    /// extern crate swish_api;
    ///
    /// use tokio_core::reactor::Core;
    /// use std::env;
    /// use swish_api::client::SwishClient;
    ///
    /// let core = Core::new().unwrap();
    /// let handle = core.handle();
    /// let current_dir = env::current_dir().unwrap();
    /// let cert_path = current_dir.join("./tests/test_cert.p12");
    /// let root_cert_path = current_dir.join("./tests/root_cert.der");
    /// let swish_client = cert_path
    ///    .into_os_string()
    ///    .to_str()
    ///    .map(|cert_path_string| {
    ///        SwishClient::new("1231181189", cert_path_string, "swish", handle)
    ///    }).unwrap();
    ///
    /// let payment_id = "111";
    /// let payment = swish_client.get_payment(payment_id);
    /// ```
    pub fn get_payment<'a>(&'a self, payment_id: &str) -> SwishBoxFuture<'a, Payment> {
        self.get(format!("paymentrequests/{}", payment_id).as_str())
    }

    /// [`RefundParams`]: struct.RefundParams.html
    /// [`CreatedRefund`]: struct.CreatedRefund.html
    ///
    /// Creates a refund with the provided [`RefundParams`].
    ///
    /// # Returns
    /// A Future with a [`CreatedRefund`].
    ///
    /// # Arguments
    ///
    /// * `params` - [`RefundParams`].
    ///
    /// # Example
    ///
    /// ```
    /// extern crate tokio_core;
    /// extern crate swish_api;
    ///
    /// use tokio_core::reactor::Core;
    /// use std::env;
    /// use swish_api::client::{RefundParams, SwishClient};
    ///
    /// let core = Core::new().unwrap();
    /// let handle = core.handle();
    /// let current_dir = env::current_dir().unwrap();
    /// let cert_path = current_dir.join("./tests/test_cert.p12");
    /// let root_cert_path = current_dir.join("./tests/root_cert.der");
    /// let swish_client = cert_path
    ///    .into_os_string()
    ///    .to_str()
    ///    .map(|cert_path_string| {
    ///        SwishClient::new("1231181189", cert_path_string, "swish", handle)
    ///    }).unwrap();
    ///
    /// let mut refund_params = RefundParams::default();
    /// refund_params.amount = 100.00;
    /// refund_params.callback_url = "https://example.com/api/swishcb/refunds";
    /// refund_params.payer_payment_reference = Some("0123456789");
    /// refund_params.message = Some("Refund for Kingston USB Flash Drive 8 GB");
    ///
    /// let refund = swish_client.create_refund(refund_params);
    /// ```
    pub fn create_refund<'a>(&'a self, params: RefundParams) -> SwishBoxFuture<'a, CreatedRefund> {
        let refund_params = RefundParams {
            payer_alias: self.merchant_swish_number.as_str(),
            ..params
        };

        let response = self.post::<CreatedRefund, RefundParams>("refunds", refund_params);

        let refund_future = response.and_then(move |(_, headers)| {
            let location = get_header_as_string(&headers, LOCATION);

            let refund = location.and_then(|location| {
                self.get_payment_id_from_location(&location)
                    .map(|refund_id| CreatedRefund {
                        id: refund_id,
                        location,
                    })
            });

            future::result(serde_json::from_value(json!(refund)).map_err(SwishClientError::from))
        });
        Box::new(refund_future)
    }

    /// [`Refund`]: struct.Refund.html
    ///
    /// Gets a refund for a given `refund_id`.
    ///
    /// # Returns
    /// A Future with a [`Refund`].
    ///
    /// # Arguments
    ///
    /// * `refund_id` - A string id for a refund
    ///
    /// # Example
    ///
    /// ```
    /// extern crate tokio_core;
    /// extern crate swish_api;
    ///
    /// use tokio_core::reactor::Core;
    /// use std::env;
    /// use swish_api::client::SwishClient;
    ///
    /// let core = Core::new().unwrap();
    /// let handle = core.handle();
    /// let current_dir = env::current_dir().unwrap();
    /// let cert_path = current_dir.join("./tests/test_cert.p12");
    /// let root_cert_path = current_dir.join("./tests/root_cert.der");
    /// let swish_client = cert_path
    ///    .into_os_string()
    ///    .to_str()
    ///    .map(|cert_path_string| {
    ///        SwishClient::new("1231181189", cert_path_string, "swish", handle)
    ///    }).unwrap();
    ///
    /// let refund_id = "111";
    /// let refund = swish_client.get_refund(refund_id);
    /// ```
    pub fn get_refund<'a>(&'a self, refund_id: &str) -> SwishBoxFuture<'a, Refund> {
        self.get(format!("refunds/{}", refund_id).as_str())
    }

    /// Reads a given cert into a Vec.
    /// Returns a Result that contains the Vec if it succeeded.
    ///
    /// # Arguments
    ///
    /// * `cert_path` - A string path to the place where the cert is
    fn read_cert(&self, cert_path: &str) -> Result<Vec<u8>, io::Error> {
        let cert_path = Path::new(&cert_path);
        let mut buf = vec![];
        let _result = File::open(cert_path).and_then(|mut f| f.read_to_end(&mut buf));
        Ok(buf)
    }

    /// Build a HTTPS client with the root_cert and the client_cert.
    /// # Returns
    /// A Result that contains the client if it succeeded.
    fn build_client(
        &self,
    ) -> Result<HttpClient<HttpsConnector<HttpConnector>, Body>, Box<error::Error>> {
        let pkcs12_cert = &self.read_cert(&self.cert_path)?;
        let client_cert = Identity::from_pkcs12(&pkcs12_cert, &self.passphrase)?;

        let tls_connector = TlsConnector::builder().identity(client_cert).build()?;

        let mut http_connector = HttpConnector::new(4);
        http_connector.enforce_http(false);

        let https_connector = HttpsConnector::from((http_connector, tls_connector));

        let client = hyper::client::Client::builder().build(https_connector);

        Ok(client)
    }

    /// Performs a http POST request to the Swish API.
    ///
    /// # Returns
    /// A Future with a Tuple that contains the body as a String
    /// and the Response headers.
    ///
    /// # Arguments
    ///
    /// * `path` - A string path
    /// * `params` - Params that implements Serialize which are json sent as the body
    fn post<'a, T: 'a, P>(
        &'a self,
        path: &str,
        params: P,
    ) -> SwishBoxFuture<'a, (String, hyper::header::HeaderMap)>
    where
        T: DeserializeOwned + fmt::Debug,
        P: Serialize,
    {
        let future_result: Result<_, SwishClientError> = self
            .get_uri(path)
            .and_then(|uri| {
                serde_json::to_string(&params)
                    .and_then(|json_params| {
                        let mut request = Request::post(uri.to_owned())
                            .body(Body::from(json_params))
                            .unwrap();
                        request
                            .headers_mut()
                            .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

                        Ok(self.perform_swish_api_request(request))
                    }).map_err(SwishClientError::from)
            }).and_then(Ok);
        Box::new(future::result(future_result).flatten())
    }

    /// Performs a http GET request to the Swish API.
    ///
    /// # Returns
    /// A Future with a Tuple that contains the body as a String
    /// and the Response headers.
    ///
    /// # Arguments
    ///
    /// * `path` - A string path
    fn get<'a, T: 'a>(&'a self, path: &str) -> SwishBoxFuture<'a, T>
    where
        T: DeserializeOwned + fmt::Debug,
    {
        let uri = self.get_uri(path).unwrap();
        let request = Request::get(uri).body(Body::empty()).unwrap();

        let future = self
            .perform_swish_api_request(request)
            .and_then(move |(body, _)| future::result(self.parse_body::<T>(&body)));
        Box::new(future)
    }

    /// Parse body as json.
    ///
    /// # Arguments
    ///
    /// * `body` - A string body
    fn parse_body<T>(&self, body: &str) -> Result<T, SwishClientError>
    where
        T: DeserializeOwned + fmt::Debug,
    {
        serde_json::from_str(&body).map_err(SwishClientError::from)
    }

    /// Parse a given string path into an Uri.
    ///
    /// # Arguments
    ///
    /// * `path` - A string path
    fn get_uri(&self, path: &str) -> Result<Uri, SwishClientError> {
        format!("{}{}", self.swish_api_url, path)
            .parse::<Uri>()
            .map_err(SwishClientError::from)
    }

    /// Gets a payment_id from a given location header string.
    ///
    /// # Arguments
    ///
    /// * `location` - A string location header
    fn get_payment_id_from_location(&self, location: &str) -> Option<String> {
        let payment_id: Vec<&str> = location.split('/').collect();
        payment_id.last().cloned().map(|id| id.to_string())
    }

    /// Performs the actual request to the Swish API.
    /// # Returns
    /// A Future with a Tuple that contains the body as a String
    /// and the Response headers.
    fn perform_swish_api_request<'a>(
        &'a self,
        request: Request<hyper::Body>,
    ) -> SwishBoxFuture<'a, (String, hyper::HeaderMap)> {
        let client = self
            .build_client()
            .expect("The HttpsClient couldn't be built, the certificate is probably wrong");

        let future = client
            .request(request)
            .map_err(SwishClientError::from)
            .and_then(move |response| {
                let status = response.status();
                let headers = response.headers().to_owned();

                response
                    .into_body()
                    .concat2()
                    .map_err(SwishClientError::from)
                    .and_then(move |body| {
                        let body = str::from_utf8(&body).unwrap();

                        if status == StatusCode::NOT_FOUND {
                            let error = RequestError {
                                http_status: StatusCode::NOT_FOUND,
                                code: None,
                                additional_information: None,
                                message: body.to_owned(),
                            };
                            return future::err(SwishClientError::from(error));
                        }

                        if !status.is_success() {
                            // Swish can sometimes return an array of errors.
                            // TODO: http_status is wrong, set it to the actually status.
                            let errors: SwishClientError =
                                match serde_json::from_str::<serde_json::Value>(body) {
                                    Ok(json) => {
                                        let errors: Vec<_> = json
                                            .as_array()
                                            .into_iter()
                                            .flat_map(|e| {
                                                e.iter()
                                                    .flat_map(|err| {
                                                        serde_json::from_value::<RequestError>(
                                                            err.clone(),
                                                        )
                                                    }).map(|request_error| RequestError {
                                                        http_status: status,
                                                        ..request_error
                                                    }).map(SwishClientError::from)
                                                    .collect::<Vec<SwishClientError>>()
                                            }).collect();
                                        SwishClientError::from(errors)
                                    }
                                    Err(err) => {
                                        let error = RequestError {
                                            additional_information: None,
                                            code: None,
                                            http_status: status,
                                            message: err.to_string(),
                                        };
                                        SwishClientError::from(error)
                                    }
                                };
                            return future::err(errors);
                        }
                        future::result(Ok((body.to_owned(), headers)))
                    })
            });
        Box::new(future)
    }
}

/// Gets a hyper::Header and turns it into a String.
///
/// # Arguments
///
/// * `headers` - hyper::Headers
fn get_header_as_string(
    headers: &hyper::header::HeaderMap,
    header: hyper::header::HeaderName,
) -> Option<String> {
    headers
        .get(header)
        .and_then(|h| h.to_str().ok().map(|h| h.to_string()))
}