Search in sources :

Example 1 with ClientToken

use of com.braintreepayments.api.models.ClientToken in project braintree_android by braintree.

the class AnalyticsSender method serializeEvents.

private static JSONObject serializeEvents(Context context, Authorization authorization, List<AnalyticsEvent> events) throws JSONException {
    AnalyticsEvent primeEvent = events.get(0);
    JSONObject requestObject = new JSONObject();
    if (authorization instanceof ClientToken) {
        requestObject.put(AUTHORIZATION_FINGERPRINT_KEY, authorization.getBearer());
    } else {
        requestObject.put(TOKENIZATION_KEY, authorization.getBearer());
    }
    JSONObject meta = primeEvent.metadata.put(PLATFORM_KEY, "Android").put(INTEGRATION_TYPE_KEY, primeEvent.getIntegrationType()).put(PLATFORM_VERSION_KEY, Integer.toString(VERSION.SDK_INT)).put(SDK_VERSION_KEY, BuildConfig.VERSION_NAME).put(MERCHANT_APP_ID_KEY, context.getPackageName()).put(MERCHANT_APP_NAME_KEY, getAppName(context)).put(DEVICE_ROOTED_KEY, isDeviceRooted()).put(DEVICE_MANUFACTURER_KEY, Build.MANUFACTURER).put(DEVICE_MODEL_KEY, Build.MODEL).put(ANDROID_ID_KEY, getAndroidId(context)).put(DEVICE_APP_GENERATED_PERSISTENT_UUID_KEY, UUIDHelper.getPersistentUUID(context)).put(IS_SIMULATOR_KEY, detectEmulator());
    requestObject.put(META_KEY, meta);
    JSONArray eventObjects = new JSONArray();
    JSONObject eventObject;
    for (AnalyticsEvent analyticsEvent : events) {
        eventObject = new JSONObject().put(KIND_KEY, analyticsEvent.event).put(TIMESTAMP_KEY, analyticsEvent.timestamp);
        eventObjects.put(eventObject);
    }
    requestObject.put(ANALYTICS_KEY, eventObjects);
    return requestObject;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ClientToken(com.braintreepayments.api.models.ClientToken)

Example 2 with ClientToken

use of com.braintreepayments.api.models.ClientToken in project braintree_android by braintree.

the class BraintreeHttpClient method get.

/**
 * Make a HTTP GET request to Braintree using the base url, path and authorization provided.
 * If the path is a full url, it will be used instead of the previously provided url.
 *
 * @param path The path or url to request from the server via GET
 * @param callback The {@link HttpResponseCallback} to receive the response or error.
 */
@Override
public void get(String path, HttpResponseCallback callback) {
    if (path == null) {
        postCallbackOnMainThread(callback, new IllegalArgumentException("Path cannot be null"));
        return;
    }
    Uri uri;
    if (path.startsWith("http")) {
        uri = Uri.parse(path);
    } else {
        uri = Uri.parse(mBaseUrl + path);
    }
    if (mAuthorization instanceof ClientToken) {
        uri = uri.buildUpon().appendQueryParameter(AUTHORIZATION_FINGERPRINT_KEY, ((ClientToken) mAuthorization).getAuthorizationFingerprint()).build();
    }
    super.get(uri.toString(), callback);
}
Also used : ClientToken(com.braintreepayments.api.models.ClientToken) Uri(android.net.Uri)

Example 3 with ClientToken

use of com.braintreepayments.api.models.ClientToken in project braintree_android by braintree.

the class BraintreeGraphQLHttpClientTest method sendsAuthorizationFingerprintAsAuthorization.

@Test
public void sendsAuthorizationFingerprintAsAuthorization() throws IOException, InvalidArgumentException {
    String baseUrl = "http://example.com/graphql";
    ClientToken clientToken = (ClientToken) Authorization.fromString(stringFromFixture("client_token.json"));
    BraintreeGraphQLHttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, clientToken.getBearer());
    HttpURLConnection connection = httpClient.init(baseUrl);
    assertEquals("Bearer " + clientToken.getAuthorizationFingerprint(), connection.getRequestProperty("Authorization"));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ClientToken(com.braintreepayments.api.models.ClientToken) Test(org.junit.Test)

Example 4 with ClientToken

use of com.braintreepayments.api.models.ClientToken in project braintree_android by braintree.

the class ConfigurationManagerUnitTest method getConfiguration_takesClientTokenIntoAccountForCache.

@Test(timeout = 1000)
public void getConfiguration_takesClientTokenIntoAccountForCache() throws InvalidArgumentException, InterruptedException {
    ClientToken clientToken = (ClientToken) Authorization.fromString(stringFromFixture("client_token_with_authorization_fingerprint_options.json"));
    when(mBraintreeFragment.getAuthorization()).thenReturn(clientToken);
    writeMockConfiguration(RuntimeEnvironment.application, clientToken.getConfigUrl(), clientToken.getAuthorizationFingerprint(), stringFromFixture("configuration/configuration.json"), System.currentTimeMillis());
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            assertEquals(stringFromFixture("configuration/configuration.json"), configuration.toJson());
            mCountDownLatch.countDown();
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
            fail(e.getMessage());
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) SharedPreferencesHelper.writeMockConfiguration(com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration) Configuration(com.braintreepayments.api.models.Configuration) ClientToken(com.braintreepayments.api.models.ClientToken) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 5 with ClientToken

use of com.braintreepayments.api.models.ClientToken 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

ClientToken (com.braintreepayments.api.models.ClientToken)6 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 Uri (android.net.Uri)1 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)1 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)1 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)1 Configuration (com.braintreepayments.api.models.Configuration)1 PostalAddress (com.braintreepayments.api.models.PostalAddress)1 VenmoAccountNonce (com.braintreepayments.api.models.VenmoAccountNonce)1 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)1 CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)1 HttpURLConnection (java.net.HttpURLConnection)1 JSONArray (org.json.JSONArray)1