tosspayments-react-native
Version:
tosspayments module for react-native
70 lines • 3.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const react_native_webview_1 = tslib_1.__importDefault(require("react-native-webview"));
const react_native_1 = require("react-native");
const react_native_send_intent_1 = tslib_1.__importDefault(require("react-native-send-intent"));
const Payments = (options) => {
const initlizePaymentsPage = react_1.useMemo(() => {
const reqeustOptionsString = JSON.stringify({
...options,
successUrl: "https://smsmsmsmin.github.io/tosspayments-react-native/callback",
failUrl: "https://smsmsmsmin.github.io/tosspayments-react-native/callback",
});
if (react_native_1.Platform.OS === "android")
return `window.onload = function() {
var clientKey = '${options.clientKey}';
var tossPayments = TossPayments(clientKey);
tossPayments.requestPayment('카드', ${reqeustOptionsString});
};
true;
`;
return `(function() {
var clientKey = '${options.clientKey}';
var tossPayments = TossPayments(clientKey);
tossPayments.requestPayment('카드', ${reqeustOptionsString});
})();
true;
`;
}, [options]);
const handleMessage = async (event) => {
const data = JSON.parse(event?.nativeEvent?.data);
if (data?.message) {
return options.onError(data);
}
return options.onSuccess(data);
};
const onShouldStartLoadWithRequest = (event) => {
if (event.url.startsWith("http://") ||
event.url.startsWith("https://") ||
event.url.startsWith("about:blank")) {
return true;
}
if (react_native_1.Platform.OS === "android") {
// @ts-ignore
react_native_send_intent_1.default.openAppWithUri(event.url)
.then((isOpened) => {
if (!isOpened) {
alert("앱 실행에 실패했습니다");
}
return false;
})
.catch((err) => {
console.log(err);
});
}
else {
react_native_1.Linking.openURL(event.url).catch((err) => {
alert("앱 실행에 실패했습니다. 설치가 되어있지 않은 경우 설치하기 버튼을 눌러주세요.");
});
return false;
}
return false;
};
return (react_1.default.createElement(react_native_webview_1.default, { useWebkit: true, style: { flex: 1 }, javaScriptEnabled: true, domStorageEnabled: true, injectedJavaScriptForMainFrameOnly: true, injectedJavaScript: initlizePaymentsPage, mixedContentMode: "compatibility", onMessage: handleMessage, originWhitelist: ["*"], sharedCookiesEnabled: true, onShouldStartLoadWithRequest: onShouldStartLoadWithRequest, source: {
uri: "https://smsmsmsmin.github.io/tosspayments-react-native/payments",
} }));
};
exports.default = Payments;
//# sourceMappingURL=Payments.js.map