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
use util::{Result, List};
use stripe::{SourceType, Currency};
use Client;
use serde;
use std::collections::HashMap;

#[derive(Deserialize, Debug)]
pub struct Payout {
    pub id: String,
    pub object: String,
    pub amount: i64,
    pub arrival_date: i64,
    pub automatic: bool,
    pub balance_transaction: String,
    pub created: i64,
    pub currency: Currency,
    pub description: String,
    pub destination: String,
    pub failure_balance_transaction: Option<String>,
    pub failure_code: Option<PayoutFailureCode>,
    pub failure_message: Option<String>,
    pub livemode: bool,
    pub metadata: HashMap<String, String>,
    pub method: PayoutMethod,
    pub source_type: SourceType,
    pub statement_descriptor: Option<String>,
    pub status: PayoutStatus,
    #[serde(rename = "type")]
    pub payout_type: PayoutType
}

#[derive(Deserialize, Debug)]
pub enum PayoutStatus {
    #[serde(rename = "paid")]
    Paid,
    #[serde(rename = "pending")]
    Pending,
    #[serde(rename = "in_transit")]
    InTransit,
    #[serde(rename = "canceled")]
    Canceled,
    #[serde(rename = "failed")]
    Failed
}

#[derive(Serialize, Deserialize, Debug)]
pub enum PayoutType {
    #[serde(rename = "bank_account")]
    BankAccount,
    #[serde(rename = "card")]
    Card,
    #[serde(rename = "alipay_account")]
    AlipayAccount,
    #[serde(rename = "bitcoin_receiver")]
    BitcoinReceiver
}

#[derive(Deserialize, Debug)]
pub enum PayoutFailureCode {
    #[serde(rename = "account_closed")]
    AccountClosed,
    #[serde(rename = "account_frozen")]
    AccountFrozen,
    #[serde(rename = "bank_account_restricted")]
    BankAccountRestricted,
    #[serde(rename = "bank_ownership_changed")]
    BankOwnershipChanged,
    #[serde(rename = "could_not_process")]
    CouldNotProcess,
    #[serde(rename = "debit_not_authorized")]
    DebitNotAuthorized,
    #[serde(rename = "insufficient_funds")]
    InsufficientFunds,
    #[serde(rename = "invalid_account_number")]
    InvalidAccountNumber,
    #[serde(rename = "invalid_currency")]
    InvalidCurrency,
    #[serde(rename = "no_account")]
    NoAccount,
    #[serde(rename = "unsupported_card")]
    UnsupportedCard
}

#[derive(Serialize, Deserialize, Debug)]
pub enum PayoutMethod {
    #[serde(rename = "standard")]
    STANDARD,
    #[serde(rename = "instant")]
    INSTANT
}


#[derive(Default, Serialize, Debug)]
pub struct PayoutParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<PayoutMethod>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_type: Option<PayoutType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_descriptor: Option<&'a str>,
}

impl Payout {
    
    pub fn create<B: serde::Serialize>(client: &Client, param: B) -> Result<Payout> {
        client.post("/payouts", param)
    }

    pub fn retrieve(client: &Client, id: &str) -> Result<Payout> {
        client.get_empty(&format!("/payouts/{}", id))
    }

    pub fn update<B: serde::Serialize>(client: &Client, id: &str, param: B) -> Result<Payout> {
        client.post(&format!("/payouts/{}", id), param)
    }

    pub fn list<B: serde::Serialize>(client: &Client, param: B) -> Result<List<Payout>> {
        client.get("/payouts", param)
    }

    pub fn cancel(client: &Client, id: &str) -> Result<Payout> {
        client.post_empty(&format!("/payouts/{}/cancel", id))
    }

}