Search in sources :

Example 6 with ResponseParsingException

use of com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException in project braintree_android by braintree.

the class AuthorizationRequestUnitTest method parseBrowserSwitchResponse_returnsErrorForInvalidMsgGUID.

@Test
public void parseBrowserSwitchResponse_returnsErrorForInvalidMsgGUID() {
    Uri uri = Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/success?payload=eyJtc2dfR1VJRCI6Im1zZ19HVUlEIn0=&payloadEnc=encrypteddata");
    when(mContextInspector.getStringPreference("com.paypal.otc.msg_guid")).thenReturn("original-msg-guid");
    when(mContextInspector.getStringPreference("com.paypal.otc.key")).thenReturn("key");
    Result result = mRequest.parseBrowserResponse(mContextInspector, uri);
    assertEquals(ResultType.Error, result.getResultType());
    assertEquals("Response invalid", result.getError().getMessage());
    assertTrue(result.getError() instanceof ResponseParsingException);
}
Also used : ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) Uri(android.net.Uri) Test(org.junit.Test)

Example 7 with ResponseParsingException

use of com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException in project braintree_android by braintree.

the class AuthorizationRequestUnitTest method parseBrowserSwitchResponse_parsesSuccessResponsesAndReturnsErrorWhenInvalidPayloadEncIsReturned.

@Test
public void parseBrowserSwitchResponse_parsesSuccessResponsesAndReturnsErrorWhenInvalidPayloadEncIsReturned() throws NoSuchFieldException, IllegalAccessException {
    setField("mMsgGuid", mRequest, "19342c28-c6d3-4948-a989-8ee1d40dae5c");
    Uri uri = Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/success?payloadEnc=9kuRy&payload=eyJ2ZXJzaW9uIjozLCJtc2dfR1VJRCI6IjE5MzQyYzI4LWM2ZDMtNDk0OC1hOTg5LThlZTFkNDBkYWU1YyIsInJlc3BvbnNlX3R5cGUiOiJjb2RlIiwiZW52aXJvbm1lbnQiOiJtb2NrIiwiZXJyb3IiOm51bGx9");
    Result result = mRequest.parseBrowserResponse(mContextInspector, uri);
    assertEquals(ResultType.Error, result.getResultType());
    assertTrue(result.getError() instanceof ResponseParsingException);
    assertEquals("java.lang.IllegalArgumentException: bad base-64", result.getError().getMessage());
}
Also used : ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) Uri(android.net.Uri) Test(org.junit.Test)

Example 8 with ResponseParsingException

use of com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException in project braintree_android by braintree.

the class AuthorizationRequest method parseBrowserResponse.

@Override
public Result parseBrowserResponse(ContextInspector contextInspector, Uri uri) {
    String status = uri.getLastPathSegment();
    String payloadEnc = uri.getQueryParameter("payloadEnc");
    JSONObject payload;
    try {
        payload = new JSONObject(new String(Base64.decode(uri.getQueryParameter("payload"), Base64.DEFAULT)));
    } catch (NullPointerException | IllegalArgumentException | JSONException e) {
        payload = new JSONObject();
    }
    if (Uri.parse(getSuccessUrl()).getLastPathSegment().equals(status)) {
        if (!payload.has("msg_GUID")) {
            return new Result(new ResponseParsingException("Response incomplete"));
        }
        if (TextUtils.isEmpty(payloadEnc) || !isValidResponse(Json.optString(payload, "msg_GUID", ""))) {
            return new Result(new ResponseParsingException("Response invalid"));
        }
        try {
            JSONObject decryptedPayloadEnc = getDecryptedPayload(payloadEnc);
            String error = Json.optString(payload, "error", "");
            // the string 'null' is coming back in production
            if (!TextUtils.isEmpty(error) && !"null".equals(error)) {
                return new Result(new BrowserSwitchException(error));
            }
            return new Result(Json.optString(payload, "environment", ""), ResponseType.authorization_code, new JSONObject().put("code", decryptedPayloadEnc.getString("payment_code")), decryptedPayloadEnc.getString("email"));
        } catch (JSONException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | InvalidKeyException | InvalidEncryptionDataException | IllegalArgumentException e) {
            return new Result(new ResponseParsingException(e));
        }
    } else if (Uri.parse(getCancelUrl()).getLastPathSegment().equals(status)) {
        String error = Json.optString(payload, "error", "");
        // the string 'null' is coming back in production
        if (!TextUtils.isEmpty(error) && !"null".equals(error)) {
            return new Result(new BrowserSwitchException(error));
        } else {
            return new Result();
        }
    } else {
        return new Result(new ResponseParsingException("Response uri invalid"));
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) JSONException(org.json.JSONException) BrowserSwitchException(com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) JSONObject(org.json.JSONObject) InvalidEncryptionDataException(com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)

Example 9 with ResponseParsingException

use of com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException in project braintree_android by braintree.

the class AppSwitchHelper method processResponseIntent.

private static Result processResponseIntent(Bundle bundle) {
    String error = bundle.getString("error");
    if (!TextUtils.isEmpty(error)) {
        return new Result(new WalletSwitchException(error));
    } else {
        String environment = bundle.getString("environment");
        String bundleResponseType = bundle.getString("response_type").toLowerCase(Locale.US);
        ResponseType response_type;
        if ("code".equals(bundleResponseType)) {
            response_type = ResponseType.authorization_code;
        } else {
            response_type = ResponseType.web;
        }
        try {
            if (ResponseType.web == response_type) {
                String webURL = bundle.getString("webURL");
                return new Result(environment, response_type, new JSONObject().put("webURL", webURL), // email not sent back in checkout requests since Hermes doesn't return that info
                null);
            } else {
                String authorization_code = bundle.getString("authorization_code");
                String email = bundle.getString("email");
                return new Result(environment, response_type, new JSONObject().put("code", authorization_code), email);
            }
        } catch (JSONException e) {
            return new Result(new ResponseParsingException(e));
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) JSONException(org.json.JSONException) WalletSwitchException(com.paypal.android.sdk.onetouch.core.exception.WalletSwitchException) Result(com.paypal.android.sdk.onetouch.core.Result) ResponseType(com.paypal.android.sdk.onetouch.core.enums.ResponseType)

Example 10 with ResponseParsingException

use of com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException in project braintree_android by braintree.

the class AuthorizationRequestUnitTest method parseBrowserSwitchResponse_returnsErrorForMissingMsgGUIDInPayload.

@Test
public void parseBrowserSwitchResponse_returnsErrorForMissingMsgGUIDInPayload() {
    Uri uri = Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/success?payload=eyJ0ZXN0IjoidGVzdCJ9");
    Result result = mRequest.parseBrowserResponse(mContextInspector, uri);
    assertEquals(ResultType.Error, result.getResultType());
    assertTrue(result.getError() instanceof ResponseParsingException);
    assertEquals("Response incomplete", result.getError().getMessage());
}
Also used : ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

ResponseParsingException (com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException)14 Test (org.junit.Test)11 Uri (android.net.Uri)10 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 Result (com.paypal.android.sdk.onetouch.core.Result)2 BrowserSwitchException (com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)1 CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)1 Request (com.paypal.android.sdk.onetouch.core.Request)1 ContextInspector (com.paypal.android.sdk.onetouch.core.base.ContextInspector)1 Protocol (com.paypal.android.sdk.onetouch.core.enums.Protocol)1 ResponseType (com.paypal.android.sdk.onetouch.core.enums.ResponseType)1 InvalidEncryptionDataException (com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)1 WalletSwitchException (com.paypal.android.sdk.onetouch.core.exception.WalletSwitchException)1 TestSetupHelper.getMockContextInspector (com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1