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.
70 lines • 3.06 kB
JavaScript
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
var _excluded = ["airwallexProps", "visible", "onClose", "footer"];
import React, { useImperativeHandle, useRef, useState } from 'react';
import PayModal from '../pay-modal';
import Airwallex from './airwallex';
import omit from '../utils/omit';
var AirwallexModal = /*#__PURE__*/React.forwardRef(function (props, ref) {
var propsAirwallexProps = props.airwallexProps,
visible = props.visible,
_onClose = props.onClose,
_props$footer = props.footer,
footer = _props$footer === void 0 ? null : _props$footer,
rest = _objectWithoutProperties(props, _excluded);
var airwallexRef = useRef(null);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
uncontrolledVisible = _useState2[0],
setUncontrolledVisible = _useState2[1];
var _useState3 = useState(),
_useState4 = _slicedToArray(_useState3, 2),
airwallexProps = _useState4[0],
setAirwallexProps = _useState4[1];
var isControlled = visible !== undefined;
var _visible = isControlled ? visible : uncontrolledVisible;
useImperativeHandle(ref, function () {
return {
open: function open(value) {
if (value) {
setAirwallexProps(value);
}
setUncontrolledVisible(true);
},
close: function close() {
setUncontrolledVisible(false);
_onClose === null || _onClose === void 0 || _onClose();
},
current: airwallexRef.current
};
});
var _airwallexProps = propsAirwallexProps !== null && propsAirwallexProps !== void 0 ? propsAirwallexProps : airwallexProps;
return /*#__PURE__*/React.createElement(PayModal, _extends({
visible: _visible,
onClose: function onClose() {
if (!isControlled) {
setUncontrolledVisible(false);
}
_onClose === null || _onClose === void 0 || _onClose();
},
footer: footer
}, rest), _airwallexProps && /*#__PURE__*/React.createElement(Airwallex, _extends({
onSuccess: function onSuccess(event) {
var _airwallexProps$onSuc;
if (!isControlled) {
setUncontrolledVisible(false);
}
_airwallexProps === null || _airwallexProps === void 0 || (_airwallexProps$onSuc = _airwallexProps.onSuccess) === null || _airwallexProps$onSuc === void 0 || _airwallexProps$onSuc.call(_airwallexProps, event);
},
onError: function onError(event) {
var _airwallexProps$onErr;
if (!isControlled) {
setUncontrolledVisible(false);
}
_airwallexProps === null || _airwallexProps === void 0 || (_airwallexProps$onErr = _airwallexProps.onError) === null || _airwallexProps$onErr === void 0 || _airwallexProps$onErr.call(_airwallexProps, event);
}
}, omit(_airwallexProps, ['onSuccess', 'onError']))));
});
AirwallexModal.displayName = 'AirwallexModal';
export default AirwallexModal;