stripe/resources/
payment_intent_ext.rs

1use serde::{Deserialize, Serialize};
2
3use crate::client::{Client, Response};
4use crate::params::{Expandable, Metadata, SearchList};
5use crate::resources::{Currency, PaymentSource, Shipping};
6use crate::{PaymentIntent, PaymentIntentCancellationReason};
7
8impl PaymentIntent {
9    /// Confirm that customer intends to pay with current or provided source. Upon confirmation, the PaymentIntent will attempt to initiate a payment.
10    ///
11    /// For more details see <https://stripe.com/docs/api/payment_intents/confirm>.
12    pub fn confirm(
13        client: &Client,
14        payment_intent_id: &str,
15        params: PaymentIntentConfirmParams<'_>,
16    ) -> Response<PaymentIntent> {
17        client.post_form(&format!("/payment_intents/{}/confirm", payment_intent_id), params)
18    }
19
20    /// Capture the funds of an existing uncaptured PaymentIntent where required_action="requires_capture".
21    ///
22    /// For more details see <https://stripe.com/docs/api/payment_intents/capture>.
23    pub fn capture(
24        client: &Client,
25        payment_intent_id: &str,
26        params: CapturePaymentIntent,
27    ) -> Response<PaymentIntent> {
28        client.post_form(&format!("/payment_intents/{}/capture", payment_intent_id), params)
29    }
30
31    /// A PaymentIntent object can be canceled when it is in one of these statuses: requires_source, requires_capture, requires_confirmation, requires_source_action.
32    ///
33    /// For more details see <https://stripe.com/docs/api/payment_intents/cancel>.
34    pub fn cancel(
35        client: &Client,
36        payment_intent_id: &str,
37        params: CancelPaymentIntent,
38    ) -> Response<PaymentIntent> {
39        client.post_form(&format!("/payment_intents/{}/cancel", payment_intent_id), params)
40    }
41
42    /// Searches for a payment intent.
43    ///
44    /// For more details see <https://stripe.com/docs/api/payment_intents/search>.
45    pub fn search(
46        client: &Client,
47        params: PaymentIntentSearchParams,
48    ) -> Response<SearchList<PaymentIntent>> {
49        client.get_query("/payment_intents/search", params)
50    }
51}
52/// The resource representing a Stripe PaymentError object.
53///
54/// For more details see <https://stripe.com/docs/api/payment_intents/object#payment_intent_object-last_payment_error>.
55#[derive(Clone, Debug, Deserialize, Serialize)]
56pub struct PaymentError {
57    #[serde(rename = "type")]
58    pub payment_error_type: PaymentErrorType,
59    pub charge: Option<String>,
60    pub code: Option<String>,
61    pub decline_code: Option<String>,
62    pub doc_url: Option<String>,
63    pub message: Option<String>,
64    pub param: Option<String>,
65    pub source: Option<Expandable<PaymentSource>>,
66}
67
68/// The resource representing a Stripe PaymentErrorType object.
69///
70/// For more details see <https://stripe.com/docs/api/payment_intents/object#payment_intent_object-last_payment_error-type>.
71#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
72pub enum PaymentErrorType {
73    #[serde(rename = "api_error")]
74    Api,
75    #[serde(rename = "api_connection_error")]
76    Connection,
77    #[serde(rename = "authentication_error")]
78    Authentication,
79    #[serde(rename = "card_error")]
80    Card,
81    #[serde(rename = "idempotency_error")]
82    Idempotency,
83    #[serde(rename = "invalid_request_error")]
84    InvalidRequest,
85    #[serde(rename = "rate_limit_error")]
86    RateLimit,
87
88    /// A variant not yet supported by the library.
89    /// It is an error to send `Other` as part of a request.
90    #[serde(other, skip_serializing)]
91    Other,
92}
93
94// TODO: This might be moved to `PaymentSourceType` if we determine
95//       that all of the variants are _always_ the same.
96//
97//       In that case this can be replaced with a deprecated type alias.
98/// Represents the way a `PaymentIntent` needs to be fulfilled.
99#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
100#[serde(rename_all = "snake_case")]
101pub enum PaymentIntentMethodType {
102    /// This `PaymentIntent` needs to be fulfilled through credit card payment.
103    Card,
104    /// This `PaymentIntent` needs to be fulfilled through an
105    /// [iDeal](https://stripe.com/docs/payments/ideal) payment.
106    Ideal,
107    /// This `PaymentIntent` needs to be fulfilled through a
108    /// [Sepa Direct Debit](https://stripe.com/docs/payments/sepa-debit) payment.
109    SepaDebit,
110}
111
112/// The resource representing a Stripe CaptureMethod object.
113///
114/// For more details see <https://stripe.com/docs/api/payment_intents/object#payment_intent_object-capture_method>.
115#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
116#[serde(rename_all = "snake_case")]
117pub enum CaptureMethod {
118    Automatic,
119    Manual,
120
121    /// A variant not yet supported by the library.
122    /// It is an error to send `Other` as part of a request.
123    #[serde(other, skip_serializing)]
124    Other,
125}
126/// The resource representing a Stripe ConfirmationMethod object.
127///
128/// For more details see <https://stripe.com/docs/api/payment_intents/object#payment_intent_object-confirmation_method>.
129#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
130#[serde(rename_all = "snake_case")]
131pub enum ConfirmationMethod {
132    Secret,
133    Publishable,
134
135    /// A variant not yet supported by the library.
136    /// It is an error to send `Other` as part of a request.
137    #[serde(other, skip_serializing)]
138    Other,
139}
140
141#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
142#[serde(rename_all = "snake_case")]
143pub enum PaymentIntentNextActionType {
144    RedirectToUrl,
145    UseStripeSdk,
146
147    /// A variant not yet supported by the library.
148    /// It is an error to send `Other` as part of a request.
149    #[serde(other, skip_serializing)]
150    Other,
151}
152
153/// The set of parameters that can be used when updating a payment_intent object.
154///
155/// For more details see <https://stripe.com/docs/api/payment_intents/update>
156#[derive(Clone, Debug, Default, Serialize)]
157pub struct PaymentIntentUpdateParams<'a> {
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub amount: Option<u64>,
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub application_fee_amount: Option<u64>,
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub currency: Option<Currency>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub customer: Option<&'a str>,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub description: Option<&'a str>,
168    #[serde(skip_serializing_if = "Option::is_none")]
169    pub metadata: Option<Metadata>,
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub receipt_email: Option<&'a str>,
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub save_source_to_customer: Option<bool>,
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub shipping: Option<Shipping>,
176    #[serde(skip_serializing_if = "Option::is_none")]
177    pub source: Option<&'a str>,
178    #[serde(skip_serializing_if = "Option::is_none")]
179    pub transfer_group: Option<&'a str>,
180}
181
182/// The set of parameters that can be used when confirming a payment_intent object.
183///
184/// For more details see <https://stripe.com/docs/api/payment_intents/confirm>
185#[derive(Clone, Debug, Default, Serialize)]
186pub struct PaymentIntentConfirmParams<'a> {
187    #[serde(skip_serializing_if = "Option::is_none")]
188    pub receipt_email: Option<&'a str>,
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub return_url: Option<&'a str>,
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub save_source_to_customer: Option<bool>,
193    #[serde(skip_serializing_if = "Option::is_none")]
194    pub shipping: Option<Shipping>,
195    #[serde(skip_serializing_if = "Option::is_none")]
196    pub source: Option<&'a str>,
197}
198
199/// The set of parameters that can be used when capturing a payment_intent object.
200///
201/// For more details see <https://stripe.com/docs/api/payment_intents/capture>
202#[derive(Clone, Debug, Default, Serialize)]
203pub struct CapturePaymentIntent {
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub amount_to_capture: Option<u64>,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub application_fee_amount: Option<u64>,
208}
209
210/// The set of parameters that can be used when canceling a payment_intent object.
211///
212/// For more details see <https://stripe.com/docs/api/payment_intents/cancel>
213#[derive(Clone, Debug, Default, Serialize)]
214pub struct CancelPaymentIntent {
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub cancellation_reason: Option<PaymentIntentCancellationReason>,
217}
218
219#[derive(Clone, Debug, Default, Serialize)]
220pub struct PaymentIntentSearchParams<'a> {
221    pub query: String,
222    #[serde(skip_serializing_if = "Option::is_none")]
223    pub limit: Option<u64>,
224    #[serde(skip_serializing_if = "Option::is_none")]
225    pub page: Option<u64>,
226    pub expand: &'a [&'a str],
227}
228
229impl<'a> PaymentIntentSearchParams<'a> {
230    pub fn new() -> PaymentIntentSearchParams<'a> {
231        PaymentIntentSearchParams { query: String::new(), limit: None, page: None, expand: &[] }
232    }
233}