paypal_rust/resources/
order.rs

1use crate::client::endpoint::{EmptyResponseBody, Endpoint};
2use crate::client::error::PayPalError;
3use crate::client::paypal::Client;
4use crate::resources::enums::order_intent::OrderIntent;
5use crate::resources::enums::order_status::OrderStatus;
6use crate::resources::enums::processing_instruction::ProcessingInstruction;
7use crate::resources::link_description::LinkDescription;
8use crate::resources::order_application_context::OrderApplicationContext;
9use crate::resources::patch::Patch;
10use crate::resources::payer::Payer;
11use crate::resources::payment_source::PaymentSource;
12use crate::resources::payment_source_response::PaymentSourceResponse;
13use crate::resources::purchase_unit::PurchaseUnit;
14use crate::resources::purchase_unit_request::PurchaseUnitRequest;
15use reqwest::Method;
16use serde::{Deserialize, Serialize};
17use serde_with::skip_serializing_none;
18use std::borrow::Cow;
19
20#[skip_serializing_none]
21#[derive(Clone, Debug, Default, Deserialize, Serialize)]
22pub struct Order {
23    /// The date and time when the transaction occurred, in Internet date and time format.
24    pub create_time: Option<String>,
25
26    /// The date and time when the transaction was last updated, in Internet date and time format.
27    pub update_time: Option<String>,
28
29    /// The ID of the order.
30    pub id: Option<String>,
31
32    /// The payment source used to fund the payment.
33    pub payment_source: Option<PaymentSourceResponse>,
34
35    /// The intent to either capture payment immediately or authorize a payment for an order after order creation.
36    pub intent: Option<OrderIntent>,
37
38    /// The customer who approves and pays for the order. The customer is also known as the payer.
39    #[deprecated]
40    pub payer: Option<Payer>,
41
42    /// An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit
43    /// represents either a full or partial order that the customer intends to purchase from the merchant.
44    pub purchase_units: Option<Vec<PurchaseUnit>>,
45
46    /// The instruction to process an order.
47    pub processing_instruction: Option<ProcessingInstruction>,
48
49    /// The order status.
50    pub status: Option<OrderStatus>,
51
52    /// An array of request-related HATEOAS links. To complete payer approval, use the approve link to redirect the payer.
53    /// The API caller has 3 hours (default setting, this which can be changed by your account manager to
54    /// 24/48/72 hours to accommodate your use case) from the time the order is created, to redirect your payer.
55    /// Once redirected, the API caller has 3 hours for the payer to approve the order and either authorize or capture the order.
56    /// If you are not using the PayPal JavaScript SDK to initiate PayPal Checkout (in context) ensure that you include
57    /// application_context.return_url is specified or you will get "We're sorry, Things don't appear to be working at the moment"
58    /// after the payer approves the payment.
59    pub links: Option<Vec<LinkDescription>>,
60}
61
62impl Order {
63    /// Creates an order.
64    pub async fn create(client: &Client, dto: CreateOrderDto) -> Result<Order, PayPalError> {
65        client.post(&CreateOrder::new(dto)).await
66    }
67
68    /// Shows details for an order, by ID.
69    pub async fn show_details(client: &Client, id: &str) -> Result<Order, PayPalError> {
70        client.get(&ShowOrderDetails::new(id.to_string())).await
71    }
72
73    /// Updates an order with a CREATED or APPROVED status. You cannot update an order with the COMPLETED status.
74    ///
75    /// To make an update, you must provide a reference_id. If you omit this value with an order
76    /// that contains only one purchase unit, PayPal sets the value to default which enables you to
77    /// use the path: "/purchase_units/@reference_id=='default'/{attribute-or-object}"
78    pub async fn patch(client: &Client, id: &str, dto: PatchOrderDto) -> Result<(), PayPalError> {
79        client.patch(&PatchOrder::new(id.to_string(), dto)).await
80    }
81
82    /// Authorizes payment for an order. To successfully authorize payment for an order, the buyer
83    /// must first approve the order or a valid payment_source must be provided in the request.
84    /// A buyer can approve the order upon being redirected to the rel:approve URL that was returned
85    /// in the HATEOAS links in the create order response.
86    pub async fn authorize_payment(
87        client: &Client,
88        id: &str,
89    ) -> Result<AuthorizePaymentForOrderResponse, PayPalError> {
90        client
91            .post(&AuthorizePaymentForOrder::new(id.to_string()))
92            .await
93    }
94
95    /// Captures payment for an order. To successfully capture payment for an order,
96    /// the buyer must first approve the order or a valid payment_source must be provided in the
97    /// request. A buyer can approve the order upon being redirected to the rel:approve URL that
98    /// was returned in the HATEOAS links in the create order response.
99    pub async fn capture(
100        client: &Client,
101        id: &str,
102        payment_source: Option<PaymentSource>,
103    ) -> Result<CapturePaymentForOrderResponse, PayPalError> {
104        client
105            .post(&CapturePaymentForOrder {
106                order_id: id.to_string(),
107                payment_source,
108            })
109            .await
110    }
111}
112
113#[skip_serializing_none]
114#[derive(Clone, Debug, Serialize)]
115pub struct CreateOrderDto {
116    /// The intent to either capture payment immediately or authorize a payment for an order after order creation.
117    ///
118    /// The possible values are:
119    /// - CAPTURE. The merchant intends to capture payment immediately after the customer makes a payment.
120    /// - AUTHORIZE. The merchant intends to authorize a payment and place funds on hold after the customer makes a payment.
121    ///  Authorized payments are best captured within three days of authorization but are available to capture for up to 29 days.
122    ///  After the three-day honor period, the original authorized payment expires and you must re-authorize the payment.
123    ///  You must make a separate request to capture payments on demand. This intent is not supported when you have more than one
124    ///  `purchase_unit` within your order.
125    pub intent: OrderIntent,
126
127    /// The customer who approves and pays for the order. The customer is also known as the payer.
128    pub payer: Option<Payer>,
129
130    /// An array of purchase units. Each purchase unit establishes a contract between a payer and the payee. Each purchase unit represents
131    /// either a full or partial order that the payer intends to purchase from the payee.
132    pub purchase_units: Vec<PurchaseUnitRequest>,
133
134    /// Customize the payer experience during the approval process for the payment with PayPal.
135    pub application_context: Option<OrderApplicationContext>,
136}
137
138#[derive(Debug)]
139struct CreateOrder {
140    pub order: CreateOrderDto,
141}
142
143impl CreateOrder {
144    pub fn new(order: CreateOrderDto) -> Self {
145        Self { order }
146    }
147}
148
149impl Endpoint for CreateOrder {
150    type QueryParams = ();
151    type RequestBody = CreateOrderDto;
152    type ResponseBody = Order;
153
154    fn path(&self) -> Cow<str> {
155        Cow::Borrowed("v2/checkout/orders")
156    }
157
158    fn request_body(&self) -> Option<Self::RequestBody> {
159        Some(self.order.clone())
160    }
161
162    fn request_method(&self) -> Method {
163        Method::POST
164    }
165}
166
167#[derive(Debug)]
168struct ShowOrderDetails {
169    /// The ID of the order for which to show details.
170    order_id: String,
171}
172
173impl ShowOrderDetails {
174    pub fn new(order_id: String) -> Self {
175        Self { order_id }
176    }
177}
178
179impl Endpoint for ShowOrderDetails {
180    type QueryParams = ();
181    type RequestBody = ();
182    type ResponseBody = Order;
183
184    fn path(&self) -> Cow<str> {
185        Cow::Owned(format!("v2/checkout/orders/{}", self.order_id))
186    }
187}
188
189#[derive(Debug)]
190pub struct PatchOrderDto {
191    pub patch: Vec<Patch>,
192}
193
194type PatchOrderResponse = EmptyResponseBody;
195
196#[derive(Debug)]
197pub struct PatchOrder {
198    order_id: String,
199    order: PatchOrderDto,
200}
201
202impl PatchOrder {
203    pub fn new(order_id: String, order: PatchOrderDto) -> Self {
204        Self { order_id, order }
205    }
206}
207
208impl Endpoint for PatchOrder {
209    type QueryParams = ();
210    type RequestBody = Vec<Patch>;
211    type ResponseBody = PatchOrderResponse;
212
213    fn path(&self) -> Cow<str> {
214        Cow::Owned(format!("v2/checkout/orders/{}", self.order_id))
215    }
216
217    fn request_body(&self) -> Option<Self::RequestBody> {
218        Some(self.order.patch.clone())
219    }
220
221    fn request_method(&self) -> Method {
222        Method::PATCH
223    }
224}
225
226/// Authorizes payment for an order. To successfully authorize payment for an order, the buyer must
227/// first approve the order or a valid payment_source must be provided in the request.
228/// A buyer can approve the order upon being redirected to the rel:approve URL that was returned in
229/// the HATEOAS links in the create order response.
230#[derive(Debug)]
231struct AuthorizePaymentForOrder {
232    /// The ID of the order for which to authorize.
233    order_id: String,
234}
235
236impl AuthorizePaymentForOrder {
237    pub fn new(order_id: String) -> Self {
238        Self { order_id }
239    }
240}
241
242#[skip_serializing_none]
243#[derive(Debug, Deserialize, Default)]
244pub struct AuthorizePaymentForOrderResponse {
245    /// The date and time when the transaction occurred, in Internet date and time format.
246    pub create_time: Option<String>,
247
248    /// The date and time when the transaction occurred, in Internet date and time format.
249    pub update_time: Option<String>,
250
251    /// The ID of the order.
252    pub id: Option<String>,
253
254    /// The payment source used to fund the payment.
255    pub payment_source: Option<PaymentSourceResponse>,
256
257    /// The intent to either capture payment immediately or authorize a payment for an order after order creation.
258    pub intent: Option<OrderIntent>,
259
260    /// The customer who approves and pays for the order. The customer is also known as the payer.
261    pub payer: Option<Payer>,
262
263    /// An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit
264    /// represents either a full or partial order that the customer intends to purchase from the merchant.
265    pub purchase_units: Option<Vec<PurchaseUnit>>,
266
267    /// The order status.r.
268    pub status: Option<OrderStatus>,
269
270    /// An array of request-related HATEOAS links. To complete payer approval, use the approve link to redirect the payer.
271    /// The API caller has 3 hours (default setting, this which can be changed by your account manager to
272    /// 24/48/72 hours to accommodate your use case) from the time the order is created, to redirect your payer.
273    /// Once redirected, the API caller has 3 hours for the payer to approve the order and either authorize or capture the order.
274    /// If you are not using the PayPal JavaScript SDK to initiate PayPal Checkout (in context) ensure that you include
275    /// application_context.return_url is specified or you will get "We're sorry, Things don't appear to be working at the moment"
276    /// after the payer approves the payment.
277    pub links: Option<Vec<LinkDescription>>,
278}
279
280impl Endpoint for AuthorizePaymentForOrder {
281    type QueryParams = ();
282    type RequestBody = ();
283    type ResponseBody = AuthorizePaymentForOrderResponse;
284
285    fn path(&self) -> Cow<str> {
286        Cow::Owned(format!("v2/checkout/orders/{}/authorize", self.order_id))
287    }
288
289    fn request_method(&self) -> Method {
290        Method::POST
291    }
292}
293
294struct CapturePaymentForOrder {
295    /// The ID of the order for which to capture.
296    order_id: String,
297
298    /// The payment source definition
299    payment_source: Option<PaymentSource>,
300}
301
302#[skip_serializing_none]
303#[derive(Debug, Deserialize, Default)]
304pub struct CapturePaymentForOrderResponse {
305    /// The date and time when the transaction occurred, in Internet date and time format.
306    pub create_time: Option<String>,
307
308    /// The date and time when the transaction occurred, in Internet date and time format.
309    pub update_time: Option<String>,
310
311    /// The ID of the order.
312    pub id: Option<String>,
313
314    /// The payment source used to fund the payment.
315    pub payment_source: Option<PaymentSourceResponse>,
316
317    /// The intent to either capture payment immediately or authorize a payment for an order after order creation.
318    pub intent: Option<OrderIntent>,
319
320    /// The customer who approves and pays for the order. The customer is also known as the payer.
321    pub payer: Option<Payer>,
322
323    /// An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit
324    /// represents either a full or partial order that the customer intends to purchase from the merchant.
325    pub purchase_units: Option<Vec<PurchaseUnit>>,
326
327    /// The order status.r.
328    pub status: Option<OrderStatus>,
329
330    /// An array of request-related HATEOAS links. To complete payer approval, use the approve link to redirect the payer.
331    /// The API caller has 3 hours (default setting, this which can be changed by your account manager to
332    /// 24/48/72 hours to accommodate your use case) from the time the order is created, to redirect your payer.
333    /// Once redirected, the API caller has 3 hours for the payer to approve the order and either authorize or capture the order.
334    /// If you are not using the PayPal JavaScript SDK to initiate PayPal Checkout (in context) ensure that you include
335    /// application_context.return_url is specified or you will get "We're sorry, Things don't appear to be working at the moment"
336    /// after the payer approves the payment.
337    pub links: Option<Vec<LinkDescription>>,
338}
339
340impl Endpoint for CapturePaymentForOrder {
341    type QueryParams = ();
342    type RequestBody = Option<PaymentSource>;
343    type ResponseBody = CapturePaymentForOrderResponse;
344
345    fn path(&self) -> Cow<str> {
346        Cow::Owned(format!("v2/checkout/orders/{}/capture", self.order_id))
347    }
348
349    fn request_body(&self) -> Option<Self::RequestBody> {
350        Some(self.payment_source.clone())
351    }
352
353    fn request_method(&self) -> Method {
354        Method::POST
355    }
356}