@Test(expected = JSONException.class) public void fromJson_throwsExceptionIfJsonParsingFails() throws JSONException { ErrorWithResponse.fromJson(stringFromFixture("random_json.json")); }
/** * @deprecated Android Pay is deprecated, use {@link GooglePayment} instead. For more information see the * <a href="https://developers.braintreepayments.com/guides/pay-with-google/overview">documentation</a> * * Call this method when you've received a successful FullWallet request in your activity's * {@link Activity#onActivityResult(int, int, Intent)} to get an {@link AndroidPayCardNonce} from a * {@link FullWallet}. * * @param fragment An instance of {@link BraintreeFragment}. * @param wallet a {@link FullWallet} from the Intent in {@link Activity#onActivityResult(int, int, Intent)}. * @param cart the {@link Cart} used when creating the {@link FullWallet}. */ @Deprecated public static void tokenize(BraintreeFragment fragment, FullWallet wallet, Cart cart) { try { fragment.postCallback(AndroidPayCardNonce.fromFullWallet(wallet, cart)); fragment.sendAnalyticsEvent("android-pay.nonce-received"); } catch (JSONException e) { fragment.sendAnalyticsEvent("android-pay.failed"); try { fragment.postCallback(ErrorWithResponse.fromJson(wallet.getPaymentMethodToken().getToken())); } catch (JSONException e1) { fragment.postCallback(e1); } } }
@Test public void parcelsCorrectly() throws JSONException { ErrorWithResponse error = ErrorWithResponse.fromJson(stringFromFixture("errors/credit_card_error_response.json")); Parcel parcel = Parcel.obtain(); error.writeToParcel(parcel, 0); parcel.setDataPosition(0); ErrorWithResponse parceled = ErrorWithResponse.CREATOR.createFromParcel(parcel); assertEquals(error.getStatusCode(), parceled.getStatusCode()); assertEquals(error.getMessage(), parceled.getMessage()); assertEquals(error.getErrorResponse(), parceled.getErrorResponse()); assertEquals(error.getFieldErrors().size(), parceled.getFieldErrors().size()); } }
@Test public void fromJson_parsesErrorsCorrectly() throws JSONException { String response = stringFromFixture("errors/credit_card_error_response.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromJson(response); assertEquals("Credit card is invalid", errorWithResponse.getMessage()); assertNull(errorWithResponse.errorFor("creditCard").errorFor("postalCode")); assertEquals("Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code", errorWithResponse.errorFor("creditCard").errorFor("base").getMessage()); assertEquals("Credit card number is required", errorWithResponse.errorFor("creditCard").errorFor("number").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); }