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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! [`PrimerClient`](struct.PrimerClient.html) is the main entry point for this library.
//!
//! Library created with [`libninja`](https://www.libninja.com).
#![allow(non_camel_case_types)]
#![allow(unused)]
pub mod model;
pub mod request;
use crate::model::*;

pub struct PrimerClient {
    pub(crate) client: httpclient::Client,
    authentication: PrimerAuthentication,
}
impl PrimerClient {
    pub fn from_env() -> Self {
        let url = std::env::var("PRIMER_BASE_URL")
            .expect("Missing environment variable PRIMER_BASE_URL");
        Self {
            client: httpclient::Client::new(Some(url)),
            authentication: PrimerAuthentication::from_env(),
        }
    }
}
impl PrimerClient {
    pub fn new(url: &str, authentication: PrimerAuthentication) -> Self {
        let client = httpclient::Client::new(Some(url.to_string()));
        Self { client, authentication }
    }
    pub fn with_authentication(mut self, authentication: PrimerAuthentication) -> Self {
        self.authentication = authentication;
        self
    }
    pub fn authenticate<'a>(
        &self,
        mut r: httpclient::RequestBuilder<'a>,
    ) -> httpclient::RequestBuilder<'a> {
        match &self.authentication {
            PrimerAuthentication::ApiKeyAuth { api_key_auth } => {
                r = r.header("X-API-KEY", api_key_auth);
            }
        }
        r
    }
    pub fn with_middleware<M: httpclient::Middleware + 'static>(
        mut self,
        middleware: M,
    ) -> Self {
        self.client = self.client.with_middleware(middleware);
        self
    }
    /**Retrieve a client session

This 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.
*/
    pub fn retrieve_client_side_token_client_session_get(
        &self,
    ) -> request::RetrieveClientSideTokenClientSessionGetRequest {
        request::RetrieveClientSideTokenClientSessionGetRequest {
            client: &self,
            client_token: None,
        }
    }
    /**Create a client session

Creating 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.

Universal Checkout automatically retrieves all the settings from the client session and the Dashboard to configure the payment methods and the checkout experience.

<b>Note:</b>
When creating a Client Session, please make sure to provide `currencyCode`, `orderId`, and at least one of `amount` or `lineItems`.
If any of these are not yet available, you can provide them when making the payment request.

<code>POST /client-session</code> does not have required fields as all fields are not always known when a client session is created.
Use <code>PATCH /client-session</code> to update the parameters throughout the checkout session.

Client tokens expire after 24 hours.
*/
    pub fn create_client_side_token_client_session_post(
        &self,
        args: request::CreateClientSideTokenClientSessionPostRequired,
    ) -> request::CreateClientSideTokenClientSessionPostRequest {
        request::CreateClientSideTokenClientSessionPostRequest {
            client: &self,
            order_id: args.order_id.to_owned(),
            currency_code: args.currency_code.to_owned(),
            amount: args.amount,
            order: args.order,
            customer_id: args.customer_id.to_owned(),
            customer: args.customer,
            metadata: args.metadata,
            payment_method: args.payment_method,
        }
    }
    /**Update client session

You can update a clients session created earlier with the `PATCH /client-session` [API call](#operation/create_client_side_token_client_session_post).

The 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).

You 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.

If 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.

If you simply wish to clear the value of the field, pass `null` as your input.

You can update `paymentMethod.vaultOnSuccess` field but updating of the `paymentMethod.options` field through `PATCH /client-session` is not supported.

The response will contain all the fields of the client session including the ones that were changed.
*/
    pub fn update_client_side_token_client_session_patch(
        &self,
        args: request::UpdateClientSideTokenClientSessionPatchRequired,
    ) -> request::UpdateClientSideTokenClientSessionPatchRequest {
        request::UpdateClientSideTokenClientSessionPatchRequest {
            client: &self,
            client_token: args.client_token.to_owned(),
            customer_id: args.customer_id.to_owned(),
            order_id: args.order_id.to_owned(),
            currency_code: args.currency_code.to_owned(),
            amount: args.amount,
            metadata: args.metadata,
            customer: args.customer,
            order: args.order,
            payment_method: args.payment_method,
        }
    }
    /**Search & list payments

<p/>

Retrieve a list of your payments.

Results are paginated, they will only return up to 100 payments maximum.
To access the next page of result, set the `cursor` query parameter to the value of `nextCursor` in
your current result payload. Use `prevCursor` to go back to the previous page.

**Note:** this endpoint returns a list of
summarized payments. Not all payments attributes are present. You can use
the query parameters to filter payments. You can separate multiple query parameters with the `&` symbol.
Query parameters with types of the form "Array of strings" (such as the status parameter) can be specified as a comma-separated list.

For example, if you wanted to get both `FAILED`  and `CANCELLED` payments, for customer `john-123`, you would use:
```bash
curl --location --request GET 'https://api.primer.io/payments?status=FAILED,CANCELLED&customer_id=john-123' \
--header 'X-Api-Key: <YOUR_API_KEY>'
```

You can alternatively specify a list by repeating the parameter multiple times.

**Note:** payments will be available within a minute from being created.
*/
    pub fn list_payments_payments_get(&self) -> request::ListPaymentsPaymentsGetRequest {
        request::ListPaymentsPaymentsGetRequest {
            client: &self,
            status: None,
            payment_method_type: None,
            processor: None,
            currency_code: None,
            from_date: None,
            to_date: None,
            order_id: None,
            min_amount: None,
            max_amount: None,
            customer_id: None,
            merchant_id: None,
            customer_email_address: None,
            last4_digits: None,
            paypal_email: None,
            klarna_email: None,
            limit: None,
            cursor: None,
        }
    }
    /**Create a payment

<p/>

Create and authorize a payment for a given customer order. You
should provide a payment method token here to avoid PCI implications.

If only a payment method token is passed, the values passed with the Client Session is used to determine the amount, currency etc.
Note: `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.

All 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.
*/
    pub fn create_payment_payments_post(
        &self,
        payment_method_token: &str,
    ) -> request::CreatePaymentPaymentsPostRequest {
        request::CreatePaymentPaymentsPostRequest {
            client: &self,
            x_idempotency_key: None,
            order_id: None,
            currency_code: None,
            amount: None,
            order: None,
            payment_method_token: payment_method_token.to_owned(),
            customer_id: None,
            customer: None,
            metadata: None,
            payment_method: None,
        }
    }
    /**Capture a payment

<p/>

If you have successfully authorized a payment, you can now
fully capture, or partially capture funds from the authorized payment, depending
on whether your selected payment processor supports it. The payment will
be updated to `SETTLED` or `SETTLING`, depending on the payment method type.

The payload sent in this capture request is completely optional. If you don't
send a payload with the capture request, the full amount that was authorized
will be sent for capture. Below are the available payload attributes, which
give you more granular control when capturing funds, if you require it.
*/
    pub fn capture_payment_payments_id_capture_post(
        &self,
        id: &str,
        amount: serde_json::Value,
        final_: bool,
    ) -> request::CapturePaymentPaymentsIdCapturePostRequest {
        request::CapturePaymentPaymentsIdCapturePostRequest {
            client: &self,
            id: id.to_owned(),
            x_idempotency_key: None,
            amount,
            final_,
        }
    }
    /**Cancel a payment

<p/>

Provided the payment has not reached `SETTLED` status, Primer will
send a "void" request to the payment processor, thereby cancelling the payment
and releasing the hold on customer funds. Upon success, the payment will transition
to `CANCELLED`. The payload is optional.
*/
    pub fn cancel_payment_payments_id_cancel_post(
        &self,
        id: &str,
        reason: &str,
    ) -> request::CancelPaymentPaymentsIdCancelPostRequest {
        request::CancelPaymentPaymentsIdCancelPostRequest {
            client: &self,
            id: id.to_owned(),
            x_idempotency_key: None,
            reason: reason.to_owned(),
        }
    }
    /**Refund a payment

<p/>

By default, this request will refund the full amount.

Optionally, pass in a lesser amount for a partial refund.
*/
    pub fn refund_payment_payments_id_refund_post(
        &self,
        args: request::RefundPaymentPaymentsIdRefundPostRequired,
    ) -> request::RefundPaymentPaymentsIdRefundPostRequest {
        request::RefundPaymentPaymentsIdRefundPostRequest {
            client: &self,
            id: args.id.to_owned(),
            x_idempotency_key: None,
            amount: args.amount,
            order_id: args.order_id.to_owned(),
            reason: args.reason.to_owned(),
        }
    }
    /**Resume a payment

<p/>

Resume a payment's workflow execution from a paused state. This
is usually required when a Workflow was paused in order to get further information
from the customer, or when waiting for an asynchronous response from a third
party connection.
*/
    pub fn resume_payment_payments_id_resume_post(
        &self,
        id: &str,
        resume_token: &str,
    ) -> request::ResumePaymentPaymentsIdResumePostRequest {
        request::ResumePaymentPaymentsIdResumePostRequest {
            client: &self,
            id: id.to_owned(),
            resume_token: resume_token.to_owned(),
        }
    }
    /**Get a payment

<p/>

Retrieve a payment by its ID.
*/
    pub fn get_payment_by_id_payments_id_get(
        &self,
        id: &str,
    ) -> request::GetPaymentByIdPaymentsIdGetRequest {
        request::GetPaymentByIdPaymentsIdGetRequest {
            client: &self,
            id: id.to_owned(),
        }
    }
    /**Save a payment method token

<p/>

Save a `SINGLE_USE` payment method token so it can be used
again later. You can optionally choose to verify the payment method
before vaulting. If verification fails, no payment method data will
be vaulted. Verification can minimise fraud and boost subscription
rates for recurring payments.

If you try to vault an already vaulted token, you will get the existing vaulted token back.
*/
    pub fn vault_payment_method_payment_methods_token_vault_post(
        &self,
        payment_method_token: &str,
        customer_id: &str,
    ) -> request::VaultPaymentMethodPaymentMethodsTokenVaultPostRequest {
        request::VaultPaymentMethodPaymentMethodsTokenVaultPostRequest {
            client: &self,
            payment_method_token: payment_method_token.to_owned(),
            customer_id: customer_id.to_owned(),
            verify: None,
        }
    }
    /**List saved payment methods

Retrieve a list of stored payment methods for a customer.*/
    pub fn get_payment_methods_payment_methods_get(
        &self,
        customer_id: &str,
    ) -> request::GetPaymentMethodsPaymentMethodsGetRequest {
        request::GetPaymentMethodsPaymentMethodsGetRequest {
            client: &self,
            customer_id: customer_id.to_owned(),
        }
    }
    /**Delete a saved payment method

Delete a saved payment method.*/
    pub fn delete_payment_method_payment_methods_token_delete(
        &self,
        payment_method_token: &str,
    ) -> request::DeletePaymentMethodPaymentMethodsTokenDeleteRequest {
        request::DeletePaymentMethodPaymentMethodsTokenDeleteRequest {
            client: &self,
            payment_method_token: payment_method_token.to_owned(),
        }
    }
    /**Update the default saved payment method

Update a saved payment method to be the default stored payment method for a customer.*/
    pub fn set_payment_method_default_payment_methods_token_default_post(
        &self,
        payment_method_token: &str,
    ) -> request::SetPaymentMethodDefaultPaymentMethodsTokenDefaultPostRequest {
        request::SetPaymentMethodDefaultPaymentMethodsTokenDefaultPostRequest {
            client: &self,
            payment_method_token: payment_method_token.to_owned(),
        }
    }
}
pub enum PrimerAuthentication {
    ApiKeyAuth { api_key_auth: String },
}
impl PrimerAuthentication {
    pub fn from_env() -> Self {
        Self::ApiKeyAuth {
            api_key_auth: std::env::var("PRIMER_API_KEY_AUTH")
                .expect("Environment variable PRIMER_API_KEY_AUTH is not set."),
        }
    }
}