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

Mark Stripe.createToken() as Deprecated #1781

Merged
merged 1 commit into from
Nov 5, 2019
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 @@ -9,43 +9,38 @@ import com.stripe.android.view.CardInputWidget
import com.stripe.example.R

/**
* Logic needed to create tokens using the [android.os.AsyncTask] methods included in the
* sdk: [Stripe.createToken].
* Logic needed to create a Card Token asynchronously using [Stripe.createCardToken].
*/
class AsyncTaskTokenController(
button: Button,
private var cardInputWidget: CardInputWidget?,
context: Context,
private val errorDialogHandler: ErrorDialogHandler,
outputListController: ListViewController,
private val outputListController: ListViewController,
private val progressDialogController: ProgressDialogController,
publishableKey: String
) {
private val stripe: Stripe = Stripe(context, publishableKey, enableLogging = true)

init {
button.setOnClickListener {
saveCard(TokenCallbackImpl(
errorDialogHandler,
outputListController,
progressDialogController
))
saveCard()
}
}

fun detach() {
cardInputWidget = null
}

private fun saveCard(tokenCallback: ApiResultCallback<Token>) {
val cardToSave = cardInputWidget?.card
if (cardToSave == null) {
errorDialogHandler.show("Invalid Card Data")
return
}

progressDialogController.show(R.string.progressMessage)
stripe.createToken(cardToSave, callback = tokenCallback)
private fun saveCard() {
cardInputWidget?.card?.let { card ->
progressDialogController.show(R.string.progressMessage)
stripe.createCardToken(card, callback = TokenCallbackImpl(
errorDialogHandler,
outputListController,
progressDialogController
))
} ?: errorDialogHandler.show("Invalid Card Data")
}

private class TokenCallbackImpl constructor(
Expand Down
27 changes: 27 additions & 0 deletions stripe/src/main/java/com/stripe/android/Stripe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ class Stripe internal constructor(
*/
@UiThread
@JvmOverloads
@Deprecated("Deprecated, replace with Stripe#createCardToken()",
ReplaceWith("Stripe#createCardToken()"))
fun createToken(
card: Card,
idempotencyKey: String? = null,
Expand All @@ -742,6 +744,31 @@ 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 card the [Card] used to create this payment token
* @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 createCardToken(
card: Card,
idempotencyKey: String? = null,
callback: ApiResultCallback<Token>
) {
createTokenFromParams(
stripeNetworkUtils.createCardTokenParams(card),
Token.TokenType.CARD,
idempotencyKey,
callback
)
}

/**
* Blocking method to create a [Token]. Do not call this on the UI thread or your app
* will crash.
Expand Down
8 changes: 4 additions & 4 deletions stripe/src/test/java/com/stripe/android/StripeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void run() {
}

@Test
public void createTokenShouldCallTokenCreator() {
public void createCardTokenShouldCallTokenCreator() {
final boolean[] tokenCreatorCalled = { false };
final Stripe stripe = createStripe(
new Stripe.TokenCreator() {
Expand All @@ -158,12 +158,12 @@ public void create(@NonNull Map<String, ?> tokenParams,
tokenCreatorCalled[0] = true;
}
});
stripe.createToken(CARD, DEFAULT_TOKEN_CALLBACK);
stripe.createCardToken(CARD, DEFAULT_TOKEN_CALLBACK);
assertTrue(tokenCreatorCalled[0]);
}

@Test
public void createTokenShouldUseProvidedKey() {
public void createCardTokenShouldUseProvidedKey() {
final Stripe stripe = createStripe(
new Stripe.TokenCreator() {
@Override
Expand All @@ -177,7 +177,7 @@ public void create(@NonNull Map<String, ?> tokenParams,
assertEquals(DEFAULT_TOKEN_CALLBACK, callback);
}
});
stripe.createToken(CARD, DEFAULT_TOKEN_CALLBACK);
stripe.createCardToken(CARD, DEFAULT_TOKEN_CALLBACK);
}

@Test
Expand Down