Skip to content

Commit

Permalink
Add CardParams methods to Stripe class (#2675)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe authored Jul 31, 2020
1 parent e4bb229 commit 62fab27
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
68 changes: 68 additions & 0 deletions stripe/src/main/java/com/stripe/android/Stripe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.stripe.android.model.AccountParams
import com.stripe.android.model.BankAccount
import com.stripe.android.model.BankAccountTokenParams
import com.stripe.android.model.Card
import com.stripe.android.model.CardParams
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.CvcTokenParams
Expand Down Expand Up @@ -1242,6 +1243,34 @@ class Stripe internal constructor(
)
}

/**
* Create a Card token asynchronously.
*
* See [Create a card token](https://stripe.com/docs/api/tokens/create_card).
* `POST /v1/tokens`
*
* @param cardParams the [CardParams] used to create this payment token
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
internal fun createCardToken(
cardParams: CardParams,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<Token>
) {
createToken(
tokenParams = cardParams,
stripeAccountId = stripeAccountId,
idempotencyKey = idempotencyKey,
callback = callback
)
}

/**
* Blocking method to create a [Token]. Do not call this on the UI thread or your app
* will crash.
Expand Down Expand Up @@ -1281,6 +1310,45 @@ class Stripe internal constructor(
)
}

/**
* Blocking method to create a [Token]. Do not call this on the UI thread or your app
* will crash.
*
* See [Create a card token](https://stripe.com/docs/api/tokens/create_card).
* `POST /v1/tokens`
*
* @param cardParams the [CardParams] to use for this token
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*
* @return a [Token] that can be used for this card
* @throws AuthenticationException failure to properly authenticate yourself (check your key)
* @throws InvalidRequestException your request has invalid parameters
* @throws APIConnectionException failure to connect to Stripe's API
* @throws CardException the card cannot be charged for some reason
* @throws APIException any other type of problem (for instance, a temporary issue with
* Stripe's servers
*/
@Throws(AuthenticationException::class, InvalidRequestException::class,
APIConnectionException::class, CardException::class, APIException::class)
@WorkerThread
@JvmOverloads
internal fun createCardTokenSynchronous(
cardParams: CardParams,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId
): Token? {
return stripeRepository.createToken(
cardParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
)
}

/**
* Create a CVC update token asynchronously.
*
Expand Down
32 changes: 32 additions & 0 deletions stripe/src/test/java/com/stripe/android/StripeEndToEndTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.nhaarman.mockitokotlin2.verify
import com.stripe.android.exception.InvalidRequestException
import com.stripe.android.model.AccountParams
import com.stripe.android.model.AddressFixtures
import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.CardFunding
import com.stripe.android.model.CardParamsFixture
Expand Down Expand Up @@ -151,6 +152,37 @@ class StripeEndToEndTest {
)
}

@Test
fun `create card token using CardParams should return object with expected data`() {
val token = defaultStripe.createCardTokenSynchronous(
CardParamsFixture.DEFAULT
)
val card = token?.card

assertThat(card)
.isEqualTo(
Card.Builder(expMonth = 12, expYear = 2025)
.id(card?.id)
.name("Jenny Rosen")
.last4("4242")
.addressLine1("123 Market St")
.addressLine1Check("unchecked")
.addressLine2("#345")
.addressCity("San Francisco")
.addressState("CA")
.addressZip("94107")
.addressZipCheck("unchecked")
.addressCountry("US")
.brand(CardBrand.Visa)
.funding(CardFunding.Credit)
.country("US")
.currency("usd")
.cvcCheck("unchecked")
.metadata(emptyMap())
.build()
)
}

private fun createStripeWithTestScope(
publishableKey: String = ApiKeyFixtures.DEFAULT_PUBLISHABLE_KEY
): Stripe {
Expand Down

0 comments on commit 62fab27

Please sign in to comment.