react-nativeandroid-intentexpopayment

Error (WebView, Expo Intent (open payment method: KakaoPay, NaverPay)


does anyone know how to use React navite Webview and expo intent-launcher to open an app (kakaopay, naver pay, payco...?).

I have no idea why this doesn't work (it works on iOS with just Linking.openURL(url)), but on android nothing works yet

Here my basic config: ("react-native": "0.74.3", "expo": "~51.0.22")

import * as IntentLauncher from "expo-intent-launcher";

const payUrl = "https://......."
pay
const onShouldStartLoadWithRequest = (event: any) => {
    console.log(event);
    const url = event.url || "";

    if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("about:blank")) {
      return true;
    }
    console.log("url", url);
    if (Platform.OS === "android") {
      try {
        IntentLauncher.startActivityAsync("android.intent.action.VIEW", {
          data: url,
        })
          .then(() => {
            console.log("Payment gateway opened successfully");
          })
          .catch((error) => {
            console.error("Failed to open payment gateway:", error);
          });
      } catch (error) {
        console.error("Error launching intent:", error);
      }
      return false;
    } else {
      Linking.openURL(url).catch((err) => {
        console.log({ err });
        Alert.alert(
          "App execution failed. If it is not installed, please press the install button."
        );
      });
      return false;
    }
  };

return (
    <View className="flex-1">
      <WebView
        ref={handleSetRef}
        useWebKit={false}
        scalesPageToFit={Platform.OS !== "ios"}
        originWhitelist={["*"]}
        source={{ uri: payU`your text`rl }}
        javaScriptEnabled={true}
        onLoad={NativeToWeb}
        onError={errorHandler}
        onMessage={WebToNative}
        onNavigationStateChange={onNavigationStateChange}
        onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
        setSupportMultipleWindows={false}
        javaScriptCanOpenWindowsAutomatically={true}
      />
    </View>
  );

I tried with the react-native-send-intent library library, but as it's not compatible with expo, I thought I'd try theirs (Expo IntentLauncher).

On iOS I have no problem, but when I launch with my android (device) after building an .apk, I get an error.

So I added packagaes in my AndroidManifest.xml for example:

<package android:name=“com.kakao.talk” /> 
<package android:name=“com.samsung.android.spay” /> 

But nothing works


Solution

  • Add this:

    if (Platform.OS === "android") {
      if (url.startsWith("intent:")) {
        const intents = url.split("#Intent;");
        const path = intents[0] || "";
        const query = intents[1] || "";
        const params = {};
        query.split(";").map((each) => {
          if (each.includes("=")) {
            const pairs = each.split("=");
            params[pairs[0]] = pairs[1];
          }
        });
        const scheme = params?.scheme;
        const packageName = params?.package;
        const data = path.replace("intent://", `${scheme}://`);
        try {
          IntentLauncher.startActivityAsync("android.intent.action.VIEW", {
            data: data,
          })
            .then(() => {
              console.log("Payment gateway opened successfully");
            })
            .catch((error) => {
              console.error("Failed to open payment gateway:", error);
            });
        } catch (error) {
          console.log("Error launching intent in if condition", error);
        }
      }