Skip to content

Instantly share code, notes, and snippets.

@Jay-flow
Created December 5, 2019 07:08
Show Gist options
  • Save Jay-flow/e86ff41f58b4f534ab87e173f407b724 to your computer and use it in GitHub Desktop.
Save Jay-flow/e86ff41f58b4f534ab87e173f407b724 to your computer and use it in GitHub Desktop.
package io.bagstation.bagstation;
import android.util.Log;
import android.app.Activity;
import android.os.Build;
import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.net.Uri;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.views.webview.ReactWebViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
import java.net.URISyntaxException;
@ReactModule(name = CustomWebViewManager.REACT_CLASS)
public class CustomWebViewManager extends ReactWebViewManager {
public static final String REACT_CLASS = "RCTCustomWebView";
protected static class CustomWebViewClient extends ReactWebViewClient {
protected ThemedReactContext reactContext;
protected Activity activity;
private static final String DEFAULT_REDIRECT_URL_WHEN_SUCCESS = "https://service.iamport.kr/payments/success";
private static final String DEFAULT_REDIRECT_URL_WHEN_FAILURE = "https://service.iamport.kr/payments/fail";
public CustomWebViewClient(ThemedReactContext reactContext, Activity activity) {
this.reactContext = reactContext;
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isPaymentOver(url)) { // 결제시도가 종료된 후, 콜백이 설정되었으면, 리액트 네이티브로 event dispatch
reactContext
.getJSModule(RCTDeviceEventEmitter.class)
.emit("message", url);
return false;
}
if (isUrlStartsWithProtocol(url)) return false;
Intent intent = null;
try {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); // Intent URI 처리
if (intent == null) return false;
startNewActivity(intent.getDataString());
return true;
} catch (URISyntaxException e) {
return false;
} catch (ActivityNotFoundException e) { // PG사에서 호출하는 url에 package 정보가 없는 경우
String scheme = intent.getScheme();
if (isPaymentSchemeNotFound(scheme)) return true;
String packageName = intent.getPackage();
if (packageName == null) return false;
startNewActivity("market://details?id=" + packageName);
return true;
}
}
/* url이 https, http 또는 javascript로 시작하는지 체크 */
private boolean isUrlStartsWithProtocol(String url) {
if (url.startsWith("https://") || url.startsWith("http://") || url.startsWith("javascript:")) return true;
return false;
}
/* 결제가 종료되었는지 여부를 판단한다 */
private boolean isPaymentOver(String url) {
if (
url.startsWith(DEFAULT_REDIRECT_URL_WHEN_FAILURE) ||
url.startsWith(DEFAULT_REDIRECT_URL_WHEN_SUCCESS)
) return true;
return false;
}
protected void startNewActivity(String parsingUri) {
Uri uri = Uri.parse(parsingUri);
Intent newIntent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(newIntent);
}
/* ActivityNotFoundException에서 market 실행여부 확인 */
protected boolean isPaymentSchemeNotFound(String scheme) {
return false;
}
}
protected static class CustomWebView extends ReactWebView {
public CustomWebView(ThemedReactContext reactContext) {
super(reactContext);
}
}
@Override
protected ReactWebView createReactWebViewInstance(ThemedReactContext reactContext) {
return new CustomWebView(reactContext);
}
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
view.setWebViewClient(new CustomWebViewClient(reactContext, reactContext.getCurrentActivity()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment