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.
@@ -1455,6 +1455,12 @@
1455
1455
  return true;
1456
1456
  }
1457
1457
  })();
1458
+ const clientWindow = (function () {
1459
+ if (isServerRendering) {
1460
+ return undefined;
1461
+ }
1462
+ return window;
1463
+ })();
1458
1464
  const clientDocument = (function () {
1459
1465
  if (isServerRendering) {
1460
1466
  return undefined;
@@ -1493,6 +1499,21 @@
1493
1499
  function isFunction(obj) {
1494
1500
  return typeof obj === 'function';
1495
1501
  }
1502
+ /**
1503
+ * @zh 检测是否为移动设备
1504
+ * @en Detect if it's a mobile device
1505
+ */
1506
+ function isMobileDevice() {
1507
+ return (clientWindow &&
1508
+ /Android|webOS|iPhone|iPad|iPod|Windows Phone|BlackBerry|Mobile|IEMobile|Opera Mini/i.test(clientWindow?.navigator.userAgent));
1509
+ }
1510
+ /**
1511
+ * @zh 检测是否为触摸设备
1512
+ * @en Detect if it's a touch device
1513
+ */
1514
+ function isTouchDevice() {
1515
+ return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
1516
+ }
1496
1517
  function isPromise$1(obj) {
1497
1518
  return (!!obj && typeof obj === 'object' && typeof obj.then === 'function');
1498
1519
  }
@@ -5099,7 +5120,7 @@
5099
5120
  black: '0, 0, 0',
5100
5121
  white: '255, 255, 255',
5101
5122
  };
5102
- const defaultProps$4 = {
5123
+ const defaultProps$5 = {
5103
5124
  visible: true,
5104
5125
  destroyOnClose: false,
5105
5126
  forceRender: false,
@@ -5110,7 +5131,7 @@
5110
5131
  stopPropagation: ['click'],
5111
5132
  };
5112
5133
  const Mask = (p) => {
5113
- const props = mergeProps(defaultProps$4, p);
5134
+ const props = mergeProps(defaultProps$5, p);
5114
5135
  const ref = React.useRef(null);
5115
5136
  useLockScroll(ref, props.visible && props.disableBodyScroll);
5116
5137
  const background = React.useMemo(() => {
@@ -5157,28 +5178,154 @@
5157
5178
  return (jsxRuntimeExports.jsx(ShouldRender, { active: active, forceRender: props.forceRender, destroyOnClose: props.destroyOnClose, children: renderToContainer(props.getContainer, node) }));
5158
5179
  };
5159
5180
 
5160
- function usePayWindowOpen({ payUrl, onClose, onSuccess, autoOpen = true, openMode = 'window', }) {
5181
+ exports.PaymentMethod = void 0;
5182
+ (function (PaymentMethod) {
5183
+ /**
5184
+ * @zh 微信(pc)
5185
+ * @en Wechat(pc)
5186
+ */
5187
+ PaymentMethod["WechatPc"] = "wechat-pc";
5188
+ /**
5189
+ * @zh 支付宝(pc)
5190
+ * @en Alipay(pc)
5191
+ */
5192
+ PaymentMethod["AlipayPc"] = "alipay-pc";
5193
+ /**
5194
+ * @zh paypal(pc)
5195
+ * @en Paypal(pc)
5196
+ */
5197
+ PaymentMethod["PaypalPc"] = "paypal-pc";
5198
+ /**
5199
+ * @zh stripe(pc)
5200
+ * @en Stripe(pc)
5201
+ */
5202
+ PaymentMethod["StripePc"] = "stripe-pc";
5203
+ /**
5204
+ * @zh 微信(H5)
5205
+ * @en Wechat(H5)
5206
+ */
5207
+ PaymentMethod["WechatH5"] = "wechat-h5";
5208
+ /**
5209
+ * @zh 支付宝(H5)
5210
+ * @en Alipay(H5)
5211
+ */
5212
+ PaymentMethod["AlipayH5"] = "alipay-h5";
5213
+ /**
5214
+ * @zh paypal(H5)
5215
+ * @en Paypal(H5)
5216
+ */
5217
+ PaymentMethod["PaypalH5"] = "paypal-h5";
5218
+ /**
5219
+ * @zh 微信内部(H5)
5220
+ * @en Wechat inside(H5)
5221
+ */
5222
+ PaymentMethod["WechatInsideH5"] = "wechat-inside-h5";
5223
+ /**
5224
+ * @zh stripe(H5)
5225
+ * @en Stripe(H5)
5226
+ */
5227
+ PaymentMethod["StripeH5"] = "stripe-h5";
5228
+ /**
5229
+ * @zh payssion支付
5230
+ * @en Payssion
5231
+ */
5232
+ PaymentMethod["Payssion"] = "payssion";
5233
+ /**
5234
+ * @zh 空中云汇
5235
+ * @en Airwallex
5236
+ */
5237
+ PaymentMethod["Airwallex"] = "airwallex";
5238
+ })(exports.PaymentMethod || (exports.PaymentMethod = {}));
5239
+ /**
5240
+ * @zh 支付页面打开模式枚举
5241
+ * @en Payment page opening mode enum
5242
+ */
5243
+ exports.OpenPayUrlMode = void 0;
5244
+ (function (OpenPayUrlMode) {
5245
+ /**
5246
+ * @zh 新页面或弹窗模式 - 使用 window.open() 打开新页面或弹窗,支持智能设备检测
5247
+ * @en New page or popup mode - Use window.open() to open new page or popup, supports smart device detection
5248
+ */
5249
+ OpenPayUrlMode["Window"] = "window";
5250
+ /**
5251
+ * @zh 新页面模式 - 使用 window.location.href 跳转到新页面
5252
+ * @en New page mode - Use window.location.href to navigate to new page
5253
+ */
5254
+ OpenPayUrlMode["Href"] = "href";
5255
+ /**
5256
+ * @zh 替换页面模式 - 使用 window.location.replace 替换当前页面
5257
+ * @en Replace page mode - Use window.location.replace to replace current page
5258
+ */
5259
+ OpenPayUrlMode["Replace"] = "replace";
5260
+ })(exports.OpenPayUrlMode || (exports.OpenPayUrlMode = {}));
5261
+
5262
+ /**
5263
+ * @zh 获取屏幕宽度
5264
+ * @en Get screen width
5265
+ */
5266
+ function getScreenWidth() {
5267
+ return (clientWindow?.innerWidth ||
5268
+ clientDocument?.documentElement?.clientWidth ||
5269
+ clientDocument?.body?.clientWidth);
5270
+ }
5271
+ const defaultProps$4 = {
5272
+ payUrl: '',
5273
+ openMode: exports.OpenPayUrlMode.Window,
5274
+ };
5275
+ function usePayWindowOpen(p) {
5276
+ const props = mergeProps(defaultProps$4, p);
5277
+ const { payUrl, onClose, onSuccess, autoOpen = true } = props;
5161
5278
  const payWindowRef = React.useRef(null);
5162
5279
  const pollTimerRef = React.useRef(null);
5163
5280
  const [isWindowMonitoring, setWindowMonitoring] = React.useState(false);
5281
+ // 创建弹窗的公共函数
5282
+ const createPopupWindow = React.useCallback((url) => {
5283
+ // 计算弹窗尺寸和位置,实现垂直水平居中
5284
+ const width = Math.min(550, window.outerWidth * 0.9);
5285
+ const height = Math.min(700, window.outerHeight);
5286
+ // 使用多种定位方法确保居中
5287
+ const left = Math.round((window.screen.availWidth - width) / 2);
5288
+ const top = Math.round((window.screen.availHeight - height) / 2);
5289
+ const screenX = left;
5290
+ const screenY = top;
5291
+ return window.open(url, 'paypal_payment', `width=${width},height=${height},left=${left},top=${top},screenX=${screenX},screenY=${screenY},scrollbars=yes,resizable=yes`);
5292
+ }, [onClose, onSuccess]);
5164
5293
  // 打开支付窗口
5165
5294
  const openWindow = React.useCallback(() => {
5166
- if (openMode === 'window') {
5295
+ if (props?.openMode === exports.OpenPayUrlMode.Window) {
5167
5296
  if (payWindowRef.current && !payWindowRef.current.closed) {
5168
5297
  payWindowRef.current.focus();
5169
5298
  }
5170
5299
  else {
5171
- payWindowRef.current = window.open(payUrl);
5172
- setWindowMonitoring(true);
5300
+ const isMobile = isMobileDevice() || isTouchDevice();
5301
+ const forceNewWindow = props?.forceNewWindow;
5302
+ if (isMobile) {
5303
+ payWindowRef.current = window.open(payUrl);
5304
+ return;
5305
+ }
5306
+ if (forceNewWindow) {
5307
+ payWindowRef.current = createPopupWindow(payUrl);
5308
+ return;
5309
+ }
5310
+ const screenWidth = getScreenWidth() || 0;
5311
+ if (screenWidth >= 768) {
5312
+ payWindowRef.current = createPopupWindow(payUrl);
5313
+ }
5314
+ else {
5315
+ payWindowRef.current = window.open(payUrl);
5316
+ }
5173
5317
  }
5318
+ setWindowMonitoring(true);
5319
+ return;
5174
5320
  }
5175
- else if (openMode === 'replace') {
5321
+ if (props?.openMode === exports.OpenPayUrlMode.Replace) {
5176
5322
  window.location.replace(payUrl);
5323
+ return;
5177
5324
  }
5178
- else if (openMode === 'href') {
5325
+ if (props?.openMode === exports.OpenPayUrlMode.Href) {
5179
5326
  window.location.href = payUrl;
5180
5327
  }
5181
- }, [payUrl, openMode]);
5328
+ }, [payUrl, props, createPopupWindow]);
5182
5329
  // 关闭窗口
5183
5330
  const closeWindow = React.useCallback(() => {
5184
5331
  payWindowRef.current?.close();
@@ -5192,7 +5339,7 @@
5192
5339
  }, [autoOpen, openWindow]);
5193
5340
  // 轮询和消息监听
5194
5341
  React.useEffect(() => {
5195
- if (!isWindowMonitoring || openMode !== 'window') {
5342
+ if (!isWindowMonitoring || props?.openMode !== exports.OpenPayUrlMode.Window) {
5196
5343
  return;
5197
5344
  }
5198
5345
  pollTimerRef.current = setInterval(() => {
@@ -5217,7 +5364,7 @@
5217
5364
  window.removeEventListener('message', handleMessage);
5218
5365
  payWindowRef.current?.close();
5219
5366
  };
5220
- }, [isWindowMonitoring, openMode, onClose, onSuccess]);
5367
+ }, [isWindowMonitoring, props, onClose, onSuccess]);
5221
5368
  return {
5222
5369
  openWindow,
5223
5370
  closeWindow,
@@ -5235,7 +5382,7 @@
5235
5382
  };
5236
5383
  const PayMask = (p) => {
5237
5384
  const props = mergeProps(defaultProps$3, p);
5238
- const { visible, payUrl, onClose, onMaskClick, onContinue, openMode, autoOpenWindow, className, destroyOnClose = true, ...rest } = props;
5385
+ const { visible, payUrl, onClose, onMaskClick, onContinue, openMode, autoOpenWindow, forceNewWindow, className, destroyOnClose = true, ...rest } = props;
5239
5386
  const [isWindowMonitoring, setIsWindowMonitoring] = React.useState(false);
5240
5387
  const { openWindow, closeWindow } = usePayWindowOpen({
5241
5388
  payUrl: payUrl || '',
@@ -5245,6 +5392,7 @@
5245
5392
  },
5246
5393
  autoOpen: autoOpenWindow,
5247
5394
  openMode,
5395
+ ...(openMode === exports.OpenPayUrlMode.Window && { forceNewWindow }),
5248
5396
  });
5249
5397
  React.useEffect(() => {
5250
5398
  if (visible && autoOpenWindow) {
@@ -9529,7 +9677,7 @@
9529
9677
  var PayssionButton$1 = React.memo(PayssionButton);
9530
9678
 
9531
9679
  const ConfirmModal = (props) => {
9532
- const { visible, title = '支付确认', desc = (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx("div", { children: "1. \u5982\u679C\u60A8\u5DF2\u5B8C\u6210\u652F\u4ED8\uFF0C\u8BF7\u70B9\u51FB[\u652F\u4ED8\u5B8C\u6210]" }), jsxRuntimeExports.jsx("div", { children: "2. \u5982\u679C\u672A\u652F\u4ED8\u6210\u529F\uFF0C\u53EF\u70B9\u51FB[\u91CD\u65B0\u652F\u4ED8]" })] })), retryButtonProps, finishButtonProps, icon = (jsxRuntimeExports.jsx("span", { role: "img", "aria-label": "success", children: "\u2714\uFE0F" })), showClose = true, maskClosable = false, onClose, payUrl, autoOpenWindow = false, openMode = 'window', } = props;
9680
+ const { visible, title = '支付确认', desc = (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx("div", { children: "1. \u5982\u679C\u60A8\u5DF2\u5B8C\u6210\u652F\u4ED8\uFF0C\u8BF7\u70B9\u51FB[\u652F\u4ED8\u5B8C\u6210]" }), jsxRuntimeExports.jsx("div", { children: "2. \u5982\u679C\u672A\u652F\u4ED8\u6210\u529F\uFF0C\u53EF\u70B9\u51FB[\u91CD\u65B0\u652F\u4ED8]" })] })), retryButtonProps, finishButtonProps, icon = (jsxRuntimeExports.jsx("span", { role: "img", "aria-label": "success", children: "\u2714\uFE0F" })), showClose = true, maskClosable = false, onClose, payUrl, autoOpenWindow = false, openMode = exports.OpenPayUrlMode.Window, forceNewWindow, } = props;
9533
9681
  const { children: retryButtonChildren = '重新支付', ...retryButtonPropsRest } = retryButtonProps || {};
9534
9682
  const { children: finishButtonChildren = '支付完成', ...finishButtonPropsRest } = finishButtonProps || {};
9535
9683
  const { closeWindow } = usePayWindowOpen({
@@ -9537,6 +9685,7 @@
9537
9685
  onClose,
9538
9686
  autoOpen: autoOpenWindow,
9539
9687
  openMode: openMode,
9688
+ ...(openMode === exports.OpenPayUrlMode.Window && { forceNewWindow }),
9540
9689
  });
9541
9690
  const handleClose = React.useCallback(() => {
9542
9691
  onClose?.();
@@ -16282,65 +16431,6 @@
16282
16431
  WechatComponent.Button = WechatButton$1;
16283
16432
  WechatComponent.Modal = WechatModal$1;
16284
16433
 
16285
- exports.PaymentMethod = void 0;
16286
- (function (PaymentMethod) {
16287
- /**
16288
- * @zh 微信(pc)
16289
- * @en Wechat(pc)
16290
- */
16291
- PaymentMethod["WechatPc"] = "wechat-pc";
16292
- /**
16293
- * @zh 支付宝(pc)
16294
- * @en Alipay(pc)
16295
- */
16296
- PaymentMethod["AlipayPc"] = "alipay-pc";
16297
- /**
16298
- * @zh paypal(pc)
16299
- * @en Paypal(pc)
16300
- */
16301
- PaymentMethod["PaypalPc"] = "paypal-pc";
16302
- /**
16303
- * @zh stripe(pc)
16304
- * @en Stripe(pc)
16305
- */
16306
- PaymentMethod["StripePc"] = "stripe-pc";
16307
- /**
16308
- * @zh 微信(H5)
16309
- * @en Wechat(H5)
16310
- */
16311
- PaymentMethod["WechatH5"] = "wechat-h5";
16312
- /**
16313
- * @zh 支付宝(H5)
16314
- * @en Alipay(H5)
16315
- */
16316
- PaymentMethod["AlipayH5"] = "alipay-h5";
16317
- /**
16318
- * @zh paypal(H5)
16319
- * @en Paypal(H5)
16320
- */
16321
- PaymentMethod["PaypalH5"] = "paypal-h5";
16322
- /**
16323
- * @zh 微信内部(H5)
16324
- * @en Wechat inside(H5)
16325
- */
16326
- PaymentMethod["WechatInsideH5"] = "wechat-inside-h5";
16327
- /**
16328
- * @zh stripe(H5)
16329
- * @en Stripe(H5)
16330
- */
16331
- PaymentMethod["StripeH5"] = "stripe-h5";
16332
- /**
16333
- * @zh payssion支付
16334
- * @en Payssion
16335
- */
16336
- PaymentMethod["Payssion"] = "payssion";
16337
- /**
16338
- * @zh 空中云汇
16339
- * @en Airwallex
16340
- */
16341
- PaymentMethod["Airwallex"] = "airwallex";
16342
- })(exports.PaymentMethod || (exports.PaymentMethod = {}));
16343
-
16344
16434
  const PayButton = React.forwardRef((props, ref) => {
16345
16435
  if (props.payMethod === exports.PaymentMethod.PaypalH5 ||
16346
16436
  props.payMethod === exports.PaymentMethod.PaypalPc) {