Skip to content

Commit

Permalink
0.0.43
Browse files Browse the repository at this point in the history
  • Loading branch information
PaysafeAutoCommitter committed Sep 2, 2024
1 parent ed2ed1d commit ca9404f
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ class FragmentSelectPaymentMethod : Fragment() {
shippingDetails = provideShippingDetails(),
customUrlScheme = "customScheme",
venmoRequest = VenmoRequest(
consumerId = "ala-bala-portocalaaaa",
merchantAccountId = "",
profileId = ""
consumerId = "[email protected]",
merchantAccountId = "merch-acc-id",
profileId = "profile-di"
)
)

Expand Down
12 changes: 6 additions & 6 deletions example/src/main/java/com/paysafe/example/util/Consts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package com.paysafe.example.util
import com.paysafe.android.core.domain.model.config.PSEnvironment

object Consts {
const val MERCHANT_BACKEND_API_KEY = ""
const val API_KEY = ""
const val CARDS_ACCOUNT_ID = ""
const val PAYPAL_ACCOUNT_ID = ""
const val VENMO_ACCOUNT_ID = ""
const val PROFILE_ID = ""
const val MERCHANT_BACKEND_API_KEY = "dGVzdF9ob3N0ZWQ6Qi1xYTItMC02MDRiM2RiNS0wLTMwMmMwMjE0N2FkYTNjYTQyMGI0N2Q4ZWI5ODEyMjYzZDQ5NGJhOTU3MTRlMTQ0ZDAyMTQwMzgyMWUzYTMzZWJmMmQwMDYyNjgyZWQwNDk1MGM3ZWJjMzE5ZmFm"
const val API_KEY = "T1QtOTE0MzAwOkItcWEyLTAtNjNjODRkYjktMS0zMDJjMDIxNDQ5NzM5NTI4YmIyMWM4NWFjZWRjZWJkMmI0ODI2MjIzZjEzODcxZWEwMjE0MmE0NjQxYTU4Zjk5OTFlZDlmMDRlMDY5OTRkNTViYzk2MTBiYmZkOQ=="
const val CARDS_ACCOUNT_ID = "1002603390"
const val PAYPAL_ACCOUNT_ID = "1002720380"
const val VENMO_ACCOUNT_ID = "1002723680"
const val PROFILE_ID = "284f1502-0cad-4256-b5e5-705ed06cfbbd"

val environment: PSEnvironment = PSEnvironment.TEST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.paysafe.android.currencyConverter

import kotlin.math.pow

final class CurrencyConverter(
private val multiplierMap: Map<PSCurrency, Int>
) {

// Method to convert minor amount based on the currency.
fun convert(amount: Int, forCurrency: String): Double {
val psCurrency = PSCurrency.values().find { it.name == forCurrency }
val power = psCurrency?.let { multiplierMap[it] } ?: 2
if (psCurrency == null) {
println("Currency doesn't need conversion: $forCurrency")
}
return amount.toDouble() / 10.0.pow(power.toDouble())
}

companion object {
// Default multiplier map
fun defaultCurrenciesMap(): Map<PSCurrency, Int> {
return mapOf(
PSCurrency.BHD to 3,
PSCurrency.BYR to 0,
PSCurrency.JPY to 0,
PSCurrency.JOD to 3,
PSCurrency.KRW to 0,
PSCurrency.KWD to 3,
PSCurrency.LYD to 3,
PSCurrency.OMR to 3,
PSCurrency.PYG to 0,
PSCurrency.TND to 3,
PSCurrency.VND to 0
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.paysafe.android.currencyConverter

enum class PSCurrency(val value: String) {
// Bahraini Dinar
BHD("BHD"),
// Belarusian Ruble (Note: The BYR was replaced by BYN in 2016)
BYR("BYR"),
// Japanese Yen
JPY("JPY"),
// Jordanian Dinar
JOD("JOD"),
// South Korean Won
KRW("KRW"),
// Kuwaiti Dinar
KWD("KWD"),
// Libyan Dinar
LYD("LYD"),
// Omani Rial
OMR("OMR"),
// Paraguayan Guaraní
PYG("PYG"),
// Tunisian Dinar
TND("TND"),
// Vietnamese Đồng
VND("VND")
}
16 changes: 13 additions & 3 deletions venmo/src/main/java/com/paysafe/android/venmo/PSVenmoController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,26 @@ internal abstract class PSVenmoController internal constructor(
}
lifecycleScope.launch(mainDispatcher) {
when (result) {
is PSResult.Success -> handleTokenizeResultSuccess(context, venmoTokenizeOptions.customUrlScheme,result)
is PSResult.Success ->
handleTokenizeResultSuccess(
context,
venmoTokenizeOptions.venmoRequest?.profileId,
venmoTokenizeOptions.amount,
venmoTokenizeOptions.customUrlScheme,
result
)
is PSResult.Failure -> handleTokenizeResultFailure(result)
}
}
}

//Checkout process implementation needed
abstract fun startVenmoCheckout(context: Context, orderId: String, sessionToken: String, clientToken: String, customUrlScheme: String?)
abstract fun startVenmoCheckout(context: Context, orderId: String, sessionToken: String, clientToken: String, profileId: String?, amount: Int, customUrlScheme: String?)

internal fun handleTokenizeResultSuccess(
context: Context,
profileId: String?,
amount: Int,
customUrlScheme:String?,
result: PSResult.Success<PaymentHandle>
) {
Expand Down Expand Up @@ -367,9 +376,10 @@ internal abstract class PSVenmoController internal constructor(
tokenizeCallback?.onFailure(paysafeException)
return
}

// Checkout process implementation needed
tokenizationAlreadyInProgress = false
startVenmoCheckout(context, orderId, sessionToken, clientToken, customUrlScheme)
startVenmoCheckout(context, orderId, sessionToken, clientToken, profileId, amount, customUrlScheme)
}

internal fun handleTokenizeResultFailure(result: PSResult.Failure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.paysafe.android.brainTreeDetails.domain.models.DeviceData
import com.paysafe.android.brainTreeDetails.domain.models.PayerInfo
import com.paysafe.android.core.data.service.PSApiClient
import com.paysafe.android.core.util.LocalLog
import com.paysafe.android.currencyConverter.CurrencyConverter
import com.paysafe.android.tokenization.PSTokenization
import com.paysafe.android.tokenization.PSTokenizationService
import com.paysafe.android.venmo.activity.VenmoConstants
Expand Down Expand Up @@ -59,14 +60,23 @@ internal class PSVenmoNativeController internal constructor(
orderId: String,
sessionToken: String,
clientToken: String,
profileId: String?,
amount: Int,
customUrlScheme: String?
) {
LocalLog.d("PSVenmoNativeController", "startVenmoCheckout")
jwtToken = sessionToken

val currencyConverter = CurrencyConverter(CurrencyConverter.defaultCurrenciesMap())
val amountString =
currencyConverter.convert(amount = amount, forCurrency = "USD").toString()

val intent = Intent(context, VenmoWebCheckoutActivity::class.java).apply {
putExtra("SESSION_TOKEN", sessionToken)
putExtra("CLIENT_TOKEN", clientToken)
putExtra("CUSTOM_URL_SCHEME", customUrlScheme)
putExtra(VenmoConstants.INTENT_EXTRA_SESSION_TOKEN, sessionToken)
putExtra(VenmoConstants.INTENT_EXTRA_CLIENT_TOKEN, clientToken)
putExtra(VenmoConstants.INTENT_EXTRA_PROFILE_ID, profileId)
putExtra(VenmoConstants.INTENT_EXTRA_AMOUNT, amountString)
putExtra(VenmoConstants.INTENT_EXTRA_CUSTOM_URL_SCHEME, customUrlScheme)
}
activityResultLauncher.launch(intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.paysafe.android.venmo.activity
object VenmoConstants {
const val INTENT_EXTRA_SESSION_TOKEN = "SESSION_TOKEN"
const val INTENT_EXTRA_CLIENT_TOKEN = "CLIENT_TOKEN"
const val INTENT_EXTRA_PROFILE_ID = "PROFILE_ID"
const val INTENT_EXTRA_CUSTOM_URL_SCHEME = "CUSTOM_URL_SCHEME"
const val INTENT_EXTRA_AMOUNT = "AMOUNT"
const val RESULT_SUCCESS = 1_000
const val RESULT_FAILED = 1_001
const val RESULT_VENMO_APP_IS_NOT_INSTALLED = 1_002
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class VenmoServiceConstants {
return VenmoConstants.INTENT_EXTRA_CUSTOM_URL_SCHEME
}

fun getIntentExtraProfileId(): String {
return VenmoConstants.INTENT_EXTRA_PROFILE_ID
}

fun getIntentExtraAmount(): String {
return VenmoConstants.INTENT_EXTRA_AMOUNT
}

fun getResultSuccess(): Any {
return VenmoConstants.RESULT_SUCCESS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal class VenmoWebCheckoutActivity : AppCompatActivity(), VenmoListener {
lateinit var braintreeClient: BraintreeClient
lateinit var sessionToken: String
lateinit var clientToken: String
lateinit var displayAmount: String
var profileId: String? = null
lateinit var customUrlScheme: String

private val observer = object : DefaultLifecycleObserver {
Expand All @@ -43,16 +45,23 @@ internal class VenmoWebCheckoutActivity : AppCompatActivity(), VenmoListener {

val sessionTokenIntent = intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_SESSION_TOKEN)
val clientTokenIntent = intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_CLIENT_TOKEN)
val customUrlSchemeIntent = intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_CUSTOM_URL_SCHEME)

if (sessionTokenIntent == null || clientTokenIntent == null || customUrlSchemeIntent == null ) {
val customUrlSchemeIntent =
intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_CUSTOM_URL_SCHEME)
val profileIdIntent = intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_PROFILE_ID)
val displayAmountIntent = intent?.getStringExtra(VenmoConstants.INTENT_EXTRA_AMOUNT)

if (sessionTokenIntent == null || clientTokenIntent == null ||
customUrlSchemeIntent == null || displayAmountIntent == null
) {
finishActivityWithResult(VenmoConstants.RESULT_FAILED, null)
return
}

sessionToken = sessionTokenIntent
clientToken = clientTokenIntent
customUrlScheme = customUrlSchemeIntent
profileId = profileIdIntent
displayAmount = displayAmountIntent

lifecycle.addObserver(observer)
}
Expand All @@ -65,25 +74,30 @@ internal class VenmoWebCheckoutActivity : AppCompatActivity(), VenmoListener {
removeLifecycleObserver()
}

fun launchVenmo() {
fun launchVenmo() {
braintreeClient =
BraintreeClient(context = this, authorization = clientToken, returnUrlScheme = customUrlScheme)
BraintreeClient(
context = this,
authorization = clientToken,
returnUrlScheme = customUrlScheme
)
venmoClient = VenmoClient(this, braintreeClient)
venmoClient.setListener(this)

val request = VenmoRequest(VenmoPaymentMethodUsage.MULTI_USE)

request.collectCustomerBillingAddress = true
request.collectCustomerShippingAddress = true
request.profileId = sessionToken
request.profileId = profileId
request.shouldVault = false
request.totalAmount = displayAmount

if (venmoClient.isVenmoAppSwitchAvailable(this)) {
venmoClient.tokenizeVenmoAccount(this, request)
} else if (!isAppInstalled(this)) {
finishActivityWithResult(VenmoConstants.RESULT_VENMO_APP_IS_NOT_INSTALLED, null)
venmoClient.showVenmoInGooglePlayStore(this)
} else{
} else {
venmoClient.tokenizeVenmoAccount(this, request)
}
}
Expand Down Expand Up @@ -136,7 +150,10 @@ internal class VenmoWebCheckoutActivity : AppCompatActivity(), VenmoListener {
fun isAppInstalled(context: Context): Boolean {
val packageManager = context.packageManager
return try {
packageManager.getPackageInfo(VenmoConstants.VENMO_PACKAGE, PackageManager.GET_ACTIVITIES)
packageManager.getPackageInfo(
VenmoConstants.VENMO_PACKAGE,
PackageManager.GET_ACTIVITIES
)
true
} catch (e: PackageManager.NameNotFoundException) {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(null)
)
Expand All @@ -542,6 +544,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand All @@ -567,6 +571,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand All @@ -591,6 +597,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand All @@ -615,6 +623,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = null,
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand All @@ -640,6 +650,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand Down Expand Up @@ -667,6 +679,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = null,
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand All @@ -692,6 +706,8 @@ class PSVenmoControllerTest {
// Act
psVenmoController.handleTokenizeResultSuccess(
context = mockActivity,
profileId = "profile-id",
amount = 1000,
customUrlScheme = "",
result = PSResult.Success(
PaymentHandle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class PSVenmoNativeControllerTest {
orderId = orderId,
sessionToken = sessionToken,
clientToken = clientToken,
profileId = "profile-id",
amount = 1000,
customUrlScheme = customUrlScheme
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class VenmoConstantsTest {
Assert.assertEquals("CUSTOM_URL_SCHEME", venmoServiceConstants.getIntentExtraCustomUrlSchemeToken())
}

@Test
fun `test INTENT_EXTRA_PROFILE_ID constant value`() {
Assert.assertEquals("PROFILE_ID", venmoServiceConstants.getIntentExtraProfileId())
}

@Test
fun `test RESULT_SUCCESS constant value`() {
Assert.assertEquals(1_000, venmoServiceConstants.getResultSuccess())
Expand Down
Loading

0 comments on commit ca9404f

Please sign in to comment.