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

Pass StripeError in onError #806

Merged
merged 1 commit into from
Feb 12, 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 @@ -12,6 +12,7 @@
import androidx.appcompat.app.AppCompatActivity;

import com.stripe.android.CustomerSession;
import com.stripe.android.StripeError;
import com.stripe.android.model.Customer;
import com.stripe.android.model.Source;
import com.stripe.android.model.SourceCardData;
Expand Down Expand Up @@ -64,7 +65,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
mSelectSourceButton.setEnabled(false);
mErrorDialogHandler.showError(errorMessage);
mProgressBar.setVisibility(View.INVISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.stripe.android.PaymentSession;
import com.stripe.android.PaymentSessionConfig;
import com.stripe.android.PaymentSessionData;
import com.stripe.android.StripeError;
import com.stripe.android.model.Address;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;
Expand Down Expand Up @@ -128,7 +129,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
mCustomer = null;
mSelectPaymentButton.setEnabled(false);
mSelectShippingButton.setEnabled(false);
Expand Down Expand Up @@ -186,7 +188,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
mProgressBar.setVisibility(View.INVISIBLE);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.stripe.android.PaymentSession;
import com.stripe.android.PaymentSessionConfig;
import com.stripe.android.PaymentSessionData;
import com.stripe.android.StripeError;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;
import com.stripe.android.model.ShippingInformation;
Expand Down Expand Up @@ -134,7 +135,8 @@ public void call(Void aVoid) {
public void onReceive(Context context, Intent intent) {
ShippingInformation shippingInformation = intent.getParcelableExtra(EXTRA_SHIPPING_INFO_DATA);
Intent shippingInfoProcessedIntent = new Intent(EVENT_SHIPPING_INFO_PROCESSED);
if (shippingInformation.getAddress() == null || !shippingInformation.getAddress().getCountry().equals(Locale.US.getCountry())) {
if (shippingInformation.getAddress() == null ||
!shippingInformation.getAddress().getCountry().equals(Locale.US.getCountry())) {
shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, false);
} else {
ArrayList<ShippingMethod> shippingMethods = getValidShippingMethods(shippingInformation);
Expand Down Expand Up @@ -242,8 +244,7 @@ private TextView[] getItemViews(View view) {
TextView quantityView = view.findViewById(R.id.tv_cart_quantity);
TextView unitPriceView = view.findViewById(R.id.tv_cart_unit_price);
TextView totalPriceView = view.findViewById(R.id.tv_cart_total_price);
TextView[] itemViews = { labelView, quantityView, unitPriceView, totalPriceView };
return itemViews;
return new TextView[]{labelView, quantityView, unitPriceView, totalPriceView};
}

private void attemptPurchase() {
Expand All @@ -260,7 +261,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
displayError("Error getting payment method");
}
});
Expand Down Expand Up @@ -300,7 +302,8 @@ private void completePurchase(String sourceId, String customerId) {

ShippingInformation shippingInformation = mPaymentSession.getPaymentSessionData().getShippingInformation();

Observable<Void> stripeResponse = stripeService.createQueryCharge(createParams(price, sourceId, customerId, shippingInformation));
final Observable<Void> stripeResponse = stripeService.createQueryCharge(
createParams(price, sourceId, customerId, shippingInformation));
final FragmentManager fragmentManager = getSupportFragmentManager();
mCompositeSubscription.add(stripeResponse
.subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -399,7 +402,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
displayError(errorMessage);
}
});
Expand All @@ -415,19 +419,20 @@ public void onError(int errorCode, @Nullable String errorMessage) {

private String formatSourceDescription(Source source) {
if (Source.CARD.equals(source.getType())) {
SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(sourceCardData.getBrand()).append(getString(R.string.ending_in)).append(sourceCardData.getLast4());
return stringBuilder.toString();
final SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel();
return sourceCardData.getBrand() + getString(R.string.ending_in) +
sourceCardData.getLast4();
}
return source.getType();
}

private ArrayList<ShippingMethod> getValidShippingMethods(ShippingInformation shippingInformation) {
@NonNull
private ArrayList<ShippingMethod> getValidShippingMethods(@NonNull ShippingInformation shippingInformation) {
ArrayList<ShippingMethod> shippingMethods = new ArrayList<>();
shippingMethods.add(new ShippingMethod("UPS Ground", "ups-ground", "Arrives in 3-5 days", 0, "USD"));
shippingMethods.add(new ShippingMethod("FedEx", "fedex", "Arrives tomorrow", 599, "USD"));
if (shippingInformation.getAddress() != null && shippingInformation.getAddress().getPostalCode().equals("94110")) {
if (shippingInformation.getAddress() != null &&
shippingInformation.getAddress().getPostalCode().equals("94110")) {
shippingMethods.add(new ShippingMethod("1 Hour Courier", "courier", "Arrives in the next hour", 1099, "USD"));
}
return shippingMethods;
Expand Down
28 changes: 15 additions & 13 deletions stripe/src/main/java/com/stripe/android/CustomerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,12 @@ public void onKeyError(int errorCode, @Nullable String errorMessage) {
// Any error eliminates all listeners

if (mCustomerRetrievalListener != null) {
mCustomerRetrievalListener.onError(errorCode, errorMessage);
mCustomerRetrievalListener.onError(errorCode, errorMessage, null);
mCustomerRetrievalListener = null;
}

if (mSourceRetrievalListener != null) {
mSourceRetrievalListener.onError(errorCode, errorMessage);
mSourceRetrievalListener.onError(errorCode, errorMessage, null);
mSourceRetrievalListener = null;
}
}
Expand Down Expand Up @@ -597,37 +597,37 @@ public void handleMessage(@NonNull Message msg) {
case CUSTOMER_SHIPPING_INFO_SAVED: {
if (messageObject instanceof Customer) {
mCustomer = (Customer) messageObject;
Intent intent = new Intent(EVENT_SHIPPING_INFO_SAVED);
final Intent intent = new Intent(EVENT_SHIPPING_INFO_SAVED);
LocalBroadcastManager.getInstance(mContextRef.get())
.sendBroadcast(intent);
}
break;
}
case CUSTOMER_ERROR: {
if (messageObject instanceof StripeException) {
StripeException exception = (StripeException) messageObject;
final StripeException exception = (StripeException) messageObject;
if (mCustomerRetrievalListener != null) {
final int errorCode = exception.getStatusCode() == null
? 400
: exception.getStatusCode();
mCustomerRetrievalListener.onError(
errorCode,
exception.getLocalizedMessage());
mCustomerRetrievalListener.onError(errorCode,
exception.getLocalizedMessage(),
exception.getStripeError());
mCustomerRetrievalListener = null;
}
resetUsageTokens();
}
break;
}
case SOURCE_ERROR: {
StripeException exception = (StripeException) messageObject;
final StripeException exception = (StripeException) messageObject;
if (mSourceRetrievalListener != null) {
final int errorCode = exception.getStatusCode() == null
? 400
: exception.getStatusCode();
mSourceRetrievalListener.onError(
errorCode,
exception.getLocalizedMessage());
mSourceRetrievalListener.onError(errorCode,
exception.getLocalizedMessage(),
exception.getStripeError());
mSourceRetrievalListener = null;
resetUsageTokens();
}
Expand Down Expand Up @@ -806,13 +806,15 @@ private static void sendErrorIntent(@NonNull WeakReference<Context> contextRef,
public interface CustomerRetrievalListener {
void onCustomerRetrieved(@NonNull Customer customer);

void onError(int errorCode, @Nullable String errorMessage);
void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError);
}

public interface SourceRetrievalListener {
void onSourceRetrieved(@NonNull Source source);

void onError(int errorCode, @Nullable String errorMessage);
void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError);
}

interface StripeApiProxy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class EphemeralKeyManager<TEphemeralKey extends AbstractEphemeralKey> {
retrieveEphemeralKey(null, null);
}

void retrieveEphemeralKey(@Nullable String actionString, Map<String, Object> arguments) {
void retrieveEphemeralKey(@Nullable String actionString,
@Nullable Map<String, Object> arguments) {
if (shouldRefreshKey(
mEphemeralKey,
mTimeBufferInSeconds,
mOverrideCalendar)) {
mEphemeralKeyProvider.createEphemeralKey(
StripeApiHandler.API_VERSION,
mEphemeralKeyProvider.createEphemeralKey(StripeApiHandler.API_VERSION,
new ClientKeyUpdateListener(this, actionString, arguments));
} else {
mListener.onKeyUpdate(mEphemeralKey, actionString, arguments);
Expand Down Expand Up @@ -115,31 +115,30 @@ void onKeyUpdate(@NonNull TEphemeralKey ephemeralKey,

private static class ClientKeyUpdateListener implements EphemeralKeyUpdateListener {

private @Nullable String mActionString;
private @Nullable Map<String, Object> mArguments;
private @NonNull
WeakReference<EphemeralKeyManager> mEphemeralKeyManagerWeakReference;
@Nullable private final String mActionString;
@Nullable private final Map<String, Object> mArguments;
@NonNull private final WeakReference<EphemeralKeyManager> mEphemeralKeyManagerRef;

ClientKeyUpdateListener(
@NonNull EphemeralKeyManager keyManager,
@Nullable String actionString,
@Nullable Map<String, Object> arguments) {
mEphemeralKeyManagerWeakReference = new WeakReference<>(keyManager);
mEphemeralKeyManagerRef = new WeakReference<>(keyManager);
mActionString = actionString;
mArguments = arguments;
}

@Override
public void onKeyUpdate(@NonNull String rawKey) {
final EphemeralKeyManager keyManager = mEphemeralKeyManagerWeakReference.get();
final EphemeralKeyManager keyManager = mEphemeralKeyManagerRef.get();
if (keyManager != null) {
keyManager.updateKey(rawKey, mActionString, mArguments);
}
}

@Override
public void onKeyUpdateFailure(int responseCode, @Nullable String message) {
final EphemeralKeyManager keyManager = mEphemeralKeyManagerWeakReference.get();
final EphemeralKeyManager keyManager = mEphemeralKeyManagerRef.get();
if (keyManager != null) {
keyManager.updateKeyError(responseCode, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
if (mPaymentSessionListener != null) {
mPaymentSessionListener.onError(errorCode, errorMessage);
mPaymentSessionListener.onCommunicatingStateChanged(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.stripe.android.R;
import com.stripe.android.SourceCallback;
import com.stripe.android.Stripe;
import com.stripe.android.StripeError;
import com.stripe.android.model.Card;
import com.stripe.android.model.Source;
import com.stripe.android.model.SourceParams;
Expand Down Expand Up @@ -168,7 +169,8 @@ public void onSourceRetrieved(@NonNull Source source) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
// No need to show this error, because it will be broadcast
// from the CustomerSession
setCommunicatingProgress(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.stripe.android.CustomerSession;
import com.stripe.android.R;
import com.stripe.android.StripeError;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;

Expand Down Expand Up @@ -107,7 +108,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
String displayedError = errorMessage == null ? "" : errorMessage;
showError(displayedError);
setCommunicatingProgress(false);
Expand Down Expand Up @@ -213,7 +215,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
// Note: if this Activity is changed to subclass StripeActivity,
// this code will make the error message show twice, since StripeActivity
// will listen to the broadcast version of the error
Expand Down Expand Up @@ -304,7 +307,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
setCommunicatingProgress(false);
}
};
Expand Down Expand Up @@ -344,7 +348,8 @@ public void onCustomerRetrieved(@NonNull Customer customer) {
}

@Override
public void onError(int errorCode, @Nullable String errorMessage) {
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
String displayedError = errorMessage == null ? "" : errorMessage;
showError(displayedError);
setCommunicatingProgress(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public void addSourceToCustomer_whenApiThrowsError_tellsListenerBroadcastsAndEmp
captured.getSerializableExtra(CustomerSession.EXTRA_EXCEPTION);
assertNotNull(ex);

verify(mockListener).onError(404, "The card is invalid");
verify(mockListener).onError(404, "The card is invalid", null);
assertTrue(session.getProductUsageTokens().isEmpty());
}

Expand Down Expand Up @@ -698,7 +698,7 @@ public void removeSourceFromCustomer_whenApiThrowsError_tellsListenerBroadcastsA
captured.getSerializableExtra(CustomerSession.EXTRA_EXCEPTION);
assertNotNull(ex);

verify(mockListener).onError(404, "The card does not exist");
verify(mockListener).onError(404, "The card does not exist", null);
assertTrue(session.getProductUsageTokens().isEmpty());
}

Expand Down Expand Up @@ -809,7 +809,7 @@ public void setDefaultSourceForCustomer_whenApiThrows_tellsListenerBroadcastsAnd
APIException ex = (APIException)
captured.getSerializableExtra(CustomerSession.EXTRA_EXCEPTION);
assertNotNull(ex);
verify(mockListener).onError(405, "auth error");
verify(mockListener).onError(405, "auth error", null);
assertTrue(session.getProductUsageTokens().isEmpty());
}

Expand Down
Loading