Skip to content

Commit

Permalink
Feature: Payment cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsdhp committed Jan 28, 2024
1 parent 9b31f06 commit 12f0737
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal fun MainScreen(
Spacer(modifier = Modifier.height(16.dp))

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

Expand All @@ -78,14 +78,19 @@ internal fun MainScreen(
Card(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(8.dp)) {
uiState.payment?.let { payment ->
Text(text = "Transaction UUID: ${payment.transactionUuid}")
Text(text = "Status Code: ${payment.statusCode}")
Text(text = "Status Name: ${payment.statusName}")
Text(text = "Description: ${payment.description}")
Text(text = "Currency: ${payment.currency}")
Text(text = "Created At: ${payment.createdAt}")
Text(text = "Updated At: ${payment.updatedAt}")
Text(text = "Total Price: ${payment.totalPrice}")
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}")
Expand All @@ -97,8 +102,8 @@ internal fun MainScreen(

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

Button(onClick = { viewModel.onGePaymentDetailClick() }) {
Text(text = "Get Payment Details")
Button(onClick = { viewModel.onGetPaymentDetailClick() }) {
Text(text = stringResource(R.string.get_payment_details))
}

Spacer(modifier = Modifier.height(16.dp))
Expand Down Expand Up @@ -160,7 +165,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 @@ -169,7 +174,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 @@ -178,7 +183,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 @@ -187,7 +192,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 @@ -196,7 +201,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 @@ -205,7 +210,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 @@ -214,7 +219,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 @@ -223,7 +228,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 @@ -232,7 +237,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 @@ -241,7 +246,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 @@ -250,7 +255,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 @@ -264,7 +269,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 @@ -281,6 +286,21 @@ internal fun MainScreen(

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))

}

if (uiState.isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
val isLoading: Boolean = false,
val error: String? = null,
val textMessage: String? = null,
val consumerKey: String = "",
val consumerSecret: String = "",
val merchantUUID: String = "",
val consumerKey: String = "81go6n9Vz1gcSL2nK5mfZxJzDRwa",
val consumerSecret: String = "MLZucMGr3z9k5kqp49jvwJCrWU4a",
val merchantUUID: String = "2a5d8dfd49794387b408d2168a461da5",
val items: List<Item> = emptyList(),
val createPayment: CreatePayment = CreatePayment(),
val payment: Payment? = null,
Expand Down Expand Up @@ -301,7 +301,7 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
}
}

fun onGePaymentDetailClick() {
fun onGetPaymentDetailClick() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, textMessage = null, error = null) }
when (val res = enzona.getPaymentDetails(
Expand Down Expand Up @@ -331,4 +331,38 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
}
}

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(),
)
}
}
}
}
}

}
23 changes: 23 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,27 @@
<string name="authenticate">Authenticate</string>
<string name="create_payment">Create Payment</string>
<string name="accept">Accept</string>
<string name="cancel_payment">Cancel Payment</string>
<string name="payment_details">Payment Details</string>
<string name="transaction_uuid_dots">Transaction UUID: %1$s</string>
<string name="status_code_dots">Status Code: %1$s</string>
<string name="status_name_dots">Status Name: %1$s</string>
<string name="description_dots">Description: %1$s</string>
<string name="currency_dots">Currency: %1$s</string>
<string name="created_at_dots">Created At: %1$s</string>
<string name="updated_at_dots">Updated At: %1$s</string>
<string name="total_price_dots">Total Price: %1$s</string>
<string name="get_payment_details">Get Payment Details</string>
<string name="currency">Currency</string>
<string name="cancel_url">Cancel Url</string>
<string name="return_url">Return Url</string>
<string name="merchant_op_id_12_digits_max">Merchant Op Id (12 digits max)</string>
<string name="description_optional">Description (Optional)</string>
<string name="invoice_number_optional">Invoice Number (Optional)</string>
<string name="discount_optional">Discount (Optional)</string>
<string name="tip_optional">Tip (Optional)</string>
<string name="shipping_optional">Shipping (Optional)</string>
<string name="buyer_identity_code_optional">Buyer Identity Code (Optional)</string>
<string name="terminal_id_optional">Terminal Id (Optional)</string>
<string name="tax_dots">Tax: %1$s</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jdsdhp.enzona.payment.embedded

import com.github.jdsdhp.enzona.payment.embedded.domain.model.CancelStatus
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.domain.model.Token
Expand Down Expand Up @@ -77,4 +78,11 @@ interface Enzona {
*/
suspend fun getPaymentDetails(transactionUuid: String): ResultValue<Payment>

/**
* Suspend function to cancel a payment using transaction UUID.
* @param transactionUuid The UUID of the transaction to be canceled.
* @return ResultValue containing the cancellation status.
*/
suspend fun cancelPayment(transactionUuid: String): ResultValue<CancelStatus>

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jdsdhp.enzona.payment.embedded

import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.AuthRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.PaymentRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.model.CancelStatus
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.domain.model.Token
Expand Down Expand Up @@ -80,4 +81,10 @@ internal class EnzonaImpl @Inject constructor(
transactionUuid = transactionUuid,
)

override suspend fun cancelPayment(transactionUuid: String): ResultValue<CancelStatus> =
paymentRemoteDatasource.cancelPayment(
token = token?.accessToken ?: "",
transactionUuid = transactionUuid,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasour
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.request.AmountDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.request.DetailsDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.request.create.CreatePaymentDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.cancel.CancelResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.create.PaymentResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.mapper.asData
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.mapper.asDomain
import com.github.jdsdhp.enzona.payment.embedded.di.IoDispatcher
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.PaymentRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.RemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.model.CancelStatus
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
Expand Down Expand Up @@ -100,5 +102,19 @@ internal class PaymentRemoteDatasourceImpl @Inject constructor(
}
}

}
override suspend fun cancelPayment(
token: String,
transactionUuid: String,
): ResultValue<CancelStatus> = withContext(dispatcher) {
remoteDatasource.call {
val res = okHttpClient.post(
fullUrl = "${Enzona.ApiUrl.OFFICIAL.url}/${Enzona.ApiUrl.OFFICIAL_PAYMENT_ENDPOINT.url}/$transactionUuid/cancel",
mediaTypeContent = "application/json",
content = "",
headers = mapOf("Authorization" to "Bearer $token"),
)
gson.fromJson(res.body?.string(), CancelResponseDto::class.java).asDomain()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ internal data class CancelResponseDto(
@SerializedName("status_denom") val statusName: String,
@SerializedName("transaction_denom") val transactionName: String,
@SerializedName("transaction_uuid") val transactionUuid: String,
@SerializedName("update_at") val updateAt: String,
@SerializedName("updated_at") val updateAt: String,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ internal data class CompleteResponseDto(
@SerializedName("status_code") val statusCode: String,
@SerializedName("terminal_id") val terminalId: String,
@SerializedName("transaction_uuid") val transactionUuid: String,
@SerializedName("update_at") val updateAt: String,
@SerializedName("updated_at") val updateAt: String,
)
Loading

0 comments on commit 12f0737

Please sign in to comment.