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.
52 lines • 3.09 kB
JavaScript
const _excluded = ["className", "children", "onClick", "createOrder", "modalProps"];
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 } from 'react';
import cs from '../utils/classNames';
import AlipayModal from './alipay-modal';
import Button from '../Button';
import { IconAlipay } from '../icons';
import { getPrefixCls } from '../utils/getPrefixCls';
const AlipayButton = /*#__PURE__*/forwardRef((props, ref) => {
const {
className,
children = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconAlipay, null), "\u652F\u4ED8\u5B9D\u652F\u4ED8"),
onClick,
createOrder,
modalProps
} = props,
others = _objectWithoutProperties(props, _excluded);
const alipayButtonRef = useRef({
nativeElement: null
});
const alipayModalRef = useRef(null);
useImperativeHandle(ref, () => alipayButtonRef.current);
const handlePayCreateOrder = useCallback(async () => {
if (createOrder) {
const options = await (createOrder === null || createOrder === void 0 ? void 0 : createOrder());
if (options) {
var _alipayModalRef$curre;
alipayModalRef === null || alipayModalRef === void 0 || (_alipayModalRef$curre = alipayModalRef.current) === null || _alipayModalRef$curre === void 0 || _alipayModalRef$curre.open(options);
}
}
}, [createOrder]);
const handleClick = useCallback(e => {
if (createOrder) {
handlePayCreateOrder();
} else {
var _alipayModalRef$curre2;
alipayModalRef === null || alipayModalRef === void 0 || (_alipayModalRef$curre2 = alipayModalRef.current) === null || _alipayModalRef$curre2 === void 0 || _alipayModalRef$curre2.open();
}
onClick === null || onClick === void 0 || onClick(e);
}, [onClick, createOrder, handlePayCreateOrder]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, _extends({
ref: alipayButtonRef,
className: cs(getPrefixCls('alipay-btn'), className),
onClick: handleClick
}, others), children), /*#__PURE__*/React.createElement(AlipayModal, _extends({
ref: alipayModalRef
}, modalProps)));
});
AlipayButton.displayName = 'AlipayButton';
export default /*#__PURE__*/memo(AlipayButton);