Search in sources :

Example 6 with CheckoutRequest

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

the class BrowserSwitchHelperUnitTest method handleBrowserResponse_sendsEventForCancel.

@Test
public void handleBrowserResponse_sendsEventForCancel() {
    Result expectedResult = new Result();
    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.Cancel), 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) 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 7 with CheckoutRequest

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

the class AppSwitchHelperUnitTest method buildAppSwitchIntent_buildsIntentForCheckoutRequest.

@Test
public void buildAppSwitchIntent_buildsIntentForCheckoutRequest() {
    CheckoutRequest request = mock(CheckoutRequest.class);
    when(request.getEnvironment()).thenReturn("test");
    when(request.getBrowserSwitchUrl(any(Context.class), any(OtcConfiguration.class))).thenReturn("web-url");
    Intent intent = AppSwitchHelper.getAppSwitchIntent(mContextInspector, mConfigManager, request, getMockRecipe(2));
    assertEquals("web", intent.getStringExtra("response_type"));
    assertEquals("web-url", intent.getStringExtra("webURL"));
}
Also used : Context(android.content.Context) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) Intent(android.content.Intent) OtcConfiguration(com.paypal.android.sdk.onetouch.core.config.OtcConfiguration) Test(org.junit.Test)

Example 8 with CheckoutRequest

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

the class AppSwitchHelper method getAppSwitchIntent.

public static Intent getAppSwitchIntent(ContextInspector contextInspector, ConfigManager configManager, Request request, Recipe recipe) {
    Intent intent = createBaseIntent(recipe.getTargetIntentAction(), WALLET_APP_PACKAGE).putExtra("version", recipe.getProtocol().getVersion()).putExtra("app_guid", InstallationIdentifier.getInstallationGUID(contextInspector.getContext())).putExtra("client_metadata_id", request.getClientMetadataId()).putExtra("client_id", request.getClientId()).putExtra("app_name", DeviceInspector.getApplicationInfoName(contextInspector.getContext())).putExtra("environment", request.getEnvironment()).putExtra("environment_url", EnvironmentManager.getEnvironmentUrl(request.getEnvironment()));
    if (request instanceof AuthorizationRequest) {
        AuthorizationRequest authorizationRequest = (AuthorizationRequest) request;
        intent.putExtra("scope", authorizationRequest.getScopeString()).putExtra("response_type", "code").putExtra("privacy_url", authorizationRequest.getPrivacyUrl()).putExtra("agreement_url", authorizationRequest.getUserAgreementUrl());
    } else {
        CheckoutRequest checkoutRequest = (CheckoutRequest) request;
        String webURL = checkoutRequest.getBrowserSwitchUrl(contextInspector.getContext(), configManager.getConfig());
        intent.putExtra("response_type", "web").putExtra("webURL", webURL);
    }
    return intent;
}
Also used : AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) Intent(android.content.Intent)

Example 9 with CheckoutRequest

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

the class PayPal method parseResponse.

/**
 * Parse the PayPal response URL using OneTouchCore.
 *
 * @param paypalRequest Original {@link PayPalRequest} that started this flow.
 * @param result Context that received the result.
 * @param intent The {@link Intent} returned in result.
 * @return A {@link PayPalAccountBuilder} or null if the intent is invalid.
 */
private static PayPalAccountBuilder parseResponse(PayPalRequest paypalRequest, Request request, Result result, Intent intent) {
    PayPalAccountBuilder paypalAccountBuilder = new PayPalAccountBuilder().clientMetadataId(request.getClientMetadataId());
    if (request instanceof CheckoutRequest && paypalRequest != null) {
        paypalAccountBuilder.intent(paypalRequest.getIntent());
    }
    if (isAppSwitch(intent)) {
        paypalAccountBuilder.source("paypal-app");
    } else {
        paypalAccountBuilder.source("paypal-browser");
    }
    JSONObject payload = result.getResponse();
    // Modify payload in 'mock' mode to scope the response
    try {
        JSONObject clientJson = payload.getJSONObject("client");
        JSONObject response = payload.getJSONObject("response");
        if (EnvironmentManager.MOCK.equalsIgnoreCase(clientJson.getString("client")) && response.getString("code") != null && !(request instanceof CheckoutRequest)) {
            payload.put("response", new JSONObject().put("code", "fake-code:" + ((AuthorizationRequest) request).getScopeString()));
        }
    } catch (JSONException ignored) {
    }
    paypalAccountBuilder.oneTouchCoreData(payload);
    return paypalAccountBuilder;
}
Also used : JSONObject(org.json.JSONObject) PayPalAccountBuilder(com.braintreepayments.api.models.PayPalAccountBuilder) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) JSONException(org.json.JSONException)

Example 10 with CheckoutRequest

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

the class PayPal method createPaymentResource.

/**
 * Create a PayPalPaymentResource on behalf of the merchant. To be used in the PayPal Checkout
 * flows for Single Payment and Billing Agreement.
 *
 * @param fragment A {@link BraintreeFragment} used to process the request.
 * @param request A {@link PayPalRequest} used to customize the request.
 * @param isBillingAgreement A boolean. If true, this will use the Billing Agreement. Otherwise,
 *        PayPal will perform a Single Payment.
 * @param callback A callback on the http request.
 */
private static void createPaymentResource(BraintreeFragment fragment, PayPalRequest request, boolean isBillingAgreement, HttpResponseCallback callback) throws JSONException, ErrorWithResponse, BraintreeException {
    String currencyCode = request.getCurrencyCode();
    if (currencyCode == null) {
        currencyCode = fragment.getConfiguration().getPayPal().getCurrencyIsoCode();
    }
    CheckoutRequest checkoutRequest = getCheckoutRequest(fragment, null);
    JSONObject parameters = new JSONObject().put(RETURN_URL_KEY, checkoutRequest.getSuccessUrl()).put(CANCEL_URL_KEY, checkoutRequest.getCancelUrl()).put(OFFER_CREDIT_KEY, request.shouldOfferCredit());
    if (fragment.getAuthorization() instanceof ClientToken) {
        parameters.put(AUTHORIZATION_FINGERPRINT_KEY, fragment.getAuthorization().getBearer());
    } else {
        parameters.put(TOKENIZATION_KEY, fragment.getAuthorization().getBearer());
    }
    if (!isBillingAgreement) {
        parameters.put(AMOUNT_KEY, request.getAmount()).put(CURRENCY_ISO_CODE_KEY, currencyCode).put(INTENT_KEY, request.getIntent());
    } else {
        if (!TextUtils.isEmpty(request.getBillingAgreementDescription())) {
            parameters.put(DESCRIPTION_KEY, request.getBillingAgreementDescription());
        }
    }
    JSONObject experienceProfile = new JSONObject();
    experienceProfile.put(NO_SHIPPING_KEY, !request.isShippingAddressRequired());
    experienceProfile.put(LANDING_PAGE_TYPE_KEY, request.getLandingPageType());
    String displayName = request.getDisplayName();
    if (TextUtils.isEmpty(displayName)) {
        displayName = fragment.getConfiguration().getPayPal().getDisplayName();
    }
    experienceProfile.put(DISPLAY_NAME_KEY, displayName);
    if (request.getLocaleCode() != null) {
        experienceProfile.put(LOCALE_CODE_KEY, request.getLocaleCode());
    }
    if (request.getShippingAddressOverride() != null) {
        experienceProfile.put(ADDRESS_OVERRIDE_KEY, true);
        PostalAddress shippingAddress = request.getShippingAddressOverride();
        parameters.put(PostalAddress.LINE_1_KEY, shippingAddress.getStreetAddress());
        parameters.put(PostalAddress.LINE_2_KEY, shippingAddress.getExtendedAddress());
        parameters.put(PostalAddress.LOCALITY_KEY, shippingAddress.getLocality());
        parameters.put(PostalAddress.REGION_KEY, shippingAddress.getRegion());
        parameters.put(PostalAddress.POSTAL_CODE_UNDERSCORE_KEY, shippingAddress.getPostalCode());
        parameters.put(PostalAddress.COUNTRY_CODE_UNDERSCORE_KEY, shippingAddress.getCountryCodeAlpha2());
        parameters.put(PostalAddress.RECIPIENT_NAME_UNDERSCORE_KEY, shippingAddress.getRecipientName());
    } else {
        experienceProfile.put(ADDRESS_OVERRIDE_KEY, false);
    }
    parameters.put(EXPERIENCE_PROFILE_KEY, experienceProfile);
    String apiUrl = isBillingAgreement ? SETUP_BILLING_AGREEMENT_ENDPOINT : CREATE_SINGLE_PAYMENT_ENDPOINT;
    String versionedPath = "/v1/" + apiUrl;
    fragment.getHttpClient().post(versionedPath, parameters.toString(), callback);
}
Also used : PostalAddress(com.braintreepayments.api.models.PostalAddress) JSONObject(org.json.JSONObject) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) ClientToken(com.braintreepayments.api.models.ClientToken)

Aggregations

CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)10 Test (org.junit.Test)7 Context (android.content.Context)5 Uri (android.net.Uri)4 Result (com.paypal.android.sdk.onetouch.core.Result)4 ContextInspector (com.paypal.android.sdk.onetouch.core.base.ContextInspector)4 Protocol (com.paypal.android.sdk.onetouch.core.enums.Protocol)4 TestSetupHelper.getMockContextInspector (com.paypal.android.sdk.onetouch.core.test.TestSetupHelper.getMockContextInspector)4 Intent (android.content.Intent)3 JSONObject (org.json.JSONObject)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 ClientToken (com.braintreepayments.api.models.ClientToken)1 Configuration (com.braintreepayments.api.models.Configuration)1 PayPalAccountBuilder (com.braintreepayments.api.models.PayPalAccountBuilder)1 PostalAddress (com.braintreepayments.api.models.PostalAddress)1 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)1 OtcConfiguration (com.paypal.android.sdk.onetouch.core.config.OtcConfiguration)1 TrackingPoint (com.paypal.android.sdk.onetouch.core.fpti.TrackingPoint)1 JSONException (org.json.JSONException)1