Skip to content

Commit

Permalink
Improve PaymentMethodCreateParams.Card creation (#862)
Browse files Browse the repository at this point in the history
When creating a `PaymentMethod` with a card, the card object
can either be created from the user's card details (e.g. card
number, CVC, expiration), or via a Stripe token (e.g. Google
Pay). `PaymentMethodCreateParams.Card.Builder`'s setters made
it confusing to the developer that there were two separate
approaches to creating a `PaymentMethodCreateParams.Card`.

With this chamge, the Builder will be used when creating with
card details, and `PaymentMethodCreateParams.Card#create` will
be used when creating with a Stripe token.

ANDROID-346
  • Loading branch information
mshafrir-stripe committed Apr 10, 2019
1 parent 65ae04a commit 6d5daac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Expand Up @@ -128,12 +128,25 @@ public static final class Card {
@Nullable private final String mCvc;
@Nullable private final String mToken;

@NonNull
public static Card create(@NonNull String token) {
return new Card(token);
}

private Card(@NonNull String token) {
this.mToken = token;
this.mNumber = null;
this.mExpiryMonth = null;
this.mExpiryYear = null;
this.mCvc = null;
}

private Card(@NonNull Card.Builder builder) {
this.mNumber = builder.mNumber;
this.mExpiryMonth = builder.mExpiryMonth;
this.mExpiryYear = builder.mExpiryYear;
this.mCvc = builder.mCvc;
this.mToken = builder.mToken;
this.mToken = null;
}

@Override
Expand Down Expand Up @@ -180,12 +193,15 @@ public Map<String, Object> toMap() {
return map;
}

/**
* Used to create a {@link Card} object with the user's card details. To create a
* {@link Card} with a Stripe token (e.g. for Google Pay), use {@link Card#create(String)}.
*/
public static final class Builder {
@Nullable private String mNumber;
@Nullable private Integer mExpiryMonth;
@Nullable private Integer mExpiryYear;
@Nullable private String mCvc;
@Nullable private String mToken;

@NonNull
public Builder setNumber(@Nullable String number) {
Expand All @@ -211,12 +227,6 @@ public Builder setCvc(@Nullable String cvc) {
return this;
}

@NonNull
public Builder setToken(@Nullable String token) {
this.mToken = token;
return this;
}

@NonNull
public Card build() {
return new Card(this);
Expand Down
5 changes: 2 additions & 3 deletions stripe/src/test/java/com/stripe/android/StripeTest.java
Expand Up @@ -1236,9 +1236,8 @@ public void createPaymentMethod_withCardToken()

final PaymentMethodCreateParams paymentMethodCreateParams =
PaymentMethodCreateParams.create(
new PaymentMethodCreateParams.Card.Builder()
.setToken(token.getId())
.build(), null);
PaymentMethodCreateParams.Card.create(token.getId()),
null);
final PaymentMethod createdPaymentMethod = stripe.createPaymentMethodSynchronous(
paymentMethodCreateParams, FUNCTIONAL_PUBLISHABLE_KEY);
assertNotNull(createdPaymentMethod);
Expand Down

0 comments on commit 6d5daac

Please sign in to comment.