pay-sdk-react
Version:
A cross-platform payment SDK for React, supporting Alipay, WeChat Pay, PayPal, Stripe, Payssion, and Airwallex, compatible with H5, PC, and App environments.
92 lines • 3.75 kB
JavaScript
const _excluded = ["visible", "payUrl", "onClose", "onMaskClick", "onContinue", "openMode", "autoOpenWindow", "className", "destroyOnClose"];
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); }
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; }
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; }
import React, { useEffect, useState } from 'react';
import Mask from '../Mask';
import { usePayWindowOpen } from '../hooks/usePayWindowOpen';
import { mergeProps } from '../utils/with-default-props';
import cs from '../utils/classNames';
import { getPrefixCls } from '../utils/getPrefixCls';
const classPrefix = getPrefixCls('mask');
const defaultProps = {
visible: false,
title: 'Pay',
desc: "Don't see the secure pay browser? We'll help you re-launch the window to complete your purchase",
buttonText: 'Click to Continue',
autoOpenWindow: true
};
const PayMask = p => {
const props = mergeProps(defaultProps, p);
const {
visible,
payUrl,
onClose,
onMaskClick,
onContinue,
openMode,
autoOpenWindow,
className,
destroyOnClose = true
} = props,
rest = _objectWithoutProperties(props, _excluded);
const [isWindowMonitoring, setIsWindowMonitoring] = useState(false);
const {
openWindow,
closeWindow
} = usePayWindowOpen({
payUrl: payUrl || '',
onClose: () => {
onClose === null || onClose === void 0 || onClose();
setIsWindowMonitoring(false);
},
autoOpen: autoOpenWindow,
openMode
});
useEffect(() => {
if (visible && autoOpenWindow) {
openWindow();
}
}, [visible, autoOpenWindow, openWindow]);
useEffect(() => {
if (visible && isWindowMonitoring && !autoOpenWindow) {
openWindow();
}
}, [visible, isWindowMonitoring, autoOpenWindow, openWindow]);
const handleContinue = () => {
if (!autoOpenWindow) {
setIsWindowMonitoring(true);
} else {
openWindow();
}
onContinue === null || onContinue === void 0 || onContinue();
};
const handleClose = () => {
onClose === null || onClose === void 0 || onClose();
closeWindow();
};
const classNames = cs(classPrefix, className);
return /*#__PURE__*/React.createElement(Mask, _extends({
visible: visible,
onMaskClick: e => {
handleClose();
onMaskClick === null || onMaskClick === void 0 || onMaskClick(e);
},
className: classNames,
destroyOnClose: destroyOnClose
}, rest), /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-content")
}, /*#__PURE__*/React.createElement("button", {
className: "".concat(classPrefix, "-close"),
onClick: handleClose
}, "\xD7"), /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-title")
}, props.title), /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-desc")
}, props.desc), /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-continue"),
onClick: handleContinue
}, props.buttonText)));
};
export * from './interface';
export default PayMask;