@akinon/pz-cybersource-uc 1.107.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitattributes ADDED
@@ -0,0 +1,15 @@
1
+ *.js text eol=lf
2
+ *.jsx text eol=lf
3
+ *.ts text eol=lf
4
+ *.tsx text eol=lf
5
+ *.json text eol=lf
6
+ *.md text eol=lf
7
+
8
+ .eslintignore text eol=lf
9
+ .eslintrc text eol=lf
10
+ .gitignore text eol=lf
11
+ .prettierrc text eol=lf
12
+ .yarnrc text eol=lf
13
+
14
+ * text=auto
15
+
package/.prettierrc ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "bracketSameLine": false,
3
+ "tabWidth": 2,
4
+ "singleQuote": true,
5
+ "jsxSingleQuote": false,
6
+ "bracketSpacing": true,
7
+ "semi": true,
8
+ "useTabs": false,
9
+ "arrowParens": "always",
10
+ "endOfLine": "lf",
11
+ "proseWrap": "never",
12
+ "trailingComma": "none"
13
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # @akinon/pz-cybersource-uc
2
+
3
+ ## 1.107.0
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@akinon/pz-cybersource-uc",
3
+ "version": "1.107.0",
4
+ "license": "MIT",
5
+ "main": "src/index.tsx",
6
+ "peerDependencies": {
7
+ "react": "^18.0.0",
8
+ "react-dom": "^18.0.0"
9
+ },
10
+ "dependencies": {
11
+ "react-redux": "8.1.3"
12
+ },
13
+ "devDependencies": {
14
+ "@types/node": "^18.7.8",
15
+ "@types/react": "^18.0.17",
16
+ "@types/react-dom": "^18.0.6",
17
+ "prettier": "^3.0.3",
18
+ "react": "^18.2.0",
19
+ "react-dom": "^18.2.0",
20
+ "typescript": "^5.2.2"
21
+ }
22
+ }
package/src/index.tsx ADDED
@@ -0,0 +1 @@
1
+ export { default as CyberSourceUcPaymentOption } from './views/payment-option';
@@ -0,0 +1,57 @@
1
+ import { CheckoutResult } from '@akinon/next/types';
2
+ import { Middleware, ThunkDispatch, AnyAction } from '@reduxjs/toolkit';
3
+ import { setupCyberSourceUc } from './reducer';
4
+ import { checkoutApi } from '@akinon/next/data/client/checkout';
5
+ import { RootState } from 'redux/store';
6
+
7
+ type CyberSourceUcPaymentData = {
8
+ capture_context: string;
9
+ client_library: string;
10
+ client_library_integrity: string;
11
+ };
12
+
13
+ const cyberSourceUcMiddleware: Middleware<
14
+ unknown,
15
+ RootState,
16
+ ThunkDispatch<RootState, any, AnyAction>
17
+ > =
18
+ ({ dispatch }) =>
19
+ (next) =>
20
+ (action) => {
21
+ const result: CheckoutResult = next(action);
22
+ const { payload } = result;
23
+ const contextList = payload?.context_list;
24
+ const preOrder = payload?.pre_order;
25
+ const { endpoints: apiEndpoints } = checkoutApi;
26
+
27
+ if (contextList) {
28
+ const cyberSourceUcContext = contextList.find(
29
+ (context) =>
30
+ context.page_slug === 'walletselectionpage' &&
31
+ context.page_context?.paymentMethod === 'cybersource_uc'
32
+ );
33
+
34
+ if (cyberSourceUcContext) {
35
+ const paymentData: CyberSourceUcPaymentData =
36
+ cyberSourceUcContext.page_context.paymentData.data;
37
+
38
+ dispatch(
39
+ apiEndpoints.setWalletSelectionPage.initiate({
40
+ payment_option: preOrder.payment_option.pk
41
+ })
42
+ );
43
+
44
+ dispatch(
45
+ setupCyberSourceUc({
46
+ captureContext: paymentData.capture_context,
47
+ clientLibrary: paymentData.client_library,
48
+ clientLibraryIntegrity: paymentData.client_library_integrity
49
+ })
50
+ );
51
+ }
52
+ }
53
+
54
+ return result;
55
+ };
56
+
57
+ export default cyberSourceUcMiddleware;
@@ -0,0 +1,39 @@
1
+ 'use client';
2
+
3
+ import { createSlice } from '@reduxjs/toolkit';
4
+
5
+ export type CyberSourceUcState = {
6
+ captureContext?: string;
7
+ clientLibrary?: string;
8
+ clientLibraryIntegrity?: string;
9
+ };
10
+
11
+ const initialState: CyberSourceUcState = {
12
+ captureContext: undefined,
13
+ clientLibrary: undefined,
14
+ clientLibraryIntegrity: undefined
15
+ };
16
+
17
+ const cyberSourceUcSlicer = createSlice({
18
+ name: 'cybersource_uc',
19
+ initialState,
20
+ reducers: {
21
+ setupCyberSourceUc: (
22
+ state,
23
+ action: {
24
+ payload: Pick<
25
+ CyberSourceUcState,
26
+ 'captureContext' | 'clientLibrary' | 'clientLibraryIntegrity'
27
+ >;
28
+ }
29
+ ) => {
30
+ state.captureContext = action.payload.captureContext;
31
+ state.clientLibrary = action.payload.clientLibrary;
32
+ state.clientLibraryIntegrity = action.payload.clientLibraryIntegrity;
33
+ }
34
+ }
35
+ });
36
+
37
+ export const { setupCyberSourceUc } = cyberSourceUcSlicer.actions;
38
+
39
+ export default cyberSourceUcSlicer.reducer;
@@ -0,0 +1,126 @@
1
+ import { useAppSelector } from '@akinon/next/redux/hooks';
2
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
3
+ import Script from 'next/script';
4
+ import { LoaderSpinner } from '@akinon/next/components';
5
+ import { useSetWalletPaymentPageMutation } from '@akinon/next/data/client/checkout';
6
+ import { getPosError } from '@akinon/next/utils';
7
+ import { CyberSourceUcState } from '../redux/reducer';
8
+
9
+ const CyberSourceUcPaymentOption = () => {
10
+ const [libraryLoaded, setLibraryLoaded] = useState<boolean>(false);
11
+ const [loading, setLoading] = useState<boolean>(true);
12
+ const [errorMessage, setErrorMessage] = useState<string | null>(null);
13
+ const timeoutRef = useRef<NodeJS.Timeout>();
14
+
15
+ const { captureContext, clientLibrary, clientLibraryIntegrity } =
16
+ useAppSelector((state): CyberSourceUcState => state.cybersource_uc);
17
+
18
+ const [setWalletPaymentPage] = useSetWalletPaymentPageMutation();
19
+
20
+ const launchCheckout = useCallback(async () => {
21
+ const sidebar = false;
22
+ const showArgs = {
23
+ containers: {
24
+ paymentSelection: '#buttonPaymentListContainer',
25
+ paymentScreen: '#embeddedPaymentContainer'
26
+ }
27
+ };
28
+
29
+ try {
30
+ const buttonContainer = document.getElementById(
31
+ 'buttonPaymentListContainer'
32
+ );
33
+ const paymentContainer = document.getElementById(
34
+ 'embeddedPaymentContainer'
35
+ );
36
+ if (buttonContainer) buttonContainer.innerHTML = '';
37
+ if (paymentContainer) paymentContainer.innerHTML = '';
38
+ setErrorMessage(null);
39
+
40
+ const accept = await window.Accept(captureContext);
41
+ const up = await accept.unifiedPayments(sidebar);
42
+ setLoading(false);
43
+ const tt = await up.show(showArgs);
44
+
45
+ const response = await setWalletPaymentPage({
46
+ payment_token: tt,
47
+ agreement: true
48
+ }).unwrap();
49
+
50
+ if (response?.errors) {
51
+ const errorMessages = Object.values(response.errors)
52
+ .filter((value) => value)
53
+ .join('. ');
54
+
55
+ if (errorMessages) {
56
+ setErrorMessage(errorMessages);
57
+ timeoutRef.current = setTimeout(() => {
58
+ setLoading(true);
59
+ launchCheckout();
60
+ }, 2000);
61
+ }
62
+ }
63
+ } catch (error) {
64
+ console.error('something went wrong: ', error);
65
+ let errorMsg = 'An error occurred during payment processing';
66
+
67
+ if (error?.data?.errors) {
68
+ const errorMessages = Object.values(error.data.errors)
69
+ .filter((value) => value)
70
+ .join('. ');
71
+ errorMsg = errorMessages || errorMsg;
72
+ } else if (error?.data?.error) {
73
+ errorMsg = error.data.error;
74
+ }
75
+
76
+ setErrorMessage(errorMsg);
77
+
78
+ timeoutRef.current = setTimeout(() => {
79
+ setLoading(true);
80
+ launchCheckout();
81
+ }, 2000);
82
+ }
83
+ }, [captureContext, setWalletPaymentPage]);
84
+
85
+ useEffect(() => {
86
+ const posErrors = getPosError();
87
+ if (posErrors && Object.keys(posErrors).length > 0) {
88
+ const errorMessages = Object.values(posErrors).join('. ');
89
+ setErrorMessage(errorMessages);
90
+ }
91
+
92
+ return () => {
93
+ if (timeoutRef.current) {
94
+ clearTimeout(timeoutRef.current);
95
+ }
96
+ };
97
+ }, []);
98
+
99
+ useEffect(() => {
100
+ if (libraryLoaded) {
101
+ launchCheckout().then();
102
+ }
103
+ }, [libraryLoaded, launchCheckout]);
104
+
105
+ return (
106
+ <div>
107
+ <div className="p-4">
108
+ {loading && <LoaderSpinner />}
109
+ <div id="buttonPaymentListContainer"></div>
110
+ <div id="paymentScreen"></div>
111
+ <div id="embeddedPaymentContainer"></div>
112
+ {errorMessage && (
113
+ <div className="text-[#ef4444] mt-4">{errorMessage}</div>
114
+ )}
115
+ </div>
116
+ <Script
117
+ src={clientLibrary}
118
+ integrity={clientLibraryIntegrity}
119
+ crossOrigin="anonymous"
120
+ onLoad={() => setLibraryLoaded(true)}
121
+ ></Script>
122
+ </div>
123
+ );
124
+ };
125
+
126
+ export default CyberSourceUcPaymentOption;