diff options
Diffstat (limited to 'wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt')
-rw-r--r-- | wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt | 99 |
1 files changed, 92 insertions, 7 deletions
diff --git a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt index b3bbc5e..e63552b 100644 --- a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt +++ b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/PaymentActivity.kt @@ -1,21 +1,39 @@ package ch.bfh.habej2.wallee_c2ec import android.os.Bundle -import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Button import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text +import androidx.compose.material3.TextField import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import ch.bfh.habej2.wallee_c2ec.client.taler.BankIntegrationClient +import ch.bfh.habej2.wallee_c2ec.client.wallee.WalleeResponseHandler +import ch.bfh.habej2.wallee_c2ec.config.TalerBankIntegrationConfig import ch.bfh.habej2.wallee_c2ec.ui.theme.Walleec2ecTheme +import com.wallee.android.till.sdk.ApiClient +import com.wallee.android.till.sdk.data.LineItem +import com.wallee.android.till.sdk.data.Transaction +import com.wallee.android.till.sdk.data.TransactionProcessingBehavior +import java.math.BigDecimal +import java.util.Currency +import java.util.Optional -class PaymentActivity : ComponentActivity() { +class PaymentActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + // TODO retrieve WithdrawalViewModel here + // maybe something like savedStateRegistry.getSavedStateProvider("current-withdrawal") + val model = WithdrawalViewModel(BankIntegrationClient( + TalerBankIntegrationConfig("TestExchange", "http://localhost:8082/c2ec", "Wallee-1", "secret"))) + val client = ApiClient(WalleeResponseHandler()) + setContent { Walleec2ecTheme { // A surface container using the 'background' color from the theme @@ -23,20 +41,87 @@ class PaymentActivity : ComponentActivity() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - // TODO use wallee sdk here for payment. Column( horizontalAlignment = Alignment.CenterHorizontally ) { Text(text = "present card, trigger payment") - - Button(onClick = { finish() }) { - // TODO: abort payment here - Text(text = "back") + + TextField( + value = "", + onValueChange = { + val optAmount = parseAmount(it) + if (optAmount.isPresent) { + model.updateAmount(optAmount.get()) + } + }, + label = { Text(text = "Enter amount") }, + placeholder = { Text(text = "amount") } + ) + + Button(enabled = false, onClick = { + val withdrawalAmount = LineItem + .ListBuilder( + model.uiState.encodedWopid, + BigDecimal("${model.uiState.amount.value}.${model.uiState.amount.frac}") + ) + .build() + + val transaction = Transaction.Builder(withdrawalAmount) + .setCurrency(Currency.getInstance(model.uiState.currency)) + .setInvoiceReference(model.uiState.encodedWopid) + .setMerchantReference(model.uiState.encodedWopid) + .setTransactionProcessingBehavior(TransactionProcessingBehavior.COMPLETE_IMMEDIATELY) + .build() + + try { + client.authorizeTransaction(transaction) + } catch (e: Exception) { + e.printStackTrace() + } + }) { + Text(text = "") + } + + Button(onClick = { + model.withdrawalOperationFailed(applicationContext) + finish() + }) { + Text(text = "abort") } } } } } } + + /** + * Format expected X[.X], X an integer + */ + private fun parseAmount(inp: String): Optional<Amount> { + + val points = inp.count { it == '.' } + if (points > 1) { + return Optional.empty() + } + + if (points == 1) { + val valueStr = inp.split(".")[0] + val fracStr = inp.split(".")[1] + return try { + val value = valueStr.toInt() + val frac = fracStr.toInt() + Optional.of(Amount(value, frac)) + } catch (ex: NumberFormatException) { + Optional.empty() + } + } + + return try { + val value = inp.toInt() + Optional.of(Amount(value, 0)) + } catch (ex: NumberFormatException) { + Optional.empty() + } + } } |