@akinon/pz-apple-pay 1.71.0

Sign up to get free protection for your applications and to get access to all the features.
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/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@akinon/pz-apple-pay",
3
+ "version": "1.71.0",
4
+ "license": "MIT",
5
+ "main": "src/index.tsx",
6
+ "peerDependencies": {
7
+ "react": "^18.0.0",
8
+ "react-dom": "^18.0.0"
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "^18.7.8",
12
+ "@types/react": "^18.0.17",
13
+ "@types/react-dom": "^18.0.6",
14
+ "prettier": "^3.0.3",
15
+ "react": "^18.2.0",
16
+ "react-dom": "^18.2.0",
17
+ "typescript": "^5.2.2"
18
+ }
19
+ }
package/readme.md ADDED
@@ -0,0 +1,21 @@
1
+ # Project Zero - Apple Pay Plugin
2
+
3
+ ## Installation
4
+
5
+ There are two ways to install the Apple Pay plugin:
6
+
7
+ ### 1. Install the npm package using Yarn
8
+
9
+ For the latest version, you can install the package using Yarn:
10
+
11
+ ```bash
12
+ yarn add @akinon/pz-apple-pay
13
+ ```
14
+
15
+ ### 2. Preferred installation method
16
+
17
+ You can also use the following command to install the extension with the latest plugins:
18
+
19
+ ```bash
20
+ npx @akinon/projectzero@latest --plugins
21
+ ```
package/src/index.tsx ADDED
@@ -0,0 +1,3 @@
1
+ import ApplePayView from './views';
2
+
3
+ export default ApplePayView;
@@ -0,0 +1,143 @@
1
+ 'use client';
2
+
3
+ import { checkoutApi } from '@akinon/next/data/client/checkout';
4
+ import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks';
5
+ import { getCookie } from '@akinon/next/utils';
6
+ import { getUrlPathWithLocale } from '@akinon/next/utils/localization';
7
+ import { RootState } from '@theme/redux/store';
8
+ import { useMemo, useState } from 'react';
9
+
10
+ export default function ApplePay() {
11
+ const { walletPaymentData, preOrder } = useAppSelector(
12
+ (state: RootState) => state.checkout
13
+ );
14
+ const dispatch = useAppDispatch();
15
+ const [errors, setErrors] = useState(null);
16
+
17
+ const paymentErrors = useMemo(() => {
18
+ if (typeof errors === 'string') {
19
+ return errors;
20
+ } else if (Array.isArray(errors)) {
21
+ return errors.join(', ');
22
+ } else if (typeof errors === 'object') {
23
+ return Object.values(errors ?? {}).join(', ');
24
+ }
25
+
26
+ return null;
27
+ }, [errors]);
28
+
29
+ const onMerchantValidation = async (event: any) => {
30
+ const walletSelectionPageResponse = await dispatch(
31
+ checkoutApi.endpoints.setWalletSelectionPage.initiate({
32
+ payment_option: preOrder.payment_option?.pk,
33
+ validationURL: event.validationURL
34
+ })
35
+ ).unwrap();
36
+
37
+ const walletPaymentPageContext =
38
+ walletSelectionPageResponse.context_list.find(
39
+ (c) => c.page_name === 'WalletPaymentPage'
40
+ );
41
+
42
+ if (!walletPaymentPageContext) {
43
+ return;
44
+ }
45
+
46
+ event.complete(walletPaymentPageContext.page_context.context_data);
47
+ };
48
+
49
+ const initPaymentRequest = async () => {
50
+ setErrors(null);
51
+
52
+ const paymentMethodData = [
53
+ {
54
+ supportedMethods: walletPaymentData.supportedMethods,
55
+ data: walletPaymentData.data
56
+ }
57
+ ];
58
+
59
+ const paymentDetails = {
60
+ total: {
61
+ label: walletPaymentData.detail.label,
62
+ amount: walletPaymentData.detail.amount
63
+ },
64
+ displayItems: [
65
+ {
66
+ ...walletPaymentData.detail
67
+ }
68
+ ]
69
+ };
70
+
71
+ const request = new PaymentRequest(
72
+ paymentMethodData,
73
+ paymentDetails
74
+ ) as any;
75
+
76
+ request.onmerchantvalidation = onMerchantValidation;
77
+
78
+ const paymentRequestResponse = await request.show();
79
+
80
+ if (paymentRequestResponse.details?.token?.paymentData) {
81
+ const paymentData = paymentRequestResponse.details.token.paymentData;
82
+
83
+ const walletPaymentPageResponse = await dispatch(
84
+ checkoutApi.endpoints.setWalletPaymentPage.initiate({
85
+ payment_token: JSON.stringify(paymentData)
86
+ })
87
+ ).unwrap();
88
+
89
+ if (walletPaymentPageResponse.errors) {
90
+ setErrors(walletPaymentPageResponse.errors);
91
+ }
92
+
93
+ if (
94
+ walletPaymentPageResponse.context_list.find(
95
+ (c) => c.page_name === 'WalletCompletePage'
96
+ )
97
+ ) {
98
+ const paymentCompleteResponse = await dispatch(
99
+ checkoutApi.endpoints.setWalletCompletePage.initiate()
100
+ ).unwrap();
101
+
102
+ if (paymentCompleteResponse.errors) {
103
+ setErrors(paymentCompleteResponse.errors);
104
+ }
105
+
106
+ if (
107
+ paymentCompleteResponse.context_list.find(
108
+ (c) => c.page_name === 'ThankYouPage'
109
+ )
110
+ ) {
111
+ const redirectUrl = paymentCompleteResponse.context_list.find(
112
+ (c) => c.page_name === 'ThankYouPage'
113
+ )?.page_context.context_data.redirect_url;
114
+
115
+ const redirectUrlWithLocale = `${
116
+ window.location.origin
117
+ }${getUrlPathWithLocale(redirectUrl, getCookie('pz-locale'))}`;
118
+
119
+ await paymentRequestResponse.complete('success');
120
+ window.location.href = redirectUrlWithLocale;
121
+ } else {
122
+ await paymentRequestResponse.complete('fail');
123
+ }
124
+ } else {
125
+ await paymentRequestResponse.complete('fail');
126
+ }
127
+ }
128
+ };
129
+
130
+ if (!walletPaymentData) {
131
+ return null;
132
+ }
133
+
134
+ return (
135
+ <div>
136
+ <button onClick={initPaymentRequest}>Pay with Apple Pay</button>
137
+
138
+ {paymentErrors?.length > 0 && (
139
+ <div className="text-error-100 text-xs mt-5">{paymentErrors}</div>
140
+ )}
141
+ </div>
142
+ );
143
+ }