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.

65 lines 3.05 kB
import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _asyncToGenerator from "@babel/runtime-corejs3/helpers/esm/asyncToGenerator"; import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties"; var _excluded = ["className", "children", "onClick", "createOrder", "modalProps"]; import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; import React, { forwardRef, memo, useCallback, useImperativeHandle, useRef } from 'react'; import cs from '../utils/classNames'; import WechatModal from './wechat-modal'; import Button from '../Button'; import { IconWechatPay } from '../icons'; import { getPrefixCls } from '../utils/getPrefixCls'; var WechatButton = /*#__PURE__*/forwardRef(function (props, ref) { var className = props.className, _props$children = props.children, children = _props$children === void 0 ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconWechatPay, null), "\u5FAE\u4FE1\u652F\u4ED8") : _props$children, onClick = props.onClick, createOrder = props.createOrder, modalProps = props.modalProps, others = _objectWithoutProperties(props, _excluded); var wechatButtonRef = useRef({ nativeElement: null }); var wechatModalRef = useRef(null); useImperativeHandle(ref, function () { return wechatButtonRef.current; }); var handlePayCreateOrder = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _wechatModalRef$curre, options; 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; (_wechatModalRef$curre = wechatModalRef.current) === null || _wechatModalRef$curre === void 0 || _wechatModalRef$curre.open(options); case 2: case "end": return _context.stop(); } }, _callee); })), [createOrder]); var handleClick = useCallback(function (e) { if (createOrder) { handlePayCreateOrder(); } else { var _wechatModalRef$curre2; wechatModalRef === null || wechatModalRef === void 0 || (_wechatModalRef$curre2 = wechatModalRef.current) === null || _wechatModalRef$curre2 === void 0 || _wechatModalRef$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: wechatButtonRef, className: cs(getPrefixCls('wechat-btn'), className), onClick: handleClick }, others), children), /*#__PURE__*/React.createElement(WechatModal, _extends({ ref: wechatModalRef }, modalProps))); }); WechatButton.displayName = 'WechatButton'; export default /*#__PURE__*/memo(WechatButton);