trustly-react-native-webview
Version:
Trustly ReactNative WebView needed when integrating Trustly Checkout in a ReactNative application.
47 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrustlyWebView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_native_1 = require("react-native");
const react_native_webview_1 = require("react-native-webview");
const common_1 = require("./common");
const onMessage = ({ data, onSuccess, onError }) => {
try {
const { type, url, eventType } = JSON.parse(data);
const isCheckoutEvent = eventType === 'checkout-event';
if (isCheckoutEvent) {
if (type === common_1.NativeEventTypes.REDIRECT) {
try {
react_native_1.Linking.openURL(url);
}
catch (e) {
console.log(`There was an error opening URL: ${url}. Error: ${e}`);
onError?.();
}
}
if (type === common_1.NativeEventTypes.ABORT || type === common_1.NativeEventTypes.ERROR) {
onError?.();
}
if (type === common_1.NativeEventTypes.SUCCESS) {
onSuccess?.();
}
}
}
catch (e) {
const error = typeof e === 'object' ? JSON.stringify(e) : e;
console.error(`Something unexpected happen when trying to map Trustly Checkout events. Error: ${error}`);
}
};
const TrustlyWebView = ({ uri, onSuccess, onError, }) => {
return ((0, jsx_runtime_1.jsx)(react_native_webview_1.WebView, { source: { uri }, injectedJavaScriptForMainFrameOnly: false, injectedJavaScript: common_1.trustlyCustomBridge, applicationNameForUserAgent: common_1.trustlyApplicationName, textZoom: 100, onMessage: ({ nativeEvent }) => {
try {
onMessage({ data: nativeEvent.data, onSuccess, onError });
}
catch {
console.error(`Format of nativeEvent.data does not match Trustly API. Data sent: ${nativeEvent.data}`);
}
} }));
};
exports.TrustlyWebView = TrustlyWebView;
exports.default = exports.TrustlyWebView;
//# sourceMappingURL=TrustlyWebView.js.map