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

Fix Google Pay support for Connect accounts #3752

Merged
merged 3 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -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,13 @@ 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()
?: R.string.stripe_google_pay_error_internal
brnunes-stripe marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
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