update
This commit is contained in:
@@ -10,7 +10,8 @@ class PaymentResultImpl @Inject constructor(
|
|||||||
) : PaymentResult {
|
) : PaymentResult {
|
||||||
|
|
||||||
override fun post(paymentResultEntity: PaymentResultEntity?): String {
|
override fun post(paymentResultEntity: PaymentResultEntity?): String {
|
||||||
return "window.WebV.onPaymentResult(${gson.toJson(paymentResultEntity)});"
|
return "window.WebV.onPaymentResult();"
|
||||||
|
// return "window.WebV.onPaymentResult(${gson.toJson(paymentResultEntity)});"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,5 +3,4 @@ package com.example.core.model
|
|||||||
enum class PaymentResultStatus {
|
enum class PaymentResultStatus {
|
||||||
OK,
|
OK,
|
||||||
ERROR
|
ERROR
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import android.webkit.WebViewClient
|
import android.webkit.WebViewClient
|
||||||
|
import android.webkit.WebSettings
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -37,6 +38,8 @@ fun PspWebView(
|
|||||||
onPayClick: (amount: Long, id: String?) -> Unit,
|
onPayClick: (amount: Long, id: String?) -> Unit,
|
||||||
onDeviceInfo: () -> String,
|
onDeviceInfo: () -> String,
|
||||||
onPrintClick: (printEntities: List<PrintEntity>) -> Unit,
|
onPrintClick: (printEntities: List<PrintEntity>) -> Unit,
|
||||||
|
onWebViewReady: (WebView) -> Unit = {},
|
||||||
|
onPageFinished: (WebView) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val canGoBack = remember { mutableStateOf(false) }
|
val canGoBack = remember { mutableStateOf(false) }
|
||||||
@@ -81,6 +84,7 @@ fun PspWebView(
|
|||||||
|
|
||||||
DisposableEffect(Unit) {
|
DisposableEffect(Unit) {
|
||||||
webView = WebViewManager.getWebView(context)
|
webView = WebViewManager.getWebView(context)
|
||||||
|
webView?.let(onWebViewReady)
|
||||||
onDispose {
|
onDispose {
|
||||||
webView?.let { WebViewManager.releaseWebView(it) }
|
webView?.let { WebViewManager.releaseWebView(it) }
|
||||||
}
|
}
|
||||||
@@ -99,12 +103,20 @@ fun PspWebView(
|
|||||||
override fun onPageFinished(view: WebView?, url: String?) {
|
override fun onPageFinished(view: WebView?, url: String?) {
|
||||||
super.onPageFinished(view, url)
|
super.onPageFinished(view, url)
|
||||||
canGoBack.value = view?.canGoBack() ?: false
|
canGoBack.value = view?.canGoBack() ?: false
|
||||||
|
view?.let(onPageFinished)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settings.apply {
|
||||||
|
@SuppressLint("SetJavaScriptEnabled")
|
||||||
|
javaScriptEnabled = true
|
||||||
|
domStorageEnabled = true
|
||||||
|
cacheMode = WebSettings.LOAD_NO_CACHE
|
||||||
|
allowFileAccess = true // Needed for Service Worker
|
||||||
|
}
|
||||||
|
clearCache(true)
|
||||||
|
|
||||||
addJavascriptInterface(bridge, "NativeBridge")
|
addJavascriptInterface(bridge, "NativeBridge")
|
||||||
|
|
||||||
loadUrl(url)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
update = {
|
update = {
|
||||||
|
|||||||
@@ -157,8 +157,9 @@ class IapPosService @Inject constructor(
|
|||||||
): PaymentResultEntity {
|
): PaymentResultEntity {
|
||||||
val result = activityResult.data?.getStringExtra("Result")
|
val result = activityResult.data?.getStringExtra("Result")
|
||||||
|
|
||||||
|
|
||||||
if (result.isNullOrEmpty()) {
|
if (result.isNullOrEmpty()) {
|
||||||
throw Exception("POS app returned no result.")
|
throw Exception("پرداخت کنسل شد")
|
||||||
}
|
}
|
||||||
return try {
|
return try {
|
||||||
val type = object : TypeToken<Map<String, Any>>() {}.type
|
val type = object : TypeToken<Map<String, Any>>() {}.type
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ android {
|
|||||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||||
"proguard-rules.pro"
|
"proguard-rules.pro"
|
||||||
)
|
)
|
||||||
|
signingConfig = signingConfigs.getByName("debug")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.stage_app.screen
|
package com.example.stage_app.screen
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
@@ -7,6 +8,8 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
@@ -17,7 +20,8 @@ import com.example.core.getDeviceName
|
|||||||
import com.example.design_system.PspWebView
|
import com.example.design_system.PspWebView
|
||||||
import com.example.stage_app.viewmodel.StageViewModel
|
import com.example.stage_app.viewmodel.StageViewModel
|
||||||
|
|
||||||
private const val tisIp = "192.168.128.73:5000"
|
private const val tisWebUrl = "http://192.168.128.73:5000"
|
||||||
|
private const val stageWebViewTag = "StageWebViewScreen"
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun StageWebViewScreen(
|
fun StageWebViewScreen(
|
||||||
@@ -28,6 +32,18 @@ fun StageWebViewScreen(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
var webView: WebView? by remember { mutableStateOf(null) }
|
var webView: WebView? by remember { mutableStateOf(null) }
|
||||||
var currentPaymentId by rememberSaveable { mutableStateOf<String?>(null) }
|
var currentPaymentId by rememberSaveable { mutableStateOf<String?>(null) }
|
||||||
|
var isPaymentProcessing by rememberSaveable { mutableStateOf(false) }
|
||||||
|
var isPrinting by rememberSaveable { mutableStateOf(false) }
|
||||||
|
var initialCallbackSent by rememberSaveable { mutableStateOf(false) }
|
||||||
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
val deviceInfo by remember {
|
||||||
|
mutableStateOf(
|
||||||
|
viewModel.deviceInfoJson(
|
||||||
|
androidId = getAndroidId(context),
|
||||||
|
getDeviceName()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// launcher to handle POS app result
|
// launcher to handle POS app result
|
||||||
val posLauncher = rememberLauncherForActivityResult(
|
val posLauncher = rememberLauncherForActivityResult(
|
||||||
@@ -36,36 +52,53 @@ fun StageWebViewScreen(
|
|||||||
val paymentResultEntity = viewModel.paymentResult(
|
val paymentResultEntity = viewModel.paymentResult(
|
||||||
id = currentPaymentId,
|
id = currentPaymentId,
|
||||||
activityResult = result
|
activityResult = result
|
||||||
|
|
||||||
)
|
)
|
||||||
webView?.post {
|
val jsResult = viewModel.postPaymentResult(paymentResultEntity)
|
||||||
webView?.evaluateJavascript(
|
val currentWebView = webView
|
||||||
viewModel.postPaymentResult(paymentResultEntity = paymentResultEntity),
|
if (currentWebView == null) {
|
||||||
null
|
Log.w(stageWebViewTag, "Skipping onPaymentResult JS callback: WebView is not ready yet.")
|
||||||
)
|
} else {
|
||||||
|
currentWebView.post {
|
||||||
|
currentWebView.evaluateJavascript(jsResult, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
isPaymentProcessing = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PspWebView(
|
PspWebView(
|
||||||
url = tisIp,
|
url = tisWebUrl,
|
||||||
webView = webView,
|
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
onWebView = { updatedWebView -> webView = updatedWebView },
|
|
||||||
onPayClick = { amount: Long, id: String? ->
|
onPayClick = { amount: Long, id: String? ->
|
||||||
|
if (isPaymentProcessing) return@PspWebView
|
||||||
|
isPaymentProcessing = true
|
||||||
currentPaymentId = id
|
currentPaymentId = id
|
||||||
viewModel.pay(amount)
|
viewModel.pay(amount)
|
||||||
.also(posLauncher::launch)
|
.also(posLauncher::launch)
|
||||||
},
|
},
|
||||||
onPrintClick = { printEntities ->
|
onPrintClick = { printEntities ->
|
||||||
viewModel.print(printEntities)
|
if (isPrinting) return@PspWebView
|
||||||
|
isPrinting = true
|
||||||
|
coroutineScope.launch {
|
||||||
|
try {
|
||||||
|
viewModel.print(printEntities)
|
||||||
|
} finally {
|
||||||
|
isPrinting = false
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onDeviceInfo = {
|
onDeviceInfo = {
|
||||||
viewModel.deviceInfoJson(
|
deviceInfo
|
||||||
androidId = getAndroidId(context),
|
},
|
||||||
getDeviceName()
|
onWebViewReady = { readyWebView ->
|
||||||
)
|
webView = readyWebView
|
||||||
|
},
|
||||||
|
onPageFinished = { loadedWebView ->
|
||||||
|
if (!initialCallbackSent) {
|
||||||
|
loadedWebView.evaluateJavascript(viewModel.postPaymentResult(null), null)
|
||||||
|
initialCallbackSent = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,10 +31,17 @@ class StageViewModel @Inject constructor(
|
|||||||
fun paymentResult(
|
fun paymentResult(
|
||||||
id: String?,
|
id: String?,
|
||||||
activityResult: ActivityResult
|
activityResult: ActivityResult
|
||||||
): PaymentResultEntity? {
|
): PaymentResultEntity {
|
||||||
return pspService.decodePosResponse(
|
return PaymentResultEntity(
|
||||||
id = id,
|
id = id,
|
||||||
activityResult = activityResult
|
status = com.example.core.model.PaymentResultStatus.ERROR,
|
||||||
|
terminalId = "123456",
|
||||||
|
stan = "123456",
|
||||||
|
rrn = "123456",
|
||||||
|
responseCode = "01",
|
||||||
|
customerCardNO = "1234567890123456",
|
||||||
|
transactionDateTime = "14030101120000",
|
||||||
|
description = "تراکنش ناموفق"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user