Skip to content

Commit

Permalink
Prevent empty string from being set during card tokenization
Browse files Browse the repository at this point in the history
  • Loading branch information
lkorth committed May 3, 2016
1 parent 9a3a688 commit d0470c8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -50,7 +51,11 @@ public BaseCardBuilder() {}
*/
@SuppressWarnings("unchecked")
public T cardNumber(String number) {
mCardnumber = number;
if (TextUtils.isEmpty(number)) {
mCardnumber = null;
} else {
mCardnumber = number;
}
return (T) this;
}

Expand All @@ -60,7 +65,11 @@ public T cardNumber(String number) {
*/
@SuppressWarnings("unchecked")
public T cvv(String cvv) {
mCvv = cvv;
if (TextUtils.isEmpty(cvv)) {
mCvv = null;
} else {
mCvv = cvv;
}
return (T) this;
}

Expand All @@ -70,7 +79,11 @@ public T cvv(String cvv) {
*/
@SuppressWarnings("unchecked")
public T expirationMonth(String expirationMonth) {
mExpirationMonth = expirationMonth;
if (TextUtils.isEmpty(expirationMonth)) {
mExpirationMonth = null;
} else {
mExpirationMonth = expirationMonth;
}
return (T) this;
}

Expand All @@ -80,7 +93,11 @@ public T expirationMonth(String expirationMonth) {
*/
@SuppressWarnings("unchecked")
public T expirationYear(String expirationYear) {
mExpirationYear = expirationYear;
if (TextUtils.isEmpty(expirationYear)) {
mExpirationYear = null;
} else {
mExpirationYear = expirationYear;
}
return (T) this;
}

Expand All @@ -90,7 +107,11 @@ public T expirationYear(String expirationYear) {
*/
@SuppressWarnings("unchecked")
public T expirationDate(String expirationDate) {
mExpirationDate = expirationDate;
if (TextUtils.isEmpty(expirationDate)) {
mExpirationDate = null;
} else {
mExpirationDate = expirationDate;
}
return (T) this;
}

Expand All @@ -100,7 +121,11 @@ public T expirationDate(String expirationDate) {
*/
@SuppressWarnings("unchecked")
public T cardholderName(String cardholderName) {
mCardholderName = cardholderName;
if (TextUtils.isEmpty(cardholderName)) {
mCardholderName = null;
} else {
mCardholderName = cardholderName;
}
return (T) this;
}

Expand All @@ -110,7 +135,11 @@ public T cardholderName(String cardholderName) {
*/
@SuppressWarnings("unchecked")
public T firstName(String firstName) {
mFirstName = firstName;
if (TextUtils.isEmpty(firstName)) {
mFirstName = null;
} else {
mFirstName = firstName;
}
return (T) this;
}

Expand All @@ -120,7 +149,11 @@ public T firstName(String firstName) {
*/
@SuppressWarnings("unchecked")
public T lastName(String lastName) {
mLastName = lastName;
if (TextUtils.isEmpty(lastName)) {
mLastName = null;
} else {
mLastName = lastName;
}
return (T) this;
}

Expand All @@ -130,7 +163,11 @@ public T lastName(String lastName) {
*/
@SuppressWarnings("unchecked")
public T countryName(String countryName) {
mCountryName = countryName;
if (TextUtils.isEmpty(countryName)) {
mCountryName = null;
} else {
mCountryName = countryName;
}
return (T) this;
}

Expand All @@ -140,7 +177,11 @@ public T countryName(String countryName) {
*/
@SuppressWarnings("unchecked")
public T locality(String locality) {
mLocality = locality;
if (TextUtils.isEmpty(locality)) {
mLocality = null;
} else {
mLocality = locality;
}
return (T) this;
}

Expand All @@ -150,7 +191,11 @@ public T locality(String locality) {
*/
@SuppressWarnings("unchecked")
public T postalCode(String postalCode) {
mPostalCode = postalCode;
if (TextUtils.isEmpty(postalCode)) {
mPostalCode = null;
} else {
mPostalCode = postalCode;
}
return (T) this;
}

Expand All @@ -160,7 +205,11 @@ public T postalCode(String postalCode) {
*/
@SuppressWarnings("unchecked")
public T region(String region) {
mRegion = region;
if (TextUtils.isEmpty(region)) {
mRegion = null;
} else {
mRegion = region;
}
return (T) this;
}

Expand All @@ -170,7 +219,11 @@ public T region(String region) {
*/
@SuppressWarnings("unchecked")
public T streetAddress(String streetAddress) {
mStreetAddress = streetAddress;
if (TextUtils.isEmpty(streetAddress)) {
mStreetAddress = null;
} else {
mStreetAddress = streetAddress;
}
return (T) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public class CardBuilderTest {

@Test
public void build_correctlyBuildsACard() throws JSONException {
CardBuilder cardBuilder = new CardBuilder().cardNumber(VISA)
.cvv("123")
CardBuilder cardBuilder = new CardBuilder()
.cardNumber(VISA)
.expirationDate("01/2015")
.expirationMonth("01")
.expirationYear("2015")
.cvv("123")
.cardholderName("Joe Smith")
.firstName("Joe")
.lastName("Smith")
Expand All @@ -37,9 +39,10 @@ public void build_correctlyBuildsACard() throws JSONException {
JSONObject jsonMetadata = json.getJSONObject(PaymentMethodBuilder.METADATA_KEY);

assertEquals(VISA, jsonCard.getString("number"));
assertEquals("123", jsonCard.getString("cvv"));
assertEquals("01/2015", jsonCard.getString("expirationDate"));
assertEquals("01", jsonCard.getString("expirationMonth"));
assertEquals("2015", jsonCard.getString("expirationYear"));
assertEquals("123", jsonCard.getString("cvv"));
assertEquals("Joe Smith", jsonCard.getString("cardholderName"));

assertEquals("Joe", jsonBillingAddress.getString("firstName"));
Expand Down Expand Up @@ -136,8 +139,29 @@ public void includesValidateOptionWhenSet() throws JSONException {
public void doesNotIncludeEmptyCreditCardWhenSerializing() throws JSONException {
CardBuilder cardBuilder = new CardBuilder();

JSONObject builtCard = new JSONObject(cardBuilder.build()).getJSONObject(CREDIT_CARD_KEY);
assertFalse(new JSONObject(cardBuilder.build()).getJSONObject(CREDIT_CARD_KEY).keys().hasNext());
assertFalse(new JSONObject(cardBuilder.build()).has(BILLING_ADDRESS_KEY));
}

assertFalse(builtCard.keys().hasNext());
@Test
public void doesNotIncludeEmptyStrings() throws JSONException {
CardBuilder cardBuilder = new CardBuilder()
.cardNumber("")
.expirationDate("")
.expirationMonth("")
.expirationYear("")
.cvv("")
.postalCode("")
.cardholderName("")
.firstName("")
.lastName("")
.streetAddress("")
.locality("")
.postalCode("")
.region("")
.countryName("");

assertFalse(new JSONObject(cardBuilder.build()).getJSONObject(CREDIT_CARD_KEY).keys().hasNext());
assertFalse(new JSONObject(cardBuilder.build()).has(BILLING_ADDRESS_KEY));
}
}

0 comments on commit d0470c8

Please sign in to comment.