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

Add support for creating a person token #2039

Merged
merged 2 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -142,7 +142,7 @@ internal class AnalyticsDataFactory @VisibleForTesting internal constructor(
internal fun getTokenCreationParams(
productUsageTokens: List<String>?,
publishableKey: String,
tokenType: String?
@Token.TokenType tokenType: String?
): Map<String, Any> {
return getEventLoggingParams(
EventName.TOKEN_CREATION,
Expand Down
58 changes: 58 additions & 0 deletions stripe/src/main/java/com/stripe/android/Stripe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.stripe.android.model.CvcTokenParams
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.PersonTokenParams
import com.stripe.android.model.PiiTokenParams
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.Source
Expand Down Expand Up @@ -1155,6 +1156,63 @@ class Stripe internal constructor(
)
}

/**
* Creates a single-use token that represents the details for a person. Use this when creating or
* updating persons associated with a Connect account.
* See [the documentation](https://stripe.com/docs/connect/account-tokens) to learn more.
*
* See [Create a person token](https://stripe.com/docs/api/tokens/create_person)
*
* @param params the person token creation params
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
fun createPersonToken(
params: PersonTokenParams,
idempotencyKey: String? = null,
callback: ApiResultCallback<Token>
) {
createTokenFromParams(
params.toParamMap(),
Token.TokenType.PERSON,
idempotencyKey,
callback
)
}

/**
* Creates a single-use token that represents the details for a person. Use this when creating or
* updating persons associated with a Connect account.
* See [the documentation](https://stripe.com/docs/connect/account-tokens) to learn more.
*
* See [Create a person token](https://stripe.com/docs/api/tokens/create_person)
*
* @param params the person token creation params
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
*
* @return a [Token] representing the person
*/
@Throws(AuthenticationException::class, InvalidRequestException::class,
APIConnectionException::class, CardException::class, APIException::class)
@WorkerThread
@JvmOverloads
fun createPersonTokenSynchronous(
params: PersonTokenParams,
idempotencyKey: String? = null
): Token? {
return stripeRepository.createToken(
params.toParamMap(),
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
),
Token.TokenType.PERSON
)
}

private fun createTokenFromParams(
tokenParams: Map<String, Any>,
@Token.TokenType tokenType: String,
Expand Down
120 changes: 120 additions & 0 deletions stripe/src/main/java/com/stripe/android/model/AddressJapanParams.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.stripe.android.model

import android.os.Parcelable
import com.stripe.android.ObjectBuilder
import java.util.Locale
import kotlinx.android.parcel.Parcelize

@Parcelize
data class AddressJapanParams(
/**
* City or ward.
*/
val city: String? = null,

/**
* Two-letter country code (ISO 3166-1 alpha-2).
*/
val country: String? = null,

/**
* Block or building number.
*/
val line1: String? = null,

/**
* Building details.
*/
val line2: String? = null,

/**
* Postal code.
*/
val postalCode: String? = null,

/**
* Prefecture.
*/
val state: String? = null,

/**
* Town or cho-me.
*/
val town: String? = null
) : StripeParamsModel, Parcelable {
override fun toParamMap(): Map<String, Any> {
return listOf(
PARAM_CITY to city,
PARAM_COUNTRY to country,
PARAM_LINE_1 to line1,
PARAM_LINE_2 to line2,
PARAM_POSTAL_CODE to postalCode,
PARAM_STATE to state,
PARAM_TOWN to town
).fold(emptyMap()) { acc, (key, value) ->
acc.plus(
value?.let { mapOf(key to it) }.orEmpty()
)
}
}

class Builder : ObjectBuilder<AddressJapanParams> {
private var city: String? = null
private var country: String? = null
private var line1: String? = null
private var line2: String? = null
private var postalCode: String? = null
private var state: String? = null
private var town: String? = null

fun setCity(city: String?): Builder = apply {
this.city = city
}

fun setCountry(country: String?): Builder = apply {
this.country = country?.toUpperCase(Locale.ROOT)
}

fun setLine1(line1: String?): Builder = apply {
this.line1 = line1
}

fun setLine2(line2: String?): Builder = apply {
this.line2 = line2
}

fun setPostalCode(postalCode: String?): Builder = apply {
this.postalCode = postalCode
}

fun setState(state: String?): Builder = apply {
this.state = state
}

fun setTown(town: String?): Builder = apply {
this.town = town
}

override fun build(): AddressJapanParams {
return AddressJapanParams(
city = city,
country = country,
line1 = line1,
line2 = line2,
postalCode = postalCode,
state = state,
town = town
)
}
}

private companion object {
private const val PARAM_CITY = "city"
private const val PARAM_COUNTRY = "country"
private const val PARAM_LINE_1 = "line1"
private const val PARAM_LINE_2 = "line2"
private const val PARAM_POSTAL_CODE = "postal_code"
private const val PARAM_STATE = "state"
private const val PARAM_TOWN = "town"
}
}
16 changes: 15 additions & 1 deletion stripe/src/main/java/com/stripe/android/model/DateOfBirth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ data class DateOfBirth(
val day: Int,
val month: Int,
val year: Int
) : Parcelable
) : StripeParamsModel, Parcelable {
override fun toParamMap(): Map<String, Any> {
return mapOf(
PARAM_DAY to day,
PARAM_MONTH to month,
PARAM_YEAR to year
)
}

private companion object {
private const val PARAM_DAY = "day"
private const val PARAM_MONTH = "month"
private const val PARAM_YEAR = "year"
}
}
Loading