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.
86 lines • 5.63 kB
JavaScript
const _excluded = ["children", "onClick", "createOrder"];
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, { forwardRef, memo, useCallback, useImperativeHandle, useRef, useState } from 'react';
import AirwallexPopup from './airwallex-popup';
import AirwallexModal from './airwallex-modal';
import Button from '../Button';
import PayMask from '../PayMask';
import omit from '../utils/omit';
const AirwallexButton = /*#__PURE__*/forwardRef((props, ref) => {
const {
children = 'Airwallex',
onClick,
createOrder
} = props,
others = _objectWithoutProperties(props, _excluded);
const airwallexButtonRef = useRef({
nativeElement: null
});
const airwallexPopupRef = useRef(null);
const airwallexModalRef = useRef(null);
const [showMask, setShowMask] = useState(false);
const [airwallexUrl, setAirwallexUrl] = useState();
useImperativeHandle(ref, () => airwallexButtonRef.current);
const handlePayCreateOrder = useCallback(async () => {
if (createOrder) {
// 1. 业务侧创建订单,获取参数
const options = await (createOrder === null || createOrder === void 0 ? void 0 : createOrder());
if (options) {
if ((props === null || props === void 0 ? void 0 : props.payMode) === 'embedded') {
if ((props === null || props === void 0 ? void 0 : props.displayType) === 'modal') {
var _airwallexModalRef$cu;
airwallexModalRef === null || airwallexModalRef === void 0 || (_airwallexModalRef$cu = airwallexModalRef.current) === null || _airwallexModalRef$cu === void 0 || _airwallexModalRef$cu.open(options);
} else {
var _airwallexPopupRef$cu;
(_airwallexPopupRef$cu = airwallexPopupRef.current) === null || _airwallexPopupRef$cu === void 0 || _airwallexPopupRef$cu.open(options);
}
} else {
setShowMask(true);
setAirwallexUrl(options.airwallexUrl);
}
}
}
}, [createOrder, props]);
const handleClick = useCallback(e => {
onClick === null || onClick === void 0 || onClick(e);
if (createOrder) {
handlePayCreateOrder();
return;
}
if ((props === null || props === void 0 ? void 0 : props.payMode) === 'embedded') {
if ((props === null || props === void 0 ? void 0 : props.displayType) === 'modal') {
var _airwallexModalRef$cu2;
airwallexModalRef === null || airwallexModalRef === void 0 || (_airwallexModalRef$cu2 = airwallexModalRef.current) === null || _airwallexModalRef$cu2 === void 0 || _airwallexModalRef$cu2.open();
} else {
var _airwallexPopupRef$cu2;
airwallexPopupRef === null || airwallexPopupRef === void 0 || (_airwallexPopupRef$cu2 = airwallexPopupRef.current) === null || _airwallexPopupRef$cu2 === void 0 || _airwallexPopupRef$cu2.open();
}
return;
}
if ((props === null || props === void 0 ? void 0 : props.payMode) === 'redirect') {
setShowMask(true);
setAirwallexUrl(props === null || props === void 0 ? void 0 : props.airwallexUrl);
}
}, [onClick, createOrder, handlePayCreateOrder, props]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, _extends({
ref: airwallexButtonRef,
onClick: handleClick
}, omit(others, ['payMode', 'displayType', 'createOrder', 'airwallexUrl', 'maskProps', 'popupProps', 'modalProps'])), children), (props === null || props === void 0 ? void 0 : props.payMode) === 'embedded' && (props === null || props === void 0 ? void 0 : props.displayType) === 'popup' && /*#__PURE__*/React.createElement(AirwallexPopup, _extends({
ref: airwallexPopupRef
}, props === null || props === void 0 ? void 0 : props.popupProps)), (props === null || props === void 0 ? void 0 : props.payMode) === 'embedded' && (props === null || props === void 0 ? void 0 : props.displayType) === 'modal' && /*#__PURE__*/React.createElement(AirwallexModal, _extends({
ref: airwallexModalRef
}, props === null || props === void 0 ? void 0 : props.modalProps)), (props === null || props === void 0 ? void 0 : props.payMode) === 'redirect' && showMask && airwallexUrl && /*#__PURE__*/React.createElement(PayMask, _extends({
visible: showMask,
onClose: () => {
var _props$maskProps, _props$maskProps$onCl;
setShowMask(false);
props === null || props === void 0 || (_props$maskProps = props.maskProps) === null || _props$maskProps === void 0 || (_props$maskProps$onCl = _props$maskProps.onClose) === null || _props$maskProps$onCl === void 0 || _props$maskProps$onCl.call(_props$maskProps);
},
payUrl: airwallexUrl,
title: 'Airwallex'
}, omit((props === null || props === void 0 ? void 0 : props.maskProps) || {}, ['onClose']))));
});
AirwallexButton.displayName = 'AirwallexButton';
export default /*#__PURE__*/memo(AirwallexButton);