Skip to content

Commit 7b9324e

Browse files
committed
[Main] Added :: Checkout SPA CX Module
1 parent a96e351 commit 7b9324e

35 files changed

+1416
-0
lines changed

modules/checkout-spa/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DISABLE_ESLINT_PLUGIN=true
2+
SKIP_PREFLIGHT_CHECK=true
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h1>Checkout Single Page Application</h1>
2+
<p>This CX is an implementation of Checkout journey using CX, Headless API and React.</p>
3+
<br/>
4+
<h4>Libraries Used</h4>
5+
<ul>
6+
<li>Please refer to package.json for Libraries Info</li>
7+
<p><b>Note</b>I have tried to keep external libraries to minimum. This CX is built up using all open source and open libraries available. This demonstrates implementation outside of Liferay</p>
8+
</ul>
9+
<br/>
10+
<h4>Setup Requirement</h4>
11+
<ol>
12+
<li>Create a commerce channel</li>
13+
<li>Create a picklist having name as any but its erc should be :- commerceChannelPicklist</li>
14+
<li>Set erc of the commerce channel to any and make an entry inside the created picklist with the same erc used in commerce channel. Name, Key and ERC of the picklist entry should be same as commerce channel erc</li>
15+
<li>Preparation of Checkout page follows the same strategy of Liferay. Just instead of Liferay checkout, deploy this client-extension</li>
16+
<li>Checkout button will redirect to this custom cx.</li>
17+
</ol>
18+
<br/>
19+
<h4>Client Extension Details</h4>
20+
<ul>
21+
<li>OAuth2.0 Authentication. Token verfication happens automatically based on Admin User Creds</li>
22+
<li>Billing Address (Compulsory)</li>
23+
<li>Shipping Address (Compulsory)</li>
24+
<li>Submit Button</li>
25+
<p>I have not integrated the payment as it is process to create reusable components to create a SPA Checkout</p>
26+
</ul>
27+
<br/>
28+
<p>Please feel free to reach out for any setup queries.</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
assemble:
2+
- from: build/static
3+
into: static
4+
checkout-spa:
5+
cssURLs:
6+
- css/main.*.css
7+
friendlyURLMapping: checkout-spa
8+
htmlElementName: checkout-spa
9+
instanceable: false
10+
name: Checkout Spa
11+
portletCategoryName: category.client-extensions
12+
type: customElement
13+
urls:
14+
- js/main.*.js
15+
useESM: true

modules/checkout-spa/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "checkout-spa",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@clayui/alert": "^3.111.0",
7+
"@clayui/button": "^3.116.0",
8+
"@clayui/form": "^3.119.0",
9+
"jquery": "^3.7.1",
10+
"jquery.cookie": "^1.4.1",
11+
"react": "^18.3.1",
12+
"react-dom": "^18.3.1",
13+
"react-scripts": "5.0.1",
14+
"react-spinners": "^0.14.1",
15+
"sass": "^1.79.1"
16+
},
17+
"scripts": {
18+
"start": "react-scripts start",
19+
"build": "react-scripts build",
20+
"eject": "react-scripts eject"
21+
},
22+
"eslintConfig": {
23+
"extends": [
24+
"react-app",
25+
"react-app/jest"
26+
]
27+
},
28+
"browserslist": {
29+
"production": [
30+
">0.2%",
31+
"not dead",
32+
"not op_mini all"
33+
],
34+
"development": [
35+
"last 1 chrome version",
36+
"last 1 firefox version",
37+
"last 1 safari version"
38+
]
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<checkout-spa route="hello-world"></checkout-spa>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

modules/checkout-spa/src/App.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from "react";
2+
import { CheckoutForm } from "./common/form/CheckoutForm";
3+
import { findCommerceOrderCookie } from "./common/cookies/cookies";
4+
import { getTranslation } from "./helper/Translation";
5+
import { ItemDetails } from "./common/sections/ItemDetails/ItemDetails";
6+
7+
export function App() {
8+
9+
const [orderPresent, setOrderPresent] = React.useState(true);
10+
const intervalRef = React.useRef(null);
11+
12+
React.useEffect(() => {
13+
intervalRef.current = setInterval(() => {
14+
const orderUUID = findCommerceOrderCookie();
15+
if (orderUUID === null || orderUUID === undefined) {
16+
setOrderPresent(false);
17+
}
18+
}, 2000);
19+
20+
return () => clearInterval(intervalRef.current);
21+
}, []);
22+
23+
const handleSaveBtnEvent = () => {
24+
clearInterval(intervalRef.current);
25+
}
26+
27+
if (orderPresent) {
28+
return (
29+
<div>
30+
<CheckoutForm onSaveSuccess = {handleSaveBtnEvent} />
31+
<ItemDetails />
32+
</div>
33+
);
34+
} else {
35+
return (
36+
<div>
37+
<h3>{getTranslation("NoOrderFoundHeading", 'en')}</h3>
38+
<h5>
39+
<span>{getTranslation("NoOrderFoundBody", 'en')}</span>
40+
</h5>
41+
</div>
42+
);
43+
}
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from "react";
2+
import ClayButton from '@clayui/button';
3+
import { getTranslation } from "../../helper/Translation";
4+
5+
export function AddAddressBtn({onClick}){
6+
7+
return(
8+
<div>
9+
<ClayButton.Group spaced>
10+
<ClayButton displayType="primary" onClick={onClick}>
11+
{getTranslation("AddNewAddress", 'en')}
12+
</ClayButton>
13+
</ClayButton.Group>
14+
</div>
15+
)
16+
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Constants = {
2+
"PICKLIST_DEF_ERC":"commerceChannelPicklist",
3+
"BILLING_ADDRESS_HEADING":"Billing Address",
4+
"SHIPPING_ADDRESS_HEADING":"Shipping Address",
5+
"NO_CHANNEL_EXISTS":"No Channel Found !!!",
6+
"COMMERCE_ORDER_COOKIE":"com.liferay.commerce.model.CommerceOrder#",
7+
"BILLING":"Billing",
8+
"SHIPPING":"Shipping"
9+
}
10+
11+
export default Constants;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const Languages = {
2+
en : {
3+
BillingAddress : "Billing Address",
4+
ShippingAddress : "Shipping Address",
5+
AddNewAddress : "Add new address",
6+
Submit : "Save",
7+
SelectAddress : "Select Address",
8+
FirstName : "First Name",
9+
LastName : "Last Name",
10+
PhoneNumber : "Phone Number",
11+
City : "City",
12+
PostalCode : "Postal Code",
13+
Address1 : "Address Line 1",
14+
Address2 : "Addresss Line 2",
15+
Address3 : "Address Line 3",
16+
GoToHome : "Go To HomePage",
17+
ThankYou : "Thank you for placing the order with us",
18+
NoOrderFoundHeading : "No Open Commerce Order Found !!!",
19+
NoOrderFoundBody : "Please add atleast 1 item to the cart to proceed",
20+
ItemName : "Prodcut Name",
21+
ItemCode : "Product Code",
22+
Quantity : "Quantity",
23+
PerUnit : "/unit",
24+
Shipping : "Shipping",
25+
Tax : "Tax",
26+
SubTotal : "Sub-Total",
27+
OrderTotal : "Order-Total"
28+
}
29+
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Constants from "../constants/Constants";
2+
3+
export function findCommerceOrderCookie(){
4+
const searchString = Constants.COMMERCE_ORDER_COOKIE;
5+
const allCookies = document.cookie.split(';');
6+
7+
for (let cookie of allCookies) {
8+
cookie = cookie.trim();
9+
10+
if (cookie.startsWith(searchString)) {
11+
return cookie.substring(cookie.indexOf('=') + 1);
12+
}
13+
}
14+
15+
return null;
16+
}
17+
18+
function findCookieByName(name){
19+
const searchString = Constants.COMMERCE_ORDER_COOKIE;
20+
const allCookies = document.cookie.split(';');
21+
22+
for (let cookie of allCookies) {
23+
cookie = cookie.trim();
24+
25+
if (cookie.startsWith(searchString)) {
26+
return cookie;
27+
}
28+
}
29+
30+
return null;
31+
}
32+
33+
export function eraseCookie(name) {
34+
35+
document.cookie = findCookieByName(name) + '=; Max-Age=0; path=/;';
36+
}

0 commit comments

Comments
 (0)