pay-sdk-react 1.1.3 → 1.1.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.
- package/dist/es/PayMask/interface.d.ts +7 -1
- package/dist/es/PayMask/pay-mask.js +10 -3
- package/dist/es/confirm-modal/index.js +7 -3
- package/dist/es/confirm-modal/interface.d.ts +7 -1
- package/dist/es/hooks/usePayWindowOpen.d.ts +16 -10
- package/dist/es/hooks/usePayWindowOpen.js +62 -22
- package/dist/es/shared/enums/index.d.ts +21 -0
- package/dist/es/shared/enums/index.js +23 -0
- package/dist/es/utils/is.d.ts +10 -1
- package/dist/es/utils/is.js +14 -2
- package/dist/lib/PayMask/pay-mask.js +6 -1
- package/dist/lib/confirm-modal/index.js +7 -2
- package/dist/lib/hooks/usePayWindowOpen.js +63 -20
- package/dist/lib/shared/enums/index.js +23 -1
- package/dist/lib/utils/is.js +16 -3
- package/dist/umd/pay-sdk-react.umd.js +162 -72
- package/dist/umd/pay-sdk-react.umd.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { OpenPayUrlMode } from '../
|
|
2
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
3
3
|
import { MaskProps } from '../Mask';
|
|
4
4
|
/**
|
|
5
5
|
* @version 1.1.0
|
|
@@ -53,4 +53,10 @@ export interface PayMaskProps extends MaskProps {
|
|
|
53
53
|
* @default "window"
|
|
54
54
|
*/
|
|
55
55
|
openMode?: OpenPayUrlMode;
|
|
56
|
+
/**
|
|
57
|
+
* @zh 是否强制打开新窗口(仅在 openMode 为 'window' 且非移动端设备时生效)
|
|
58
|
+
* @en Whether to force open new window (only effective when openMode is 'window' and on non-mobile devices)
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
forceNewWindow?: boolean;
|
|
56
62
|
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
|
|
2
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
2
3
|
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
3
4
|
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
|
|
4
|
-
var _excluded = ["visible", "payUrl", "onClose", "onMaskClick", "onContinue", "openMode", "autoOpenWindow", "className", "destroyOnClose"];
|
|
5
|
+
var _excluded = ["visible", "payUrl", "onClose", "onMaskClick", "onContinue", "openMode", "autoOpenWindow", "forceNewWindow", "className", "destroyOnClose"];
|
|
6
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
8
|
import React, { useEffect, useState } from 'react';
|
|
6
9
|
import Mask from '../Mask';
|
|
7
10
|
import { usePayWindowOpen } from '../hooks/usePayWindowOpen';
|
|
8
11
|
import { mergeProps } from '../utils/with-default-props';
|
|
9
12
|
import cs from '../utils/classNames';
|
|
10
13
|
import { getPrefixCls } from '../utils/getPrefixCls';
|
|
14
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
11
15
|
var classPrefix = getPrefixCls('mask');
|
|
12
16
|
var defaultProps = {
|
|
13
17
|
visible: false,
|
|
@@ -25,6 +29,7 @@ var PayMask = function PayMask(p) {
|
|
|
25
29
|
onContinue = props.onContinue,
|
|
26
30
|
openMode = props.openMode,
|
|
27
31
|
autoOpenWindow = props.autoOpenWindow,
|
|
32
|
+
forceNewWindow = props.forceNewWindow,
|
|
28
33
|
className = props.className,
|
|
29
34
|
_props$destroyOnClose = props.destroyOnClose,
|
|
30
35
|
destroyOnClose = _props$destroyOnClose === void 0 ? true : _props$destroyOnClose,
|
|
@@ -33,7 +38,7 @@ var PayMask = function PayMask(p) {
|
|
|
33
38
|
_useState2 = _slicedToArray(_useState, 2),
|
|
34
39
|
isWindowMonitoring = _useState2[0],
|
|
35
40
|
setIsWindowMonitoring = _useState2[1];
|
|
36
|
-
var _usePayWindowOpen = usePayWindowOpen({
|
|
41
|
+
var _usePayWindowOpen = usePayWindowOpen(_objectSpread({
|
|
37
42
|
payUrl: payUrl || '',
|
|
38
43
|
onClose: function onClose() {
|
|
39
44
|
_onClose === null || _onClose === void 0 || _onClose();
|
|
@@ -41,7 +46,9 @@ var PayMask = function PayMask(p) {
|
|
|
41
46
|
},
|
|
42
47
|
autoOpen: autoOpenWindow,
|
|
43
48
|
openMode: openMode
|
|
44
|
-
}
|
|
49
|
+
}, openMode === OpenPayUrlMode.Window && {
|
|
50
|
+
forceNewWindow: forceNewWindow
|
|
51
|
+
})),
|
|
45
52
|
openWindow = _usePayWindowOpen.openWindow,
|
|
46
53
|
closeWindow = _usePayWindowOpen.closeWindow;
|
|
47
54
|
useEffect(function () {
|
|
@@ -7,6 +7,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
|
|
7
7
|
import React, { useCallback } from 'react';
|
|
8
8
|
import { usePayWindowOpen } from '../hooks/usePayWindowOpen';
|
|
9
9
|
import PayModal from '../pay-modal';
|
|
10
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
10
11
|
var ConfirmModal = function ConfirmModal(props) {
|
|
11
12
|
var visible = props.visible,
|
|
12
13
|
_props$title = props.title,
|
|
@@ -29,7 +30,8 @@ var ConfirmModal = function ConfirmModal(props) {
|
|
|
29
30
|
_props$autoOpenWindow = props.autoOpenWindow,
|
|
30
31
|
autoOpenWindow = _props$autoOpenWindow === void 0 ? false : _props$autoOpenWindow,
|
|
31
32
|
_props$openMode = props.openMode,
|
|
32
|
-
openMode = _props$openMode === void 0 ?
|
|
33
|
+
openMode = _props$openMode === void 0 ? OpenPayUrlMode.Window : _props$openMode,
|
|
34
|
+
forceNewWindow = props.forceNewWindow;
|
|
33
35
|
var _ref = retryButtonProps || {},
|
|
34
36
|
_ref$children = _ref.children,
|
|
35
37
|
retryButtonChildren = _ref$children === void 0 ? '重新支付' : _ref$children,
|
|
@@ -38,12 +40,14 @@ var ConfirmModal = function ConfirmModal(props) {
|
|
|
38
40
|
_ref2$children = _ref2.children,
|
|
39
41
|
finishButtonChildren = _ref2$children === void 0 ? '支付完成' : _ref2$children,
|
|
40
42
|
finishButtonPropsRest = _objectWithoutProperties(_ref2, _excluded2);
|
|
41
|
-
var _usePayWindowOpen = usePayWindowOpen({
|
|
43
|
+
var _usePayWindowOpen = usePayWindowOpen(_objectSpread({
|
|
42
44
|
payUrl: payUrl || '',
|
|
43
45
|
onClose: onClose,
|
|
44
46
|
autoOpen: autoOpenWindow,
|
|
45
47
|
openMode: openMode
|
|
46
|
-
}
|
|
48
|
+
}, openMode === OpenPayUrlMode.Window && {
|
|
49
|
+
forceNewWindow: forceNewWindow
|
|
50
|
+
})),
|
|
47
51
|
closeWindow = _usePayWindowOpen.closeWindow;
|
|
48
52
|
var handleClose = useCallback(function () {
|
|
49
53
|
onClose === null || onClose === void 0 || onClose();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OpenPayUrlMode } from '../
|
|
1
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
2
2
|
import { PayModalProps } from '../pay-modal';
|
|
3
3
|
export interface ConfirmModalProps extends PayModalProps {
|
|
4
4
|
/**
|
|
@@ -18,4 +18,10 @@ export interface ConfirmModalProps extends PayModalProps {
|
|
|
18
18
|
* @default "window"
|
|
19
19
|
*/
|
|
20
20
|
openMode?: OpenPayUrlMode;
|
|
21
|
+
/**
|
|
22
|
+
* @zh 是否强制打开新窗口(仅在 openMode 为 'window' 且非移动端设备时生效)
|
|
23
|
+
* @en Whether to force open new window (only effective when openMode is 'window' and on non-mobile devices)
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
forceNewWindow?: boolean;
|
|
21
27
|
}
|
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @en If you want users to be able to abandon payment at any time and return to your application, then href is a good choice;
|
|
5
|
-
* @zh 使用 replace: 如果您的业务逻辑是"一旦点击支付,就不能轻易返回",replace 会更合适。例如,点击支付后,您的后端已经创建了一张"待支付"的订单。为了防止用户通过"后退"按钮回到上一个页面再次创建订单,使用 replace 可以强制用户要么完成支付,要么关闭页面,而不能"后退"到上一步。
|
|
6
|
-
* @en Use replace: If your business logic is "once clicked, payment cannot be easily returned", replace is more suitable. For example, after clicking payment, your backend has already created an "awaiting payment" order. To prevent users from returning to the previous page by clicking the "back" button and creating an order again, using replace can force users to either complete payment or close the page, and cannot "back" to the previous step.
|
|
7
|
-
*/
|
|
8
|
-
export type OpenPayUrlMode = 'window' | 'replace' | 'href';
|
|
9
|
-
interface UsePayWindowOpenOptions {
|
|
2
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
3
|
+
interface BaseUsePayWindowOpenOptions {
|
|
10
4
|
payUrl: string;
|
|
11
5
|
onClose?: () => void;
|
|
12
6
|
onSuccess?: () => void;
|
|
13
7
|
autoOpen?: boolean;
|
|
14
|
-
openMode?: OpenPayUrlMode;
|
|
15
8
|
}
|
|
16
|
-
|
|
9
|
+
interface WindowModeOptions extends BaseUsePayWindowOpenOptions {
|
|
10
|
+
openMode?: OpenPayUrlMode.Window;
|
|
11
|
+
/**
|
|
12
|
+
* @zh 是否强制打开新窗口(仅在非移动端设备生效)
|
|
13
|
+
* @en Whether to force open new window (only effective on non-mobile devices)
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
forceNewWindow?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface OtherModeOptions extends BaseUsePayWindowOpenOptions {
|
|
19
|
+
openMode?: OpenPayUrlMode.Replace | OpenPayUrlMode.Href;
|
|
20
|
+
}
|
|
21
|
+
type UsePayWindowOpenProps = WindowModeOptions | OtherModeOptions;
|
|
22
|
+
export declare function usePayWindowOpen(p: UsePayWindowOpenProps): {
|
|
17
23
|
openWindow: () => void;
|
|
18
24
|
closeWindow: () => void;
|
|
19
25
|
payWindowRef: import("react").MutableRefObject<Window | null>;
|
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
2
|
+
import { isMobileDevice, isTouchDevice } from '../utils/is';
|
|
3
|
+
import { mergeProps } from '../utils/with-default-props';
|
|
2
4
|
import { useEffect, useRef, useCallback, useState } from 'react';
|
|
3
|
-
|
|
5
|
+
import { OpenPayUrlMode } from '../shared/enums';
|
|
6
|
+
import { clientDocument, clientWindow } from '../utils/dom';
|
|
4
7
|
/**
|
|
5
|
-
* @zh
|
|
6
|
-
* @en
|
|
7
|
-
* @zh 使用 replace: 如果您的业务逻辑是"一旦点击支付,就不能轻易返回",replace 会更合适。例如,点击支付后,您的后端已经创建了一张"待支付"的订单。为了防止用户通过"后退"按钮回到上一个页面再次创建订单,使用 replace 可以强制用户要么完成支付,要么关闭页面,而不能"后退"到上一步。
|
|
8
|
-
* @en Use replace: If your business logic is "once clicked, payment cannot be easily returned", replace is more suitable. For example, after clicking payment, your backend has already created an "awaiting payment" order. To prevent users from returning to the previous page by clicking the "back" button and creating an order again, using replace can force users to either complete payment or close the page, and cannot "back" to the previous step.
|
|
8
|
+
* @zh 获取屏幕宽度
|
|
9
|
+
* @en Get screen width
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
function getScreenWidth() {
|
|
12
|
+
var _clientDocument$docum, _clientDocument$body;
|
|
13
|
+
return (clientWindow === null || clientWindow === void 0 ? void 0 : clientWindow.innerWidth) || (clientDocument === null || clientDocument === void 0 || (_clientDocument$docum = clientDocument.documentElement) === null || _clientDocument$docum === void 0 ? void 0 : _clientDocument$docum.clientWidth) || (clientDocument === null || clientDocument === void 0 || (_clientDocument$body = clientDocument.body) === null || _clientDocument$body === void 0 ? void 0 : _clientDocument$body.clientWidth);
|
|
14
|
+
}
|
|
15
|
+
var defaultProps = {
|
|
16
|
+
payUrl: '',
|
|
17
|
+
openMode: OpenPayUrlMode.Window
|
|
18
|
+
};
|
|
19
|
+
export function usePayWindowOpen(p) {
|
|
20
|
+
var props = mergeProps(defaultProps, p);
|
|
21
|
+
var payUrl = props.payUrl,
|
|
22
|
+
onClose = props.onClose,
|
|
23
|
+
onSuccess = props.onSuccess,
|
|
24
|
+
_props$autoOpen = props.autoOpen,
|
|
25
|
+
autoOpen = _props$autoOpen === void 0 ? true : _props$autoOpen;
|
|
19
26
|
var payWindowRef = useRef(null);
|
|
20
27
|
var pollTimerRef = useRef(null);
|
|
21
28
|
var _useState = useState(false),
|
|
@@ -23,21 +30,54 @@ export function usePayWindowOpen(_ref) {
|
|
|
23
30
|
isWindowMonitoring = _useState2[0],
|
|
24
31
|
setWindowMonitoring = _useState2[1];
|
|
25
32
|
|
|
33
|
+
// 创建弹窗的公共函数
|
|
34
|
+
var createPopupWindow = useCallback(function (url) {
|
|
35
|
+
// 计算弹窗尺寸和位置,实现垂直水平居中
|
|
36
|
+
var width = Math.min(550, window.outerWidth * 0.9);
|
|
37
|
+
var height = Math.min(700, window.outerHeight);
|
|
38
|
+
|
|
39
|
+
// 使用多种定位方法确保居中
|
|
40
|
+
var left = Math.round((window.screen.availWidth - width) / 2);
|
|
41
|
+
var top = Math.round((window.screen.availHeight - height) / 2);
|
|
42
|
+
var screenX = left;
|
|
43
|
+
var screenY = top;
|
|
44
|
+
return window.open(url, 'paypal_payment', "width=".concat(width, ",height=").concat(height, ",left=").concat(left, ",top=").concat(top, ",screenX=").concat(screenX, ",screenY=").concat(screenY, ",scrollbars=yes,resizable=yes"));
|
|
45
|
+
}, [onClose, onSuccess]);
|
|
46
|
+
|
|
26
47
|
// 打开支付窗口
|
|
27
48
|
var openWindow = useCallback(function () {
|
|
28
|
-
if (openMode ===
|
|
49
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === OpenPayUrlMode.Window) {
|
|
29
50
|
if (payWindowRef.current && !payWindowRef.current.closed) {
|
|
30
51
|
payWindowRef.current.focus();
|
|
31
52
|
} else {
|
|
32
|
-
|
|
33
|
-
|
|
53
|
+
var isMobile = isMobileDevice() || isTouchDevice();
|
|
54
|
+
var forceNewWindow = props === null || props === void 0 ? void 0 : props.forceNewWindow;
|
|
55
|
+
if (isMobile) {
|
|
56
|
+
payWindowRef.current = window.open(payUrl);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (forceNewWindow) {
|
|
60
|
+
payWindowRef.current = createPopupWindow(payUrl);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
var screenWidth = getScreenWidth() || 0;
|
|
64
|
+
if (screenWidth >= 768) {
|
|
65
|
+
payWindowRef.current = createPopupWindow(payUrl);
|
|
66
|
+
} else {
|
|
67
|
+
payWindowRef.current = window.open(payUrl);
|
|
68
|
+
}
|
|
34
69
|
}
|
|
35
|
-
|
|
70
|
+
setWindowMonitoring(true);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === OpenPayUrlMode.Replace) {
|
|
36
74
|
window.location.replace(payUrl);
|
|
37
|
-
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === OpenPayUrlMode.Href) {
|
|
38
78
|
window.location.href = payUrl;
|
|
39
79
|
}
|
|
40
|
-
}, [payUrl,
|
|
80
|
+
}, [payUrl, props, createPopupWindow]);
|
|
41
81
|
|
|
42
82
|
// 关闭窗口
|
|
43
83
|
var closeWindow = useCallback(function () {
|
|
@@ -55,7 +95,7 @@ export function usePayWindowOpen(_ref) {
|
|
|
55
95
|
|
|
56
96
|
// 轮询和消息监听
|
|
57
97
|
useEffect(function () {
|
|
58
|
-
if (!isWindowMonitoring || openMode !==
|
|
98
|
+
if (!isWindowMonitoring || (props === null || props === void 0 ? void 0 : props.openMode) !== OpenPayUrlMode.Window) {
|
|
59
99
|
return;
|
|
60
100
|
}
|
|
61
101
|
pollTimerRef.current = setInterval(function () {
|
|
@@ -82,7 +122,7 @@ export function usePayWindowOpen(_ref) {
|
|
|
82
122
|
window.removeEventListener('message', handleMessage);
|
|
83
123
|
(_payWindowRef$current3 = payWindowRef.current) === null || _payWindowRef$current3 === void 0 || _payWindowRef$current3.close();
|
|
84
124
|
};
|
|
85
|
-
}, [isWindowMonitoring,
|
|
125
|
+
}, [isWindowMonitoring, props, onClose, onSuccess]);
|
|
86
126
|
return {
|
|
87
127
|
openWindow: openWindow,
|
|
88
128
|
closeWindow: closeWindow,
|
|
@@ -55,3 +55,24 @@ export declare enum PaymentMethod {
|
|
|
55
55
|
*/
|
|
56
56
|
Airwallex = "airwallex"
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* @zh 支付页面打开模式枚举
|
|
60
|
+
* @en Payment page opening mode enum
|
|
61
|
+
*/
|
|
62
|
+
export declare enum OpenPayUrlMode {
|
|
63
|
+
/**
|
|
64
|
+
* @zh 新页面或弹窗模式 - 使用 window.open() 打开新页面或弹窗,支持智能设备检测
|
|
65
|
+
* @en New page or popup mode - Use window.open() to open new page or popup, supports smart device detection
|
|
66
|
+
*/
|
|
67
|
+
Window = "window",
|
|
68
|
+
/**
|
|
69
|
+
* @zh 新页面模式 - 使用 window.location.href 跳转到新页面
|
|
70
|
+
* @en New page mode - Use window.location.href to navigate to new page
|
|
71
|
+
*/
|
|
72
|
+
Href = "href",
|
|
73
|
+
/**
|
|
74
|
+
* @zh 替换页面模式 - 使用 window.location.replace 替换当前页面
|
|
75
|
+
* @en Replace page mode - Use window.location.replace to replace current page
|
|
76
|
+
*/
|
|
77
|
+
Replace = "replace"
|
|
78
|
+
}
|
|
@@ -55,4 +55,27 @@ export var PaymentMethod = /*#__PURE__*/function (PaymentMethod) {
|
|
|
55
55
|
*/
|
|
56
56
|
PaymentMethod["Airwallex"] = "airwallex";
|
|
57
57
|
return PaymentMethod;
|
|
58
|
+
}({});
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @zh 支付页面打开模式枚举
|
|
62
|
+
* @en Payment page opening mode enum
|
|
63
|
+
*/
|
|
64
|
+
export var OpenPayUrlMode = /*#__PURE__*/function (OpenPayUrlMode) {
|
|
65
|
+
/**
|
|
66
|
+
* @zh 新页面或弹窗模式 - 使用 window.open() 打开新页面或弹窗,支持智能设备检测
|
|
67
|
+
* @en New page or popup mode - Use window.open() to open new page or popup, supports smart device detection
|
|
68
|
+
*/
|
|
69
|
+
OpenPayUrlMode["Window"] = "window";
|
|
70
|
+
/**
|
|
71
|
+
* @zh 新页面模式 - 使用 window.location.href 跳转到新页面
|
|
72
|
+
* @en New page mode - Use window.location.href to navigate to new page
|
|
73
|
+
*/
|
|
74
|
+
OpenPayUrlMode["Href"] = "href";
|
|
75
|
+
/**
|
|
76
|
+
* @zh 替换页面模式 - 使用 window.location.replace 替换当前页面
|
|
77
|
+
* @en Replace page mode - Use window.location.replace to replace current page
|
|
78
|
+
*/
|
|
79
|
+
OpenPayUrlMode["Replace"] = "replace";
|
|
80
|
+
return OpenPayUrlMode;
|
|
58
81
|
}({});
|
package/dist/es/utils/is.d.ts
CHANGED
|
@@ -22,7 +22,16 @@ export declare const isClassComponent: (element: any) => boolean;
|
|
|
22
22
|
export declare const isDOMElement: (element: any) => boolean;
|
|
23
23
|
export declare const isLink: (text?: React.ReactNode) => boolean;
|
|
24
24
|
export declare function isImageLinkAvailable(url: string): Promise<boolean>;
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @zh 检测是否为移动设备
|
|
27
|
+
* @en Detect if it's a mobile device
|
|
28
|
+
*/
|
|
29
|
+
export declare function isMobileDevice(): boolean | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* @zh 检测是否为触摸设备
|
|
32
|
+
* @en Detect if it's a touch device
|
|
33
|
+
*/
|
|
34
|
+
export declare function isTouchDevice(): boolean;
|
|
26
35
|
export declare function isEmail(email: string): boolean;
|
|
27
36
|
export declare function isSmsCode(code: string): boolean;
|
|
28
37
|
export declare function isUsPhoneNumber(phone: string): boolean;
|
package/dist/es/utils/is.js
CHANGED
|
@@ -90,6 +90,11 @@ export var isLink = function isLink(text) {
|
|
|
90
90
|
export function isImageLinkAvailable(_x) {
|
|
91
91
|
return _isImageLinkAvailable.apply(this, arguments);
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @zh 检测是否为移动设备
|
|
96
|
+
* @en Detect if it's a mobile device
|
|
97
|
+
*/
|
|
93
98
|
function _isImageLinkAvailable() {
|
|
94
99
|
_isImageLinkAvailable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
|
|
95
100
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
@@ -113,8 +118,15 @@ function _isImageLinkAvailable() {
|
|
|
113
118
|
}));
|
|
114
119
|
return _isImageLinkAvailable.apply(this, arguments);
|
|
115
120
|
}
|
|
116
|
-
export function
|
|
117
|
-
return clientWindow && /Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|Mobile/i.test(clientWindow === null || clientWindow === void 0 ? void 0 : clientWindow.navigator.userAgent);
|
|
121
|
+
export function isMobileDevice() {
|
|
122
|
+
return clientWindow && /Android|webOS|iPhone|iPad|iPod|Windows Phone|BlackBerry|Mobile|IEMobile|Opera Mini/i.test(clientWindow === null || clientWindow === void 0 ? void 0 : clientWindow.navigator.userAgent);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @zh 检测是否为触摸设备
|
|
126
|
+
* @en Detect if it's a touch device
|
|
127
|
+
*/
|
|
128
|
+
export function isTouchDevice() {
|
|
129
|
+
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
|
|
118
130
|
}
|
|
119
131
|
export function isEmail(email) {
|
|
120
132
|
var emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
|
|
@@ -13,6 +13,7 @@ var _usePayWindowOpen = require("../hooks/usePayWindowOpen");
|
|
|
13
13
|
var _withDefaultProps = require("../utils/with-default-props");
|
|
14
14
|
var _classNames = _interopRequireDefault(require("../utils/classNames"));
|
|
15
15
|
var _getPrefixCls = require("../utils/getPrefixCls");
|
|
16
|
+
var _enums = require("../shared/enums");
|
|
16
17
|
var _interface = require("./interface");
|
|
17
18
|
Object.keys(_interface).forEach(function (key) {
|
|
18
19
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -44,6 +45,7 @@ const PayMask = p => {
|
|
|
44
45
|
onContinue,
|
|
45
46
|
openMode,
|
|
46
47
|
autoOpenWindow,
|
|
48
|
+
forceNewWindow,
|
|
47
49
|
className,
|
|
48
50
|
destroyOnClose = true,
|
|
49
51
|
...rest
|
|
@@ -59,7 +61,10 @@ const PayMask = p => {
|
|
|
59
61
|
setIsWindowMonitoring(false);
|
|
60
62
|
},
|
|
61
63
|
autoOpen: autoOpenWindow,
|
|
62
|
-
openMode
|
|
64
|
+
openMode,
|
|
65
|
+
...(openMode === _enums.OpenPayUrlMode.Window && {
|
|
66
|
+
forceNewWindow
|
|
67
|
+
})
|
|
63
68
|
});
|
|
64
69
|
(0, _react.useEffect)(() => {
|
|
65
70
|
if (visible && autoOpenWindow) {
|
|
@@ -9,6 +9,7 @@ exports.default = void 0;
|
|
|
9
9
|
var _react = _interopRequireWildcard(require("react"));
|
|
10
10
|
var _usePayWindowOpen = require("../hooks/usePayWindowOpen");
|
|
11
11
|
var _payModal = _interopRequireDefault(require("../pay-modal"));
|
|
12
|
+
var _enums = require("../shared/enums");
|
|
12
13
|
var _interface = require("./interface");
|
|
13
14
|
Object.keys(_interface).forEach(function (key) {
|
|
14
15
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -38,7 +39,8 @@ const ConfirmModal = props => {
|
|
|
38
39
|
onClose,
|
|
39
40
|
payUrl,
|
|
40
41
|
autoOpenWindow = false,
|
|
41
|
-
openMode =
|
|
42
|
+
openMode = _enums.OpenPayUrlMode.Window,
|
|
43
|
+
forceNewWindow
|
|
42
44
|
} = props;
|
|
43
45
|
const {
|
|
44
46
|
children: retryButtonChildren = '重新支付',
|
|
@@ -54,7 +56,10 @@ const ConfirmModal = props => {
|
|
|
54
56
|
payUrl: payUrl || '',
|
|
55
57
|
onClose,
|
|
56
58
|
autoOpen: autoOpenWindow,
|
|
57
|
-
openMode: openMode
|
|
59
|
+
openMode: openMode,
|
|
60
|
+
...(openMode === _enums.OpenPayUrlMode.Window && {
|
|
61
|
+
forceNewWindow
|
|
62
|
+
})
|
|
58
63
|
});
|
|
59
64
|
const handleClose = (0, _react.useCallback)(() => {
|
|
60
65
|
onClose === null || onClose === void 0 || onClose();
|
|
@@ -4,40 +4,83 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.usePayWindowOpen = usePayWindowOpen;
|
|
7
|
+
var _is = require("../utils/is");
|
|
8
|
+
var _withDefaultProps = require("../utils/with-default-props");
|
|
7
9
|
var _react = require("react");
|
|
10
|
+
var _enums = require("../shared/enums");
|
|
11
|
+
var _dom = require("../utils/dom");
|
|
8
12
|
/**
|
|
9
|
-
* @zh
|
|
10
|
-
* @en
|
|
11
|
-
* @zh 使用 replace: 如果您的业务逻辑是"一旦点击支付,就不能轻易返回",replace 会更合适。例如,点击支付后,您的后端已经创建了一张"待支付"的订单。为了防止用户通过"后退"按钮回到上一个页面再次创建订单,使用 replace 可以强制用户要么完成支付,要么关闭页面,而不能"后退"到上一步。
|
|
12
|
-
* @en Use replace: If your business logic is "once clicked, payment cannot be easily returned", replace is more suitable. For example, after clicking payment, your backend has already created an "awaiting payment" order. To prevent users from returning to the previous page by clicking the "back" button and creating an order again, using replace can force users to either complete payment or close the page, and cannot "back" to the previous step.
|
|
13
|
+
* @zh 获取屏幕宽度
|
|
14
|
+
* @en Get screen width
|
|
13
15
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
openMode
|
|
21
|
-
}
|
|
16
|
+
function getScreenWidth() {
|
|
17
|
+
var _clientDocument$docum, _clientDocument$body;
|
|
18
|
+
return (_dom.clientWindow === null || _dom.clientWindow === void 0 ? void 0 : _dom.clientWindow.innerWidth) || (_dom.clientDocument === null || _dom.clientDocument === void 0 || (_clientDocument$docum = _dom.clientDocument.documentElement) === null || _clientDocument$docum === void 0 ? void 0 : _clientDocument$docum.clientWidth) || (_dom.clientDocument === null || _dom.clientDocument === void 0 || (_clientDocument$body = _dom.clientDocument.body) === null || _clientDocument$body === void 0 ? void 0 : _clientDocument$body.clientWidth);
|
|
19
|
+
}
|
|
20
|
+
const defaultProps = {
|
|
21
|
+
payUrl: '',
|
|
22
|
+
openMode: _enums.OpenPayUrlMode.Window
|
|
23
|
+
};
|
|
24
|
+
function usePayWindowOpen(p) {
|
|
25
|
+
const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
|
|
26
|
+
const {
|
|
27
|
+
payUrl,
|
|
28
|
+
onClose,
|
|
29
|
+
onSuccess,
|
|
30
|
+
autoOpen = true
|
|
31
|
+
} = props;
|
|
22
32
|
const payWindowRef = (0, _react.useRef)(null);
|
|
23
33
|
const pollTimerRef = (0, _react.useRef)(null);
|
|
24
34
|
const [isWindowMonitoring, setWindowMonitoring] = (0, _react.useState)(false);
|
|
25
35
|
|
|
36
|
+
// 创建弹窗的公共函数
|
|
37
|
+
const createPopupWindow = (0, _react.useCallback)(url => {
|
|
38
|
+
// 计算弹窗尺寸和位置,实现垂直水平居中
|
|
39
|
+
const width = Math.min(550, window.outerWidth * 0.9);
|
|
40
|
+
const height = Math.min(700, window.outerHeight);
|
|
41
|
+
|
|
42
|
+
// 使用多种定位方法确保居中
|
|
43
|
+
const left = Math.round((window.screen.availWidth - width) / 2);
|
|
44
|
+
const top = Math.round((window.screen.availHeight - height) / 2);
|
|
45
|
+
const screenX = left;
|
|
46
|
+
const screenY = top;
|
|
47
|
+
return window.open(url, 'paypal_payment', `width=${width},height=${height},left=${left},top=${top},screenX=${screenX},screenY=${screenY},scrollbars=yes,resizable=yes`);
|
|
48
|
+
}, [onClose, onSuccess]);
|
|
49
|
+
|
|
26
50
|
// 打开支付窗口
|
|
27
51
|
const openWindow = (0, _react.useCallback)(() => {
|
|
28
|
-
if (openMode ===
|
|
52
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === _enums.OpenPayUrlMode.Window) {
|
|
29
53
|
if (payWindowRef.current && !payWindowRef.current.closed) {
|
|
30
54
|
payWindowRef.current.focus();
|
|
31
55
|
} else {
|
|
32
|
-
|
|
33
|
-
|
|
56
|
+
const isMobile = (0, _is.isMobileDevice)() || (0, _is.isTouchDevice)();
|
|
57
|
+
const forceNewWindow = props === null || props === void 0 ? void 0 : props.forceNewWindow;
|
|
58
|
+
if (isMobile) {
|
|
59
|
+
payWindowRef.current = window.open(payUrl);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (forceNewWindow) {
|
|
63
|
+
payWindowRef.current = createPopupWindow(payUrl);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const screenWidth = getScreenWidth() || 0;
|
|
67
|
+
if (screenWidth >= 768) {
|
|
68
|
+
payWindowRef.current = createPopupWindow(payUrl);
|
|
69
|
+
} else {
|
|
70
|
+
payWindowRef.current = window.open(payUrl);
|
|
71
|
+
}
|
|
34
72
|
}
|
|
35
|
-
|
|
73
|
+
setWindowMonitoring(true);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === _enums.OpenPayUrlMode.Replace) {
|
|
36
77
|
window.location.replace(payUrl);
|
|
37
|
-
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if ((props === null || props === void 0 ? void 0 : props.openMode) === _enums.OpenPayUrlMode.Href) {
|
|
38
81
|
window.location.href = payUrl;
|
|
39
82
|
}
|
|
40
|
-
}, [payUrl,
|
|
83
|
+
}, [payUrl, props, createPopupWindow]);
|
|
41
84
|
|
|
42
85
|
// 关闭窗口
|
|
43
86
|
const closeWindow = (0, _react.useCallback)(() => {
|
|
@@ -55,7 +98,7 @@ function usePayWindowOpen({
|
|
|
55
98
|
|
|
56
99
|
// 轮询和消息监听
|
|
57
100
|
(0, _react.useEffect)(() => {
|
|
58
|
-
if (!isWindowMonitoring || openMode !==
|
|
101
|
+
if (!isWindowMonitoring || (props === null || props === void 0 ? void 0 : props.openMode) !== _enums.OpenPayUrlMode.Window) {
|
|
59
102
|
return;
|
|
60
103
|
}
|
|
61
104
|
pollTimerRef.current = setInterval(() => {
|
|
@@ -82,7 +125,7 @@ function usePayWindowOpen({
|
|
|
82
125
|
window.removeEventListener('message', handleMessage);
|
|
83
126
|
(_payWindowRef$current3 = payWindowRef.current) === null || _payWindowRef$current3 === void 0 || _payWindowRef$current3.close();
|
|
84
127
|
};
|
|
85
|
-
}, [isWindowMonitoring,
|
|
128
|
+
}, [isWindowMonitoring, props, onClose, onSuccess]);
|
|
86
129
|
return {
|
|
87
130
|
openWindow,
|
|
88
131
|
closeWindow,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.PaymentMethod = void 0;
|
|
6
|
+
exports.PaymentMethod = exports.OpenPayUrlMode = void 0;
|
|
7
7
|
let PaymentMethod = exports.PaymentMethod = /*#__PURE__*/function (PaymentMethod) {
|
|
8
8
|
/**
|
|
9
9
|
* @zh 微信(pc)
|
|
@@ -61,4 +61,26 @@ let PaymentMethod = exports.PaymentMethod = /*#__PURE__*/function (PaymentMethod
|
|
|
61
61
|
*/
|
|
62
62
|
PaymentMethod["Airwallex"] = "airwallex";
|
|
63
63
|
return PaymentMethod;
|
|
64
|
+
}({});
|
|
65
|
+
/**
|
|
66
|
+
* @zh 支付页面打开模式枚举
|
|
67
|
+
* @en Payment page opening mode enum
|
|
68
|
+
*/
|
|
69
|
+
let OpenPayUrlMode = exports.OpenPayUrlMode = /*#__PURE__*/function (OpenPayUrlMode) {
|
|
70
|
+
/**
|
|
71
|
+
* @zh 新页面或弹窗模式 - 使用 window.open() 打开新页面或弹窗,支持智能设备检测
|
|
72
|
+
* @en New page or popup mode - Use window.open() to open new page or popup, supports smart device detection
|
|
73
|
+
*/
|
|
74
|
+
OpenPayUrlMode["Window"] = "window";
|
|
75
|
+
/**
|
|
76
|
+
* @zh 新页面模式 - 使用 window.location.href 跳转到新页面
|
|
77
|
+
* @en New page mode - Use window.location.href to navigate to new page
|
|
78
|
+
*/
|
|
79
|
+
OpenPayUrlMode["Href"] = "href";
|
|
80
|
+
/**
|
|
81
|
+
* @zh 替换页面模式 - 使用 window.location.replace 替换当前页面
|
|
82
|
+
* @en Replace page mode - Use window.location.replace to replace current page
|
|
83
|
+
*/
|
|
84
|
+
OpenPayUrlMode["Replace"] = "replace";
|
|
85
|
+
return OpenPayUrlMode;
|
|
64
86
|
}({});
|
package/dist/lib/utils/is.js
CHANGED
|
@@ -20,7 +20,7 @@ exports.isFunction = isFunction;
|
|
|
20
20
|
exports.isIOS = isIOS;
|
|
21
21
|
exports.isImageLinkAvailable = isImageLinkAvailable;
|
|
22
22
|
exports.isLink = void 0;
|
|
23
|
-
exports.
|
|
23
|
+
exports.isMobileDevice = isMobileDevice;
|
|
24
24
|
exports.isNull = isNull;
|
|
25
25
|
exports.isNullOrUndefined = isNullOrUndefined;
|
|
26
26
|
exports.isNumber = isNumber;
|
|
@@ -30,6 +30,7 @@ exports.isReactComponent = void 0;
|
|
|
30
30
|
exports.isRegExp = isRegExp;
|
|
31
31
|
exports.isSmsCode = isSmsCode;
|
|
32
32
|
exports.isString = isString;
|
|
33
|
+
exports.isTouchDevice = isTouchDevice;
|
|
33
34
|
exports.isUndefined = isUndefined;
|
|
34
35
|
exports.isUsPhoneNumber = isUsPhoneNumber;
|
|
35
36
|
exports.isWindow = isWindow;
|
|
@@ -131,8 +132,20 @@ async function isImageLinkAvailable(url) {
|
|
|
131
132
|
img.src = url;
|
|
132
133
|
});
|
|
133
134
|
}
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @zh 检测是否为移动设备
|
|
138
|
+
* @en Detect if it's a mobile device
|
|
139
|
+
*/
|
|
140
|
+
function isMobileDevice() {
|
|
141
|
+
return _dom.clientWindow && /Android|webOS|iPhone|iPad|iPod|Windows Phone|BlackBerry|Mobile|IEMobile|Opera Mini/i.test(_dom.clientWindow === null || _dom.clientWindow === void 0 ? void 0 : _dom.clientWindow.navigator.userAgent);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* @zh 检测是否为触摸设备
|
|
145
|
+
* @en Detect if it's a touch device
|
|
146
|
+
*/
|
|
147
|
+
function isTouchDevice() {
|
|
148
|
+
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
|
|
136
149
|
}
|
|
137
150
|
function isEmail(email) {
|
|
138
151
|
const emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
|