Skip to content

Commit

Permalink
Feature: Get payment details
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsdhp committed Jan 28, 2024
1 parent c2d73fc commit 9b31f06
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,45 @@ internal fun MainScreen(
) {
Spacer(modifier = Modifier.height(16.dp))

Text(
text = "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 = "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}")
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.onGePaymentDetailClick() }) {
Text(text = "Get Payment Details")
}

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

HorizontalDivider()

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

Text(
text = stringResource(id = R.string.authenticate),
style = MaterialTheme.typography.titleLarge,
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 Down Expand Up @@ -70,8 +74,8 @@ class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel(
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,48 @@ 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 onGePaymentDetailClick() {
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, "onGePaymentDetailClick: Success = $res")
_uiState.update {
it.copy(
payment = res.data.copy(links = res.data.links),
isLoading = false,
textMessage = "Payment details updated!",
)
}
}

is ResultValue.Error -> {
Log.d(TAG, "onGePaymentDetailClick: Error = $res")
_uiState.update {
it.copy(
isLoading = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ interface Enzona {
items: List<Item>,
): ResultValue<Payment>

/**
* Suspend function to get payment details using transaction UUID.
* @param transactionUuid The UUID of the transaction.
* @return ResultValue containing the payment information.
*/
suspend fun getPaymentDetails(transactionUuid: String): ResultValue<Payment>

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ internal class EnzonaImpl @Inject constructor(
terminalId = terminalId,
)

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

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource

import com.github.jdsdhp.enzona.payment.embedded.Enzona
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.util.get
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.util.post
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.create.CreatePaymentResponseDto
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
Expand Down Expand Up @@ -82,7 +83,20 @@ internal class PaymentRemoteDatasourceImpl @Inject constructor(
headers = mapOf("Authorization" to "Bearer $token"),
)

gson.fromJson(res.body?.string(), CreatePaymentResponseDto::class.java).asDomain()
gson.fromJson(res.body?.string(), PaymentResponseDto::class.java).asDomain()
}
}

override suspend fun getPaymentDetails(
token: String,
transactionUuid: String,
): ResultValue<Payment> = withContext(dispatcher) {
remoteDatasource.call {
val res = okHttpClient.get(
fullUrl = "${Enzona.ApiUrl.OFFICIAL.url}/${Enzona.ApiUrl.OFFICIAL_PAYMENT_ENDPOINT.url}/$transactionUuid",
headers = mapOf("Authorization" to "Bearer $token"),
)
gson.fromJson(res.body?.string(), PaymentResponseDto::class.java).asDomain()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ internal fun OkHttpClient.post(
return newCall(request).execute()
}

/**
* Executes an HTTP GET request using OkHttpClient.
* @param fullUrl The full URL for the GET request.
* @param headers A map of headers to be included in the request.
* @return The response of the GET request.
*/
internal fun OkHttpClient.get(
fullUrl: String,
headers: Map<String, String>,
): Response {
val request: Request = Request.Builder()
.url(fullUrl)
.apply { headers.forEach { addHeader(it.key, it.value) } }
.build()
return newCall(request).execute()
}

/**
* Configures OkHttpClient.Builder to ignore SSL/TLS certificates.
* Use with caution and only in scenarios where certificate validation is not required.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.resp
import com.google.gson.annotations.SerializedName

@Keep
internal data class CreatePaymentResponseDto(
internal data class PaymentResponseDto(
@SerializedName("amount") val amount: AmountResponseDto,
@SerializedName("created_at") val createdAt: String,
@SerializedName("currency") val currency: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.requ
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.request.ItemDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.ItemResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.LinkResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.create.CreatePaymentResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.create.PaymentResponseDto
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Amount
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Details
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Item
Expand Down Expand Up @@ -64,7 +64,7 @@ internal fun ItemResponseDto.asDomain(): Item =
internal fun LinkResponseDto.asDomain(): Link =
Link(rel = rel, method = method, href = href)

internal fun CreatePaymentResponseDto.asDomain() = Payment(
internal fun PaymentResponseDto.asDomain() = Payment(
transactionUuid,
createdAt,
updateAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ internal interface PaymentRemoteDatasource {
items: List<Item>,
): ResultValue<Payment>

/**
* Suspend function to get payment details remotely.
* @param token The authentication token.
* @param transactionUuid The UUID of the transaction.
* @return ResultValue containing the payment details.
*/
suspend fun getPaymentDetails(token: String, transactionUuid: String): ResultValue<Payment>

}

0 comments on commit 9b31f06

Please sign in to comment.