Skip to content

Commit

Permalink
Remove toJson() from StripeJsonModel
Browse files Browse the repository at this point in the history
`toJson()` was previously used in the implementation of `equals()`
(see #813). Now that it is no longer used in `equals()`, it is
no longer needed.

Remove all implementations of toJson() and rename `StripeJsonModel`
to `StripeModel`.
  • Loading branch information
mshafrir-stripe committed Jun 10, 2019
1 parent 1a55ad4 commit cddc6ef
Show file tree
Hide file tree
Showing 52 changed files with 410 additions and 1,882 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void retrievePaymentIntent() {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
paymentIntent -> mPaymentIntentValue.setText(paymentIntent != null ?
paymentIntent.toJson().toString() :
new JSONObject(paymentIntent.toMap()).toString() :
getString(R.string.error_while_retrieving_payment_intent)),
throwable -> Log.e(TAG, throwable.toString())
);
Expand Down Expand Up @@ -188,7 +188,8 @@ private void confirmPaymentIntent(@NonNull final Card card) {
.subscribe(
paymentIntent -> {
if (paymentIntent != null) {
mPaymentIntentValue.setText(paymentIntent.toJson().toString());
mPaymentIntentValue.setText(
new JSONObject(paymentIntent.toMap()).toString());

if (paymentIntent.requiresAction()) {
Toast.makeText(PaymentIntentActivity.this,
Expand Down
33 changes: 2 additions & 31 deletions stripe/src/main/java/com/stripe/android/EphemeralKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.stripe.android.model.StripeJsonModel;
import com.stripe.android.model.StripeModel;
import com.stripe.android.utils.ObjectUtils;

import org.json.JSONArray;
Expand All @@ -26,7 +26,7 @@
* See <a href="https://stripe.com/docs/mobile/android/standard#prepare-your-api">
* Using Android Standard UI Components - Prepare your API</a> for more details on ephemeral keys.
*/
abstract class EphemeralKey extends StripeJsonModel implements Parcelable {
abstract class EphemeralKey extends StripeModel implements Parcelable {

static final String FIELD_CREATED = "created";
static final String FIELD_EXPIRES = "expires";
Expand Down Expand Up @@ -84,35 +84,6 @@ abstract class EphemeralKey extends StripeJsonModel implements Parcelable {
mType = type;
}

@NonNull
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
JSONArray associatedObjectsArray = new JSONArray();
JSONObject associatedObject = new JSONObject();

try {
jsonObject.put(FIELD_CREATED, mCreated);
jsonObject.put(FIELD_EXPIRES, mExpires);
jsonObject.put(FIELD_OBJECT, mObject);
jsonObject.put(FIELD_ID, mId);
jsonObject.put(FIELD_SECRET, mSecret);
jsonObject.put(FIELD_LIVEMODE, mLiveMode);

associatedObject.put(FIELD_TYPE, mType);
associatedObject.put(FIELD_ID, mObjectId);
associatedObjectsArray.put(associatedObject);

jsonObject.put(FIELD_ASSOCIATED_OBJECTS, associatedObjectsArray);
} catch (JSONException impossible) {
// An exception can only be thrown from put operations if the key is null
// or the value is a non-finite number.
throw new IllegalArgumentException("JSONObject creation exception thrown.");
}

return jsonObject;
}

@NonNull
@Override
public Map<String, Object> toMap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
* Represents a listener for Ephemeral Key Update events.
Expand Down
35 changes: 18 additions & 17 deletions stripe/src/main/java/com/stripe/android/IssuingCardPinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Methods for retrieval / update of a Stripe Issuing card
Expand Down Expand Up @@ -149,16 +150,16 @@ public void onKeyUpdate(@NonNull IssuingCardEphemeralKey ephemeralKey,
null);
return;
}
String cardId = (String) arguments.get(ARGUMENT_CARD_ID);
String verificationId = (String) arguments.get(ARGUMENT_VERIFICATION_ID);
String userOneTimeCode = (String) arguments.get(ARGUMENT_ONE_TIME_CODE);

final String cardId =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_CARD_ID));
final String verificationId =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_VERIFICATION_ID));
final String userOneTimeCode =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_ONE_TIME_CODE));
try {
final String pin = mApiHandler.retrieveIssuingCardPin(
cardId,
verificationId,
userOneTimeCode,
ephemeralKey.getSecret());
final String pin = mApiHandler.retrieveIssuingCardPin(cardId, verificationId,
userOneTimeCode, ephemeralKey.getSecret());
listener.onIssuingCardPinRetrieved(pin);

} catch (InvalidRequestException e) {
Expand Down Expand Up @@ -218,17 +219,17 @@ public void onKeyUpdate(@NonNull IssuingCardEphemeralKey ephemeralKey,
null);
return;
}
String cardId = (String) arguments.get(ARGUMENT_CARD_ID);
String newPin = (String) arguments.get(ARGUMENT_NEW_PIN);
String verificationId = (String) arguments.get(ARGUMENT_VERIFICATION_ID);
String userOneTimeCode = (String) arguments.get(ARGUMENT_ONE_TIME_CODE);

final String cardId =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_CARD_ID));
final String newPin =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_NEW_PIN));
final String verificationId =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_VERIFICATION_ID));
final String userOneTimeCode =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_ONE_TIME_CODE));
try {
mApiHandler.updateIssuingCardPin(
cardId,
newPin,
verificationId,
userOneTimeCode,
mApiHandler.updateIssuingCardPin(cardId, newPin, verificationId, userOneTimeCode,
ephemeralKey.getSecret());
listener.onIssuingCardPinUpdated();
} catch (InvalidRequestException e) {
Expand Down
16 changes: 1 addition & 15 deletions stripe/src/main/java/com/stripe/android/model/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import java.util.Map;

import static com.stripe.android.model.StripeJsonUtils.optString;
import static com.stripe.android.model.StripeJsonUtils.putStringIfNotNull;

/**
* Model for an owner <a href="https://stripe.com/docs/api#source_object-owner-address">address</a>
* object in the Source api.
*/
public final class Address extends StripeJsonModel implements Parcelable {
public final class Address extends StripeModel implements Parcelable {

@IntDef({
RequiredBillingAddressFields.NONE,
Expand Down Expand Up @@ -141,19 +140,6 @@ public Map<String, Object> toMap() {
return map;
}

@NonNull
@Override
public JSONObject toJson() {
final JSONObject jsonObject = new JSONObject();
putStringIfNotNull(jsonObject, FIELD_CITY, mCity);
putStringIfNotNull(jsonObject, FIELD_COUNTRY, mCountry);
putStringIfNotNull(jsonObject, FIELD_LINE_1, mLine1);
putStringIfNotNull(jsonObject, FIELD_LINE_2, mLine2);
putStringIfNotNull(jsonObject, FIELD_POSTAL_CODE, mPostalCode);
putStringIfNotNull(jsonObject, FIELD_STATE, mState);
return jsonObject;
}

@Nullable
public static Address fromString(@Nullable String jsonString) {
try {
Expand Down
35 changes: 1 addition & 34 deletions stripe/src/main/java/com/stripe/android/model/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
import static com.stripe.android.model.StripeJsonUtils.optHash;
import static com.stripe.android.model.StripeJsonUtils.optInteger;
import static com.stripe.android.model.StripeJsonUtils.optString;
import static com.stripe.android.model.StripeJsonUtils.putIntegerIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putStringHashIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putStringIfNotNull;

/**
* A model object representing a Card in the Android SDK.
*/
public final class Card extends StripeJsonModel implements StripePaymentSource {
public final class Card extends StripeModel implements StripePaymentSource {

@Retention(RetentionPolicy.SOURCE)
@StringDef({
Expand Down Expand Up @@ -790,36 +787,6 @@ public String getCvcCheck() {
return cvcCheck;
}

@NonNull
@Override
public JSONObject toJson() {
final JSONObject object = new JSONObject();
putStringIfNotNull(object, FIELD_NAME, name);
putStringIfNotNull(object, FIELD_ADDRESS_CITY, addressCity);
putStringIfNotNull(object, FIELD_ADDRESS_COUNTRY, addressCountry);
putStringIfNotNull(object, FIELD_ADDRESS_LINE1, addressLine1);
putStringIfNotNull(object, FIELD_ADDRESS_LINE1_CHECK, addressLine1Check);
putStringIfNotNull(object, FIELD_ADDRESS_LINE2, addressLine2);
putStringIfNotNull(object, FIELD_ADDRESS_STATE, addressState);
putStringIfNotNull(object, FIELD_ADDRESS_ZIP, addressZip);
putStringIfNotNull(object, FIELD_ADDRESS_ZIP_CHECK, addressZipCheck);
putStringIfNotNull(object, FIELD_BRAND, brand);
putStringIfNotNull(object, FIELD_CURRENCY, currency);
putStringIfNotNull(object, FIELD_COUNTRY, country);
putStringIfNotNull(object, FIELD_CUSTOMER, customerId);
putIntegerIfNotNull(object, FIELD_EXP_MONTH, expMonth);
putIntegerIfNotNull(object, FIELD_EXP_YEAR, expYear);
putStringIfNotNull(object, FIELD_FINGERPRINT, fingerprint);
putStringIfNotNull(object, FIELD_FUNDING, funding);
putStringIfNotNull(object, FIELD_CVC_CHECK, cvcCheck);
putStringIfNotNull(object, FIELD_LAST4, last4);
putStringIfNotNull(object, FIELD_ID, id);
putStringIfNotNull(object, FIELD_TOKENIZATION_METHOD, tokenizationMethod);
putStringHashIfNotNull(object, FIELD_METADATA, metadata);
putStringIfNotNull(object, FIELD_OBJECT, VALUE_CARD);
return object;
}

@NonNull
@Override
public Map<String, Object> toMap() {
Expand Down
45 changes: 9 additions & 36 deletions stripe/src/main/java/com/stripe/android/model/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
import static com.stripe.android.model.StripeJsonUtils.optBoolean;
import static com.stripe.android.model.StripeJsonUtils.optInteger;
import static com.stripe.android.model.StripeJsonUtils.optString;
import static com.stripe.android.model.StripeJsonUtils.putBooleanIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putIntegerIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putObjectIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putStringIfNotNull;

/**
* Model for a Stripe Customer object
*/
public final class Customer extends StripeJsonModel {
public final class Customer extends StripeModel {

private static final String FIELD_ID = "id";
private static final String FIELD_OBJECT = "object";
Expand Down Expand Up @@ -83,6 +79,7 @@ public List<CustomerSource> getSources() {
return mSources;
}

@Nullable
public Boolean getHasMore() {
return mHasMore;
}
Expand All @@ -105,50 +102,26 @@ public CustomerSource getSourceById(@NonNull String sourceId) {
return null;
}

@NonNull
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
putStringIfNotNull(jsonObject, FIELD_ID, mId);
putStringIfNotNull(jsonObject, FIELD_OBJECT, VALUE_CUSTOMER);
putStringIfNotNull(jsonObject, FIELD_DEFAULT_SOURCE, mDefaultSource);
StripeJsonModel.putStripeJsonModelIfNotNull(jsonObject,
FIELD_SHIPPING,
mShippingInformation);
JSONObject sourcesObject = new JSONObject();
putStringIfNotNull(sourcesObject, FIELD_OBJECT, VALUE_LIST);
putBooleanIfNotNull(sourcesObject, FIELD_HAS_MORE, mHasMore);
putIntegerIfNotNull(sourcesObject, FIELD_TOTAL_COUNT, mTotalCount);
putStripeJsonModelListIfNotNull(sourcesObject, FIELD_DATA, mSources);
putStringIfNotNull(sourcesObject, FIELD_URL, mUrl);

putObjectIfNotNull(jsonObject, FIELD_SOURCES, sourcesObject);

return jsonObject;
}

@NonNull
@Override
public Map<String, Object> toMap() {
final AbstractMap<String, Object> map = new HashMap<>();
map.put(FIELD_ID, mId);
map.put(FIELD_OBJECT, VALUE_CUSTOMER);
map.put(FIELD_DEFAULT_SOURCE, mDefaultSource);

StripeJsonModel.putStripeJsonModelMapIfNotNull(
map,
FIELD_SHIPPING,
mShippingInformation);
map.put(FIELD_SHIPPING, mShippingInformation != null ? mShippingInformation.toMap() : null);

final AbstractMap<String, Object> sourcesObject = new HashMap<>();
sourcesObject.put(FIELD_HAS_MORE, mHasMore);
sourcesObject.put(FIELD_TOTAL_COUNT, mTotalCount);
sourcesObject.put(FIELD_OBJECT, VALUE_LIST);
sourcesObject.put(FIELD_URL, mUrl);
StripeJsonModel.putStripeJsonModelListIfNotNull(
sourcesObject,
FIELD_DATA,
mSources);

final List<Map<String, Object>> customerSourceMaps = new ArrayList<>(mSources.size());
for (CustomerSource customerSource : mSources) {
customerSourceMaps.add(customerSource.toMap());
}
sourcesObject.put(FIELD_DATA, customerSourceMaps);
StripeNetworkUtils.removeNullAndEmptyParams(sourcesObject);

map.put(FIELD_SOURCES, sourcesObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Model of the "data" object inside a {@link Customer} "source" object.
*/
public final class CustomerSource extends StripeJsonModel implements StripePaymentSource {
public final class CustomerSource extends StripeModel implements StripePaymentSource {

@NonNull private final StripePaymentSource mStripePaymentSource;

Expand Down Expand Up @@ -121,17 +121,6 @@ public Map<String, Object> toMap() {
return new HashMap<>();
}

@NonNull
@Override
public JSONObject toJson() {
if (mStripePaymentSource instanceof Source) {
return ((Source) mStripePaymentSource).toJson();
} else if (mStripePaymentSource instanceof Card) {
return ((Card) mStripePaymentSource).toJson();
}
return new JSONObject();
}

@Override
public boolean equals(@Nullable Object obj) {
return this == obj || (obj instanceof CustomerSource && typedEquals((CustomerSource) obj));
Expand Down
32 changes: 1 addition & 31 deletions stripe/src/main/java/com/stripe/android/model/PaymentIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@
import java.util.Map;
import java.util.Objects;

import static com.stripe.android.model.StripeJsonUtils.listToJsonArray;
import static com.stripe.android.model.StripeJsonUtils.optBoolean;
import static com.stripe.android.model.StripeJsonUtils.optCurrency;
import static com.stripe.android.model.StripeJsonUtils.optLong;
import static com.stripe.android.model.StripeJsonUtils.optMap;
import static com.stripe.android.model.StripeJsonUtils.optString;
import static com.stripe.android.model.StripeJsonUtils.putArrayIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putBooleanIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putLongIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putMapIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putStringIfNotNull;

/**
* A PaymentIntent tracks the process of collecting a payment from your customer.
Expand All @@ -39,7 +33,7 @@
* <li><a href="https://stripe.com/docs/api/payment_intents">PaymentIntents API</a></li>
* </ul>
*/
public final class PaymentIntent extends StripeJsonModel {
public final class PaymentIntent extends StripeModel {
private static final String VALUE_PAYMENT_INTENT = "payment_intent";

static final String FIELD_ID = "id";
Expand Down Expand Up @@ -328,30 +322,6 @@ private static List<String> jsonArrayToList(@Nullable JSONArray jsonArray) {
return list;
}

@NonNull
@Override
public JSONObject toJson() {
final JSONObject jsonObject = new JSONObject();
putStringIfNotNull(jsonObject, FIELD_ID, mId);
putStringIfNotNull(jsonObject, FIELD_OBJECT, mObjectType);
putArrayIfNotNull(jsonObject, FIELD_PAYMENT_METHOD_TYPES,
listToJsonArray(mPaymentMethodTypes));
putLongIfNotNull(jsonObject, FIELD_AMOUNT, mAmount);
putLongIfNotNull(jsonObject, FIELD_CANCELED, mCanceledAt);
putStringIfNotNull(jsonObject, FIELD_CAPTURE_METHOD, mCaptureMethod);
putStringIfNotNull(jsonObject, FIELD_CLIENT_SECRET, mClientSecret);
putStringIfNotNull(jsonObject, FIELD_CONFIRMATION_METHOD, mConfirmationMethod);
putLongIfNotNull(jsonObject, FIELD_CREATED, mCreated);
putStringIfNotNull(jsonObject, FIELD_CURRENCY, mCurrency);
putStringIfNotNull(jsonObject, FIELD_DESCRIPTION, mDescription);
putBooleanIfNotNull(jsonObject, FIELD_LIVEMODE, mLiveMode);
putMapIfNotNull(jsonObject, FIELD_NEXT_ACTION, mNextAction);
putStringIfNotNull(jsonObject, FIELD_RECEIPT_EMAIL, mReceiptEmail);
putStringIfNotNull(jsonObject, FIELD_SOURCE, mSource);
putStringIfNotNull(jsonObject, FIELD_STATUS, mStatus != null ? mStatus.code : null);
return jsonObject;
}

@NonNull
@Override
public Map<String, Object> toMap() {
Expand Down
Loading

0 comments on commit cddc6ef

Please sign in to comment.