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

Remove null values in PaymentMethod.BillingDetails#toMap() #1006

Merged
merged 2 commits into from
May 28, 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
26 changes: 20 additions & 6 deletions stripe/src/main/java/com/stripe/android/model/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.stripe.android.StripeNetworkUtils;
import com.stripe.android.utils.ObjectUtils;

import org.json.JSONException;
Expand Down Expand Up @@ -118,12 +119,25 @@ public String getState() {
@Override
public Map<String, Object> toMap() {
final AbstractMap<String, Object> map = new HashMap<>();
map.put(FIELD_CITY, mCity);
map.put(FIELD_COUNTRY, mCountry);
map.put(FIELD_LINE_1, mLine1);
map.put(FIELD_LINE_2, mLine2);
map.put(FIELD_POSTAL_CODE, mPostalCode);
map.put(FIELD_STATE, mState);
if (mCity != null) {
map.put(FIELD_CITY, mCity);
}
if (mCountry != null) {
map.put(FIELD_COUNTRY, mCountry);
}
if (mLine1 != null) {
map.put(FIELD_LINE_1, mLine1);
}
if (mLine2 != null) {
map.put(FIELD_LINE_2, mLine2);
}
if (mPostalCode != null) {
map.put(FIELD_POSTAL_CODE, mPostalCode);
}
if (mState != null) {
map.put(FIELD_STATE, mState);
}
StripeNetworkUtils.removeNullAndEmptyParams(map);
return map;
}

Expand Down
30 changes: 20 additions & 10 deletions stripe/src/main/java/com/stripe/android/model/PaymentMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;

import com.stripe.android.StripeNetworkUtils;
import com.stripe.android.model.wallets.Wallet;
import com.stripe.android.model.wallets.WalletFactory;
import com.stripe.android.utils.ObjectUtils;
Expand Down Expand Up @@ -275,12 +276,12 @@ public PaymentMethod build() {
}

public static final class BillingDetails extends StripeJsonModel {
private static final String FIELD_ADDRESS = "address";
private static final String FIELD_EMAIL = "email";
private static final String FIELD_NAME = "name";
private static final String FIELD_PHONE = "phone";
static final String FIELD_ADDRESS = "address";
static final String FIELD_EMAIL = "email";
static final String FIELD_NAME = "name";
static final String FIELD_PHONE = "phone";

@NonNull public final Address address;
@Nullable public final Address address;
public final String email;
public final String name;
public final String phone;
Expand All @@ -296,10 +297,19 @@ private BillingDetails(@NonNull Builder builder) {
@Override
public Map<String, Object> toMap() {
final Map<String, Object> billingDetails = new HashMap<>();
billingDetails.put(FIELD_ADDRESS, address.toMap());
billingDetails.put(FIELD_EMAIL, email);
billingDetails.put(FIELD_NAME, name);
billingDetails.put(FIELD_PHONE, phone);
if (address != null) {
billingDetails.put(FIELD_ADDRESS, address.toMap());
}
if (email != null) {
billingDetails.put(FIELD_EMAIL, email);
}
if (name != null) {
billingDetails.put(FIELD_NAME, name);
}
if (phone != null) {
billingDetails.put(FIELD_PHONE, phone);
}
StripeNetworkUtils.removeNullAndEmptyParams(billingDetails);
return billingDetails;
}

Expand All @@ -308,7 +318,7 @@ public Map<String, Object> toMap() {
public JSONObject toJson() {
final JSONObject billingDetails = new JSONObject();
try {
billingDetails.put(FIELD_ADDRESS, address.toJson());
billingDetails.put(FIELD_ADDRESS, address != null ? address.toJson() : null);
billingDetails.put(FIELD_EMAIL, email);
billingDetails.put(FIELD_NAME, name);
billingDetails.put(FIELD_PHONE, phone);
Expand Down
11 changes: 3 additions & 8 deletions stripe/src/test/java/com/stripe/android/model/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static com.stripe.android.testharness.JsonTestUtils.assertMapEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* Test class for {@link Address}.
Expand Down Expand Up @@ -39,14 +38,10 @@ public class AddressTest {
private static final Address ADDRESS = Address.fromString(JSON_ADDRESS);

@Test
public void fromJsonString_backToJson_createsIdenticalElement() {
public void fromJsonString_backToJson_createsIdenticalElement() throws JSONException {
assertNotNull(ADDRESS);
try {
JSONObject rawConversion = new JSONObject(JSON_ADDRESS);
assertJsonEquals(rawConversion, ADDRESS.toJson());
} catch (JSONException jsonException) {
fail("Test Data failure: " + jsonException.getLocalizedMessage());
}
JSONObject rawConversion = new JSONObject(JSON_ADDRESS);
assertJsonEquals(rawConversion, ADDRESS.toJson());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PaymentMethodTest {
public static final String RAW_CARD_JSON = "{\n" +
Expand Down Expand Up @@ -165,4 +167,16 @@ public void equals_withEqualPaymentMethods_shouldReturnTrue() {
public void fromString_shouldReturnExpectedPaymentMethod() {
assertEquals(CARD_PAYMENT_METHOD, PaymentMethod.fromString(RAW_CARD_JSON));
}

@Test
public void billingDetailsToMap_removesNullValues() {
final Map<String, Object> billingDetails =
new PaymentMethod.BillingDetails.Builder()
.setName("name")
.build()
.toMap();
assertEquals(1, billingDetails.size());
assertFalse(billingDetails.containsKey(PaymentMethod.BillingDetails.FIELD_ADDRESS));
assertTrue(billingDetails.containsKey(PaymentMethod.BillingDetails.FIELD_NAME));
}
}