checkout_core 0.0.147

core traits and structs for the checkout_controller crate
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::context::Context;
use crate::customer::{Address, Contact};
use crate::error::{new_application_error, new_invalid_state_error, Error};
use crate::internal_context::InternalContext;
use crate::invoice::Invoice;
use crate::item::Item;
use crate::money::{Currency, Money};
use crate::order::Order;
use crate::payment::handlers as payment_handlers;
use crate::payment::{Initiator, PaymentProcessor};
use crate::shipping::{Fulfillment, FulfillmentSelection, ShippingQuote};

pub mod handlers;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(tag = "name", rename_all = "snake_case")]
pub enum State {
    Shopping,                                            // transitions to ItemsConfirmed
    ItemsConfirmed { reserved_since: DateTime<Utc> }, // transitions to Shopping, PaymentInProgress or Completed
    PaymentInProgress { reserved_since: DateTime<Utc> }, // can only enter this state from ItemsConfirmed. not allowed to free items in this state, unless payment is cancelled. transitions to either Shopping (if payment is cancelled) or Completed
    Completed, // can only enter this state from ItemsConfirmed or PaymentInProgress. this is a terminal state
}

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct Checkout<P> {
    pub id: String,
    pub state: State,
    pub currency: Currency,
    pub promo_codes: Vec<String>,
    pub items: Vec<Item>,
    pub contact: Option<Contact>,
    pub shipping_address: Option<Address>,
    pub fulfillment: Option<Fulfillment>,
    pub shipping_quotes: Vec<ShippingQuote>,
    pub invoice: Option<Invoice>,
    pub payment_id: Option<String>,
    pub order_id: Option<String>,
    // associations
    pub payment: Option<P>,
    pub order: Option<Order<P>>,
}

#[async_trait]
pub trait CheckoutStore {
    async fn create_checkout<P: Sync + Send + Serialize + DeserializeOwned>(
        &mut self,
        co: &Checkout<P>,
    ) -> Result<(), Error>;
    async fn update_checkout<P: Sync + Send + Serialize + DeserializeOwned>(
        &mut self,
        co: &Checkout<P>,
    ) -> Result<(), Error>;
    async fn get_checkout<P: Sync + Send + Serialize + DeserializeOwned>(
        &mut self,
        id: &str,
    ) -> Result<Option<Checkout<P>>, Error>;
    async fn get_checkout_for_update<P: Sync + Send + Serialize + DeserializeOwned>(
        &mut self,
        id: &str,
    ) -> Result<Option<Checkout<P>>, Error>;
}

// constructor
impl<P: Sync + Send + Serialize + DeserializeOwned> Checkout<P> {
    pub async fn new<C: Context + Send>(
        ctx: &mut C,
        currency: Currency,
        contact: Option<Contact>,
        shipping_address: Option<Address>,
    ) -> Result<Self, Error> {
        let co = Self {
            id: Uuid::new_v4().to_string(),
            state: State::Shopping,
            currency,
            promo_codes: vec![],
            items: vec![],
            contact,
            shipping_address,
            fulfillment: None,
            shipping_quotes: vec![],
            invoice: None,
            payment_id: None,
            order_id: None,
            // associations
            payment: None,
            order: None,
        };

        ctx.create_checkout(&co).await?;

        Ok(co)
    }
}

// state transitions
impl<P: Sync + Send + Serialize + DeserializeOwned> Checkout<P> {
    async fn enter_shopping_state<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        self.reset_shipping_options();
        ctx.free_items(&self.items).await?;
        Ok(self.state = State::Shopping)
    }

    async fn enter_items_confirmed_state<C: Context + Send>(
        &mut self,
        ctx: &mut C,
    ) -> Result<(), Error> {
        self.reset_shipping_options();
        match ctx.reserve_items(&self.items).await? {
            None => {
                if let Some(address) = &self.shipping_address {
                    self.shipping_quotes = ctx
                        .get_shipping_quotes(
                            &self.currency,
                            &self.promo_codes,
                            &self.items,
                            address,
                        )
                        .await?;
                }

                self.update_item_prices(ctx).await?;
                self.update_invoice(ctx).await?;
                self.state = State::ItemsConfirmed {
                    reserved_since: Utc::now(),
                };
                Ok(())
            }
            Some(stock_issues) => Err(new_application_error("STOCK_ISSUE", stock_issues)),
        }
    }

    async fn enter_payment_in_progress_state<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        args: <C as PaymentProcessor>::InitArgs,
        reserved_since: DateTime<Utc>,
    ) -> Result<(), Error> {
        let charge_amount = self.invoice.as_ref().unwrap().initial_charge_amount.clone();
        let payment =
            payment_handlers::create_handler(ctx, Initiator::Checkout(self), charge_amount, args)
                .await?;
        self.payment_id = Some(payment.id.clone());
        self.state = State::PaymentInProgress { reserved_since };

        Ok(())
    }

    async fn enter_completed_state(&mut self) -> Result<(), Error> {
        Ok(self.state = State::Completed)
    }
}

// private methods
impl<P: Sync + Send + Serialize + DeserializeOwned> Checkout<P> {
    async fn ensure_shopping_state<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        match self.state {
            State::Shopping => Ok(()),
            State::ItemsConfirmed { reserved_since: _ } => self.enter_shopping_state(ctx).await,
            _ => Err(new_invalid_state_error(
                "must be in the Shopping or ItemsConfirmed state",
            )),
        }
    }

    async fn ensure_items_confirmed_state<C: Context + Send>(
        &mut self,
        ctx: &mut C,
    ) -> Result<(), Error> {
        match self.state {
            State::ItemsConfirmed { reserved_since: _ } => Ok(()),
            State::Shopping => self.enter_items_confirmed_state(ctx).await,
            _ => Err(new_invalid_state_error(
                "must be in the Shopping or ItemsConfirmed state",
            )),
        }
    }

    async fn ensure_payment_in_progress_state<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        args: <C as PaymentProcessor>::InitArgs,
    ) -> Result<(), Error> {
        match self.state {
            State::PaymentInProgress { reserved_since: _ } => Ok(()),
            State::ItemsConfirmed { reserved_since } => {
                // check for contact information
                if self.contact.is_none() {
                    return Err(new_application_error(
                        "MISSING_CONTACT_INFORMATION",
                        "please provide contact information to continue",
                    ));
                }

                // check for a fulfillment type
                if self.fulfillment.is_none() {
                    return Err(new_application_error(
                        "MISSING_FULFILLMENT_TYPE",
                        "please select a fulfillment type to continue",
                    ));
                }

                self.enter_payment_in_progress_state(ctx, args, reserved_since)
                    .await
            }
            _ => Err(new_invalid_state_error(
                "must be in the ItemsConfirmed or PaymentInProgress state",
            )),
        }
    }

    async fn ensure_completed_state(&mut self) -> Result<(), Error> {
        match self.state {
            State::Completed => Ok(()),
            State::ItemsConfirmed { reserved_since: _ } => {
                // check for contact information
                if self.contact.is_none() {
                    return Err(new_application_error(
                        "MISSING_CONTACT_INFORMATION",
                        "please provide contact information to continue",
                    ));
                }

                // check for a fulfillment type
                if self.fulfillment.is_none() {
                    return Err(new_application_error(
                        "MISSING_FULFILLMENT_TYPE",
                        "please select a fulfillment type to continue",
                    ));
                }

                self.enter_completed_state().await
            }
            State::PaymentInProgress { reserved_since: _ } => self.enter_completed_state().await,
            _ => Err(new_invalid_state_error(
                "must be in the PaymentInProgress or Completed state",
            )),
        }
    }

    async fn ensure_not_payment_in_progress_or_completed_state(&mut self) -> Result<(), Error> {
        match self.state {
            State::PaymentInProgress { reserved_since: _ } | State::Completed => Err(
                new_invalid_state_error("should be in any state other than PaymentInProgress or Completed"),
            ),
            _ => Ok(()),
        }
    }

    async fn update_invoice<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        Ok(self.invoice = Some(ctx.generate_invoice(self).await?))
    }

    async fn update_item_prices<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        let item_prices = ctx
            .calculate_item_prices(&self.currency, &self.promo_codes, &self.items)
            .await?;

        for item_price in item_prices.iter() {
            for item in self.items.iter_mut() {
                if item.sku == item_price.sku {
                    item.price = item_price.price.clone();
                    item.discount = item_price.discount.clone();
                    break;
                }
            }
        }

        Ok(())
    }

    fn reset_shipping_options(&mut self) {
        self.fulfillment = None;
        self.shipping_quotes = vec![];
    }
}

// public methods
impl<P: Sync + Send + Serialize + DeserializeOwned> Checkout<P> {
    pub async fn add_item<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        sku: String,
        quantity: u64,
    ) -> Result<(), Error> {
        self.ensure_shopping_state(ctx).await?;

        let mut is_added = false;
        for item in self.items.iter_mut() {
            if item.sku == sku {
                item.quantity += quantity;
                is_added = true;
                break;
            }
        }

        if !is_added {
            let product = ctx.resolve_product(&self.currency, &sku).await?;
            let item = Item {
                sku: sku.to_string(),
                url: product.url.clone(),
                title: product.title.clone(),
                description: product.description.clone(),
                quantity,
                price: product.price.clone(),
                discount: Money::new(self.currency.clone(), "0"),
                images: product.images.clone(),
            };
            self.items.push(item);
        }

        self.update_item_prices(ctx).await?;
        self.update_invoice(ctx).await?;

        ctx.update_checkout(self).await
    }

    pub async fn remove_item<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        sku: String,
        quantity: u64,
    ) -> Result<(), Error> {
        self.ensure_shopping_state(ctx).await?;

        let mut index_to_remove: Option<usize> = None;
        for (index, item) in self.items.iter_mut().enumerate() {
            if item.sku == sku {
                if item.quantity <= quantity {
                    index_to_remove = Some(index);
                    break;
                }

                item.quantity -= quantity;
                break;
            }
        }

        if let Some(index) = index_to_remove {
            self.items.remove(index);
        }

        self.update_item_prices(ctx).await?;
        self.update_invoice(ctx).await?;

        ctx.update_checkout(self).await
    }

    pub async fn confirm_items<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        self.ensure_items_confirmed_state(ctx).await?;

        ctx.update_checkout(self).await
    }

    pub async fn update_contact<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        contact: Contact,
    ) -> Result<(), Error> {
        self.ensure_not_payment_in_progress_or_completed_state()
            .await?;

        self.contact = Some(contact);

        ctx.update_checkout(self).await
    }

    pub async fn update_shipping_address<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        address: Address,
    ) -> Result<(), Error> {
        self.ensure_not_payment_in_progress_or_completed_state()
            .await?;

        self.reset_shipping_options();
        self.shipping_address = Some(address);
        if let State::ItemsConfirmed { reserved_since: _ } = self.state {
            self.shipping_quotes = ctx
                .get_shipping_quotes(
                    &self.currency,
                    &self.promo_codes,
                    &self.items,
                    self.shipping_address.as_ref().unwrap(),
                )
                .await?;
        }

        self.update_invoice(ctx).await?;
        ctx.update_checkout(self).await
    }

    pub async fn update_fulfillment<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        fulfillment: FulfillmentSelection,
    ) -> Result<(), Error> {
        self.ensure_items_confirmed_state(ctx).await?;

        match fulfillment {
            FulfillmentSelection::Pickup => {
                self.fulfillment = Some(Fulfillment::Pickup);
            }
            FulfillmentSelection::Shipping { quote_id } => {
                let mut updated = false;

                for quote in self.shipping_quotes.iter() {
                    if quote.id == quote_id {
                        self.fulfillment = Some(Fulfillment::Shipping {
                            quote: quote.clone(),
                        });
                        updated = true;
                        break;
                    }
                }

                if !updated {
                    return Err(new_invalid_state_error(
                        "the requested quote is not available",
                    ));
                }
            }
        }

        self.update_invoice(ctx).await?;
        ctx.update_checkout(self).await
    }

    pub async fn initiate_payment<C: Context + Send>(
        &mut self,
        ctx: &mut C,
        args: <C as PaymentProcessor>::InitArgs,
    ) -> Result<(), Error> {
        self.ensure_payment_in_progress_state(ctx, args).await?;

        ctx.update_checkout(self).await
    }

    pub async fn initiate_order<C: Context + Send>(
        &mut self,
        internal_ctx: &InternalContext,
        ctx: &mut C,
        payment_args: <C as PaymentProcessor>::InitArgs,
    ) -> Result<(), Error> {
        if let State::ItemsConfirmed { reserved_since } = self.state {
            self.ensure_completed_state().await?;
            let order = Order::new_from_checkout_missing_payment(
                internal_ctx,
                ctx,
                self,
                reserved_since,
                payment_args,
            )
            .await?;
            self.order_id = Some(order.id.clone());

            return ctx.update_checkout(self).await;
        } else {
            return Err(new_invalid_state_error(
                "must be in the ItemsConfirmed state",
            ));
        }
    }

    pub async fn release<C: Context + Send>(&mut self, ctx: &mut C) -> Result<(), Error> {
        match self.state {
            State::Shopping => Ok(()),
            State::ItemsConfirmed { reserved_since: _ }
            | State::PaymentInProgress { reserved_since: _ } => {
                if let State::PaymentInProgress { reserved_since: _ } = self.state {
                    self.payment_id = None;
                }

                self.enter_shopping_state(ctx).await?;
                self.update_item_prices(ctx).await?;
                self.update_invoice(ctx).await?;

                ctx.update_checkout(self).await
            }
            _ => Err(new_invalid_state_error(
                "must be in the shopping or items_confirmed state",
            )),
        }
    }

    pub async fn complete<C: Context + Send>(
        &mut self,
        internal_ctx: &InternalContext,
        ctx: &mut C,
    ) -> Result<(), Error> {
        if !matches!(self.state, State::PaymentInProgress { reserved_since: _ }) {
            return Err(new_invalid_state_error(
                "must be in the PaymentInProgress state",
            ));
        }

        self.ensure_completed_state().await?;
        let order = Order::new_from_checkout(internal_ctx, ctx, self).await?;
        self.order_id = Some(order.id.clone());

        ctx.update_checkout(self).await
    }

    pub async fn populate_associations<C: Context + Send>(
        &mut self,
        ctx: &mut C,
    ) -> Result<(), Error> {
        if let Some(ref payment_id) = self.payment_id {
            self.payment = ctx.get_payment(payment_id).await?;
        }

        if let Some(ref order_id) = self.order_id {
            let mut maybe_order: Option<Order<P>> = ctx.get_order(order_id).await?;
            if let Some(ref mut order) = maybe_order {
                order.populate_associations(ctx).await?;
            }
            self.order = maybe_order;
        }

        Ok(())
    }
}