Skip to content

Commit

Permalink
Merge pull request #3 from jdsdhp/develop
Browse files Browse the repository at this point in the history
Feature: Payment details, cancellation and completion
  • Loading branch information
jdsdhp authored Jan 28, 2024
2 parents cfc0525 + 71e6546 commit ab7258d
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal fun MainScreen(
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
) {

Spacer(modifier = Modifier.height(16.dp))

Text(
Expand Down Expand Up @@ -121,7 +122,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Currency") },
label = { Text(text = stringResource(R.string.currency)) },
value = uiState.createPayment.currency,
onValueChange = { viewModel.onCurrencyChanged(it) },
)
Expand All @@ -130,7 +131,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Cancel Url") },
label = { Text(text = stringResource(R.string.cancel_url)) },
value = uiState.createPayment.cancelUrl,
onValueChange = { viewModel.onCancelUrlChanged(it) },
)
Expand All @@ -139,7 +140,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Return Url") },
label = { Text(text = stringResource(R.string.return_url)) },
value = uiState.createPayment.returnUrl,
onValueChange = { viewModel.onReturnUrlChanged(it) },
)
Expand All @@ -148,7 +149,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Merchant Op Id (12 digits max)") },
label = { Text(text = stringResource(R.string.merchant_op_id_12_digits_max)) },
value = uiState.createPayment.merchantOpId,
onValueChange = { viewModel.onMerchantOpIdChanged(it) },
)
Expand All @@ -157,7 +158,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Description (Optional)") },
label = { Text(text = stringResource(R.string.description_optional)) },
value = uiState.createPayment.description,
onValueChange = { viewModel.onDescriptionChanged(it) },
)
Expand All @@ -166,7 +167,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Invoice Number (Optional)") },
label = { Text(text = stringResource(R.string.invoice_number_optional)) },
value = uiState.createPayment.invoiceNumber,
onValueChange = { viewModel.onInvoiceNumberChanged(it) },
)
Expand All @@ -175,7 +176,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Discount (Optional)") },
label = { Text(text = stringResource(R.string.discount_optional)) },
value = uiState.createPayment.discount.toString(),
onValueChange = { viewModel.onDiscountChanged(it) },
)
Expand All @@ -184,7 +185,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Tip (Optional)") },
label = { Text(text = stringResource(R.string.tip_optional)) },
value = uiState.createPayment.tip.toString(),
onValueChange = { viewModel.onTipChanged(it) },
)
Expand All @@ -193,7 +194,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Shipping (Optional)") },
label = { Text(text = stringResource(R.string.shipping_optional)) },
value = uiState.createPayment.shipping.toString(),
onValueChange = { viewModel.onShippingChanged(it) },
)
Expand All @@ -202,7 +203,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Buyer Identity Code (Optional)") },
label = { Text(text = stringResource(R.string.buyer_identity_code_optional)) },
value = uiState.createPayment.buyerIdentityCode,
onValueChange = { viewModel.onBuyerIdentityCodeChanged(it) },
)
Expand All @@ -211,7 +212,7 @@ internal fun MainScreen(

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Terminal Id (Optional)") },
label = { Text(text = stringResource(R.string.terminal_id_optional)) },
value = uiState.createPayment.terminalId,
onValueChange = { viewModel.onTerminalIdChanged(it) },
)
Expand All @@ -225,7 +226,7 @@ internal fun MainScreen(
Text(text = it.name + " (${it.quantity})")
Text(text = it.description)
Text(text = "$${it.price}")
Text(text = "Tax: ${it.tax}")
Text(text = stringResource(R.string.tax_dots, it.tax))
}
}
Spacer(modifier = Modifier.height(4.dp))
Expand All @@ -242,6 +243,84 @@ internal fun MainScreen(

HorizontalDivider()

Spacer(modifier = Modifier.height(16.dp))

Text(
text = stringResource(R.string.payment_details),
style = MaterialTheme.typography.titleLarge,
)

Spacer(modifier = Modifier.height(16.dp))

Card(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(8.dp)) {
uiState.payment?.let { payment ->
Text(
text = stringResource(
R.string.transaction_uuid_dots,
payment.transactionUuid
)
)
Text(text = stringResource(R.string.status_code_dots, payment.statusCode))
Text(text = stringResource(R.string.status_name_dots, payment.statusName))
Text(text = stringResource(R.string.description_dots, payment.description))
Text(text = stringResource(R.string.currency_dots, payment.currency))
Text(text = stringResource(R.string.created_at_dots, payment.createdAt))
Text(text = stringResource(R.string.updated_at_dots, payment.updatedAt))
Text(text = stringResource(R.string.total_price_dots, payment.totalPrice))
payment.links.forEach {
Spacer(modifier = Modifier.height(4.dp))
Text(text = "${it.rel} - ${it.method} - ${it.href}")
}
Spacer(modifier = Modifier.height(4.dp))
}
}
}

Spacer(modifier = Modifier.height(16.dp))

Button(onClick = { viewModel.onGetPaymentDetailClick() }) {
Text(text = stringResource(R.string.get_payment_details))
}

Spacer(modifier = Modifier.height(16.dp))



HorizontalDivider()

Spacer(modifier = Modifier.height(16.dp))

Text(
text = stringResource(R.string.cancel_payment),
style = MaterialTheme.typography.titleLarge,
)

Spacer(modifier = Modifier.height(16.dp))

Button(onClick = { viewModel.onCancelPaymentClick() }) {
Text(text = stringResource(R.string.cancel_payment))
}

Spacer(modifier = Modifier.height(16.dp))

HorizontalDivider()

Spacer(modifier = Modifier.height(16.dp))

Text(
text = stringResource(R.string.complete_payment),
style = MaterialTheme.typography.titleLarge,
)

Spacer(modifier = Modifier.height(16.dp))

Button(onClick = { viewModel.onCompletePaymentClick() }) {
Text(text = stringResource(R.string.complete_payment))
}

Spacer(modifier = Modifier.height(16.dp))

}

if (uiState.isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jdsdhp.enzona.payment.embedded.Enzona
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Item
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Payment
import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,6 +17,8 @@ import kotlinx.coroutines.launch
import java.util.regex.Pattern
import javax.inject.Inject

private const val TAG = "dev/tag"

@HiltViewModel
class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel() {

Expand All @@ -26,8 +29,9 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
val consumerKey: String = "",
val consumerSecret: String = "",
val merchantUUID: String = "",
val createPayment: CreatePayment = CreatePayment(),
val items: List<Item> = emptyList(),
val createPayment: CreatePayment = CreatePayment(),
val payment: Payment? = null,
)

private val _uiState: MutableStateFlow<UiState> = MutableStateFlow(UiState())
Expand All @@ -52,7 +56,7 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
_uiState.update {
it.copy(
items = listOf(
Item(
/*Item(
quantity = 2,
name = "Mango",
description = "Product One Description",
Expand All @@ -65,13 +69,13 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
description = "Product Two Description",
price = 2.0,
tax = 2.0,
),
),*/
Item(
quantity = 1,
name = "Guava",
description = "Product Three Description",
price = 3.0,
tax = 1.0,
price = 1.0,
tax = 0.0,
),
),
)
Expand Down Expand Up @@ -233,7 +237,7 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
)
when (val res = enzona.authenticate()) {
is ResultValue.Success -> {
Log.d("dev/tag", "onAuthButtonClick: Success = $res")
Log.d(TAG, "onAuthButtonClick: Success = $res")
_uiState.update {
it.copy(
isLoading = false,
Expand All @@ -243,7 +247,7 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
}

is ResultValue.Error -> {
Log.d("dev/tag", "onAuthButtonClick: Error = $res")
Log.d(TAG, "onAuthButtonClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
Expand Down Expand Up @@ -274,17 +278,116 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
items = _uiState.value.items,
)) {
is ResultValue.Success -> {
Log.d("dev/tag", "onCreatePaymentClick: Success = $res")
Log.d(TAG, "onCreatePaymentClick: Success = $res")
_uiState.update {
it.copy(
payment = res.data,
isLoading = false,
textMessage = "Payment created successfully!",
)
}
}

is ResultValue.Error -> {
Log.d("dev/tag", "onCreatePaymentClick: Error = $res")
Log.d(TAG, "onCreatePaymentClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
error = res.exception.toString(),
)
}
}
}
}
}

fun onGetPaymentDetailClick() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, textMessage = null, error = null) }
when (val res = enzona.getPaymentDetails(
transactionUuid = _uiState.value.payment?.transactionUuid ?: "",
)) {
is ResultValue.Success -> {
Log.d(TAG, "onGetPaymentDetailClick: Success = $res")
_uiState.update {
it.copy(
payment = res.data.copy(
links = _uiState.value.payment?.links ?: emptyList()
),
isLoading = false,
textMessage = "Payment details updated!",
)
}
}

is ResultValue.Error -> {
Log.d(TAG, "onGetPaymentDetailClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
error = res.exception.toString(),
)
}
}
}
}
}

fun onCancelPaymentClick() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, textMessage = null, error = null) }
when (val res = enzona.cancelPayment(
transactionUuid = _uiState.value.payment?.transactionUuid ?: "",
)) {
is ResultValue.Success -> {
Log.d(TAG, "oCancelPaymentClick: Success = $res")
_uiState.update {
it.copy(
payment = it.payment?.copy(
updatedAt = res.data.updateAt,
statusCode = res.data.statusCode,
statusName = res.data.statusName,
),
isLoading = false,
textMessage = "Payment canceled successfully!",
)
}
}

is ResultValue.Error -> {
Log.d(TAG, "oCancelPaymentClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
error = res.exception.toString(),
)
}
}
}
}
}

fun onCompletePaymentClick() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, textMessage = null, error = null) }
when (val res = enzona.completePayment(
transactionUuid = _uiState.value.payment?.transactionUuid ?: "",
)) {
is ResultValue.Success -> {
Log.d(TAG, "onCompletePaymentClick: Success = $res")
_uiState.update {
it.copy(
payment = res.data.copy(
links = _uiState.value.payment?.links ?: emptyList()
),
isLoading = false,
textMessage = "Payment completed successfully!",
)
}
}

is ResultValue.Error -> {
Log.d(TAG, "onCompletePaymentClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
Expand Down
Loading

0 comments on commit ab7258d

Please sign in to comment.