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.

73 lines (72 loc) 3.06 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends")); var _react = _interopRequireWildcard(require("react")); var _airwallexPaymentElements = require("airwallex-payment-elements"); var _dom = require("../utils/dom"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const Airwallex = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => { const { type = 'fullFeaturedCard', options, initOptions, style, onReady: propsOnReady, onSuccess: propsOnSuccess, onError: propsOnError, ...others } = props; const airwallexRef = (0, _react.useRef)(null); (0, _react.useImperativeHandle)(ref, () => airwallexRef.current); (0, _react.useEffect)(() => { (0, _airwallexPaymentElements.loadAirwallex)({ env: 'demo', ...initOptions }).then(airwallex => { if (airwallex) { const cardElement = airwallex.createElement(type, options); if (cardElement) { cardElement.mount(type); } } }); }, [options, type, initOptions]); (0, _react.useEffect)(() => { const el = _dom.clientDocument === null || _dom.clientDocument === void 0 ? void 0 : _dom.clientDocument.getElementById(type); const onReady = event => { propsOnReady === null || propsOnReady === void 0 || propsOnReady(event); }; const onSuccess = event => { propsOnSuccess === null || propsOnSuccess === void 0 || propsOnSuccess(event); }; const onError = event => { propsOnError === null || propsOnError === void 0 || propsOnError(event); }; if (el) { (0, _dom.on)(el, 'onReady', onReady); (0, _dom.on)(el, 'onSuccess', onSuccess); (0, _dom.on)(el, 'onError', onError); } return () => { if (el) { (0, _dom.off)(el, 'onReady', onReady); (0, _dom.off)(el, 'onSuccess', onSuccess); (0, _dom.off)(el, 'onError', onError); } }; }, [type, propsOnReady, propsOnSuccess, propsOnError]); return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({ ref: airwallexRef, id: String(type), style: { width: '100%', ...style } }, others)); }); Airwallex.displayName = 'Airwallex'; var _default = exports.default = /*#__PURE__*/_react.default.memo(Airwallex);