Skip to content

Commit

Permalink
feat : MobileWebMode 때 url 변경을 체크 가능케함
Browse files Browse the repository at this point in the history
1. IamPortMobileModeWebViewClient 상속해서 사용
2. LiveData mobileWebModeShouldOverrideUrlLoading observe 해서 사용
refactor : example 앱 gradle update
fix : bankpay 앱 종료시 데이터 nullable 처리
  • Loading branch information
kjh5833 committed Jul 13, 2021
1 parent 2f952e4 commit 8b770eb
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 91 deletions.
38 changes: 19 additions & 19 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,42 @@ dependencies {
// implementation fileTree(dir: "libs", include: ["*.jar"])
// implementation fileTree(dir: "libs", include: ["*.aar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$Versions.kotlin_stdlib_jdk"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

// Android Support/Architecture
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation "com.google.android.material:material:1.3.0"
implementation "com.google.android.material:material:1.4.0"

// multidex
implementation 'androidx.multidex:multidex:2.0.1'

// Gson
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.code.gson:gson:2.8.7'

// Logger
// implementation 'com.orhanobut:logger:2.2.0'

implementation "io.insert-koin:koin-androidx-scope:$Versions.koin_version"
// Koin AndroidX ViewModel features
implementation "io.insert-koin:koin-androidx-viewmodel:$Versions.koin_version"
// Koin AndroidX Fragment features
implementation "io.insert-koin:koin-androidx-fragment:$Versions.koin_version"
// Koin AndroidX WorkManager
implementation "io.insert-koin:koin-androidx-workmanager:$Versions.koin_version"
// Koin AndroidX Jetpack Compose
// implementation "io.insert-koin:koin-androidx-compose:$Versions.koin_version"
// Koin AndroidX Experimental features
implementation "io.insert-koin:koin-androidx-ext:$Versions.koin_version"
// implementation "io.insert-koin:koin-androidx-scope:$Versions.koin_version"
//// Koin AndroidX ViewModel features
// implementation "io.insert-koin:koin-androidx-viewmodel:$Versions.koin_version"
//// Koin AndroidX Fragment features
// implementation "io.insert-koin:koin-androidx-fragment:$Versions.koin_version"
//// Koin AndroidX WorkManager
// implementation "io.insert-koin:koin-androidx-workmanager:$Versions.koin_version"
//// Koin AndroidX Jetpack Compose
//// implementation "io.insert-koin:koin-androidx-compose:$Versions.koin_version"
//// Koin AndroidX Experimental features
// implementation "io.insert-koin:koin-androidx-ext:$Versions.koin_version"

implementation project(':sdk')
// implementation 'com.github.iamport:iamport-android:v0.0.6-dev15'
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/com/iamport/sampleapp/BaseApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package com.iamport.sampleapp

import android.app.Application
import com.iamport.sdk.domain.core.Iamport
import org.koin.android.ext.koin.androidContext
import org.koin.android.logger.AndroidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level


class BaseApplication : Application() {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/iamport/sampleapp/MyWebViewClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.iamport.sampleapp

import android.util.Log
import android.webkit.WebResourceRequest
import android.webkit.WebView
import com.iamport.sdk.domain.strategy.webview.IamPortMobileModeWebViewClient

open class MyWebViewClient : IamPortMobileModeWebViewClient() {

override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {

Log.i("MyWebViewClient", "updated webview url ${view?.url}")

return super.shouldOverrideUrlLoading(view, request)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.iamport.sampleapp.ViewModel
import com.iamport.sampleapp.MyWebViewClient
import com.iamport.sampleapp.databinding.WebViewModeFragmentBinding
import com.iamport.sdk.data.sdk.IamPortRequest
import com.iamport.sdk.data.sdk.PG
import com.iamport.sdk.data.sdk.PayMethod
import com.iamport.sdk.domain.core.Iamport
import com.iamport.sdk.domain.utils.CONST
import com.iamport.sdk.domain.utils.Util
import com.iamport.sdk.domain.utils.EventObserver
import java.util.*

/**
Expand All @@ -41,13 +35,22 @@ class MobileWebViewModeFragment : Fragment() {

override fun onStart() {
super.onStart()
if(!createdView) {
if (!createdView) {
return
}

// 모바일 웹 단독 모드
binding?.webview?.let {
it.loadUrl(CONST.PAYMENT_MOBILE_WEB_FILE_URL)

// 통상적인 경우의 custom webviewClient
it.webViewClient = MyWebViewClient()

// oreo 미만에서 url 변경만 보고 싶은경우
Iamport.mobileWebModeShouldOverrideUrlLoading()?.observe(this, EventObserver { uri ->
Log.i("SAMPLE", "changed url :: $uri")
})

// 모바일 웹 단독 모드
it.loadUrl(CONST.PAYMENT_MOBILE_WEB_FILE_URL)
// it.loadUrl("https://www.iamport.kr/demo") // 아임포트 데모 페이지
Iamport.pluginMobileWebSupporter(it) // 로컬 데모 페이지
createdView = false
Expand Down
1 change: 0 additions & 1 deletion sdk/src/main/assets/mobileweb.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ <h1 id="header-value"></h1>
buyer_postcode: '06018',
m_redirect_url: 'https://www.iamport.kr?orderId=order_uid_123456789#result',
// m_redirect_url: 'https://httpstat.us/200',
app_scheme: 'iamport',
}

IMP.request_pay(data, function(response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import com.orhanobut.logger.Logger
import org.koin.core.component.KoinApiExtension

@KoinApiExtension
class JsNativeInterface(val payment: Payment, val gson: Gson, private val bus: WebViewLiveDataEventBus, val evaluateJS: ((String) -> Unit)) : IamportKoinComponent {
class JsNativeInterface(val payment: Payment, val gson: Gson, val evaluateJS: ((String) -> Unit)) : IamportKoinComponent {
private val bus: WebViewLiveDataEventBus = WebViewLiveDataEventBus

/**
* 아임포트 JS SDK 에서 콜백 호출시에 해당 함수 동작
Expand Down
8 changes: 8 additions & 0 deletions sdk/src/main/java/com/iamport/sdk/domain/core/Iamport.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.iamport.sdk.domain.core

import android.app.Application
import android.net.Uri
import android.util.Log
import android.webkit.WebView
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -275,6 +276,13 @@ object Iamport {
impCallbackFunction?.invoke(iamPortResponse)
}

/**
* MobileWebMode 일 때, 웹뷰의 url 이 변경되면 값이 전달됨
*/
fun mobileWebModeShouldOverrideUrlLoading(): LiveData<Event<Uri>>? {
return iamportSdk?.mobileWebModeShouldOverrideUrlLoading()
}

/**
* 결제 요청
* @param ((IamPortApprove?) -> Unit)? : (옵셔널) 차이 최종 결제 요청전 콜백
Expand Down
5 changes: 2 additions & 3 deletions sdk/src/main/java/com/iamport/sdk/domain/di/appModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.iamport.sdk.domain.strategy.webview.NiceTransWebViewStrategy
import com.iamport.sdk.domain.strategy.webview.WebViewStrategy
import com.iamport.sdk.domain.utils.CONST
import com.iamport.sdk.domain.utils.NativeLiveDataEventBus
import com.iamport.sdk.domain.utils.WebViewLiveDataEventBus
import com.iamport.sdk.presentation.viewmodel.MainViewModel
import com.iamport.sdk.presentation.viewmodel.WebViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
Expand All @@ -22,12 +21,12 @@ import org.koin.dsl.module
@OptIn(KoinApiExtension::class)
val appModule = module {
viewModel { MainViewModel(get(), get()) }
viewModel { WebViewModel(get(), get()) }
viewModel { WebViewModel(get()) }
single { IamportReceiver() }
single(named("${CONST.KOIN_KEY}Gson")) { Gson() }

single { StrategyRepository() }
single { WebViewLiveDataEventBus() }
// single { WebViewLiveDataEventBus() }
single { NativeLiveDataEventBus() }

single { JudgeStrategy() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.iamport.sdk.domain.strategy.base.IStrategy
import com.iamport.sdk.domain.strategy.base.JudgeStrategy
import com.iamport.sdk.domain.strategy.chai.ChaiStrategy
import com.iamport.sdk.domain.strategy.webview.CertificationWebViewStrategy
import com.iamport.sdk.domain.strategy.webview.IamPortMobileModeWebViewClient
import com.iamport.sdk.domain.strategy.webview.NiceTransWebViewStrategy
import com.iamport.sdk.domain.strategy.webview.WebViewStrategy
import com.orhanobut.logger.Logger
Expand All @@ -20,9 +21,12 @@ class StrategyRepository : IamportKoinComponent {

val judgeStrategy: JudgeStrategy by inject() // 결제 중 BG 폴링하는 차이 전략
val chaiStrategy: ChaiStrategy by inject() // 결제 중 BG 폴링하는 차이 전략
var mobileWebModeStrategy: IamPortMobileModeWebViewClient? = null


private val webViewStrategy: WebViewStrategy by inject() // webview 사용하는 pg
private val niceTransWebViewStrategy: NiceTransWebViewStrategy by inject() //

private val niceTransWebViewStrategy: NiceTransWebViewStrategy by inject()

private val certificationWebViewStrategy: CertificationWebViewStrategy by inject() //

Expand All @@ -33,6 +37,11 @@ class StrategyRepository : IamportKoinComponent {
CHAI, NICE, WEB
}

fun init() {
chaiStrategy.init()
mobileWebModeStrategy = null
}

fun failSdkFinish(payment: Payment) {
when (getPaymentKinds(payment)) {
PaymentKinds.CHAI -> chaiStrategy.failFinish("사용자가 결제확인 서비스 종료하셨습니다")
Expand Down Expand Up @@ -67,13 +76,15 @@ class StrategyRepository : IamportKoinComponent {
} ?: run { return PaymentKinds.WEB } // default WEB
}

// for 결제요청
fun getWebViewStrategy(payment: Payment): IStrategy {
return when (getPaymentKinds(payment)) {
PaymentKinds.NICE -> niceTransWebViewStrategy
else -> webViewStrategy
}
}

// for webview mode inject
fun getWebViewClient(payment: Payment): WebViewClient {
return when (getPaymentKinds(payment)) {
PaymentKinds.NICE -> niceTransWebViewStrategy
Expand All @@ -85,4 +96,16 @@ class StrategyRepository : IamportKoinComponent {
return niceTransWebViewStrategy
}

fun getMobileWebModeClient(): IamPortMobileModeWebViewClient {
return mobileWebModeStrategy ?: run {
mobileWebModeStrategy = IamPortMobileModeWebViewClient()
mobileWebModeStrategy as IamPortMobileModeWebViewClient
}
}

fun updateMobileWebModeClient(client: IamPortMobileModeWebViewClient) {
mobileWebModeStrategy = client
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import androidx.annotation.RequiresApi
import com.google.gson.Gson
import com.iamport.sdk.data.sdk.IamPortResponse
import com.iamport.sdk.data.sdk.Payment
import com.iamport.sdk.domain.di.IamportKoinComponent
import com.iamport.sdk.domain.utils.CONST
import com.iamport.sdk.domain.utils.Event
import com.iamport.sdk.domain.utils.WebViewLiveDataEventBus
import com.orhanobut.logger.Logger
import com.orhanobut.logger.Logger.d
import org.koin.core.component.KoinApiExtension
import org.koin.core.component.inject
import org.koin.core.qualifier.named

@KoinApiExtension
open class BaseWebViewStrategy : WebViewClient(), IStrategy, IamportKoinComponent {
open class BaseWebViewStrategy : WebViewClient(), IStrategy {

protected val gson: Gson by inject(named("${CONST.KOIN_KEY}Gson"))
protected val bus: WebViewLiveDataEventBus by inject()
// protected val gson: Gson by inject(named("${CONST.KOIN_KEY}Gson"))
// protected val bus: WebViewLiveDataEventBus by inject()

protected val gson: Gson = Gson()
protected val bus: WebViewLiveDataEventBus = WebViewLiveDataEventBus

lateinit var payment: Payment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class JudgeStrategy : BaseStrategy(), IamportKoinComponent {
it.pg_provider == myPg
}
}
Logger.e("user :: $user")
Logger.d("user :: $user")

return when (user) {
null -> defUser.pg_provider?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
package com.iamport.sdk.domain.strategy.webview

import android.net.Uri
import android.os.Build
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import androidx.annotation.RequiresApi
import com.iamport.sdk.data.sdk.Payment
import com.iamport.sdk.domain.strategy.base.BaseWebViewStrategy
import com.iamport.sdk.domain.utils.Event
import com.iamport.sdk.domain.utils.Util
import com.orhanobut.logger.Logger.d
import org.koin.core.component.KoinApiExtension


@KoinApiExtension
open class CertificationWebViewStrategy : WebViewStrategy()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.iamport.sdk.domain.strategy.webview

import android.os.Build
import android.webkit.WebResourceRequest
import android.webkit.WebView
import androidx.annotation.RequiresApi
import com.iamport.sdk.domain.utils.Event
import com.orhanobut.logger.Logger

open class IamPortMobileModeWebViewClient : NiceTransWebViewStrategy() {

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {

request?.url?.let {
// bus.changeUrl.postValue(Event(it))
bus.changeUrl.value = (Event(it))
}

return super.shouldOverrideUrlLoading(view, request)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import com.iamport.sdk.data.nice.NiceBankpay
import com.iamport.sdk.data.sdk.ProvidePgPkg
import com.iamport.sdk.domain.utils.Event
import com.orhanobut.logger.Logger.*
import org.koin.core.component.KoinApiExtension
import java.net.URLDecoder
import java.nio.charset.StandardCharsets

@KoinApiExtension
class NiceTransWebViewStrategy : WebViewStrategy() {
open class NiceTransWebViewStrategy : WebViewStrategy() {

// private val niceApi: NiceApi by inject()

private var webView: WebView? = null
private lateinit var bankTid: String
private lateinit var niceTransUrl: String
private var bankTid: String = ""
private var niceTransUrl: String = ""

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
Expand All @@ -36,8 +34,8 @@ class NiceTransWebViewStrategy : WebViewStrategy() {
bankTid = it.getQueryParameter(NiceBankpay.USER_KEY).toString()
niceTransUrl = it.getQueryParameter(NiceBankpay.CALLBACKPARAM).toString()

makeBankPayData(it)?.let {
bus.niceTransRequestParam.postValue(Event(it)) // 뱅크페이 앱 열기
makeBankPayData(it)?.let { data ->
bus.niceTransRequestParam.postValue(Event(data)) // 뱅크페이 앱 열기
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import com.iamport.sdk.domain.strategy.base.BaseWebViewStrategy
import com.iamport.sdk.domain.utils.Event
import com.iamport.sdk.domain.utils.Util
import com.orhanobut.logger.Logger.d
import org.koin.core.component.KoinApiExtension


@KoinApiExtension
open class WebViewStrategy : BaseWebViewStrategy() {

override suspend fun doWork(payment: Payment) {
Expand Down
Loading

0 comments on commit 8b770eb

Please sign in to comment.