Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Nov 4, 2024
1 parent 430f358 commit a94d865
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.stripe.android.link.model.LinkAccount
import com.stripe.android.link.repositories.LinkRepository
import com.stripe.android.link.ui.inline.SignUpConsentAction
import com.stripe.android.link.ui.inline.UserInput
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.ConsumerPaymentDetailsUpdateParams
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.ConsumerSessionLookup
import com.stripe.android.model.ConsumerSignUpConsentAction
Expand Down Expand Up @@ -237,6 +239,19 @@ internal class DefaultLinkAccountManager @Inject constructor(
}
}

override suspend fun updatePaymentDetails(updateParams: ConsumerPaymentDetailsUpdateParams): Result<ConsumerPaymentDetails> {
TODO("Not yet implemented")
}

override suspend fun deletePaymentDetails(paymentDetailsId: String): Result<Unit> {
TODO("Not yet implemented")
}

override suspend fun listPaymentDetails(): Result<ConsumerPaymentDetails> {
val clientSecret = linkAccount.value?.clientSecret ?: return Result.failure(Throwable("no link account found"))
return linkRepository.listPaymentDetails(clientSecret, consumerPublishableKey)
}

@VisibleForTesting
internal fun setAccountNullable(
consumerSession: ConsumerSession?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.stripe.android.link.model.AccountStatus
import com.stripe.android.link.model.LinkAccount
import com.stripe.android.link.ui.inline.SignUpConsentAction
import com.stripe.android.link.ui.inline.UserInput
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.ConsumerPaymentDetailsUpdateParams
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.ConsumerSessionLookup
import com.stripe.android.model.PaymentMethodCreateParams
Expand Down Expand Up @@ -69,4 +71,19 @@ internal interface LinkAccountManager {
* Confirms a verification code sent to the user.
*/
suspend fun confirmVerification(code: String): Result<LinkAccount>

/**
* Update an existing payment method in the signed in consumer account.
*/
suspend fun updatePaymentDetails(updateParams: ConsumerPaymentDetailsUpdateParams): Result<ConsumerPaymentDetails>

/**
* Delete the payment method from the signed in consumer account.
*/
suspend fun deletePaymentDetails(paymentDetailsId: String): Result<Unit>

/**
* Fetch all saved payment methods for the signed in consumer.
*/
suspend fun listPaymentDetails(): Result<ConsumerPaymentDetails>
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ internal class LinkApiRepository @Inject constructor(
}
}

override suspend fun listPaymentDetails(
consumerSessionClientSecret: String,
consumerPublishableKey: String?
): Result<ConsumerPaymentDetails> {
return stripeRepository.listPaymentDetails(
clientSecret = consumerSessionClientSecret,
paymentMethodTypes = emptySet(),
requestOptions = consumerPublishableKey?.let {
ApiRequest.Options(it)
} ?: ApiRequest.Options(
publishableKeyProvider(),
stripeAccountIdProvider()
)
)
}

private fun buildRequestOptions(
consumerAccountPublishableKey: String? = null,
): ApiRequest.Options {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.link.repositories

import com.stripe.android.link.LinkPaymentDetails
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.ConsumerSessionLookup
import com.stripe.android.model.ConsumerSessionSignup
Expand Down Expand Up @@ -71,4 +72,12 @@ internal interface LinkRepository {
consumerSessionClientSecret: String,
consumerPublishableKey: String?,
): Result<ConsumerSession>

/**
* Fetch all saved payment methods for the consumer.
*/
suspend fun listPaymentDetails(
consumerSessionClientSecret: String,
consumerPublishableKey: String?
): Result<ConsumerPaymentDetails>
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.ConfirmStripeIntentParams
import com.stripe.android.model.ConfirmStripeIntentParams.Companion.PARAM_CLIENT_SECRET
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.CreateFinancialConnectionsSessionForDeferredPaymentParams
import com.stripe.android.model.CreateFinancialConnectionsSessionParams
Expand All @@ -77,6 +78,7 @@ import com.stripe.android.model.StripeIntent
import com.stripe.android.model.Token
import com.stripe.android.model.TokenParams
import com.stripe.android.model.parsers.CardMetadataJsonParser
import com.stripe.android.model.parsers.ConsumerPaymentDetailsJsonParser
import com.stripe.android.model.parsers.ConsumerPaymentDetailsShareJsonParser
import com.stripe.android.model.parsers.ConsumerSessionJsonParser
import com.stripe.android.model.parsers.CustomerJsonParser
Expand Down Expand Up @@ -1435,6 +1437,27 @@ class StripeApiRepository @JvmOverloads internal constructor(
)
}

override suspend fun listPaymentDetails(
clientSecret: String,
paymentMethodTypes: Set<String>,
requestOptions: ApiRequest.Options
): Result<ConsumerPaymentDetails> {
return fetchStripeModelResult(
apiRequestFactory.createPost(
listConsumerPaymentDetailsUrl,
requestOptions,
mapOf(
"request_surface" to "android_payment_element",
"credentials" to mapOf(
"consumer_session_client_secret" to clientSecret
),
"types" to paymentMethodTypes.toList()
)
),
ConsumerPaymentDetailsJsonParser
)
}

private suspend fun retrieveElementsSession(
params: ElementsSessionParams,
options: ApiRequest.Options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.stripe.android.model.BankStatuses
import com.stripe.android.model.CardMetadata
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.CreateFinancialConnectionsSessionForDeferredPaymentParams
import com.stripe.android.model.CreateFinancialConnectionsSessionParams
Expand Down Expand Up @@ -380,6 +381,15 @@ interface StripeRepository {
params: Map<String, String>? = null
): Result<MobileCardElementConfig>



@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
suspend fun listPaymentDetails(
clientSecret: String,
paymentMethodTypes: Set<String>,
requestOptions: ApiRequest.Options
): Result<ConsumerPaymentDetails>

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun buildPaymentUserAgent(attribution: Set<String> = emptySet()): String
}
1 change: 1 addition & 0 deletions payments-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ android {

dependencies {
implementation project(":stripe-core")
implementation project(":payments-core")
implementation libs.androidx.appCompat
implementation libs.androidx.coreKtx
implementation libs.kotlin.serialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,30 @@ data class ConsumerPaymentDetails(
data class Card(
override val id: String,
override val last4: String,
) : PaymentDetails(id, type = "card")
) : PaymentDetails(id, type = "card") {
companion object {
/**
* Reads the address from the [PaymentMethodCreateParams] mapping format to the format
* used by [ConsumerPaymentDetails].
*/
fun getAddressFromMap(
cardPaymentMethodCreateParamsMap: Map<String, Any>
): Pair<String, Any>? {
val billingDetails =
cardPaymentMethodCreateParamsMap["billing_details"] as? Map<*, *>
val address = billingDetails?.get("address") as? Map<*, *>
return address?.let {
// card["billing_details"]["address"] becomes card["billing_address"]
"billing_address" to mapOf(
// card["billing_details"]["address"]["country"]
// becomes card["billing_address"]["country_code"]
"country_code" to it["country"],
"postal_code" to it["postal_code"]
)
}
}
}
}

@Parcelize
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.stripe.android.model

import android.os.Parcelable
import androidx.annotation.RestrictTo
import kotlinx.parcelize.Parcelize

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
class ConsumerPaymentDetailsUpdateParams(
val id: String,
val isDefault: Boolean? = null,
val cardPaymentMethodCreateParams: PaymentMethodCreateParams? = null
) : StripeParamsModel, Parcelable {

override fun toParamMap(): Map<String, Any> {
val params: MutableMap<String, Any> = mutableMapOf()

isDefault?.let { params["is_default"] = it }

cardPaymentMethodCreateParams?.toParamMap()?.let { map ->
(map["card"] as? Map<*, *>)?.let { card ->
card["exp_month"]?.let { params["exp_month"] = it }
card["exp_year"]?.let { params["exp_year"] = it }
}
ConsumerPaymentDetails.Card.getAddressFromMap(map)?.let {
params[it.first] = it.second
}
}

return params
}
}

0 comments on commit a94d865

Please sign in to comment.