Search in sources :

Example 1 with BrowserSwitchException

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

the class AuthorizationRequestUnitTest method parseBrowserSwitchResponse_parsesSuccessResponseAndReturnsErrorWhenErrorIsPresent.

@Test
public void parseBrowserSwitchResponse_parsesSuccessResponseAndReturnsErrorWhenErrorIsPresent() throws NoSuchFieldException, IllegalAccessException {
    setField("mMsgGuid", mRequest, "19342c28-c6d3-4948-a989-8ee1d40dae5c");
    setField("mEncryptionKey", mRequest, EncryptionUtils.hexStringToByteArray("96BF26A3851D0127AF474CC556D9C296A9A02171CD319EE5249C4B3033FC7BB6"));
    Uri uri = Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/success?payloadEnc=9kuRyRmR8PpMoc3umclWyuGeibf4%2FIWdDyF9ehFL0TWI33GJ83iAHC7NdZszNRXrsa5uvO20N6BpGCKm3M%2F%2FjXzI5XrkxHhpKnH4j96mLNlTjDI2012cUZ7hotqm8rahnFkobykEwfI6OTGu3Zk%2Bc6FQ4Vi44nEeIw7Ocs%2Fiw%2BwzXGaZz3LgJajediKcwL%2BrDJIRgoZTnjzT2aM2No7wvORpAk%2FBRmAp1QxZkzTor0zcYgPEia%2B%2FivuEDRFKDUIC%2BC4GRulZB70Bzo7FBSvSOyy1rldRjaLg9W8bhBd8WOXjyg%2F0gLryjIqppmA%2FWFwUH7nBrbqpOyCyp5hskE5NZSNFZEokUEh4oOHI%2BMQshZETuivdWEjNrvbWq8bp2JHv5fnM9NjqmHY%2Bu23sEHQoJQ%3D%3D&payload=eyJ2ZXJzaW9uIjozLCJtc2dfR1VJRCI6IjE5MzQyYzI4LWM2ZDMtNDk0OC1hOTg5LThlZTFkNDBkYWU1YyIsImVycm9yIjoiVGhlcmUgd2FzIGFuIGVycm9yIn0=");
    Result result = mRequest.parseBrowserResponse(mContextInspector, uri);
    assertEquals(ResultType.Error, result.getResultType());
    assertTrue(result.getError() instanceof BrowserSwitchException);
    assertEquals("There was an error", result.getError().getMessage());
}
Also used : BrowserSwitchException(com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException) Uri(android.net.Uri) Test(org.junit.Test)

Example 2 with BrowserSwitchException

use of com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException 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 3 with BrowserSwitchException

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

the class AuthorizationRequestUnitTest method parseBrowserResponse_parsesErrorsFromCancelResponses.

@Test
public void parseBrowserResponse_parsesErrorsFromCancelResponses() throws JSONException {
    Uri uri = Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/cancel?payload=eyJ2ZXJzaW9uIjozLCJtc2dfR1VJRCI6bnVsbCwicmVzcG9uc2VfdHlwZSI6bnVsbCwiZW52aXJvbm1lbnQiOiJtb2NrIiwiZXJyb3IiOnsiZGVidWdfaWQiOm51bGwsIm1lc3NhZ2UiOiJFbmNyeXB0ZWQgcGF5bG9hZCBoYXMgZXhwaXJlZCJ9LCJsYW5ndWFnZSI6bnVsbH0=");
    Result result = mRequest.parseBrowserResponse(mContextInspector, uri);
    assertEquals(ResultType.Error, result.getResultType());
    assertTrue(result.getError() instanceof BrowserSwitchException);
    JSONAssert.assertEquals("{\"debug_id\":null,\"message\":\"Encrypted payload has expired\"}", result.getError().getMessage(), JSONCompareMode.NON_EXTENSIBLE);
}
Also used : BrowserSwitchException(com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException) Uri(android.net.Uri) Test(org.junit.Test)

Example 4 with BrowserSwitchException

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

the class CheckoutRequest method parseBrowserResponse.

@Override
public Result parseBrowserResponse(ContextInspector contextInspector, Uri uri) {
    String status = uri.getLastPathSegment();
    if (!Uri.parse(getSuccessUrl()).getLastPathSegment().equals(status)) {
        // return cancel result
        return new Result();
    }
    String requestXoToken = Uri.parse(mApprovalUrl).getQueryParameter(mTokenQueryParamKey);
    String responseXoToken = uri.getQueryParameter(mTokenQueryParamKey);
    if (responseXoToken != null && TextUtils.equals(requestXoToken, responseXoToken)) {
        try {
            JSONObject response = new JSONObject();
            response.put("webURL", uri.toString());
            return new Result(null, /*don't know the environment here*/
            ResponseType.web, response, null);
        } catch (JSONException e) {
            return new Result(new ResponseParsingException(e));
        }
    } else {
        return new Result(new BrowserSwitchException("The response contained inconsistent data."));
    }
}
Also used : JSONObject(org.json.JSONObject) ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) JSONException(org.json.JSONException) BrowserSwitchException(com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException)

Aggregations

BrowserSwitchException (com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException)4 Uri (android.net.Uri)2 ResponseParsingException (com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 InvalidEncryptionDataException (com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 BadPaddingException (javax.crypto.BadPaddingException)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1