Skip to content

Commit

Permalink
Support more Offsite Payment Methods (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmniX authored Oct 16, 2024
1 parent a3ab621 commit d20e101
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,15 @@ internal class KomojuPaymentScreenModel(private val config: KomojuSDK.Configurat
private fun Payment.handle() {
when (this) {
is Payment.Konbini -> mutableRouter.value = Router.Replace(KomojuPaymentRoute.KonbiniAwaitingPayment(config, payment = this))
is Payment.PayPay -> _offSitePaymentURL.value = redirectURL
is Payment.RakutenPay -> _offSitePaymentURL.value = redirectURL
is Payment.OffSitePayment -> _offSitePaymentURL.value = redirectURL
else -> Unit
}
}

private fun PaymentMethod.validate() = when (this) {
is PaymentMethod.CreditCard -> state.value.creditCardDisplayData.validate()
is PaymentMethod.Konbini -> state.value.konbiniDisplayData.validate(state.value.commonDisplayData)
is PaymentMethod.PayPay -> true // No input required
is PaymentMethod.RakutenPay -> true // No input required
is PaymentMethod.OffSitePayment -> true // No input required for Offsite payment
else -> false
}

Expand Down Expand Up @@ -200,8 +198,6 @@ internal class KomojuPaymentScreenModel(private val config: KomojuSDK.Configurat
)

private fun PaymentMethod.toPaymentRequest(): PaymentRequest = when (this) {
is PaymentMethod.AliPay -> TODO()
is PaymentMethod.AuPay -> TODO()
is PaymentMethod.BankTransfer -> TODO()
is PaymentMethod.BitCash -> TODO()
is PaymentMethod.CreditCard -> error("Credit Card needs to generate tokens first!")
Expand All @@ -211,14 +207,12 @@ internal class KomojuPaymentScreenModel(private val config: KomojuSDK.Configurat
email = state.value.commonDisplayData.email,
)

is PaymentMethod.MerPay -> TODO()
is PaymentMethod.NetCash -> TODO()
is PaymentMethod.Other -> TODO()
is PaymentMethod.Paidy -> TODO()
is PaymentMethod.PayEasy -> TODO()
is PaymentMethod.PayPay -> PaymentRequest.PayPay(paymentMethod = this)
is PaymentMethod.RakutenPay -> PaymentRequest.RakutenPay(paymentMethod = this)
is PaymentMethod.WebMoney -> TODO()
is PaymentMethod.OffSitePayment -> PaymentRequest.OffSitePaymentRequest(this)
}

fun onCloseClicked() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,39 @@ import com.komoju.android.sdk.ui.composables.PrimaryButton
import com.komoju.android.sdk.ui.theme.KomojuMobileSdkTheme
import com.komoju.android.sdk.ui.theme.LocalI18nTexts
import com.komoju.mobile.sdk.entities.PaymentMethod
import com.komoju.mobile.sdk.types.OffSitePaymentType

@Composable
internal fun AppPayForm(paymentMethod: PaymentMethod, onPayButtonClicked: () -> Unit) {
internal fun OffSitePayForm(paymentMethod: PaymentMethod.OffSitePayment, onPayButtonClicked: () -> Unit) {
val titleKey = remember(paymentMethod) {
when (paymentMethod) {
is PaymentMethod.AliPay -> "PAYMENT_VIA_ALI_PAY"
is PaymentMethod.AuPay -> "PAYMENT_VIA_AU_PAY"
is PaymentMethod.MerPay -> "PAYMENT_VIA_MER_PAY"
is PaymentMethod.PayPay -> "PAYMENT_VIA_PAY_PAY"
is PaymentMethod.RakutenPay -> "PAYMENT_VIA_RAKUTEN"
when (paymentMethod.type) {
OffSitePaymentType.ALI_PAY -> "PAYMENT_VIA_ALI_PAY"
OffSitePaymentType.AU_PAY -> "PAYMENT_VIA_AU_PAY"
OffSitePaymentType.MER_PAY -> "PAYMENT_VIA_MER_PAY"
OffSitePaymentType.PAY_PAY -> "PAYMENT_VIA_PAY_PAY"
OffSitePaymentType.RAKUTEN_PAY -> "PAYMENT_VIA_RAKUTEN"
else -> null
}
}

val messageKey = remember(paymentMethod) {
when (paymentMethod) {
is PaymentMethod.AliPay -> "ALI_PAY_REDIRECT_MESSAGE"
is PaymentMethod.AuPay -> "AU_PAY_REDIRECT_MESSAGE"
is PaymentMethod.MerPay -> "MER_PAY_REDIRECT_MESSAGE"
is PaymentMethod.PayPay -> "PAY_PAY_REDIRECT_MESSAGE"
is PaymentMethod.RakutenPay -> "RAKUTEN_REDIRECT_MESSAGE"
when (paymentMethod.type) {
OffSitePaymentType.ALI_PAY -> "ALI_PAY_REDIRECT_MESSAGE"
OffSitePaymentType.AU_PAY -> "AU_PAY_REDIRECT_MESSAGE"
OffSitePaymentType.MER_PAY -> "MER_PAY_REDIRECT_MESSAGE"
OffSitePaymentType.PAY_PAY -> "PAY_PAY_REDIRECT_MESSAGE"
OffSitePaymentType.RAKUTEN_PAY -> "RAKUTEN_REDIRECT_MESSAGE"
else -> null
}
}

val paymentButtonKey = remember(paymentMethod) {
when (paymentMethod) {
is PaymentMethod.AliPay -> "CONTINUE_TO_ALI_PAY"
is PaymentMethod.AuPay -> "CONTINUE_TO_AU_PAY"
is PaymentMethod.MerPay -> "CONTINUE_TO_MER_PAY"
is PaymentMethod.PayPay -> "CONTINUE_TO_PAY_PAY"
is PaymentMethod.RakutenPay -> "CONTINUE_TO_RAKUTEN"
when (paymentMethod.type) {
OffSitePaymentType.ALI_PAY -> "CONTINUE_TO_ALI_PAY"
OffSitePaymentType.AU_PAY -> "CONTINUE_TO_AU_PAY"
OffSitePaymentType.MER_PAY -> "CONTINUE_TO_MER_PAY"
OffSitePaymentType.PAY_PAY -> "CONTINUE_TO_PAY_PAY"
OffSitePaymentType.RAKUTEN_PAY -> "CONTINUE_TO_RAKUTEN"
else -> null
}
}
Expand Down Expand Up @@ -94,15 +95,15 @@ internal fun AppPayForm(paymentMethod: PaymentMethod, onPayButtonClicked: () ->
@Preview(showBackground = true)
private fun AppPayFormPreview() {
KomojuMobileSdkTheme {
AppPayForm(
PaymentMethod.PayPay(
OffSitePayForm(
PaymentMethod.OffSitePayment(
displayName = "PayPay",
hashedGateway = "paypay",
exchangeRate = 1.0,
currency = "JPY",
amount = "100",
additionalFields = listOf(),
isOffsite = false,
type = OffSitePaymentType.PAY_PAY,
),
) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ internal fun PaymentMethodForm(
},
)

is PaymentMethod.AliPay,
is PaymentMethod.AuPay,
is PaymentMethod.MerPay,
is PaymentMethod.PayPay,
is PaymentMethod.RakutenPay,
-> AppPayForm(paymentMethod, onPayButtonClicked = {
onPaymentRequested(paymentMethod)
})
is PaymentMethod.OffSitePayment -> OffSitePayForm(
paymentMethod,
onPayButtonClicked = {
onPaymentRequested(paymentMethod)
},
)

is PaymentMethod.BankTransfer -> BankForm(
bankTransfer = paymentMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.komoju.android.sdk.R
import com.komoju.android.sdk.ui.theme.Gray200
import com.komoju.android.sdk.ui.theme.KomojuDarkGreen
import com.komoju.mobile.sdk.entities.PaymentMethod
import com.komoju.mobile.sdk.types.OffSitePaymentType

@Composable
internal fun PaymentMethodsRow(paymentMethods: List<PaymentMethod>, selectedPaymentMethod: PaymentMethod?, onSelected: (PaymentMethod) -> Unit) {
Expand Down Expand Up @@ -58,18 +59,20 @@ private fun PaymentMethodComposable(paymentMethod: PaymentMethod, isSelected: Bo

private val PaymentMethod.displayIcon
get() = when (this) {
is PaymentMethod.AliPay -> R.drawable.komoju_ic_alipay
is PaymentMethod.AuPay -> R.drawable.komoju_ic_au_pay
is PaymentMethod.OffSitePayment -> when (type) {
OffSitePaymentType.ALI_PAY -> R.drawable.komoju_ic_alipay
OffSitePaymentType.AU_PAY -> R.drawable.komoju_ic_au_pay
OffSitePaymentType.MER_PAY -> R.drawable.komoju_ic_merpay
OffSitePaymentType.PAY_PAY -> R.drawable.komoju_ic_paypay
OffSitePaymentType.RAKUTEN_PAY -> R.drawable.komoju_ic_rakuten_pay
}
is PaymentMethod.BankTransfer -> R.drawable.komoju_ic_bank_transfer
is PaymentMethod.BitCash -> R.drawable.komoju_ic_bitcash
is PaymentMethod.CreditCard -> R.drawable.komoju_ic_credit_card
is PaymentMethod.Konbini -> R.drawable.komoju_ic_konbini
is PaymentMethod.MerPay -> R.drawable.komoju_ic_merpay
is PaymentMethod.NetCash -> R.drawable.komoju_ic_credit_card
is PaymentMethod.Paidy -> R.drawable.komoju_ic_paidy
is PaymentMethod.PayEasy -> R.drawable.komoju_ic_pay_easy
is PaymentMethod.PayPay -> R.drawable.komoju_ic_paypay
is PaymentMethod.RakutenPay -> R.drawable.komoju_ic_rakuten_pay
is PaymentMethod.WebMoney -> R.drawable.komoju_ic_web_money
is PaymentMethod.Other -> R.drawable.komoju_ic_credit_card
}
Expand Down Expand Up @@ -97,14 +100,14 @@ private fun PaymentMethodComposablePreview() {
brands = listOf(),
displayName = "Konbini",
),
PaymentMethod.PayPay(
PaymentMethod.OffSitePayment(
hashedGateway = "",
exchangeRate = 0.0,
currency = "",
amount = "0",
additionalFields = listOf(),
isOffsite = false,
displayName = "PayPay",
type = OffSitePaymentType.PAY_PAY,
),
)
PaymentMethodsRow(paymentMethods, paymentMethods.first()) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal object AmountUtils {
if (amount.isBlank()) return ""
val locale = currency.toLocale()
return NumberFormat.getCurrencyInstance(locale).apply {
this.maximumFractionDigits = 2
this.minimumFractionDigits = 2
this.maximumFractionDigits = 0
this.minimumFractionDigits = 0
this.currency = PlatformCurrency.getInstance(locale)
}.format(amount.toDouble())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fun Item(item: Item, onItemChanged: (Item) -> Unit = {}, onItemClicked: () -> Un
tint = if (item.isFavorite) Color.Red else Color.DarkGray,
)
}
Text("¥" + item.price + ".00", fontSize = 18.sp, color = Color.Black, fontWeight = FontWeight.Bold, modifier = Modifier.padding(4.dp))
Text("¥" + item.price, fontSize = 18.sp, color = Color.Black, fontWeight = FontWeight.Bold, modifier = Modifier.padding(4.dp))
Text(stringResource(item.name), fontSize = 14.sp, color = Color.Black, fontWeight = FontWeight.Medium, lineHeight = 16.sp, modifier = Modifier.padding(horizontal = 4.dp))
Text(
stringResource(R.string.model) + ": " + item.model + ", " + stringResource(item.color),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ sealed interface Payment {
val confirmationCode: String?,
) : Payment

data class PayPay(override val status: PaymentStatus, override val amount: String, override val currency: String, val redirectURL: String) : Payment

data class CreditCard(override val status: PaymentStatus, override val amount: String, override val currency: String) : Payment

data class RakutenPay(override val status: PaymentStatus, override val amount: String, override val currency: String, val redirectURL: String) : Payment
data class OffSitePayment(override val status: PaymentStatus, override val amount: String, override val currency: String, val type: String, val redirectURL: String) : Payment

data class Error(val code: String, val message: String, override val amount: String, override val currency: String) : Payment {
override val status: PaymentStatus = PaymentStatus.EXPIRED
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.komoju.mobile.sdk.entities

import com.komoju.mobile.sdk.types.OffSitePaymentType

sealed interface PaymentMethod {
val displayName: String
val hashedGateway: String
Expand All @@ -18,44 +20,14 @@ sealed interface PaymentMethod {
val brands: List<String>,
) : PaymentMethod

data class PayPay(
override val displayName: String,
override val hashedGateway: String,
override val exchangeRate: Double,
override val currency: String,
override val amount: String,
override val additionalFields: List<String>,
val isOffsite: Boolean,
) : PaymentMethod

data class MerPay(
override val displayName: String,
override val hashedGateway: String,
override val exchangeRate: Double,
override val currency: String,
override val amount: String,
override val additionalFields: List<String>,
val isOffsite: Boolean,
) : PaymentMethod

data class RakutenPay(
data class OffSitePayment(
override val displayName: String,
override val hashedGateway: String,
override val exchangeRate: Double,
override val currency: String,
override val amount: String,
override val additionalFields: List<String>,
val isOffsite: Boolean,
) : PaymentMethod

data class AuPay(
override val displayName: String,
override val hashedGateway: String,
override val exchangeRate: Double,
override val currency: String,
override val amount: String,
override val additionalFields: List<String>,
val isOffsite: Boolean,
val type: OffSitePaymentType,
) : PaymentMethod

data class BankTransfer(
Expand Down Expand Up @@ -115,17 +87,6 @@ sealed interface PaymentMethod {
val isOffsite: Boolean,
) : PaymentMethod

data class AliPay(
override val displayName: String,
override val hashedGateway: String,
override val exchangeRate: Double,
override val currency: String,
override val amount: String,
override val additionalFields: List<String>,
val isOffsite: Boolean,
val secondIcon: String,
) : PaymentMethod

data class Konbini(
override val displayName: String,
override val hashedGateway: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ sealed interface PaymentRequest {
val paymentMethod: PaymentMethod

data class Konbini(override val paymentMethod: PaymentMethod.Konbini, val konbiniBrand: KonbiniBrand, val email: String) : PaymentRequest

data class PayPay(override val paymentMethod: PaymentMethod.PayPay) : PaymentRequest
data class RakutenPay(override val paymentMethod: PaymentMethod.RakutenPay) : PaymentRequest
data class OffSitePaymentRequest(override val paymentMethod: PaymentMethod.OffSitePayment) : PaymentRequest
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ internal data class PaymentRequestDto(@SerialName("payment_details") val payment
email = paymentRequest.email,
),
)
is PaymentRequest.PayPay -> PaymentRequestDto(
is PaymentRequest.OffSitePaymentRequest -> PaymentRequestDto(
paymentDetails = PaymentDetails(
type = "paypay",
),
)

is PaymentRequest.RakutenPay -> PaymentRequestDto(
paymentDetails = PaymentDetails(
type = "rakutenpay",
type = paymentRequest.paymentMethod.type.id,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import com.komoju.mobile.sdk.entities.Payment
import com.komoju.mobile.sdk.entities.PaymentStatus
import com.komoju.mobile.sdk.remote.dtos.PaymentResponseDto
import com.komoju.mobile.sdk.remote.dtos.SessionResponse
import com.komoju.mobile.sdk.types.OffSitePaymentType

internal object PaymentMapper {
fun map(response: PaymentResponseDto): Payment = map(response.payment)

fun map(response: SessionResponse): Payment = map(response.payment)

private fun map(payment: PaymentResponseDto.Payment?): Payment = when (payment?.paymentDetails?.type) {
private fun map(payment: PaymentResponseDto.Payment?): Payment = when (val type = payment?.paymentDetails?.type) {
"konbini" -> Payment.Konbini(
amount = payment.amount.orEmpty(),
currency = payment.currency.orEmpty(),
Expand All @@ -23,24 +24,23 @@ internal object PaymentMapper {
confirmationCode = payment.paymentDetails.confirmationCode,
)

"paypay" -> Payment.PayPay(
amount = payment.amount.orEmpty(),
currency = payment.currency.orEmpty(),
redirectURL = payment.paymentDetails.redirectUrl.orEmpty(),
status = PaymentStatus.fromString(payment.status.orEmpty()),
)

"credit_card" -> Payment.CreditCard(
amount = payment.amount.orEmpty(),
currency = payment.currency.orEmpty(),
status = PaymentStatus.fromString(payment.status.orEmpty()),
)

"rakutenpay" -> Payment.RakutenPay(
OffSitePaymentType.RAKUTEN_PAY.id,
OffSitePaymentType.AU_PAY.id,
OffSitePaymentType.ALI_PAY.id,
OffSitePaymentType.MER_PAY.id,
OffSitePaymentType.PAY_PAY.id,
-> Payment.OffSitePayment(
amount = payment.amount.orEmpty(),
currency = payment.currency.orEmpty(),
redirectURL = payment.paymentDetails.redirectUrl.orEmpty(),
status = PaymentStatus.fromString(payment.status.orEmpty()),
type = type,
)

else -> error("Invalid payment type")
Expand Down
Loading

0 comments on commit d20e101

Please sign in to comment.