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.

69 lines 3.28 kB
import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties"; var _excluded = ["type", "options", "initOptions", "style", "onReady", "onSuccess", "onError"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; import { loadAirwallex } from 'airwallex-payment-elements'; import { clientDocument, off, on } from '../utils/dom'; var Airwallex = /*#__PURE__*/forwardRef(function (props, ref) { var _props$type = props.type, type = _props$type === void 0 ? 'fullFeaturedCard' : _props$type, options = props.options, initOptions = props.initOptions, style = props.style, propsOnReady = props.onReady, propsOnSuccess = props.onSuccess, propsOnError = props.onError, others = _objectWithoutProperties(props, _excluded); var airwallexRef = useRef(null); useImperativeHandle(ref, function () { return airwallexRef.current; }); useEffect(function () { loadAirwallex(_objectSpread({ env: 'demo' }, initOptions)).then(function (airwallex) { if (airwallex) { var cardElement = airwallex.createElement(type, options); if (cardElement) { cardElement.mount(type); } } }); }, [options, type, initOptions]); useEffect(function () { var el = clientDocument === null || clientDocument === void 0 ? void 0 : clientDocument.getElementById(type); var onReady = function onReady(event) { propsOnReady === null || propsOnReady === void 0 || propsOnReady(event); }; var onSuccess = function onSuccess(event) { propsOnSuccess === null || propsOnSuccess === void 0 || propsOnSuccess(event); }; var onError = function onError(event) { propsOnError === null || propsOnError === void 0 || propsOnError(event); }; if (el) { on(el, 'onReady', onReady); on(el, 'onSuccess', onSuccess); on(el, 'onError', onError); } return function () { if (el) { off(el, 'onReady', onReady); off(el, 'onSuccess', onSuccess); off(el, 'onError', onError); } }; }, [type, propsOnReady, propsOnSuccess, propsOnError]); return /*#__PURE__*/React.createElement("div", _extends({ ref: airwallexRef, id: String(type), style: _objectSpread({ width: '100%' }, style) }, others)); }); Airwallex.displayName = 'Airwallex'; export default /*#__PURE__*/React.memo(Airwallex);