pay-sdk-react 1.1.5 → 1.1.6
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 +6 -0
- package/dist/es/PayMask/pay-mask.js +5 -2
- package/dist/es/Paypal/interface.d.ts +16 -0
- package/dist/es/Paypal/paypal-button.js +98 -12
- package/dist/es/Wechat/wechat.js +1 -1
- package/dist/es/hooks/usePayWindowOpen.d.ts +6 -0
- package/dist/es/hooks/usePayWindowOpen.js +9 -4
- package/dist/lib/PayMask/pay-mask.js +2 -0
- package/dist/lib/Paypal/paypal-button.js +65 -2
- package/dist/lib/Wechat/wechat.js +1 -1
- package/dist/lib/hooks/usePayWindowOpen.js +8 -4
- package/dist/umd/pay-sdk-react.umd.js +68 -7
- package/dist/umd/pay-sdk-react.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -59,4 +59,10 @@ export interface PayMaskProps extends MaskProps {
|
|
|
59
59
|
* @default false
|
|
60
60
|
*/
|
|
61
61
|
forceNewWindow?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* @zh 组件卸载时是否关闭第三方支付页面,只在 “openMode” 为 “window” 时生效
|
|
64
|
+
* @en Whether to close the third-party payment page when component unmounts, only effective when "openMode" is "window"
|
|
65
|
+
* @default true
|
|
66
|
+
*/
|
|
67
|
+
closeWindowOnUnmount?: boolean;
|
|
62
68
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
var _excluded = ["visible", "payUrl", "onClose", "onMaskClick", "onContinue", "openMode", "autoOpenWindow", "forceNewWindow", "className", "destroyOnClose"];
|
|
2
|
+
var _excluded = ["visible", "payUrl", "onClose", "onMaskClick", "onContinue", "openMode", "autoOpenWindow", "forceNewWindow", "className", "destroyOnClose", "closeWindowOnUnmount"];
|
|
3
3
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
4
4
|
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; }
|
|
5
5
|
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; }
|
|
@@ -42,6 +42,8 @@ var PayMask = function PayMask(p) {
|
|
|
42
42
|
className = props.className,
|
|
43
43
|
_props$destroyOnClose = props.destroyOnClose,
|
|
44
44
|
destroyOnClose = _props$destroyOnClose === void 0 ? true : _props$destroyOnClose,
|
|
45
|
+
_props$closeWindowOnU = props.closeWindowOnUnmount,
|
|
46
|
+
closeWindowOnUnmount = _props$closeWindowOnU === void 0 ? true : _props$closeWindowOnU,
|
|
45
47
|
rest = _objectWithoutProperties(props, _excluded);
|
|
46
48
|
var _useState = useState(false),
|
|
47
49
|
_useState2 = _slicedToArray(_useState, 2),
|
|
@@ -54,7 +56,8 @@ var PayMask = function PayMask(p) {
|
|
|
54
56
|
setIsWindowMonitoring(false);
|
|
55
57
|
},
|
|
56
58
|
autoOpen: autoOpenWindow,
|
|
57
|
-
openMode: openMode
|
|
59
|
+
openMode: openMode,
|
|
60
|
+
closeWindowOnUnmount: closeWindowOnUnmount
|
|
58
61
|
}, openMode === OpenPayUrlMode.Window && {
|
|
59
62
|
forceNewWindow: forceNewWindow
|
|
60
63
|
})),
|
|
@@ -22,6 +22,22 @@ export interface PaypalButtonProps extends ButtonProps {
|
|
|
22
22
|
* @en Function to create order, returns Paypal required params
|
|
23
23
|
*/
|
|
24
24
|
createOrder?: CreateOrderPaypalFn;
|
|
25
|
+
/**
|
|
26
|
+
* @zh 支付成功检测函数,返回支付状态
|
|
27
|
+
* @en Function to check payment status
|
|
28
|
+
*/
|
|
29
|
+
checkPaymentStatus?: () => Promise<boolean>;
|
|
30
|
+
/**
|
|
31
|
+
* @zh 支付成功回调
|
|
32
|
+
* @en Payment success callback
|
|
33
|
+
*/
|
|
34
|
+
onPaymentSuccess?: () => void;
|
|
35
|
+
/**
|
|
36
|
+
* @zh 组件卸载时是否关闭第三方支付页面,只在 “checkPaymentStatus” 检测支付状态函数调用时且 openMode 为 “window” 时生效
|
|
37
|
+
* @en Whether to close the third-party payment page when component unmounts, only effective when "checkPaymentStatus" is called and openMode is "window"
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
closeWindowOnUnmount?: boolean;
|
|
25
41
|
}
|
|
26
42
|
export type PaypalButtonRef = ButtonRef;
|
|
27
43
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var _excluded = ["className", "children", "onClick", "paypalUrl", "createOrder", "maskProps"],
|
|
1
|
+
var _excluded = ["className", "children", "onClick", "paypalUrl", "createOrder", "maskProps", "checkPaymentStatus", "onPaymentSuccess", "closeWindowOnUnmount"],
|
|
2
2
|
_excluded2 = ["onClose"];
|
|
3
3
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
4
4
|
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
@@ -13,7 +13,7 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
|
|
|
13
13
|
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
14
14
|
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
15
15
|
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
16
|
-
import React, { forwardRef, memo, useCallback, useImperativeHandle, useRef, useState } from 'react';
|
|
16
|
+
import React, { forwardRef, memo, useCallback, useImperativeHandle, useRef, useState, useEffect } from 'react';
|
|
17
17
|
import { IconPaypalText, IconPaypalWhite } from '../icons';
|
|
18
18
|
import cs from '../utils/classNames';
|
|
19
19
|
import PayMask from '../PayMask';
|
|
@@ -27,6 +27,10 @@ var PaypalButton = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
27
27
|
propsPaypalUrl = props.paypalUrl,
|
|
28
28
|
createOrder = props.createOrder,
|
|
29
29
|
maskProps = props.maskProps,
|
|
30
|
+
checkPaymentStatus = props.checkPaymentStatus,
|
|
31
|
+
onPaymentSuccess = props.onPaymentSuccess,
|
|
32
|
+
_props$closeWindowOnU = props.closeWindowOnUnmount,
|
|
33
|
+
closeWindowOnUnmount = _props$closeWindowOnU === void 0 ? true : _props$closeWindowOnU,
|
|
30
34
|
others = _objectWithoutProperties(props, _excluded);
|
|
31
35
|
var _ref = maskProps || {},
|
|
32
36
|
onMaskClose = _ref.onClose,
|
|
@@ -42,33 +46,106 @@ var PaypalButton = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
42
46
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
43
47
|
paypalUrl = _useState4[0],
|
|
44
48
|
setPaypalUrl = _useState4[1];
|
|
49
|
+
var pollingTimerRef = useRef(null);
|
|
45
50
|
useImperativeHandle(ref, function () {
|
|
46
51
|
return paypalButtonRef.current;
|
|
47
52
|
});
|
|
48
|
-
|
|
53
|
+
|
|
54
|
+
// 支付状态轮询
|
|
55
|
+
var startPaymentPolling = useCallback(function () {
|
|
56
|
+
if (!checkPaymentStatus) return;
|
|
57
|
+
|
|
58
|
+
// 先清除之前的定时器
|
|
59
|
+
if (pollingTimerRef.current) {
|
|
60
|
+
clearTimeout(pollingTimerRef.current);
|
|
61
|
+
pollingTimerRef.current = null;
|
|
62
|
+
}
|
|
63
|
+
var _poll = /*#__PURE__*/function () {
|
|
64
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
65
|
+
var isSuccess, _t;
|
|
66
|
+
return _regenerator().w(function (_context) {
|
|
67
|
+
while (1) switch (_context.n) {
|
|
68
|
+
case 0:
|
|
69
|
+
_context.p = 0;
|
|
70
|
+
_context.n = 1;
|
|
71
|
+
return checkPaymentStatus();
|
|
72
|
+
case 1:
|
|
73
|
+
isSuccess = _context.v;
|
|
74
|
+
if (!isSuccess) {
|
|
75
|
+
_context.n = 2;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
setShowMask(false);
|
|
79
|
+
onPaymentSuccess === null || onPaymentSuccess === void 0 || onPaymentSuccess();
|
|
80
|
+
if (pollingTimerRef.current) {
|
|
81
|
+
clearTimeout(pollingTimerRef.current);
|
|
82
|
+
pollingTimerRef.current = null;
|
|
83
|
+
}
|
|
84
|
+
return _context.a(2);
|
|
85
|
+
case 2:
|
|
86
|
+
// 继续轮询
|
|
87
|
+
if (pollingTimerRef.current) {
|
|
88
|
+
clearTimeout(pollingTimerRef.current);
|
|
89
|
+
}
|
|
90
|
+
pollingTimerRef.current = setTimeout(_poll, 3000);
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
92
|
+
_context.n = 4;
|
|
93
|
+
break;
|
|
94
|
+
case 3:
|
|
95
|
+
_context.p = 3;
|
|
96
|
+
_t = _context.v;
|
|
97
|
+
// 出错时继续轮询
|
|
98
|
+
if (pollingTimerRef.current) {
|
|
99
|
+
clearTimeout(pollingTimerRef.current);
|
|
100
|
+
}
|
|
101
|
+
pollingTimerRef.current = setTimeout(_poll, 3000);
|
|
102
|
+
case 4:
|
|
103
|
+
return _context.a(2);
|
|
104
|
+
}
|
|
105
|
+
}, _callee, null, [[0, 3]]);
|
|
106
|
+
}));
|
|
107
|
+
return function poll() {
|
|
108
|
+
return _ref2.apply(this, arguments);
|
|
109
|
+
};
|
|
110
|
+
}();
|
|
111
|
+
_poll();
|
|
112
|
+
}, [checkPaymentStatus, onPaymentSuccess]);
|
|
113
|
+
|
|
114
|
+
// 停止轮询
|
|
115
|
+
var stopPaymentPolling = useCallback(function () {
|
|
116
|
+
if (pollingTimerRef.current) {
|
|
117
|
+
clearTimeout(pollingTimerRef.current);
|
|
118
|
+
pollingTimerRef.current = null;
|
|
119
|
+
}
|
|
120
|
+
}, []);
|
|
121
|
+
var handlePayCreateOrder = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
49
122
|
var _yield$createOrder, _paypalUrl;
|
|
50
|
-
return _regenerator().w(function (
|
|
51
|
-
while (1) switch (
|
|
123
|
+
return _regenerator().w(function (_context2) {
|
|
124
|
+
while (1) switch (_context2.n) {
|
|
52
125
|
case 0:
|
|
53
126
|
if (!createOrder) {
|
|
54
|
-
|
|
127
|
+
_context2.n = 2;
|
|
55
128
|
break;
|
|
56
129
|
}
|
|
57
|
-
|
|
130
|
+
_context2.n = 1;
|
|
58
131
|
return createOrder();
|
|
59
132
|
case 1:
|
|
60
|
-
_yield$createOrder =
|
|
133
|
+
_yield$createOrder = _context2.v;
|
|
61
134
|
_paypalUrl = _yield$createOrder.paypalUrl;
|
|
62
135
|
if (_paypalUrl) {
|
|
63
136
|
// 2. 唤起蒙层
|
|
64
137
|
setShowMask(true);
|
|
65
138
|
setPaypalUrl(_paypalUrl);
|
|
139
|
+
// 3. 开始轮询支付状态
|
|
140
|
+
if (checkPaymentStatus) {
|
|
141
|
+
startPaymentPolling();
|
|
142
|
+
}
|
|
66
143
|
}
|
|
67
144
|
case 2:
|
|
68
|
-
return
|
|
145
|
+
return _context2.a(2);
|
|
69
146
|
}
|
|
70
|
-
},
|
|
71
|
-
})), [createOrder]);
|
|
147
|
+
}, _callee2);
|
|
148
|
+
})), [createOrder, checkPaymentStatus, startPaymentPolling]);
|
|
72
149
|
var handleOpen = useCallback(function () {
|
|
73
150
|
if (propsPaypalUrl) {
|
|
74
151
|
setShowMask(true);
|
|
@@ -83,6 +160,13 @@ var PaypalButton = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
83
160
|
}
|
|
84
161
|
onClick === null || onClick === void 0 || onClick(e);
|
|
85
162
|
}, [onClick, createOrder, handlePayCreateOrder, handleOpen]);
|
|
163
|
+
|
|
164
|
+
// 组件卸载时清理轮询
|
|
165
|
+
useEffect(function () {
|
|
166
|
+
return function () {
|
|
167
|
+
stopPaymentPolling();
|
|
168
|
+
};
|
|
169
|
+
}, [stopPaymentPolling]);
|
|
86
170
|
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, _extends({
|
|
87
171
|
ref: paypalButtonRef,
|
|
88
172
|
className: cs(getPrefixCls('paypal-btn'), className),
|
|
@@ -91,10 +175,12 @@ var PaypalButton = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
91
175
|
visible: showMask,
|
|
92
176
|
onClose: function onClose() {
|
|
93
177
|
setShowMask(false);
|
|
178
|
+
stopPaymentPolling();
|
|
94
179
|
onMaskClose === null || onMaskClose === void 0 || onMaskClose();
|
|
95
180
|
},
|
|
96
181
|
payUrl: paypalUrl,
|
|
97
|
-
title: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconPaypalWhite, null), "PayPal")
|
|
182
|
+
title: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconPaypalWhite, null), "PayPal"),
|
|
183
|
+
closeWindowOnUnmount: closeWindowOnUnmount
|
|
98
184
|
}, restMaskProps)));
|
|
99
185
|
});
|
|
100
186
|
PaypalButton.displayName = 'PaypalButton';
|
package/dist/es/Wechat/wechat.js
CHANGED
|
@@ -8,7 +8,7 @@ import React, { memo, useEffect, useState } from 'react';
|
|
|
8
8
|
import cs from '../utils/classNames';
|
|
9
9
|
import makeQrcode from '../utils/qrcode';
|
|
10
10
|
import { getPrefixCls } from '../utils/getPrefixCls';
|
|
11
|
-
var classPrefix = getPrefixCls('wechat');
|
|
11
|
+
var classPrefix = getPrefixCls('wechat-qrcode');
|
|
12
12
|
var WechatQrCode = function WechatQrCode(_ref) {
|
|
13
13
|
var _ref$wechatType = _ref.wechatType,
|
|
14
14
|
wechatType = _ref$wechatType === void 0 ? 'qrcode' : _ref$wechatType,
|
|
@@ -5,6 +5,12 @@ interface BaseUsePayWindowOpenOptions {
|
|
|
5
5
|
onClose?: () => void;
|
|
6
6
|
onSuccess?: () => void;
|
|
7
7
|
autoOpen?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* @zh 组件卸载时是否关闭支付窗口
|
|
10
|
+
* @en Whether to close payment window when component unmounts
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
closeWindowOnUnmount?: boolean;
|
|
8
14
|
}
|
|
9
15
|
interface WindowModeOptions extends BaseUsePayWindowOpenOptions {
|
|
10
16
|
openMode?: OpenPayUrlMode.Window;
|
|
@@ -19,7 +19,8 @@ function getScreenWidth() {
|
|
|
19
19
|
}
|
|
20
20
|
var defaultProps = {
|
|
21
21
|
payUrl: '',
|
|
22
|
-
openMode: OpenPayUrlMode.Window
|
|
22
|
+
openMode: OpenPayUrlMode.Window,
|
|
23
|
+
closeWindowOnUnmount: true
|
|
23
24
|
};
|
|
24
25
|
export function usePayWindowOpen(p) {
|
|
25
26
|
var props = mergeProps(defaultProps, p);
|
|
@@ -27,7 +28,9 @@ export function usePayWindowOpen(p) {
|
|
|
27
28
|
onClose = props.onClose,
|
|
28
29
|
onSuccess = props.onSuccess,
|
|
29
30
|
_props$autoOpen = props.autoOpen,
|
|
30
|
-
autoOpen = _props$autoOpen === void 0 ? true : _props$autoOpen
|
|
31
|
+
autoOpen = _props$autoOpen === void 0 ? true : _props$autoOpen,
|
|
32
|
+
_props$closeWindowOnU = props.closeWindowOnUnmount,
|
|
33
|
+
closeWindowOnUnmount = _props$closeWindowOnU === void 0 ? true : _props$closeWindowOnU;
|
|
31
34
|
var payWindowRef = useRef(null);
|
|
32
35
|
var pollTimerRef = useRef(null);
|
|
33
36
|
var _useState = useState(false),
|
|
@@ -122,10 +125,12 @@ export function usePayWindowOpen(p) {
|
|
|
122
125
|
}
|
|
123
126
|
window.addEventListener('message', handleMessage);
|
|
124
127
|
return function () {
|
|
125
|
-
var _payWindowRef$current3;
|
|
126
128
|
clearInterval(pollTimerRef.current);
|
|
127
129
|
window.removeEventListener('message', handleMessage);
|
|
128
|
-
(
|
|
130
|
+
if (closeWindowOnUnmount) {
|
|
131
|
+
var _payWindowRef$current3;
|
|
132
|
+
(_payWindowRef$current3 = payWindowRef.current) === null || _payWindowRef$current3 === void 0 || _payWindowRef$current3.close();
|
|
133
|
+
}
|
|
129
134
|
};
|
|
130
135
|
}, [isWindowMonitoring, props, onClose, onSuccess]);
|
|
131
136
|
return {
|
|
@@ -48,6 +48,7 @@ const PayMask = p => {
|
|
|
48
48
|
forceNewWindow,
|
|
49
49
|
className,
|
|
50
50
|
destroyOnClose = true,
|
|
51
|
+
closeWindowOnUnmount = true,
|
|
51
52
|
...rest
|
|
52
53
|
} = props;
|
|
53
54
|
const [isWindowMonitoring, setIsWindowMonitoring] = (0, _react.useState)(false);
|
|
@@ -62,6 +63,7 @@ const PayMask = p => {
|
|
|
62
63
|
},
|
|
63
64
|
autoOpen: autoOpenWindow,
|
|
64
65
|
openMode,
|
|
66
|
+
closeWindowOnUnmount,
|
|
65
67
|
...(openMode === _enums.OpenPayUrlMode.Window && {
|
|
66
68
|
forceNewWindow
|
|
67
69
|
})
|
|
@@ -21,6 +21,9 @@ const PaypalButton = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
21
21
|
paypalUrl: propsPaypalUrl,
|
|
22
22
|
createOrder,
|
|
23
23
|
maskProps,
|
|
24
|
+
checkPaymentStatus,
|
|
25
|
+
onPaymentSuccess,
|
|
26
|
+
closeWindowOnUnmount = true,
|
|
24
27
|
...others
|
|
25
28
|
} = props;
|
|
26
29
|
const {
|
|
@@ -32,7 +35,54 @@ const PaypalButton = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
32
35
|
});
|
|
33
36
|
const [showMask, setShowMask] = (0, _react.useState)(false);
|
|
34
37
|
const [paypalUrl, setPaypalUrl] = (0, _react.useState)();
|
|
38
|
+
const pollingTimerRef = (0, _react.useRef)(null);
|
|
35
39
|
(0, _react.useImperativeHandle)(ref, () => paypalButtonRef.current);
|
|
40
|
+
|
|
41
|
+
// 支付状态轮询
|
|
42
|
+
const startPaymentPolling = (0, _react.useCallback)(() => {
|
|
43
|
+
if (!checkPaymentStatus) return;
|
|
44
|
+
|
|
45
|
+
// 先清除之前的定时器
|
|
46
|
+
if (pollingTimerRef.current) {
|
|
47
|
+
clearTimeout(pollingTimerRef.current);
|
|
48
|
+
pollingTimerRef.current = null;
|
|
49
|
+
}
|
|
50
|
+
const poll = async () => {
|
|
51
|
+
try {
|
|
52
|
+
const isSuccess = await checkPaymentStatus();
|
|
53
|
+
if (isSuccess) {
|
|
54
|
+
setShowMask(false);
|
|
55
|
+
onPaymentSuccess === null || onPaymentSuccess === void 0 || onPaymentSuccess();
|
|
56
|
+
if (pollingTimerRef.current) {
|
|
57
|
+
clearTimeout(pollingTimerRef.current);
|
|
58
|
+
pollingTimerRef.current = null;
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// 继续轮询
|
|
63
|
+
if (pollingTimerRef.current) {
|
|
64
|
+
clearTimeout(pollingTimerRef.current);
|
|
65
|
+
}
|
|
66
|
+
pollingTimerRef.current = setTimeout(poll, 3000);
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
|
+
} catch (error) {
|
|
69
|
+
// 出错时继续轮询
|
|
70
|
+
if (pollingTimerRef.current) {
|
|
71
|
+
clearTimeout(pollingTimerRef.current);
|
|
72
|
+
}
|
|
73
|
+
pollingTimerRef.current = setTimeout(poll, 3000);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
poll();
|
|
77
|
+
}, [checkPaymentStatus, onPaymentSuccess]);
|
|
78
|
+
|
|
79
|
+
// 停止轮询
|
|
80
|
+
const stopPaymentPolling = (0, _react.useCallback)(() => {
|
|
81
|
+
if (pollingTimerRef.current) {
|
|
82
|
+
clearTimeout(pollingTimerRef.current);
|
|
83
|
+
pollingTimerRef.current = null;
|
|
84
|
+
}
|
|
85
|
+
}, []);
|
|
36
86
|
const handlePayCreateOrder = (0, _react.useCallback)(async () => {
|
|
37
87
|
if (createOrder) {
|
|
38
88
|
// 1. 业务侧创建订单,获取参数
|
|
@@ -43,9 +93,13 @@ const PaypalButton = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
43
93
|
// 2. 唤起蒙层
|
|
44
94
|
setShowMask(true);
|
|
45
95
|
setPaypalUrl(paypalUrl);
|
|
96
|
+
// 3. 开始轮询支付状态
|
|
97
|
+
if (checkPaymentStatus) {
|
|
98
|
+
startPaymentPolling();
|
|
99
|
+
}
|
|
46
100
|
}
|
|
47
101
|
}
|
|
48
|
-
}, [createOrder]);
|
|
102
|
+
}, [createOrder, checkPaymentStatus, startPaymentPolling]);
|
|
49
103
|
const handleOpen = (0, _react.useCallback)(() => {
|
|
50
104
|
if (propsPaypalUrl) {
|
|
51
105
|
setShowMask(true);
|
|
@@ -60,6 +114,13 @@ const PaypalButton = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
60
114
|
}
|
|
61
115
|
onClick === null || onClick === void 0 || onClick(e);
|
|
62
116
|
}, [onClick, createOrder, handlePayCreateOrder, handleOpen]);
|
|
117
|
+
|
|
118
|
+
// 组件卸载时清理轮询
|
|
119
|
+
(0, _react.useEffect)(() => {
|
|
120
|
+
return () => {
|
|
121
|
+
stopPaymentPolling();
|
|
122
|
+
};
|
|
123
|
+
}, [stopPaymentPolling]);
|
|
63
124
|
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Button.default, _extends({
|
|
64
125
|
ref: paypalButtonRef,
|
|
65
126
|
className: (0, _classNames.default)((0, _getPrefixCls.getPrefixCls)('paypal-btn'), className),
|
|
@@ -68,10 +129,12 @@ const PaypalButton = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
68
129
|
visible: showMask,
|
|
69
130
|
onClose: () => {
|
|
70
131
|
setShowMask(false);
|
|
132
|
+
stopPaymentPolling();
|
|
71
133
|
onMaskClose === null || onMaskClose === void 0 || onMaskClose();
|
|
72
134
|
},
|
|
73
135
|
payUrl: paypalUrl,
|
|
74
|
-
title: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_icons.IconPaypalWhite, null), "PayPal")
|
|
136
|
+
title: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_icons.IconPaypalWhite, null), "PayPal"),
|
|
137
|
+
closeWindowOnUnmount: closeWindowOnUnmount
|
|
75
138
|
}, restMaskProps)));
|
|
76
139
|
});
|
|
77
140
|
PaypalButton.displayName = 'PaypalButton';
|
|
@@ -10,7 +10,7 @@ var _qrcode = _interopRequireDefault(require("../utils/qrcode"));
|
|
|
10
10
|
var _getPrefixCls = require("../utils/getPrefixCls");
|
|
11
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
12
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
13
|
-
const classPrefix = (0, _getPrefixCls.getPrefixCls)('wechat');
|
|
13
|
+
const classPrefix = (0, _getPrefixCls.getPrefixCls)('wechat-qrcode');
|
|
14
14
|
const WechatQrCode = ({
|
|
15
15
|
wechatType = 'qrcode',
|
|
16
16
|
wechatUrl,
|
|
@@ -19,7 +19,8 @@ function getScreenWidth() {
|
|
|
19
19
|
}
|
|
20
20
|
const defaultProps = {
|
|
21
21
|
payUrl: '',
|
|
22
|
-
openMode: _enums.OpenPayUrlMode.Window
|
|
22
|
+
openMode: _enums.OpenPayUrlMode.Window,
|
|
23
|
+
closeWindowOnUnmount: true
|
|
23
24
|
};
|
|
24
25
|
function usePayWindowOpen(p) {
|
|
25
26
|
const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
|
|
@@ -27,7 +28,8 @@ function usePayWindowOpen(p) {
|
|
|
27
28
|
payUrl,
|
|
28
29
|
onClose,
|
|
29
30
|
onSuccess,
|
|
30
|
-
autoOpen = true
|
|
31
|
+
autoOpen = true,
|
|
32
|
+
closeWindowOnUnmount = true
|
|
31
33
|
} = props;
|
|
32
34
|
const payWindowRef = (0, _react.useRef)(null);
|
|
33
35
|
const pollTimerRef = (0, _react.useRef)(null);
|
|
@@ -120,10 +122,12 @@ function usePayWindowOpen(p) {
|
|
|
120
122
|
}
|
|
121
123
|
window.addEventListener('message', handleMessage);
|
|
122
124
|
return () => {
|
|
123
|
-
var _payWindowRef$current3;
|
|
124
125
|
clearInterval(pollTimerRef.current);
|
|
125
126
|
window.removeEventListener('message', handleMessage);
|
|
126
|
-
(
|
|
127
|
+
if (closeWindowOnUnmount) {
|
|
128
|
+
var _payWindowRef$current3;
|
|
129
|
+
(_payWindowRef$current3 = payWindowRef.current) === null || _payWindowRef$current3 === void 0 || _payWindowRef$current3.close();
|
|
130
|
+
}
|
|
127
131
|
};
|
|
128
132
|
}, [isWindowMonitoring, props, onClose, onSuccess]);
|
|
129
133
|
return {
|
|
@@ -5271,10 +5271,11 @@
|
|
|
5271
5271
|
const defaultProps$4 = {
|
|
5272
5272
|
payUrl: '',
|
|
5273
5273
|
openMode: exports.OpenPayUrlMode.Window,
|
|
5274
|
+
closeWindowOnUnmount: true,
|
|
5274
5275
|
};
|
|
5275
5276
|
function usePayWindowOpen(p) {
|
|
5276
5277
|
const props = mergeProps(defaultProps$4, p);
|
|
5277
|
-
const { payUrl, onClose, onSuccess, autoOpen = true } = props;
|
|
5278
|
+
const { payUrl, onClose, onSuccess, autoOpen = true, closeWindowOnUnmount = true, } = props;
|
|
5278
5279
|
const payWindowRef = React.useRef(null);
|
|
5279
5280
|
const pollTimerRef = React.useRef(null);
|
|
5280
5281
|
const [isWindowMonitoring, setWindowMonitoring] = React.useState(false);
|
|
@@ -5362,7 +5363,9 @@
|
|
|
5362
5363
|
return () => {
|
|
5363
5364
|
clearInterval(pollTimerRef.current);
|
|
5364
5365
|
window.removeEventListener('message', handleMessage);
|
|
5365
|
-
|
|
5366
|
+
if (closeWindowOnUnmount) {
|
|
5367
|
+
payWindowRef.current?.close();
|
|
5368
|
+
}
|
|
5366
5369
|
};
|
|
5367
5370
|
}, [isWindowMonitoring, props, onClose, onSuccess]);
|
|
5368
5371
|
return {
|
|
@@ -5382,7 +5385,7 @@
|
|
|
5382
5385
|
};
|
|
5383
5386
|
const PayMask = (p) => {
|
|
5384
5387
|
const props = mergeProps(defaultProps$3, p);
|
|
5385
|
-
const { visible, payUrl, onClose, onMaskClick, onContinue, openMode, autoOpenWindow, forceNewWindow, className, destroyOnClose = true, ...rest } = props;
|
|
5388
|
+
const { visible, payUrl, onClose, onMaskClick, onContinue, openMode, autoOpenWindow, forceNewWindow, className, destroyOnClose = true, closeWindowOnUnmount = true, ...rest } = props;
|
|
5386
5389
|
const [isWindowMonitoring, setIsWindowMonitoring] = React.useState(false);
|
|
5387
5390
|
const { openWindow, closeWindow } = usePayWindowOpen({
|
|
5388
5391
|
payUrl: payUrl || '',
|
|
@@ -5392,6 +5395,7 @@
|
|
|
5392
5395
|
},
|
|
5393
5396
|
autoOpen: autoOpenWindow,
|
|
5394
5397
|
openMode,
|
|
5398
|
+
closeWindowOnUnmount,
|
|
5395
5399
|
...(openMode === exports.OpenPayUrlMode.Window && { forceNewWindow }),
|
|
5396
5400
|
});
|
|
5397
5401
|
React.useEffect(() => {
|
|
@@ -5520,12 +5524,58 @@
|
|
|
5520
5524
|
Button.displayName = 'Button';
|
|
5521
5525
|
|
|
5522
5526
|
const PaypalButton = React.forwardRef((props, ref) => {
|
|
5523
|
-
const { className, children = (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx(IconPaypalText, {}), " Buy Now"] })), onClick, paypalUrl: propsPaypalUrl, createOrder, maskProps, ...others } = props;
|
|
5527
|
+
const { className, children = (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx(IconPaypalText, {}), " Buy Now"] })), onClick, paypalUrl: propsPaypalUrl, createOrder, maskProps, checkPaymentStatus, onPaymentSuccess, closeWindowOnUnmount = true, ...others } = props;
|
|
5524
5528
|
const { onClose: onMaskClose, ...restMaskProps } = maskProps || {};
|
|
5525
5529
|
const paypalButtonRef = React.useRef({ nativeElement: null });
|
|
5526
5530
|
const [showMask, setShowMask] = React.useState(false);
|
|
5527
5531
|
const [paypalUrl, setPaypalUrl] = React.useState();
|
|
5532
|
+
const pollingTimerRef = React.useRef(null);
|
|
5528
5533
|
React.useImperativeHandle(ref, () => paypalButtonRef.current);
|
|
5534
|
+
// 支付状态轮询
|
|
5535
|
+
const startPaymentPolling = React.useCallback(() => {
|
|
5536
|
+
if (!checkPaymentStatus)
|
|
5537
|
+
return;
|
|
5538
|
+
// 先清除之前的定时器
|
|
5539
|
+
if (pollingTimerRef.current) {
|
|
5540
|
+
clearTimeout(pollingTimerRef.current);
|
|
5541
|
+
pollingTimerRef.current = null;
|
|
5542
|
+
}
|
|
5543
|
+
const poll = async () => {
|
|
5544
|
+
try {
|
|
5545
|
+
const isSuccess = await checkPaymentStatus();
|
|
5546
|
+
if (isSuccess) {
|
|
5547
|
+
setShowMask(false);
|
|
5548
|
+
onPaymentSuccess?.();
|
|
5549
|
+
if (pollingTimerRef.current) {
|
|
5550
|
+
clearTimeout(pollingTimerRef.current);
|
|
5551
|
+
pollingTimerRef.current = null;
|
|
5552
|
+
}
|
|
5553
|
+
return;
|
|
5554
|
+
}
|
|
5555
|
+
// 继续轮询
|
|
5556
|
+
if (pollingTimerRef.current) {
|
|
5557
|
+
clearTimeout(pollingTimerRef.current);
|
|
5558
|
+
}
|
|
5559
|
+
pollingTimerRef.current = setTimeout(poll, 3000);
|
|
5560
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5561
|
+
}
|
|
5562
|
+
catch (error) {
|
|
5563
|
+
// 出错时继续轮询
|
|
5564
|
+
if (pollingTimerRef.current) {
|
|
5565
|
+
clearTimeout(pollingTimerRef.current);
|
|
5566
|
+
}
|
|
5567
|
+
pollingTimerRef.current = setTimeout(poll, 3000);
|
|
5568
|
+
}
|
|
5569
|
+
};
|
|
5570
|
+
poll();
|
|
5571
|
+
}, [checkPaymentStatus, onPaymentSuccess]);
|
|
5572
|
+
// 停止轮询
|
|
5573
|
+
const stopPaymentPolling = React.useCallback(() => {
|
|
5574
|
+
if (pollingTimerRef.current) {
|
|
5575
|
+
clearTimeout(pollingTimerRef.current);
|
|
5576
|
+
pollingTimerRef.current = null;
|
|
5577
|
+
}
|
|
5578
|
+
}, []);
|
|
5529
5579
|
const handlePayCreateOrder = React.useCallback(async () => {
|
|
5530
5580
|
if (createOrder) {
|
|
5531
5581
|
// 1. 业务侧创建订单,获取参数
|
|
@@ -5534,9 +5584,13 @@
|
|
|
5534
5584
|
// 2. 唤起蒙层
|
|
5535
5585
|
setShowMask(true);
|
|
5536
5586
|
setPaypalUrl(paypalUrl);
|
|
5587
|
+
// 3. 开始轮询支付状态
|
|
5588
|
+
if (checkPaymentStatus) {
|
|
5589
|
+
startPaymentPolling();
|
|
5590
|
+
}
|
|
5537
5591
|
}
|
|
5538
5592
|
}
|
|
5539
|
-
}, [createOrder]);
|
|
5593
|
+
}, [createOrder, checkPaymentStatus, startPaymentPolling]);
|
|
5540
5594
|
const handleOpen = React.useCallback(() => {
|
|
5541
5595
|
if (propsPaypalUrl) {
|
|
5542
5596
|
setShowMask(true);
|
|
@@ -5552,10 +5606,17 @@
|
|
|
5552
5606
|
}
|
|
5553
5607
|
onClick?.(e);
|
|
5554
5608
|
}, [onClick, createOrder, handlePayCreateOrder, handleOpen]);
|
|
5609
|
+
// 组件卸载时清理轮询
|
|
5610
|
+
React.useEffect(() => {
|
|
5611
|
+
return () => {
|
|
5612
|
+
stopPaymentPolling();
|
|
5613
|
+
};
|
|
5614
|
+
}, [stopPaymentPolling]);
|
|
5555
5615
|
return (jsxRuntimeExports.jsxs(React.Fragment, { children: [jsxRuntimeExports.jsx(Button, { ref: paypalButtonRef, className: cs(getPrefixCls('paypal-btn'), className), onClick: handleClick, ...others, children: children }), showMask && paypalUrl && (jsxRuntimeExports.jsx(PayMask, { visible: showMask, onClose: () => {
|
|
5556
5616
|
setShowMask(false);
|
|
5617
|
+
stopPaymentPolling();
|
|
5557
5618
|
onMaskClose?.();
|
|
5558
|
-
}, payUrl: paypalUrl, title: jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx(IconPaypalWhite, {}), "PayPal"] }), ...restMaskProps }))] }));
|
|
5619
|
+
}, payUrl: paypalUrl, title: jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx(IconPaypalWhite, {}), "PayPal"] }), closeWindowOnUnmount: closeWindowOnUnmount, ...restMaskProps }))] }));
|
|
5559
5620
|
});
|
|
5560
5621
|
PaypalButton.displayName = 'PaypalButton';
|
|
5561
5622
|
var PaypalButton$1 = React.memo(PaypalButton);
|
|
@@ -16351,7 +16412,7 @@
|
|
|
16351
16412
|
});
|
|
16352
16413
|
};
|
|
16353
16414
|
|
|
16354
|
-
const classPrefix = getPrefixCls('wechat');
|
|
16415
|
+
const classPrefix = getPrefixCls('wechat-qrcode');
|
|
16355
16416
|
const WechatQrCode = ({ wechatType = 'qrcode', wechatUrl, tipText = '请使用微信APP扫码支付', className, style, }) => {
|
|
16356
16417
|
const [imageUrl, setImageUrl] = React.useState('');
|
|
16357
16418
|
React.useEffect(() => {
|