UNPKG

react-native-directpay-ipg

Version:
161 lines (136 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _reactNative = _interopRequireDefault(require("pusher-js/react-native")); var _reactNative2 = require("react-native"); var _reactNativeDirectpayIpg = require("react-native-directpay-ipg"); var _reactNativeWebview = _interopRequireDefault(require("react-native-webview")); var _reactNativeAwesomeLoading = _interopRequireDefault(require("react-native-awesome-loading")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const sessionUrl = stage => { return stage === _reactNativeDirectpayIpg.IPGStage.PROD ? 'https://gateway.directpay.lk/api/v3/create-session' : 'https://test-gateway.directpay.lk/api/v3/create-session'; }; class IPGComponent extends _react.default.Component { constructor(props) { super(props); _defineProperty(this, "pusher", void 0); this.state = { link: '', token: null, loading: true, webloading: false }; } async createSession() { try { const response = await fetch(sessionUrl(this.props.stage), { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'x-plugin-source': 'REACT-NATIVE', 'x-plugin-version': '0.1.4', 'Authorization': 'hmac ' + this.props.signature }, body: this.props.dataString }); const json = await response.json(); console.log(json); if (json.status === 200) { this.setState({ token: json.data.token, link: json.data.link }); this.initPusher(json.data.ak, json.data.ch); } else { this.props.callback(json); } } catch (error) { this.props.callback({ status: 400, data: { code: 'SERVER_ERROR', title: 'Failed proceed payment', message: 'Failed proceed payment' } }); } finally { this.setState({ loading: false }); } } async initPusher(ak, ch) { this.pusher = new _reactNative.default(ak, { cluster: 'ap2' }); this.bindToPusher(ch); } bindToPusher(ch) { setTimeout(() => { let connection = this.pusher.connection.bind('error', function (err) { if (err.error.data.code === 4004) { console.log('Over limit!'); } }); if (connection.state === 'connected') { const channel = this.pusher.subscribe(ch); channel.bind('SDK_' + this.state.token, data => { this.props.callback(data.response); }); } else if (connection.state === 'connecting') { this.bindToPusher(ch); } }, 1000); } componentDidMount() { this.createSession(); } componentWillUnmount() { if (this.pusher) { this.pusher.unsubscribe('dp_plugin_dev'); this.pusher.disconnect(); } } IndicatorLoadingView() { return /*#__PURE__*/_react.default.createElement(_reactNativeAwesomeLoading.default, { indicatorId: 8, size: 50, isActive: true, text: "Please wait..." }); } render() { return this.state.loading ? this.IndicatorLoadingView() : /*#__PURE__*/_react.default.createElement(_reactNative2.SafeAreaView, null, /*#__PURE__*/_react.default.createElement(_reactNativeWebview.default, { startInLoadingState: true, javaScriptEnabled: true, renderLoading: this.IndicatorLoadingView, style: styles.container, source: { uri: this.state.link } })); } } const styles = _reactNative2.StyleSheet.create({ container: { width: _reactNative2.Dimensions.get('window').width, height: _reactNative2.Dimensions.get('window').height, marginVertical: 20 }, indicator: { position: 'absolute', width: _reactNative2.Dimensions.get('window').width, height: _reactNative2.Dimensions.get('window').height, display: 'flex', justifyContent: 'center', alignItems: 'center' } }); var _default = IPGComponent; exports.default = _default; //# sourceMappingURL=IPGComponent.js.map