UNPKG

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.

106 lines 5.91 kB
import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _asyncToGenerator from "@babel/runtime-corejs3/helpers/esm/asyncToGenerator"; import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties"; var _excluded = ["children", "onClick", "createOrder"]; import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; 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'; var AirwallexButton = /*#__PURE__*/forwardRef(function (props, ref) { var _props$children = props.children, children = _props$children === void 0 ? 'Airwallex' : _props$children, onClick = props.onClick, createOrder = props.createOrder, others = _objectWithoutProperties(props, _excluded); var airwallexButtonRef = useRef({ nativeElement: null }); var airwallexPopupRef = useRef(null); var airwallexModalRef = useRef(null); var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), showMask = _useState2[0], setShowMask = _useState2[1]; var _useState3 = useState(), _useState4 = _slicedToArray(_useState3, 2), airwallexUrl = _useState4[0], setAirwallexUrl = _useState4[1]; useImperativeHandle(ref, function () { return airwallexButtonRef.current; }); var handlePayCreateOrder = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var options, _airwallexModalRef$cu, _airwallexPopupRef$cu; return _regeneratorRuntime.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!createOrder) { _context.next = 2; break; } _context.next = 1; return createOrder(); case 1: options = _context.sent; if (options) { if ((props === null || props === void 0 ? void 0 : props.payMode) === 'embedded') { if ((props === null || props === void 0 ? void 0 : props.displayType) === 'modal') { airwallexModalRef === null || airwallexModalRef === void 0 || (_airwallexModalRef$cu = airwallexModalRef.current) === null || _airwallexModalRef$cu === void 0 || _airwallexModalRef$cu.open(options); } else { (_airwallexPopupRef$cu = airwallexPopupRef.current) === null || _airwallexPopupRef$cu === void 0 || _airwallexPopupRef$cu.open(options); } } else { setShowMask(true); setAirwallexUrl(options.airwallexUrl); } } case 2: case "end": return _context.stop(); } }, _callee); })), [createOrder, props]); var handleClick = useCallback(function (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: function 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);