Created
February 5, 2021 15:24
-
-
Save jaeho0613/51b63da745e2b9f05b719a3453c012c4 to your computer and use it in GitHub Desktop.
Kakao Pay - PayActivity.java (2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jaeho; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.webkit.WebChromeClient; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParser; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class PayActivity extends AppCompatActivity { | |
// ''''' 생략 '''''' // | |
public class MyWebViewClient extends WebViewClient { | |
// ''''' 생략 '''''' // | |
// 결제 요청 단계 - 통신을 받을 Response 변수 | |
Response.Listener<String> approvalResponse = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
Log.e("Debug", response); | |
} | |
}; | |
// 결제 요청 단계 - 통신을 넘겨줄 Request 변수 | |
StringRequest approvalRequest = new StringRequest(Request.Method.POST, "https://kapi.kakao.com/v1/payment/approve", approvalResponse, errorListener) { | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
Map<String, String> params = new HashMap<>(); | |
params.put("cid", "TC0ONETIME"); | |
params.put("tid", tidPin); | |
params.put("partner_order_id", "1001"); | |
params.put("partner_user_id", "gorany"); | |
params.put("pg_token", pgToken); | |
params.put("total_amount", productPrice); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> headers = new HashMap<>(); | |
headers.put("Authorization", "KakaoAK " + "dfda30650082a405be23fce878038ad4"); | |
return headers; | |
} | |
}; | |
// URL 변경시 발생 이벤트 | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
Log.e("Debug", "url" + url); | |
if (url != null && url.contains("pg_token=")) { | |
String pg_Token = url.substring(url.indexOf("pg_token=") + 9); | |
pgToken = pg_Token; | |
requestQueue.add(approvalRequest); | |
} else if (url != null && url.startsWith("intent://")) { | |
try { | |
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | |
Intent existPackage = getPackageManager().getLaunchIntentForPackage(intent.getPackage()); | |
if (existPackage != null) { | |
startActivity(intent); | |
} | |
return true; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
view.loadUrl(url); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment