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.
65 lines • 3.79 kB
JavaScript
const _excluded = ["children", "onClick", "displayType", "stripePopupProps", "stripeModalProps", "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 } from 'react';
import StripePopup from './stripe-popup';
import StripeModal from './stripe-modal';
import Button from '../Button';
const StripeButton = /*#__PURE__*/forwardRef((props, ref) => {
const {
children = 'Stripe',
onClick,
displayType = 'popup',
stripePopupProps,
stripeModalProps,
createOrder
} = props,
others = _objectWithoutProperties(props, _excluded);
const stripeButtonRef = useRef({
nativeElement: null
});
const stripePopupRef = useRef(null);
const stripeModalRef = useRef(null);
useImperativeHandle(ref, () => stripeButtonRef.current);
const handlePayCreateOrder = useCallback(async () => {
if (createOrder) {
// 1. 业务侧创建订单,获取参数
const options = await (createOrder === null || createOrder === void 0 ? void 0 : createOrder());
if (options !== null && options !== void 0 && options.clientSecret && options !== null && options !== void 0 && options.stripeKey) {
// 2. 唤起弹窗
if (displayType === 'modal') {
var _stripeModalRef$curre;
(_stripeModalRef$curre = stripeModalRef.current) === null || _stripeModalRef$curre === void 0 || _stripeModalRef$curre.open(options);
} else {
var _stripePopupRef$curre;
(_stripePopupRef$curre = stripePopupRef.current) === null || _stripePopupRef$curre === void 0 || _stripePopupRef$curre.open(options);
}
}
}
}, [createOrder, displayType]);
const handleClick = useCallback(e => {
if (createOrder) {
handlePayCreateOrder();
} else {
if (displayType === 'modal') {
var _stripeModalRef$curre2;
stripeModalRef === null || stripeModalRef === void 0 || (_stripeModalRef$curre2 = stripeModalRef.current) === null || _stripeModalRef$curre2 === void 0 || _stripeModalRef$curre2.open();
} else {
var _stripePopupRef$curre2;
stripePopupRef === null || stripePopupRef === void 0 || (_stripePopupRef$curre2 = stripePopupRef.current) === null || _stripePopupRef$curre2 === void 0 || _stripePopupRef$curre2.open();
}
}
onClick === null || onClick === void 0 || onClick(e);
}, [onClick, createOrder, handlePayCreateOrder, displayType]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, _extends({
ref: stripeButtonRef,
onClick: handleClick
}, others), children), displayType === 'popup' && /*#__PURE__*/React.createElement(StripePopup, _extends({
ref: stripePopupRef
}, stripePopupProps)), displayType === 'modal' && /*#__PURE__*/React.createElement(StripeModal, _extends({
ref: stripeModalRef
}, stripeModalProps)));
});
StripeButton.displayName = 'StripeButton';
export default /*#__PURE__*/memo(StripeButton);