twilio_rust_openapi/apis/
api20100401_payment_api.rs

1/*
2 * Twilio - Api
3 *
4 * This is the public Twilio REST API.
5 *
6 * The version of the OpenAPI document: 1.0.0
7 * Contact: support@twilio.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17/// struct for passing parameters to the method [`create_payments`]
18#[derive(Clone, Debug)]
19pub struct CreatePaymentsParams {
20    /// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource.
21    pub account_sid: String,
22    /// The SID of the call that will create the resource. Call leg associated with this sid is expected to provide payment information thru DTMF.
23    pub call_sid: String,
24    /// A unique token that will be used to ensure that multiple API calls with the same information do not result in multiple transactions. This should be a unique string value per API call and can be a randomly generated.
25    pub idempotency_key: String,
26    /// Provide an absolute or relative URL to receive status updates regarding your Pay session. Read more about the [expected StatusCallback values](https://www.twilio.com/docs/voice/api/payment-resource#statuscallback)
27    pub status_callback: String,
28    pub bank_account_type: Option<String>,
29    /// A positive decimal value less than 1,000,000 to charge against the credit card or bank account. Default currency can be overwritten with `currency` field. Leave blank or set to 0 to tokenize.
30    pub charge_amount: Option<f64>,
31    /// The currency of the `charge_amount`, formatted as [ISO 4127](http://www.iso.org/iso/home/standards/currency_codes.htm) format. The default value is `USD` and all values allowed from the Pay Connector are accepted.
32    pub currency: Option<String>,
33    /// The description can be used to provide more details regarding the transaction. This information is submitted along with the payment details to the Payment Connector which are then posted on the transactions.
34    pub description: Option<String>,
35    /// A list of inputs that should be accepted. Currently only `dtmf` is supported. All digits captured during a pay session are redacted from the logs.
36    pub input: Option<String>,
37    /// A positive integer that is used to validate the length of the `PostalCode` inputted by the user. User must enter this many digits.
38    pub min_postal_code_length: Option<i32>,
39    /// A single-level JSON object used to pass custom parameters to payment processors. (Required for ACH payments). The information that has to be included here depends on the <Pay> Connector. [Read more](https://www.twilio.com/console/voice/pay-connectors).
40    pub parameter: Option<serde_json::Value>,
41    /// This is the unique name corresponding to the Pay Connector installed in the Twilio Add-ons. Learn more about [<Pay> Connectors](https://www.twilio.com/console/voice/pay-connectors). The default value is `Default`.
42    pub payment_connector: Option<String>,
43    pub payment_method: Option<String>,
44    /// Indicates whether the credit card postal code (zip code) is a required piece of payment information that must be provided by the caller. The default is `true`.
45    pub postal_code: Option<bool>,
46    /// Indicates whether the credit card security code is a required piece of payment information that must be provided by the caller. The default is `true`.
47    pub security_code: Option<bool>,
48    /// The number of seconds that <Pay> should wait for the caller to press a digit between each subsequent digit, after the first one, before moving on to validate the digits captured. The default is `5`, maximum is `600`.
49    pub timeout: Option<i32>,
50    pub token_type: Option<String>,
51    /// Credit card types separated by space that Pay should accept. The default value is `visa mastercard amex`
52    pub valid_card_types: Option<String>
53}
54
55/// struct for passing parameters to the method [`update_payments`]
56#[derive(Clone, Debug)]
57pub struct UpdatePaymentsParams {
58    /// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will update the resource.
59    pub account_sid: String,
60    /// The SID of the call that will update the resource. This should be the same call sid that was used to create payments resource.
61    pub call_sid: String,
62    /// The SID of Payments session that needs to be updated.
63    pub sid: String,
64    /// A unique token that will be used to ensure that multiple API calls with the same information do not result in multiple transactions. This should be a unique string value per API call and can be a randomly generated.
65    pub idempotency_key: String,
66    /// Provide an absolute or relative URL to receive status updates regarding your Pay session. Read more about the [Update](https://www.twilio.com/docs/voice/api/payment-resource#statuscallback-update) and [Complete/Cancel](https://www.twilio.com/docs/voice/api/payment-resource#statuscallback-cancelcomplete) POST requests.
67    pub status_callback: String,
68    pub capture: Option<String>,
69    pub status: Option<String>
70}
71
72
73/// struct for typed errors of method [`create_payments`]
74#[derive(Debug, Clone, Serialize, Deserialize)]
75#[serde(untagged)]
76pub enum CreatePaymentsError {
77    UnknownValue(serde_json::Value),
78}
79
80/// struct for typed errors of method [`update_payments`]
81#[derive(Debug, Clone, Serialize, Deserialize)]
82#[serde(untagged)]
83pub enum UpdatePaymentsError {
84    UnknownValue(serde_json::Value),
85}
86
87
88/// create an instance of payments. This will start a new payments session
89pub async fn create_payments(configuration: &configuration::Configuration, params: CreatePaymentsParams) -> Result<models::ApiPeriodV2010PeriodAccountPeriodCallPeriodPayments, Error<CreatePaymentsError>> {
90    let local_var_configuration = configuration;
91
92    // unbox the parameters
93    let account_sid = params.account_sid;
94    let call_sid = params.call_sid;
95    let idempotency_key = params.idempotency_key;
96    let status_callback = params.status_callback;
97    let bank_account_type = params.bank_account_type;
98    let charge_amount = params.charge_amount;
99    let currency = params.currency;
100    let description = params.description;
101    let input = params.input;
102    let min_postal_code_length = params.min_postal_code_length;
103    let parameter = params.parameter;
104    let payment_connector = params.payment_connector;
105    let payment_method = params.payment_method;
106    let postal_code = params.postal_code;
107    let security_code = params.security_code;
108    let timeout = params.timeout;
109    let token_type = params.token_type;
110    let valid_card_types = params.valid_card_types;
111
112
113    let local_var_client = &local_var_configuration.client;
114
115    let local_var_uri_str = format!("{}/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Payments.json", local_var_configuration.base_path, AccountSid=crate::apis::urlencode(account_sid), CallSid=crate::apis::urlencode(call_sid));
116    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
117
118    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
119        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
120    }
121    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
122        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
123    };
124    let mut local_var_form_params = std::collections::HashMap::new();
125    local_var_form_params.insert("IdempotencyKey", idempotency_key.to_string());
126    local_var_form_params.insert("StatusCallback", status_callback.to_string());
127    if let Some(local_var_param_value) = bank_account_type {
128        local_var_form_params.insert("BankAccountType", local_var_param_value.to_string());
129    }
130    if let Some(local_var_param_value) = charge_amount {
131        local_var_form_params.insert("ChargeAmount", local_var_param_value.to_string());
132    }
133    if let Some(local_var_param_value) = currency {
134        local_var_form_params.insert("Currency", local_var_param_value.to_string());
135    }
136    if let Some(local_var_param_value) = description {
137        local_var_form_params.insert("Description", local_var_param_value.to_string());
138    }
139    if let Some(local_var_param_value) = input {
140        local_var_form_params.insert("Input", local_var_param_value.to_string());
141    }
142    if let Some(local_var_param_value) = min_postal_code_length {
143        local_var_form_params.insert("MinPostalCodeLength", local_var_param_value.to_string());
144    }
145    if let Some(local_var_param_value) = parameter {
146        local_var_form_params.insert("Parameter", local_var_param_value.to_string());
147    }
148    if let Some(local_var_param_value) = payment_connector {
149        local_var_form_params.insert("PaymentConnector", local_var_param_value.to_string());
150    }
151    if let Some(local_var_param_value) = payment_method {
152        local_var_form_params.insert("PaymentMethod", local_var_param_value.to_string());
153    }
154    if let Some(local_var_param_value) = postal_code {
155        local_var_form_params.insert("PostalCode", local_var_param_value.to_string());
156    }
157    if let Some(local_var_param_value) = security_code {
158        local_var_form_params.insert("SecurityCode", local_var_param_value.to_string());
159    }
160    if let Some(local_var_param_value) = timeout {
161        local_var_form_params.insert("Timeout", local_var_param_value.to_string());
162    }
163    if let Some(local_var_param_value) = token_type {
164        local_var_form_params.insert("TokenType", local_var_param_value.to_string());
165    }
166    if let Some(local_var_param_value) = valid_card_types {
167        local_var_form_params.insert("ValidCardTypes", local_var_param_value.to_string());
168    }
169    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
170
171    let local_var_req = local_var_req_builder.build()?;
172    let local_var_resp = local_var_client.execute(local_var_req).await?;
173
174    let local_var_status = local_var_resp.status();
175    let local_var_content = local_var_resp.text().await?;
176
177    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
178        serde_json::from_str(&local_var_content).map_err(Error::from)
179    } else {
180        let local_var_entity: Option<CreatePaymentsError> = serde_json::from_str(&local_var_content).ok();
181        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
182        Err(Error::ResponseError(local_var_error))
183    }
184}
185
186/// update an instance of payments with different phases of payment flows.
187pub async fn update_payments(configuration: &configuration::Configuration, params: UpdatePaymentsParams) -> Result<models::ApiPeriodV2010PeriodAccountPeriodCallPeriodPayments, Error<UpdatePaymentsError>> {
188    let local_var_configuration = configuration;
189
190    // unbox the parameters
191    let account_sid = params.account_sid;
192    let call_sid = params.call_sid;
193    let sid = params.sid;
194    let idempotency_key = params.idempotency_key;
195    let status_callback = params.status_callback;
196    let capture = params.capture;
197    let status = params.status;
198
199
200    let local_var_client = &local_var_configuration.client;
201
202    let local_var_uri_str = format!("{}/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Payments/{Sid}.json", local_var_configuration.base_path, AccountSid=crate::apis::urlencode(account_sid), CallSid=crate::apis::urlencode(call_sid), Sid=crate::apis::urlencode(sid));
203    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
204
205    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
206        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
207    }
208    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
209        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
210    };
211    let mut local_var_form_params = std::collections::HashMap::new();
212    local_var_form_params.insert("IdempotencyKey", idempotency_key.to_string());
213    local_var_form_params.insert("StatusCallback", status_callback.to_string());
214    if let Some(local_var_param_value) = capture {
215        local_var_form_params.insert("Capture", local_var_param_value.to_string());
216    }
217    if let Some(local_var_param_value) = status {
218        local_var_form_params.insert("Status", local_var_param_value.to_string());
219    }
220    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
221
222    let local_var_req = local_var_req_builder.build()?;
223    let local_var_resp = local_var_client.execute(local_var_req).await?;
224
225    let local_var_status = local_var_resp.status();
226    let local_var_content = local_var_resp.text().await?;
227
228    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
229        serde_json::from_str(&local_var_content).map_err(Error::from)
230    } else {
231        let local_var_entity: Option<UpdatePaymentsError> = serde_json::from_str(&local_var_content).ok();
232        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
233        Err(Error::ResponseError(local_var_error))
234    }
235}
236