strapi-plugin-payone-provider 1.6.3 → 1.6.4

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.
@@ -1,306 +1,327 @@
1
- import React from "react";
2
- import ApplePayButton from "apple-pay-button";
3
- import { Box, Typography, Alert } from "@strapi/design-system";
4
- import { request } from "@strapi/helper-plugin";
5
- import pluginId from "../../../pluginId";
6
-
7
- const ApplePayBtn = ({
8
- amount,
9
- onTokenReceived,
10
- onError,
11
- settings,
12
- buttonStyle = "black",
13
- type = "pay",
14
- }) => {
15
- const handleEventsForApplePay = (session, amountValue, currencyCode) => {
16
- session.onvalidatemerchant = async (event) => {
17
- try {
18
- const merchantSession = await request(
19
- `/${pluginId}/validate-apple-pay-merchant`,
20
- {
21
- method: "POST",
22
- body: {
23
- domain: window.location.hostname,
24
- displayName: settings?.merchantName || "Store",
25
- currency: currencyCode,
26
- },
27
- }
28
- );
29
-
30
- if (merchantSession.error) {
31
- throw new Error(
32
- merchantSession.error.message || "Merchant validation failed"
33
- );
34
- }
35
-
36
- const sessionData = merchantSession.data || merchantSession;
37
-
38
- if (!sessionData || !sessionData.merchantIdentifier) {
39
- console.error(
40
- "[Apple Pay Button] Invalid merchant session: missing merchantIdentifier"
41
- );
42
- throw new Error(
43
- "Invalid merchant session: missing merchantIdentifier"
44
- );
45
- }
46
-
47
- session.completeMerchantValidation(sessionData);
48
- console.log(
49
- "[Apple Pay Button] Merchant validation completed successfully"
50
- );
51
- } catch (error) {
52
- console.error("[Apple Pay Button] Merchant validation error:", error);
53
- if (onError) {
54
- onError(error);
55
- }
56
- session.completeMerchantValidation({});
57
- }
58
- };
59
-
60
- session.onpaymentmethodselected = (event) => {
61
- const update = {
62
- newTotal: {
63
- label: settings?.merchantName || "Total",
64
- type: "final",
65
- amount: amountValue,
66
- },
67
- };
68
- session.completePaymentMethodSelection(update);
69
- };
70
-
71
- session.onshippingmethodselected = (event) => {
72
- const update = {
73
- newTotal: {
74
- label: settings?.merchantName || "Total",
75
- type: "final",
76
- amount: amountValue,
77
- },
78
- };
79
- session.completeShippingMethodSelection(update);
80
- };
81
-
82
- session.onshippingcontactselected = (event) => {
83
- const update = {
84
- newTotal: {
85
- label: settings?.merchantName || "Total",
86
- type: "final",
87
- amount: amountValue,
88
- },
89
- };
90
- session.completeShippingContactSelection(update);
91
- };
92
-
93
- session.onpaymentauthorized = async (event) => {
94
- try {
95
- const paymentData = event.payment;
96
-
97
- if (!paymentData || !paymentData.token) {
98
- const result = {
99
- status: window.ApplePaySession.STATUS_FAILURE,
100
- };
101
- session.completePayment(result);
102
- if (onError) {
103
- onError(new Error("Payment token is missing"));
104
- }
105
- return;
106
- }
107
-
108
- const tokenObject = paymentData.token;
109
-
110
- if (!tokenObject.paymentData) {
111
- console.error(
112
- "[Apple Pay] Invalid token structure: missing paymentData"
113
- );
114
- const result = {
115
- status: window.ApplePaySession.STATUS_FAILURE,
116
- };
117
- session.completePayment(result);
118
- if (onError) {
119
- onError(new Error("Invalid Apple Pay token structure"));
120
- }
121
- return;
122
- }
123
-
124
- // Encode token as Base64 for transmission
125
- let tokenString;
126
- try {
127
- tokenString = btoa(
128
- unescape(encodeURIComponent(JSON.stringify(tokenObject)))
129
- );
130
- } catch (e) {
131
- console.error("[Apple Pay] Token encoding error:", e);
132
- tokenString = btoa(
133
- unescape(encodeURIComponent(JSON.stringify(tokenObject)))
134
- );
135
- }
136
-
137
- if (onTokenReceived) {
138
- const result = await onTokenReceived(tokenString, {
139
- paymentToken: tokenObject,
140
- billingContact: paymentData.billingContact,
141
- shippingContact: paymentData.shippingContact,
142
- amount: amountValue, //
143
- currency: currencyCode,
144
- });
145
-
146
- if (result && typeof result.then === "function") {
147
- await result;
148
- }
149
-
150
- const paymentResult = {
151
- status: window.ApplePaySession.STATUS_SUCCESS,
152
- };
153
- session.completePayment(paymentResult);
154
- } else {
155
- const paymentResult = {
156
- status: window.ApplePaySession.STATUS_SUCCESS,
157
- };
158
- session.completePayment(paymentResult);
159
- }
160
- } catch (error) {
161
- console.error("[Apple Pay] Payment authorization error:", error);
162
- const result = {
163
- status: window.ApplePaySession.STATUS_FAILURE,
164
- };
165
- session.completePayment(result);
166
- if (onError) {
167
- onError(error);
168
- }
169
- }
170
- };
171
-
172
- session.oncancel = (event) => {
173
- console.log("[Apple Pay Button] Session cancelled by user");
174
- };
175
- };
176
-
177
- const handleApplePayClick = () => {
178
- if (!settings?.mid) {
179
- const error = new Error(
180
- "Merchant ID is not configured. Please set Merchant ID in plugin settings."
181
- );
182
- if (onError) {
183
- onError(error);
184
- }
185
- return;
186
- }
187
-
188
- if (typeof window === "undefined" || !window.ApplePaySession) {
189
- if (onError) {
190
- onError(new Error("Apple Pay is not supported in this environment."));
191
- }
192
- return;
193
- }
194
-
195
- const amountValue = amount ? (parseFloat(amount) / 100).toFixed(2) : "0.00";
196
- const applePayConfig = settings?.applePayConfig || {};
197
- const supportedNetworks = applePayConfig.supportedNetworks || [
198
- "visa",
199
- "masterCard",
200
- "amex",
201
- ];
202
- const merchantCapabilities = applePayConfig.merchantCapabilities || [
203
- "supports3DS",
204
- ];
205
- const currencyCode = applePayConfig.currencyCode || "EUR";
206
- const countryCode = applePayConfig.countryCode || "DE";
207
-
208
- const applePayRequest = {
209
- countryCode: countryCode,
210
- currencyCode: currencyCode,
211
- merchantCapabilities: merchantCapabilities,
212
- supportedNetworks: supportedNetworks,
213
- total: {
214
- label: settings?.merchantName || "Total",
215
- type: "final",
216
- amount: amountValue,
217
- },
218
- };
219
-
220
- const session = new window.ApplePaySession(3, applePayRequest);
221
-
222
- handleEventsForApplePay(session, amountValue, currencyCode);
223
-
224
- session.begin();
225
- };
226
-
227
- const mode = (settings?.mode || "test").toLowerCase();
228
- const isLiveMode = mode === "live";
229
-
230
- if (!settings?.mid) {
231
- return (
232
- <Box>
233
- <Alert closeLabel="Close" title="Merchant ID Missing" variant="warning">
234
- <Typography variant="pi" marginTop={2}>
235
- Merchant ID is not configured. Please set Merchant ID in plugin
236
- settings. You can find your merchantIdentifier in PMI at:
237
- CONFIGURATION → PAYMENT PORTALS → [Your Portal] → Payment type
238
- configuration tab.
239
- </Typography>
240
- </Alert>
241
- </Box>
242
- );
243
- }
244
-
245
- if (!isLiveMode) {
246
- return (
247
- <Box>
248
- <Alert
249
- closeLabel="Close"
250
- title=" Apple Pay Only Works in Live Mode"
251
- variant="danger"
252
- >
253
- <Typography variant="pi" marginTop={2}>
254
- <strong>Apple Pay is only supported in live mode.</strong> According
255
- to Payone documentation, test mode support will be available at a
256
- later time.
257
- </Typography>
258
- <Typography variant="pi" style={{ marginLeft: "8px" }}>
259
- Please switch to <strong>live mode</strong> in plugin settings to
260
- use Apple Pay.
261
- </Typography>
262
- </Alert>
263
- </Box>
264
- );
265
- }
266
-
267
- const buttonStyleMap = {
268
- black: "black",
269
- white: "white",
270
- "white-outline": "white-outline",
271
- };
272
-
273
- const buttonTypeMap = {
274
- pay: "plain",
275
- buy: "buy",
276
- donate: "donate",
277
- "check-out": "check-out",
278
- book: "book",
279
- subscribe: "subscribe",
280
- };
281
-
282
- const nativeButtonStyle = buttonStyleMap[buttonStyle] || "black";
283
- const nativeButtonType = buttonTypeMap[type] || "plain";
284
-
285
- return (
286
- <Box style={{ minHeight: "40px", width: "100%" }}>
287
- <ApplePayButton
288
- onClick={handleApplePayClick}
289
- buttonStyle={nativeButtonStyle}
290
- type={nativeButtonType}
291
- style={{ width: "220px", height: "40px" }}
292
- />
293
- <br /> <br />
294
- <Typography
295
- variant="pi"
296
- textColor="neutral600"
297
- style={{ fontSize: "12px", marginTop: "8px", marginRight: "6px" }}
298
- >
299
- Apple Pay does NOT work on localhost. Use a production domain with
300
- HTTPS.
301
- </Typography>
302
- </Box>
303
- );
304
- };
305
-
306
- export default ApplePayBtn;
1
+ import React from "react";
2
+ import ApplePayButton from "apple-pay-button";
3
+ import { Box, Typography, Alert } from "@strapi/design-system";
4
+ import { request } from "@strapi/helper-plugin";
5
+ import pluginId from "../../../pluginId";
6
+
7
+ const ApplePayBtn = ({
8
+ amount,
9
+ onTokenReceived,
10
+ onError,
11
+ settings,
12
+ buttonStyle = "black",
13
+ type = "pay",
14
+ }) => {
15
+ const handleEventsForApplePay = (session, amountValue, currencyCode) => {
16
+ session.onvalidatemerchant = async (event) => {
17
+ try {
18
+ const applePayConfig = settings?.applePayConfig || {};
19
+ const requestCurrency =
20
+ currencyCode || applePayConfig.currencyCode || "EUR";
21
+ const requestCountryCode = applePayConfig.countryCode || "DE";
22
+
23
+ const merchantSession = await request(
24
+ `/${pluginId}/validate-apple-pay-merchant`,
25
+ {
26
+ method: "POST",
27
+ body: {
28
+ domain: window.location.hostname,
29
+ domainName: window.location.hostname,
30
+ displayName: settings?.merchantName || "Store",
31
+ currency: requestCurrency,
32
+ countryCode: requestCountryCode,
33
+ },
34
+ }
35
+ );
36
+
37
+ if (merchantSession.error) {
38
+ const errorMessage =
39
+ merchantSession.error.message || "Merchant validation failed";
40
+ const errorDetails = merchantSession.error.details || "";
41
+
42
+ // If it's a 403 error, provide more specific guidance
43
+ if (merchantSession.error.status === 403) {
44
+ throw new Error(
45
+ `403 Forbidden: Authentication failed with Payone. ` +
46
+ `Please check your Payone credentials (aid, portalid, mid, key) in plugin settings. ` +
47
+ `Also ensure that: 1) Mode is set to "live" (Apple Pay only works in live mode), ` +
48
+ `2) Your domain is registered with Payone Merchant Services, ` +
49
+ `3) Merchant ID (mid) matches your merchantIdentifier in PMI. ` +
50
+ `Details: ${errorDetails || errorMessage}`
51
+ );
52
+ }
53
+
54
+ throw new Error(
55
+ errorMessage + (errorDetails ? ` - ${errorDetails}` : "")
56
+ );
57
+ }
58
+
59
+ const sessionData = merchantSession.data || merchantSession;
60
+
61
+ if (!sessionData || !sessionData.merchantIdentifier) {
62
+ throw new Error(
63
+ "Invalid merchant session: missing merchantIdentifier. " +
64
+ "Please check your Payone Apple Pay configuration in PMI (CONFIGURATION → PAYMENT PORTALS → [Your Portal] → Payment type configuration tab)."
65
+ );
66
+ }
67
+
68
+ session.completeMerchantValidation(sessionData);
69
+ } catch (error) {
70
+
71
+ // Don't call completeMerchantValidation with empty object - this causes user cancellation
72
+ // Instead, let the error propagate so user can see what went wrong
73
+ if (onError) {
74
+ onError(error);
75
+ }
76
+
77
+ // Complete with failure status to show error to user
78
+ try {
79
+ session.completeMerchantValidation({});
80
+ } catch (completeError) {
81
+ // Silent fail
82
+ }
83
+ }
84
+ };
85
+
86
+ session.onpaymentmethodselected = (event) => {
87
+ const update = {
88
+ newTotal: {
89
+ label: settings?.merchantName || "Total",
90
+ type: "final",
91
+ amount: amountValue,
92
+ },
93
+ };
94
+ session.completePaymentMethodSelection(update);
95
+ };
96
+
97
+ session.onshippingmethodselected = (event) => {
98
+ const update = {
99
+ newTotal: {
100
+ label: settings?.merchantName || "Total",
101
+ type: "final",
102
+ amount: amountValue,
103
+ },
104
+ };
105
+ session.completeShippingMethodSelection(update);
106
+ };
107
+
108
+ session.onshippingcontactselected = (event) => {
109
+ const update = {
110
+ newTotal: {
111
+ label: settings?.merchantName || "Total",
112
+ type: "final",
113
+ amount: amountValue,
114
+ },
115
+ };
116
+ session.completeShippingContactSelection(update);
117
+ };
118
+
119
+ session.onpaymentauthorized = async (event) => {
120
+ try {
121
+ const paymentData = event.payment;
122
+
123
+ if (!paymentData || !paymentData.token) {
124
+ const result = {
125
+ status: window.ApplePaySession.STATUS_FAILURE,
126
+ };
127
+ session.completePayment(result);
128
+ if (onError) {
129
+ onError(new Error("Payment token is missing"));
130
+ }
131
+ return;
132
+ }
133
+
134
+ const tokenObject = paymentData.token;
135
+
136
+ if (!tokenObject.paymentData) {
137
+ const result = {
138
+ status: window.ApplePaySession.STATUS_FAILURE,
139
+ };
140
+ session.completePayment(result);
141
+ if (onError) {
142
+ onError(new Error("Invalid Apple Pay token structure"));
143
+ }
144
+ return;
145
+ }
146
+
147
+ // Encode token as Base64 for transmission
148
+ let tokenString;
149
+ try {
150
+ tokenString = btoa(
151
+ unescape(encodeURIComponent(JSON.stringify(tokenObject)))
152
+ );
153
+ } catch (e) {
154
+ tokenString = btoa(
155
+ unescape(encodeURIComponent(JSON.stringify(tokenObject)))
156
+ );
157
+ }
158
+
159
+ if (onTokenReceived) {
160
+ const result = await onTokenReceived(tokenString, {
161
+ paymentToken: tokenObject,
162
+ billingContact: paymentData.billingContact,
163
+ shippingContact: paymentData.shippingContact,
164
+ amount: amountValue, //
165
+ currency: currencyCode,
166
+ });
167
+
168
+ if (result && typeof result.then === "function") {
169
+ await result;
170
+ }
171
+
172
+ const paymentResult = {
173
+ status: window.ApplePaySession.STATUS_SUCCESS,
174
+ };
175
+ session.completePayment(paymentResult);
176
+ } else {
177
+ const paymentResult = {
178
+ status: window.ApplePaySession.STATUS_SUCCESS,
179
+ };
180
+ session.completePayment(paymentResult);
181
+ }
182
+ } catch (error) {
183
+ const result = {
184
+ status: window.ApplePaySession.STATUS_FAILURE,
185
+ };
186
+ session.completePayment(result);
187
+ if (onError) {
188
+ onError(error);
189
+ }
190
+ }
191
+ };
192
+
193
+ session.oncancel = (event) => {
194
+ // Session cancelled by user
195
+ };
196
+ };
197
+
198
+ const handleApplePayClick = () => {
199
+ if (!settings?.mid) {
200
+ const error = new Error(
201
+ "Merchant ID is not configured. Please set Merchant ID in plugin settings."
202
+ );
203
+ if (onError) {
204
+ onError(error);
205
+ }
206
+ return;
207
+ }
208
+
209
+ if (typeof window === "undefined" || !window.ApplePaySession) {
210
+ if (onError) {
211
+ onError(new Error("Apple Pay is not supported in this environment."));
212
+ }
213
+ return;
214
+ }
215
+
216
+ const amountValue = amount ? (parseFloat(amount) / 100).toFixed(2) : "0.00";
217
+ const applePayConfig = settings?.applePayConfig || {};
218
+ const supportedNetworks = applePayConfig.supportedNetworks || [
219
+ "visa",
220
+ "masterCard",
221
+ "amex",
222
+ ];
223
+ const merchantCapabilities = applePayConfig.merchantCapabilities || [
224
+ "supports3DS",
225
+ ];
226
+ const currencyCode = applePayConfig.currencyCode || "EUR";
227
+ const countryCode = applePayConfig.countryCode || "DE";
228
+
229
+ const applePayRequest = {
230
+ countryCode: countryCode,
231
+ currencyCode: currencyCode,
232
+ merchantCapabilities: merchantCapabilities,
233
+ supportedNetworks: supportedNetworks,
234
+ total: {
235
+ label: settings?.merchantName || "Total",
236
+ type: "final",
237
+ amount: amountValue,
238
+ },
239
+ };
240
+
241
+ const session = new window.ApplePaySession(3, applePayRequest);
242
+
243
+ handleEventsForApplePay(session, amountValue, currencyCode);
244
+
245
+ session.begin();
246
+ };
247
+
248
+ const mode = (settings?.mode || "test").toLowerCase();
249
+ const isLiveMode = mode === "live";
250
+
251
+ if (!settings?.mid) {
252
+ return (
253
+ <Box>
254
+ <Alert closeLabel="Close" title="Merchant ID Missing" variant="warning">
255
+ <Typography variant="pi" marginTop={2}>
256
+ Merchant ID is not configured. Please set Merchant ID in plugin
257
+ settings. You can find your merchantIdentifier in PMI at:
258
+ CONFIGURATION PAYMENT PORTALS [Your Portal] → Payment type
259
+ configuration tab.
260
+ </Typography>
261
+ </Alert>
262
+ </Box>
263
+ );
264
+ }
265
+
266
+ if (!isLiveMode) {
267
+ return (
268
+ <Box>
269
+ <Alert
270
+ closeLabel="Close"
271
+ title=" Apple Pay Only Works in Live Mode"
272
+ variant="danger"
273
+ >
274
+ <Typography variant="pi" marginTop={2}>
275
+ <strong>Apple Pay is only supported in live mode.</strong> According
276
+ to Payone documentation, test mode support will be available at a
277
+ later time.
278
+ </Typography>
279
+ <Typography variant="pi" style={{ marginLeft: "8px" }}>
280
+ Please switch to <strong>live mode</strong> in plugin settings to
281
+ use Apple Pay.
282
+ </Typography>
283
+ </Alert>
284
+ </Box>
285
+ );
286
+ }
287
+
288
+ const buttonStyleMap = {
289
+ black: "black",
290
+ white: "white",
291
+ "white-outline": "white-outline",
292
+ };
293
+
294
+ const buttonTypeMap = {
295
+ pay: "plain",
296
+ buy: "buy",
297
+ donate: "donate",
298
+ "check-out": "check-out",
299
+ book: "book",
300
+ subscribe: "subscribe",
301
+ };
302
+
303
+ const nativeButtonStyle = buttonStyleMap[buttonStyle] || "black";
304
+ const nativeButtonType = buttonTypeMap[type] || "plain";
305
+
306
+ return (
307
+ <Box style={{ minHeight: "40px", width: "100%" }}>
308
+ <ApplePayButton
309
+ onClick={handleApplePayClick}
310
+ buttonStyle={nativeButtonStyle}
311
+ type={nativeButtonType}
312
+ style={{ width: "220px", height: "40px" }}
313
+ />
314
+ <br /> <br />
315
+ <Typography
316
+ variant="pi"
317
+ textColor="neutral600"
318
+ style={{ fontSize: "12px", marginTop: "8px", marginRight: "6px" }}
319
+ >
320
+ Apple Pay does NOT work on localhost. Use a production domain with
321
+ HTTPS.
322
+ </Typography>
323
+ </Box>
324
+ );
325
+ };
326
+
327
+ export default ApplePayBtn;