-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from vuestorefront/feat-#82/checkout-middleware
feat: #82/checkout middleware
- Loading branch information
Showing
11 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Checkout middleware | ||
|
||
Router middleware responsible for checking if customer can visit certain steps of checkout page. | ||
Uses following helpers to verify if a page can be displayed to the customer: | ||
|
||
* `canEnterPayment` | ||
|
||
* `canEnterThankYou` | ||
|
||
* `canEnterShipping` | ||
|
||
* `canEnterBilling` | ||
|
||
If a helper returns false, customer will be redirected to homepage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Context } from '@nuxt/types'; | ||
import { Order } from '@vue-storefront/vendure'; | ||
import { ARRANGING_PAYMENT } from './constants'; | ||
|
||
export const canEnterThankYou = (context: Context): boolean => Boolean(context.route.query?.order); | ||
|
||
export const canEnterShipping = (cart: Order): boolean => Boolean(cart?.customer); | ||
|
||
export const canEnterBilling = (cart: Order): boolean => Boolean(cart?.shippingAddress?.streetLine1 && cart?.shippingAddress?.country); | ||
|
||
export const canEnterPayment = (cart: Order): boolean => canEnterShipping(cart) && canEnterBilling(cart) && cart?.shipping && cart?.state === ARRANGING_PAYMENT; | ||
|
||
export enum CheckoutSteps { | ||
Shipping = 'shipping', | ||
Billing = 'billing', | ||
Payment = 'payment', | ||
ThankYou = 'thank-you' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { canEnterShipping, canEnterBilling, canEnterPayment, canEnterThankYou, CheckoutSteps } from '../helpers'; | ||
|
||
export default async ({ app, $vsf }) => { | ||
const currentPath = app.context.route.fullPath.split('/checkout/')[1]; | ||
|
||
if (!currentPath) return; | ||
|
||
const cart = await $vsf.$vendure.api.getCart(); | ||
const activeCart = cart?.data?.activeOrder; | ||
|
||
if (!cart?.data || !activeCart) return; | ||
|
||
if (currentPath === CheckoutSteps.Shipping && !canEnterShipping(activeCart)) { | ||
app.context.redirect('/'); | ||
|
||
} else if (currentPath === CheckoutSteps.Billing && !canEnterBilling(activeCart)) { | ||
app.context.redirect('/'); | ||
|
||
} else if (currentPath === CheckoutSteps.Payment && !canEnterPayment(activeCart)) { | ||
app.context.redirect('/'); | ||
|
||
} else if (currentPath === CheckoutSteps.ThankYou && !canEnterThankYou(app.context)) { | ||
app.context.redirect('/'); | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters