primer_api/
lib.rs

1//! [`PrimerClient`](struct.PrimerClient.html) is the main entry point for this library.
2//!
3//! Library created with [`libninja`](https://www.libninja.com).
4#![allow(non_camel_case_types)]
5#![allow(unused)]
6pub mod model;
7pub mod request;
8use crate::model::*;
9
10pub struct PrimerClient {
11    pub(crate) client: httpclient::Client,
12    authentication: PrimerAuthentication,
13}
14impl PrimerClient {
15    pub fn from_env() -> Self {
16        let url = std::env::var("PRIMER_BASE_URL")
17            .expect("Missing environment variable PRIMER_BASE_URL");
18        Self {
19            client: httpclient::Client::new(Some(url)),
20            authentication: PrimerAuthentication::from_env(),
21        }
22    }
23}
24impl PrimerClient {
25    pub fn new(url: &str, authentication: PrimerAuthentication) -> Self {
26        let client = httpclient::Client::new(Some(url.to_string()));
27        Self { client, authentication }
28    }
29    pub fn with_authentication(mut self, authentication: PrimerAuthentication) -> Self {
30        self.authentication = authentication;
31        self
32    }
33    pub fn authenticate<'a>(
34        &self,
35        mut r: httpclient::RequestBuilder<'a>,
36    ) -> httpclient::RequestBuilder<'a> {
37        match &self.authentication {
38            PrimerAuthentication::ApiKeyAuth { api_key_auth } => {
39                r = r.header("X-API-KEY", api_key_auth);
40            }
41        }
42        r
43    }
44    pub fn with_middleware<M: httpclient::Middleware + 'static>(
45        mut self,
46        middleware: M,
47    ) -> Self {
48        self.client = self.client.with_middleware(middleware);
49        self
50    }
51    /**Retrieve a client session
52
53This API call retrieves all the details associated with the client session corresponding to the client token that is provided in the request. The fields with empty values are excluded from the response.
54*/
55    pub fn retrieve_client_side_token_client_session_get(
56        &self,
57    ) -> request::RetrieveClientSideTokenClientSessionGetRequest {
58        request::RetrieveClientSideTokenClientSessionGetRequest {
59            client: &self,
60            client_token: None,
61        }
62    }
63    /**Create a client session
64
65Creating a client session generates a client token: a temporary key used to initialize [Universal Checkout](https://primer.io/docs/accept-payments/setup-universal-checkout/installation/web) and authenticate it against your account.
66
67Universal Checkout automatically retrieves all the settings from the client session and the Dashboard to configure the payment methods and the checkout experience.
68
69<b>Note:</b>
70When creating a Client Session, please make sure to provide `currencyCode`, `orderId`, and at least one of `amount` or `lineItems`.
71If any of these are not yet available, you can provide them when making the payment request.
72
73<code>POST /client-session</code> does not have required fields as all fields are not always known when a client session is created.
74Use <code>PATCH /client-session</code> to update the parameters throughout the checkout session.
75
76Client tokens expire after 24 hours.
77*/
78    pub fn create_client_side_token_client_session_post(
79        &self,
80        args: request::CreateClientSideTokenClientSessionPostRequired,
81    ) -> request::CreateClientSideTokenClientSessionPostRequest {
82        request::CreateClientSideTokenClientSessionPostRequest {
83            client: &self,
84            order_id: args.order_id.to_owned(),
85            currency_code: args.currency_code.to_owned(),
86            amount: args.amount,
87            order: args.order,
88            customer_id: args.customer_id.to_owned(),
89            customer: args.customer,
90            metadata: args.metadata,
91            payment_method: args.payment_method,
92        }
93    }
94    /**Update client session
95
96You can update a clients session created earlier with the `PATCH /client-session` [API call](#operation/create_client_side_token_client_session_post).
97
98The only required field for the request is `clientToken`. Other supported request fields are same as for the `POST /client-session` [API call](#operation/create_client_side_token_client_session_post).
99
100You need to specify only the fields you wish to update. However, if the items that are to be updated are of type `array`, then you need to provide the complete array along with modified items.
101
102If you wish to update nested fields on the client session, such as the customer `emailAddress` field, you can pass the `customer` object with only one field, `emailAddress`, to update.
103
104If you simply wish to clear the value of the field, pass `null` as your input.
105
106You can update `paymentMethod.vaultOnSuccess` field but updating of the `paymentMethod.options` field through `PATCH /client-session` is not supported.
107
108The response will contain all the fields of the client session including the ones that were changed.
109*/
110    pub fn update_client_side_token_client_session_patch(
111        &self,
112        args: request::UpdateClientSideTokenClientSessionPatchRequired,
113    ) -> request::UpdateClientSideTokenClientSessionPatchRequest {
114        request::UpdateClientSideTokenClientSessionPatchRequest {
115            client: &self,
116            client_token: args.client_token.to_owned(),
117            customer_id: args.customer_id.to_owned(),
118            order_id: args.order_id.to_owned(),
119            currency_code: args.currency_code.to_owned(),
120            amount: args.amount,
121            metadata: args.metadata,
122            customer: args.customer,
123            order: args.order,
124            payment_method: args.payment_method,
125        }
126    }
127    /**Search & list payments
128
129<p/>
130
131Retrieve a list of your payments.
132
133Results are paginated, they will only return up to 100 payments maximum.
134To access the next page of result, set the `cursor` query parameter to the value of `nextCursor` in
135your current result payload. Use `prevCursor` to go back to the previous page.
136
137**Note:** this endpoint returns a list of
138summarized payments. Not all payments attributes are present. You can use
139the query parameters to filter payments. You can separate multiple query parameters with the `&` symbol.
140Query parameters with types of the form "Array of strings" (such as the status parameter) can be specified as a comma-separated list.
141
142For example, if you wanted to get both `FAILED`  and `CANCELLED` payments, for customer `john-123`, you would use:
143```bash
144curl --location --request GET 'https://api.primer.io/payments?status=FAILED,CANCELLED&customer_id=john-123' \
145--header 'X-Api-Key: <YOUR_API_KEY>'
146```
147
148You can alternatively specify a list by repeating the parameter multiple times.
149
150**Note:** payments will be available within a minute from being created.
151*/
152    pub fn list_payments_payments_get(&self) -> request::ListPaymentsPaymentsGetRequest {
153        request::ListPaymentsPaymentsGetRequest {
154            client: &self,
155            status: None,
156            payment_method_type: None,
157            processor: None,
158            currency_code: None,
159            from_date: None,
160            to_date: None,
161            order_id: None,
162            min_amount: None,
163            max_amount: None,
164            customer_id: None,
165            merchant_id: None,
166            customer_email_address: None,
167            last4_digits: None,
168            paypal_email: None,
169            klarna_email: None,
170            limit: None,
171            cursor: None,
172        }
173    }
174    /**Create a payment
175
176<p/>
177
178Create and authorize a payment for a given customer order. You
179should provide a payment method token here to avoid PCI implications.
180
181If only a payment method token is passed, the values passed with the Client Session is used to determine the amount, currency etc.
182Note: `amount`, `currencyCode` and `orderId` are required during payment creation. Make sure to pass these fields when creating a client session, or if not yet available, when creating a payment.
183
184All fields provided on this request will take preference over any field on the `order` associated with the client session. E.g. if you pass `amount` on this request, it will override the `amount` on the `order` associated with the Client Session.
185*/
186    pub fn create_payment_payments_post(
187        &self,
188        payment_method_token: &str,
189    ) -> request::CreatePaymentPaymentsPostRequest {
190        request::CreatePaymentPaymentsPostRequest {
191            client: &self,
192            x_idempotency_key: None,
193            order_id: None,
194            currency_code: None,
195            amount: None,
196            order: None,
197            payment_method_token: payment_method_token.to_owned(),
198            customer_id: None,
199            customer: None,
200            metadata: None,
201            payment_method: None,
202        }
203    }
204    /**Capture a payment
205
206<p/>
207
208If you have successfully authorized a payment, you can now
209fully capture, or partially capture funds from the authorized payment, depending
210on whether your selected payment processor supports it. The payment will
211be updated to `SETTLED` or `SETTLING`, depending on the payment method type.
212
213The payload sent in this capture request is completely optional. If you don't
214send a payload with the capture request, the full amount that was authorized
215will be sent for capture. Below are the available payload attributes, which
216give you more granular control when capturing funds, if you require it.
217*/
218    pub fn capture_payment_payments_id_capture_post(
219        &self,
220        id: &str,
221        amount: serde_json::Value,
222        final_: bool,
223    ) -> request::CapturePaymentPaymentsIdCapturePostRequest {
224        request::CapturePaymentPaymentsIdCapturePostRequest {
225            client: &self,
226            id: id.to_owned(),
227            x_idempotency_key: None,
228            amount,
229            final_,
230        }
231    }
232    /**Cancel a payment
233
234<p/>
235
236Provided the payment has not reached `SETTLED` status, Primer will
237send a "void" request to the payment processor, thereby cancelling the payment
238and releasing the hold on customer funds. Upon success, the payment will transition
239to `CANCELLED`. The payload is optional.
240*/
241    pub fn cancel_payment_payments_id_cancel_post(
242        &self,
243        id: &str,
244        reason: &str,
245    ) -> request::CancelPaymentPaymentsIdCancelPostRequest {
246        request::CancelPaymentPaymentsIdCancelPostRequest {
247            client: &self,
248            id: id.to_owned(),
249            x_idempotency_key: None,
250            reason: reason.to_owned(),
251        }
252    }
253    /**Refund a payment
254
255<p/>
256
257By default, this request will refund the full amount.
258
259Optionally, pass in a lesser amount for a partial refund.
260*/
261    pub fn refund_payment_payments_id_refund_post(
262        &self,
263        args: request::RefundPaymentPaymentsIdRefundPostRequired,
264    ) -> request::RefundPaymentPaymentsIdRefundPostRequest {
265        request::RefundPaymentPaymentsIdRefundPostRequest {
266            client: &self,
267            id: args.id.to_owned(),
268            x_idempotency_key: None,
269            amount: args.amount,
270            order_id: args.order_id.to_owned(),
271            reason: args.reason.to_owned(),
272        }
273    }
274    /**Resume a payment
275
276<p/>
277
278Resume a payment's workflow execution from a paused state. This
279is usually required when a Workflow was paused in order to get further information
280from the customer, or when waiting for an asynchronous response from a third
281party connection.
282*/
283    pub fn resume_payment_payments_id_resume_post(
284        &self,
285        id: &str,
286        resume_token: &str,
287    ) -> request::ResumePaymentPaymentsIdResumePostRequest {
288        request::ResumePaymentPaymentsIdResumePostRequest {
289            client: &self,
290            id: id.to_owned(),
291            resume_token: resume_token.to_owned(),
292        }
293    }
294    /**Get a payment
295
296<p/>
297
298Retrieve a payment by its ID.
299*/
300    pub fn get_payment_by_id_payments_id_get(
301        &self,
302        id: &str,
303    ) -> request::GetPaymentByIdPaymentsIdGetRequest {
304        request::GetPaymentByIdPaymentsIdGetRequest {
305            client: &self,
306            id: id.to_owned(),
307        }
308    }
309    /**Save a payment method token
310
311<p/>
312
313Save a `SINGLE_USE` payment method token so it can be used
314again later. You can optionally choose to verify the payment method
315before vaulting. If verification fails, no payment method data will
316be vaulted. Verification can minimise fraud and boost subscription
317rates for recurring payments.
318
319If you try to vault an already vaulted token, you will get the existing vaulted token back.
320*/
321    pub fn vault_payment_method_payment_methods_token_vault_post(
322        &self,
323        payment_method_token: &str,
324        customer_id: &str,
325    ) -> request::VaultPaymentMethodPaymentMethodsTokenVaultPostRequest {
326        request::VaultPaymentMethodPaymentMethodsTokenVaultPostRequest {
327            client: &self,
328            payment_method_token: payment_method_token.to_owned(),
329            customer_id: customer_id.to_owned(),
330            verify: None,
331        }
332    }
333    /**List saved payment methods
334
335Retrieve a list of stored payment methods for a customer.*/
336    pub fn get_payment_methods_payment_methods_get(
337        &self,
338        customer_id: &str,
339    ) -> request::GetPaymentMethodsPaymentMethodsGetRequest {
340        request::GetPaymentMethodsPaymentMethodsGetRequest {
341            client: &self,
342            customer_id: customer_id.to_owned(),
343        }
344    }
345    /**Delete a saved payment method
346
347Delete a saved payment method.*/
348    pub fn delete_payment_method_payment_methods_token_delete(
349        &self,
350        payment_method_token: &str,
351    ) -> request::DeletePaymentMethodPaymentMethodsTokenDeleteRequest {
352        request::DeletePaymentMethodPaymentMethodsTokenDeleteRequest {
353            client: &self,
354            payment_method_token: payment_method_token.to_owned(),
355        }
356    }
357    /**Update the default saved payment method
358
359Update a saved payment method to be the default stored payment method for a customer.*/
360    pub fn set_payment_method_default_payment_methods_token_default_post(
361        &self,
362        payment_method_token: &str,
363    ) -> request::SetPaymentMethodDefaultPaymentMethodsTokenDefaultPostRequest {
364        request::SetPaymentMethodDefaultPaymentMethodsTokenDefaultPostRequest {
365            client: &self,
366            payment_method_token: payment_method_token.to_owned(),
367        }
368    }
369}
370pub enum PrimerAuthentication {
371    ApiKeyAuth { api_key_auth: String },
372}
373impl PrimerAuthentication {
374    pub fn from_env() -> Self {
375        Self::ApiKeyAuth {
376            api_key_auth: std::env::var("PRIMER_API_KEY_AUTH")
377                .expect("Environment variable PRIMER_API_KEY_AUTH is not set."),
378        }
379    }
380}