Skip to content

[Main] Added :: Checkout SPA CX Module #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/checkout-spa/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DISABLE_ESLINT_PLUGIN=true
SKIP_PREFLIGHT_CHECK=true
28 changes: 28 additions & 0 deletions modules/checkout-spa/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Checkout Single Page Application</h1>
<p>This CX is an implementation of Checkout journey using CX, Headless API and React.</p>
<br/>
<h4>Libraries Used</h4>
<ul>
<li>Please refer to package.json for Libraries Info</li>
<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>
</ul>
<br/>
<h4>Setup Requirement</h4>
<ol>
<li>Create a commerce channel</li>
<li>Create a picklist having name as any but its erc should be :- commerceChannelPicklist</li>
<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>
<li>Preparation of Checkout page follows the same strategy of Liferay. Just instead of Liferay checkout, deploy this client-extension</li>
<li>Checkout button will redirect to this custom cx.</li>
</ol>
<br/>
<h4>Client Extension Details</h4>
<ul>
<li>OAuth2.0 Authentication. Token verfication happens automatically based on Admin User Creds</li>
<li>Billing Address (Compulsory)</li>
<li>Shipping Address (Compulsory)</li>
<li>Submit Button</li>
<p>I have not integrated the payment as it is process to create reusable components to create a SPA Checkout</p>
</ul>
<br/>
<p>Please feel free to reach out for any setup queries.</p>
15 changes: 15 additions & 0 deletions modules/checkout-spa/client-extension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
assemble:
- from: build/static
into: static
checkout-spa:
cssURLs:
- css/main.*.css
friendlyURLMapping: checkout-spa
htmlElementName: checkout-spa
instanceable: false
name: Checkout Spa
portletCategoryName: category.client-extensions
type: customElement
urls:
- js/main.*.js
useESM: true
40 changes: 40 additions & 0 deletions modules/checkout-spa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "checkout-spa",
"version": "0.1.0",
"private": true,
"dependencies": {
"@clayui/alert": "^3.111.0",
"@clayui/button": "^3.116.0",
"@clayui/form": "^3.119.0",
"jquery": "^3.7.1",
"jquery.cookie": "^1.4.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"react-spinners": "^0.14.1",
"sass": "^1.79.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
43 changes: 43 additions & 0 deletions modules/checkout-spa/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<checkout-spa route="hello-world"></checkout-spa>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
44 changes: 44 additions & 0 deletions modules/checkout-spa/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react";
import { CheckoutForm } from "./common/form/CheckoutForm";
import { findCommerceOrderCookie } from "./common/cookies/cookies";
import { getTranslation } from "./helper/Translation";
import { ItemDetails } from "./common/sections/ItemDetails/ItemDetails";

export function App() {

const [orderPresent, setOrderPresent] = React.useState(true);
const intervalRef = React.useRef(null);

React.useEffect(() => {
intervalRef.current = setInterval(() => {
const orderUUID = findCommerceOrderCookie();
if (orderUUID === null || orderUUID === undefined) {
setOrderPresent(false);
}
}, 2000);

return () => clearInterval(intervalRef.current);
}, []);

const handleSaveBtnEvent = () => {
clearInterval(intervalRef.current);
}

if (orderPresent) {
return (
<div>
<CheckoutForm onSaveSuccess = {handleSaveBtnEvent} />
<ItemDetails />
</div>
);
} else {
return (
<div>
<h3>{getTranslation("NoOrderFoundHeading", 'en')}</h3>
<h5>
<span>{getTranslation("NoOrderFoundBody", 'en')}</span>
</h5>
</div>
);
}
}
17 changes: 17 additions & 0 deletions modules/checkout-spa/src/common/buttons/AddAddressBtn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import ClayButton from '@clayui/button';
import { getTranslation } from "../../helper/Translation";

export function AddAddressBtn({onClick}){

return(
<div>
<ClayButton.Group spaced>
<ClayButton displayType="primary" onClick={onClick}>
{getTranslation("AddNewAddress", 'en')}
</ClayButton>
</ClayButton.Group>
</div>
)

}
11 changes: 11 additions & 0 deletions modules/checkout-spa/src/common/constants/Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Constants = {
"PICKLIST_DEF_ERC":"commerceChannelPicklist",
"BILLING_ADDRESS_HEADING":"Billing Address",
"SHIPPING_ADDRESS_HEADING":"Shipping Address",
"NO_CHANNEL_EXISTS":"No Channel Found !!!",
"COMMERCE_ORDER_COOKIE":"com.liferay.commerce.model.CommerceOrder#",
"BILLING":"Billing",
"SHIPPING":"Shipping"
}

export default Constants;
30 changes: 30 additions & 0 deletions modules/checkout-spa/src/common/constants/Language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const Languages = {
en : {
BillingAddress : "Billing Address",
ShippingAddress : "Shipping Address",
AddNewAddress : "Add new address",
Submit : "Save",
SelectAddress : "Select Address",
FirstName : "First Name",
LastName : "Last Name",
PhoneNumber : "Phone Number",
City : "City",
PostalCode : "Postal Code",
Address1 : "Address Line 1",
Address2 : "Addresss Line 2",
Address3 : "Address Line 3",
GoToHome : "Go To HomePage",
ThankYou : "Thank you for placing the order with us",
NoOrderFoundHeading : "No Open Commerce Order Found !!!",
NoOrderFoundBody : "Please add atleast 1 item to the cart to proceed",
ItemName : "Prodcut Name",
ItemCode : "Product Code",
Quantity : "Quantity",
PerUnit : "/unit",
Shipping : "Shipping",
Tax : "Tax",
SubTotal : "Sub-Total",
OrderTotal : "Order-Total"
}

}
36 changes: 36 additions & 0 deletions modules/checkout-spa/src/common/cookies/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Constants from "../constants/Constants";

export function findCommerceOrderCookie(){
const searchString = Constants.COMMERCE_ORDER_COOKIE;
const allCookies = document.cookie.split(';');

for (let cookie of allCookies) {
cookie = cookie.trim();

if (cookie.startsWith(searchString)) {
return cookie.substring(cookie.indexOf('=') + 1);
}
}

return null;
}

function findCookieByName(name){
const searchString = Constants.COMMERCE_ORDER_COOKIE;
const allCookies = document.cookie.split(';');

for (let cookie of allCookies) {
cookie = cookie.trim();

if (cookie.startsWith(searchString)) {
return cookie;
}
}

return null;
}

export function eraseCookie(name) {

document.cookie = findCookieByName(name) + '=; Max-Age=0; path=/;';
}
13 changes: 13 additions & 0 deletions modules/checkout-spa/src/common/createData/callOrderPlacedApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { patchUpdatedAccountData } from "../../helper/CommerceAccount";
import { patchCommerceOrder } from "../../helper/Order";
import { getTranslation } from "../../helper/Translation";
import BounceLoader from "react-spinners/ClipLoader";


export function callOrderPlacedApi(billingAddressId, shippingAddressId, accountId, accountName){

patchUpdatedAccountData(accountId, accountName, billingAddressId, shippingAddressId);

patchCommerceOrder(billingAddressId, shippingAddressId);

}
41 changes: 41 additions & 0 deletions modules/checkout-spa/src/common/createData/postAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import baseFetch from "../services/liferay/api";

export function postAddress(formData){

console.log("formData from address form is "+JSON.stringify(formData));

let postAddressUrl = `/o/headless-admin-user/v1.0/accounts/${formData.accountId}/postal-addresses`;

const jsonData = {
"addressCountry": "India",
"addressLocality": formData.city,
"addressRegion": "Delhi",
"addressType": formData.type.toLowerCase(),
"name": formData.firstName + formData.lastName,
"phoneNumber": formData.phoneNumber,
"postalCode": formData.zip,
"primary": false,
"streetAddressLine1": formData.address1,
"streetAddressLine2": formData.address2,
"streetAddressLine3": formData.address3
}


console.log("jsonData for address before being posted is "+JSON.stringify(jsonData));
async function postData(){
await baseFetch(postAddressUrl,{
method: 'POST',
body: JSON.stringify(jsonData)
}).then((res)=>{
if (!res.ok){
throw new Error("Exception while posting address");
}
return res.json();
}).then((data) => {
console.log(data);
});
}

postData();

}
Loading