Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payments with Device Feature #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions core_localization/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<string name="customer_detail_menu_tickets">Tickets</string>

<string name="payment_name_surname">Name Surname</string>
<string name="payment_meter">Meter</string>
<string name="payment_device">Device</string>
<string name="payment_token">Token</string>
<string name="payment_customer">Customer Id</string>
<string name="payment_customer_hint">########</string>
Expand All @@ -86,7 +86,7 @@
<string name="payment_detail_amount">Amount</string>
<string name="payment_detail_token">Token</string>
<string name="payment_detail_payment_type">Payment Type</string>
<string name="payment_detail_meter">Meter</string>
<string name="payment_detail_device">Device</string>
<string name="payment_detail_date">Date</string>
<string name="payment_detail_status">Status</string>
<string name="payment_detail_status_success">Success</string>
Expand Down Expand Up @@ -126,6 +126,7 @@
<string name="appliance_to_sell_empty">You do not have any appliance to sell yet!</string>
<string name="appliance_empty_layout_content">You haven\'t sold any appliance yet</string>
<string name="appliance_empty_layout__with_customer_content">You haven\'t sold any appliance to %1s yet, \nwould you like to sell one?</string>
<string name="appliance_under_maintenance_layout_content">The appliance feature is undergoing maintenance. You will be notified once it\'s completed.</string>

<string name="ticket_detail_id">Ticket Id</string>
<string name="ticket_detail_description">Description</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,93 @@
package com.inensus.core_network

import com.google.gson.Gson
import com.inensus.core_network.model.Error

import com.google.gson.*
import com.inensus.core_network.model.ServiceError
import retrofit2.HttpException
import timber.log.Timber
import java.net.SocketTimeoutException
import com.google.gson.annotations.SerializedName
import java.lang.reflect.Type


fun Throwable.toServiceError(): ServiceError {
Timber.e(this)

return when (this) {
is HttpException -> parse(response()?.errorBody()?.string())
is SocketTimeoutException -> ServiceError(Error("Request has been timed out. Please check your connection and try again"))
else -> ServiceError(Error("An error occurred. Please try again later"))
is HttpException -> {
val errorBody = response()?.errorBody()?.string()
parse(errorBody)
}
is SocketTimeoutException ->ServiceError(ServiceError.Data(listOf("Request has been timed out. Please check your connection and try again"), 0))
else -> ServiceError(ServiceError.Data(listOf("An error occurred. Please try again later"), 0))
}
}

private fun parse(errorBody: String?): ServiceError =
try {
Gson().fromJson(errorBody, ServiceError::class.java)
} catch (e: Throwable) {
private fun parse(errorBody: String?): ServiceError {
return try {
if (!errorBody.isNullOrBlank()) {
val gson = GsonBuilder()
.registerTypeAdapter(ServiceError::class.java, ServiceErrorDeserializer())
.create()
val errorResponse = gson.fromJson(errorBody, ServiceError::class.java)
return errorResponse
} else {
// If error body is blank or null, create a default error
ServiceError(ServiceError.Data(listOf("An error occurred. Please try again later"), 0))
}
}catch (e: Throwable) {
Timber.e(e)
ServiceError(Error("An error occurred. Please try again later"))
}
// Handle other exceptions if necessary
ServiceError(ServiceError.Data(listOf("An error occurred. Please try again later"), 0))
}
}

class ServiceErrorDeserializer : JsonDeserializer<ServiceError> {
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): ServiceError {
val jsonObject = json?.asJsonObject
val data = jsonObject?.getAsJsonObject("data")
val messageElement = data?.get("message")

val message = when {
messageElement?.isJsonArray == true -> {
// If "message" is an array, handle it accordingly
parseMessageArray(messageElement.asJsonArray)
}
messageElement?.isJsonPrimitive == true -> {
// If "message" is a single string, convert it to a list
listOf(messageElement.asString)
}
else -> emptyList()
}

return ServiceError(ServiceError.Data(message, data?.get("status_code")?.asInt ?: 0))
}

private fun parseMessageArray(jsonArray: JsonArray): List<String> {
val messageList = mutableListOf<String>()

jsonArray.forEach { element ->
if (element.isJsonPrimitive) {
messageList.add(element.asString)
} else if (element.isJsonObject) {
// If the element is an object, extract specific fields or convert it to a string
val messageString = extractMessageFromJsonObject(element.asJsonObject)
messageList.add(messageString)
}
}

return messageList
}
}
private fun extractMessageFromJsonObject(jsonObject: JsonObject): String {
// Customize this logic based on your JSON structure
val file = jsonObject.getAsJsonPrimitive("file")?.asString ?: ""
val line = jsonObject.getAsJsonPrimitive("line")?.asInt ?: 0
// Extract other fields as needed

return "$file:$line"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ data class Customer(
@SerializedName("name") val name: String,
@SerializedName("surname") val surname: String,
@SerializedName("addresses") val addresses: List<Address>,
@SerializedName("meters") val meters: List<MeterGroup>?
@SerializedName("devices") val devices: List<DeviceGroup>?
): Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Meter(@SerializedName("id") val id: Int, @SerializedName("serial_number") val serialNumber: String) : Parcelable
data class Device(@SerializedName("id") val id: Int, @SerializedName("serial_number") val serialNumber: String) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.inensus.core_network.model

import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize

@Parcelize
data class DeviceGroup(@SerializedName("id") val id: Int, @SerializedName("device_serial") val deviceSerial: String, @SerializedName("device") val device: Device) : Parcelable

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class Payment(
@SerializedName("created_at") val createdAt: Date,
@SerializedName("transaction") val transaction: Transaction?,
@SerializedName("original_agent") val originalAgent: OriginalAgent?,
@SerializedName("meter") val meter: PaymentMeter?,
@SerializedName("device") val device: PaymentDevice?,
@SerializedName("token") val token: Token?
) : Parcelable

Expand All @@ -27,10 +27,8 @@ data class Transaction(@SerializedName("type") val type: String, @SerializedName
data class OriginalAgent(@SerializedName("status") val status: Int) : Parcelable

@Parcelize
data class PaymentMeter(@SerializedName("serial_number") val serialNumber: String, @SerializedName("meter_parameter") val meterParameter: MeterParameter) : Parcelable
data class PaymentDevice(@SerializedName("device_serial") val deviceSerial: String, @SerializedName("person") val person: Owner) : Parcelable

@Parcelize
data class MeterParameter(@SerializedName("owner") val owner: Owner) : Parcelable

@Parcelize
data class Owner(@SerializedName("name") val name: String, @SerializedName("surname") val surname: String) : Parcelable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package com.inensus.core_network.model

import com.google.gson.annotations.SerializedName

data class ServiceError(@SerializedName("data") val error: Error)
data class ServiceError(@SerializedName("data") val data: Data) {

data class Error(val message: String)
data class Data(
@SerializedName("message") val message: List<String>,
@SerializedName("status_code") val statusCode: Int
// Add other fields as needed
)
}

data class Error(val messages: List<String>)
2 changes: 1 addition & 1 deletion core_ui/src/main/java/com/inensus/core_ui/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class BaseFragment : Fragment() {

private fun observeError() {
provideViewModel().error.observe(viewLifecycleOwner, Observer {
showAlertDialog(context, it.error.message)
showAlertDialog(context, it.data.message[0])
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,26 @@ class ApplianceListFragment : Fragment() {
}

private fun updateAppliancesData(appliances: List<ApplianceTransaction>, loadCustomerType: LoadingApplianceListType) {
val adapter = (rvAppliances.adapter as ApplianceListAdapter)

if (loadCustomerType == LoadingApplianceListType.PAGINATE) {
adapter.updateData(ArrayList(adapter.appliances).apply { addAll(appliances) })
} else {
rvAppliances.adapter = ApplianceListAdapter().apply {
onItemClick = {
viewModel.onApplianceTapped(it)
}
// appliances pages will be empty until Device feature updates has done in app.
viewModel.saveAppliancesState(emptyList<ApplianceTransaction>())

this.appliances = appliances
notifyDataSetChanged()
}
}
/* val adapter = (rvAppliances.adapter as ApplianceListAdapter)

viewModel.saveAppliancesState((rvAppliances.adapter as ApplianceListAdapter).appliances)
if (loadCustomerType == LoadingApplianceListType.PAGINATE) {
adapter.updateData(ArrayList(adapter.appliances).apply { addAll(appliances) })
} else {
rvAppliances.adapter = ApplianceListAdapter().apply {
onItemClick = {
viewModel.onApplianceTapped(it)
}

this.appliances = appliances
notifyDataSetChanged()
}
}

viewModel.saveAppliancesState((rvAppliances.adapter as ApplianceListAdapter).appliances)*/
}

private fun handleApplianceTapped(appliance: ApplianceTransaction) {
Expand All @@ -180,10 +184,11 @@ class ApplianceListFragment : Fragment() {
private fun updateView(customer: Customer?) {
createAppliance.visibility = if (customer != null) View.VISIBLE else View.GONE
view?.findViewById<TextView>(R.id.tvEmptyDescription)?.text =
if (customer != null) getString(
getString(R.string.appliance_under_maintenance_layout_content)
/* if (customer != null) getString(
R.string.appliance_empty_layout__with_customer_content,
customer.name
) else getString(R.string.appliance_empty_layout_content)
) else getString(R.string.appliance_empty_layout_content)*/
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.inensus.feature_payment.payment_detail.model

import com.google.gson.annotations.SerializedName
import com.inensus.core_network.model.PaymentMeter
import com.inensus.core_network.model.PaymentDevice
import com.inensus.core_network.model.Token
import java.math.BigDecimal
import java.util.*
Expand All @@ -11,12 +11,12 @@ data class PaymentDetail(
@SerializedName("original_transaction_type") val provider: String,
@SerializedName("type") val type: String,
@SerializedName("sender") val sender: String,
@SerializedName("message") val meter: String,
@SerializedName("message") val device: String,
@SerializedName("amount") val amount: BigDecimal,
@SerializedName("created_at") val createdAt: Date,
@SerializedName("original_transaction") val originalTransaction: OriginalTransaction?,
@SerializedName("sms") val sms: Sms?,
@SerializedName("meter") val paymentMeter: PaymentMeter?,
@SerializedName("device") val paymentDevice: PaymentDevice?,
@SerializedName("token") val token: Token?
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PaymentDetailCreator(private val context: Context) {
KeyValue.Default(
context.getString(R.string.payment_detail_customer), context.getString(
R.string.customer_name_surname,
paymentDetail.paymentMeter?.meterParameter?.owner?.name,
paymentDetail.paymentMeter?.meterParameter?.owner?.surname
paymentDetail.paymentDevice?.person?.name,
paymentDetail.paymentDevice?.person?.surname
)
),
KeyValue.Amount(
Expand All @@ -27,7 +27,7 @@ class PaymentDetailCreator(private val context: Context) {
),
KeyValue.Default(context.getString(R.string.payment_detail_token), paymentDetail.token?.token ?: ""),
KeyValue.Default(context.getString(R.string.payment_detail_payment_type), paymentDetail.type),
KeyValue.Default(context.getString(R.string.payment_detail_meter), paymentDetail.meter),
KeyValue.Default(context.getString(R.string.payment_detail_device), paymentDetail.device),
KeyValue.Default(
context.getString(R.string.payment_detail_date),
DateUtils.convertDateToString(paymentDetail.createdAt, DATE_FORMAT_FULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.inensus.feature_payment.payment_form.model
import com.google.gson.annotations.SerializedName

data class ConfirmPaymentRequest(
@SerializedName("meter_serial_number") val meter: String?,
@SerializedName("device_serial") val device: String?,
@SerializedName("amount") val amount: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.inensus.core.sharedpreferences.SharedPreferenceWrapper
import com.inensus.feature_payment.payment_form.model.ConfirmPaymentRequest

class PaymentFormRepository(private val service: PaymentService, private val preferences: SharedPreferenceWrapper) {
var meter: String? = null
var device: String? = null
var amount: String? = null

fun confirmPayment() = service.confirmPayment(preferences.baseUrl + CONFIRM_PAYMENT_ENDPOINT, ConfirmPaymentRequest(meter, amount))
fun confirmPayment() = service.confirmPayment(preferences.baseUrl + CONFIRM_PAYMENT_ENDPOINT, ConfirmPaymentRequest(device, amount))

companion object {
private const val CONFIRM_PAYMENT_ENDPOINT = "transactions/agent"
Expand Down
Loading