Skip to content

Commit

Permalink
Fix Google Pay support for Connect accounts (#3752)
Browse files Browse the repository at this point in the history
  • Loading branch information
brnunes-stripe authored May 24, 2021
1 parent 2b0ad6b commit 9deeac3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ internal class StripeGooglePayActivity : AppCompatActivity() {
private val publishableKey: String by lazy {
PaymentConfiguration.getInstance(this).publishableKey
}
private val stripeAccountId: String? by lazy {
PaymentConfiguration.getInstance(this).stripeAccountId
}
private val viewModel: StripeGooglePayViewModel by viewModels {
StripeGooglePayViewModel.Factory(
application,
publishableKey,
stripeAccountId,
args
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ internal class StripeGooglePayContract :
}
}

internal fun Status.getErrorResourceID(): Int? {
return when (this) {
internal fun StripeGooglePayContract.Result.Error.getErrorResourceID(): Int? {
return when (googlePayStatus) {
Status.RESULT_SUCCESS -> null
Status.RESULT_INTERNAL_ERROR, Status.RESULT_CANCELED, Status.RESULT_DEAD_CLIENT -> R.string.stripe_google_pay_error_internal
Status.RESULT_INTERNAL_ERROR, Status.RESULT_CANCELED, Status.RESULT_DEAD_CLIENT, null ->
R.string.stripe_google_pay_error_internal
else -> {
when (this.statusCode) {
when (googlePayStatus.statusCode) {
CommonStatusCodes.API_NOT_CONNECTED, // "The client attempted to call a method from an API that failed to connect."
CommonStatusCodes.CANCELED, // -> "The result was canceled either due to client disconnect or PendingResult.cancel()."
CommonStatusCodes.DEVELOPER_ERROR, // -> "The application is misconfigured."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlin.coroutines.CoroutineContext
internal class StripeGooglePayViewModel(
application: Application,
private val publishableKey: String,
private val stripeAccountId: String? = null,
private val args: StripeGooglePayContract.Args,
private val stripeRepository: StripeRepository,
private val appName: String,
Expand Down Expand Up @@ -77,7 +78,10 @@ internal class StripeGooglePayViewModel(
requireNotNull(
stripeRepository.createPaymentMethod(
params,
ApiRequest.Options(publishableKey)
ApiRequest.Options(
publishableKey,
stripeAccountId
)
)
)
}
Expand All @@ -88,13 +92,16 @@ internal class StripeGooglePayViewModel(
internal class Factory(
private val application: Application,
private val publishableKey: String,
private val stripeAccountId: String? = null,
private val args: StripeGooglePayContract.Args
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val appName = application.applicationInfo.loadLabel(application.packageManager).toString()
val appName =
application.applicationInfo.loadLabel(application.packageManager).toString()
return StripeGooglePayViewModel(
application,
publishableKey,
stripeAccountId,
args,
StripeApiRepository(application, publishableKey),
appName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,12 @@ internal class PaymentSheetViewModel internal constructor(
confirmPaymentSelection(paymentSelection)
}
is StripeGooglePayContract.Result.Error -> {
logger.error("Error processing Google Pay payment", googlePayResult.exception)
eventReporter.onPaymentFailure(PaymentSelection.GooglePay)
paymentIntent.value?.let { it ->
resetViewState(
it,
googlePayResult.googlePayStatus?.getErrorResourceID()
googlePayResult.getErrorResourceID()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.stripe.android.ApiKeyFixtures
import com.stripe.android.PaymentConfiguration
import com.stripe.android.model.GooglePayFixtures.GOOGLE_PAY_RESULT_WITH_NO_BILLING_ADDRESS
import com.stripe.android.model.PaymentIntentFixtures
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.StripeJsonUtils
import com.stripe.android.networking.AbsFakeStripeRepository
import com.stripe.android.networking.ApiRequest
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.json.JSONObject
Expand Down Expand Up @@ -156,12 +160,45 @@ class StripeGooglePayViewModelTest {
)
}

@Test
fun `createPaymentMethod() with stripeAccount should include stripeAccount in request`() {
val stripeAccount = "account_id"
var requestOptions: ApiRequest.Options? = null
val stripeRepository = object : AbsFakeStripeRepository() {
override suspend fun createPaymentMethod(
paymentMethodCreateParams: PaymentMethodCreateParams,
options: ApiRequest.Options
): PaymentMethod? {
requestOptions = options
return null
}
}
val viewModel = StripeGooglePayViewModel(
ApplicationProvider.getApplicationContext(),
ApiKeyFixtures.FAKE_PUBLISHABLE_KEY,
stripeAccount,
ARGS,
stripeRepository,
"App Name",
testDispatcher
)

val params = PaymentMethodCreateParams.createFromGooglePay(
GOOGLE_PAY_RESULT_WITH_NO_BILLING_ADDRESS
)

viewModel.createPaymentMethod(params).observeForever {
assertThat(requestOptions?.stripeAccount).isEqualTo(stripeAccount)
}
}

private fun createViewModel(
args: StripeGooglePayContract.Args = ARGS
): StripeGooglePayViewModel {
return StripeGooglePayViewModel(
ApplicationProvider.getApplicationContext(),
ApiKeyFixtures.FAKE_PUBLISHABLE_KEY,
null,
args,
FakeStripeRepository(),
"App Name",
Expand Down

0 comments on commit 9deeac3

Please sign in to comment.