Search in sources :

Example 1 with Result

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

the class PayPal method onActivityResult.

/**
 * The result from PayPal's request.
 *
 * @param fragment A {@link BraintreeFragment} used to process the request.
 * @param data Data associated with the result.
 */
protected static void onActivityResult(final BraintreeFragment fragment, int resultCode, Intent data) {
    Request request = getPersistedRequest(fragment.getApplicationContext());
    if (resultCode == Activity.RESULT_OK && data != null && request != null) {
        boolean isAppSwitch = isAppSwitch(data);
        Result result = PayPalOneTouchCore.parseResponse(fragment.getApplicationContext(), request, data);
        switch(result.getResultType()) {
            case Error:
                fragment.postCallback(new BrowserSwitchException(result.getError().getMessage()));
                sendAnalyticsEventForSwitchResult(fragment, request, isAppSwitch, "failed");
                break;
            case Cancel:
                sendAnalyticsEventForSwitchResult(fragment, request, isAppSwitch, "canceled");
                fragment.postCancelCallback(BraintreeRequestCodes.PAYPAL);
                break;
            case Success:
                onSuccess(fragment, data, request, result);
                sendAnalyticsEventForSwitchResult(fragment, request, isAppSwitch, "succeeded");
                break;
        }
    } else {
        String type;
        if (request != null) {
            type = request.getClass().getSimpleName().toLowerCase();
        } else {
            type = "unknown";
        }
        fragment.sendAnalyticsEvent("paypal." + type + ".canceled");
        if (resultCode != Activity.RESULT_CANCELED) {
            fragment.postCancelCallback(BraintreeRequestCodes.PAYPAL);
        }
    }
}
Also used : Request(com.paypal.android.sdk.onetouch.core.Request) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BillingAgreementRequest(com.paypal.android.sdk.onetouch.core.BillingAgreementRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) PendingRequest(com.paypal.android.sdk.onetouch.core.sdk.PendingRequest) BrowserSwitchException(com.braintreepayments.api.exceptions.BrowserSwitchException) Result(com.paypal.android.sdk.onetouch.core.Result)

Example 2 with Result

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

the class BrowserSwitchHelperUnitTest method handleBrowserResponse_sendsEventForReturn.

@Test
public void handleBrowserResponse_sendsEventForReturn() {
    Result expectedResult = new Result("test", ResponseType.web, new JSONObject(), "");
    CheckoutRequest request = mock(CheckoutRequest.class);
    when(request.parseBrowserResponse(any(ContextInspector.class), any(Uri.class))).thenReturn(expectedResult);
    BrowserSwitchHelper.parseBrowserSwitchResponse(mContextInspector, request, mock(Uri.class));
    verify(request).trackFpti(any(Context.class), eq(TrackingPoint.Return), isNull(Protocol.class));
}
Also used : Context(android.content.Context) ContextInspector(com.paypal.android.sdk.onetouch.core.base.ContextInspector) TestSetupHelper.getMockContextInspector(com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector) JSONObject(org.json.JSONObject) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) Protocol(com.paypal.android.sdk.onetouch.core.enums.Protocol) Uri(android.net.Uri) Result(com.paypal.android.sdk.onetouch.core.Result) Test(org.junit.Test)

Example 3 with Result

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

the class BrowserSwitchHelperUnitTest method handleBrowserResponse_parsesResponse.

@Test
public void handleBrowserResponse_parsesResponse() {
    Result expectedResult = new Result();
    CheckoutRequest request = mock(CheckoutRequest.class);
    when(request.parseBrowserResponse(any(ContextInspector.class), any(Uri.class))).thenReturn(expectedResult);
    Result result = BrowserSwitchHelper.parseBrowserSwitchResponse(mContextInspector, request, mock(Uri.class));
    assertEquals(expectedResult, result);
}
Also used : ContextInspector(com.paypal.android.sdk.onetouch.core.base.ContextInspector) TestSetupHelper.getMockContextInspector(com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) Uri(android.net.Uri) Result(com.paypal.android.sdk.onetouch.core.Result) Test(org.junit.Test)

Example 4 with Result

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

the class AppSwitchHelperUnitTest method parseAppSwitchResponse_parsesErrorAndReturnsResult.

@Test
public void parseAppSwitchResponse_parsesErrorAndReturnsResult() {
    Request request = mock(AuthorizationRequest.class);
    when(request.validateV1V2Response(any(ContextInspector.class), any(Bundle.class))).thenReturn(true);
    Intent intent = new Intent().putExtra("error", "there was an error");
    Result result = AppSwitchHelper.parseAppSwitchResponse(mContextInspector, request, intent);
    verify(request).trackFpti(any(Context.class), eq(TrackingPoint.Return), isNull(Protocol.class));
    assertTrue(result.getError() instanceof WalletSwitchException);
    assertEquals("there was an error", result.getError().getMessage());
}
Also used : Context(android.content.Context) ContextInspector(com.paypal.android.sdk.onetouch.core.base.ContextInspector) TestSetupHelper.getMockContextInspector(com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector) Bundle(android.os.Bundle) Request(com.paypal.android.sdk.onetouch.core.Request) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) Intent(android.content.Intent) WalletSwitchException(com.paypal.android.sdk.onetouch.core.exception.WalletSwitchException) Protocol(com.paypal.android.sdk.onetouch.core.enums.Protocol) Result(com.paypal.android.sdk.onetouch.core.Result) Test(org.junit.Test)

Example 5 with Result

use of com.paypal.android.sdk.onetouch.core.Result 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)

Aggregations

Result (com.paypal.android.sdk.onetouch.core.Result)11 CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)10 ContextInspector (com.paypal.android.sdk.onetouch.core.base.ContextInspector)9 TestSetupHelper.getMockContextInspector (com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector)9 Test (org.junit.Test)9 Context (android.content.Context)8 Protocol (com.paypal.android.sdk.onetouch.core.enums.Protocol)8 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)6 Request (com.paypal.android.sdk.onetouch.core.Request)6 Intent (android.content.Intent)5 Bundle (android.os.Bundle)5 Uri (android.net.Uri)4 JSONObject (org.json.JSONObject)4 WalletSwitchException (com.paypal.android.sdk.onetouch.core.exception.WalletSwitchException)3 ResponseParsingException (com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 BrowserSwitchException (com.braintreepayments.api.exceptions.BrowserSwitchException)1 PayPalRequest (com.braintreepayments.api.models.PayPalRequest)1 BillingAgreementRequest (com.paypal.android.sdk.onetouch.core.BillingAgreementRequest)1 ResponseType (com.paypal.android.sdk.onetouch.core.enums.ResponseType)1