All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.braintreepayments.api.data.BraintreeData Maven / Gradle / Ivy

There is a newer version: 1.7.10
Show newest version
package com.braintreepayments.api.data;

import android.app.Activity;
import android.content.Context;

import com.devicecollector.DeviceCollector;

import java.lang.reflect.Method;
import java.util.UUID;

/**
 * BraintreeData is used to collect device information to aid fraud detection and prevention.
 */
public final class BraintreeData {

    private String mFraudMerchantId;
    private String mDeviceSessionId;
    private String mCorrelationId;
    private DeviceCollector deviceCollector;

    /**
     * Creates a new BraintreeData instance for fraud detection.
     *
     * @param activity The currently visible activity.
     * @param environment The Braintree environment to use.
     */
    public BraintreeData(Activity activity, BraintreeEnvironment environment) {
        this(activity, environment.getMerchantId(), environment.getCollectorUrl());
    }

    /**
     * Creates a new BraintreeData instance for fraud detection. This should be used in conjunction
     * with a non-aggregate fraud id.
     *
     * @param activity The currently visible activity.
     * @param fraudMerchantId The fraud merchant id from Braintree.
     * @param collectorUrl The fraud collector url from Braintree.
     */
    public BraintreeData(Activity activity, String fraudMerchantId, String collectorUrl) {
        mFraudMerchantId = fraudMerchantId;
        mCorrelationId = getCorrelationId(activity);
        deviceCollector = new DeviceCollector(activity);
        deviceCollector.setMerchantId(mFraudMerchantId);
        deviceCollector.setCollectorUrl(collectorUrl);
    }

    /**
     * Call to get device_data to send to Braintree
     * @return String device_data JSON-encoded String of device data.
     */
    public String collectDeviceData() {
        if(mDeviceSessionId == null) {
            mDeviceSessionId = UUID.randomUUID().toString().replace("-", "");
            deviceCollector.collect(mDeviceSessionId);
        }

        String data = "{\"device_session_id\":\"" + mDeviceSessionId + "\"," +
                "\"fraud_merchant_id\":\"" + mFraudMerchantId + "\"";
        if (mCorrelationId != null) {
            data += ",\"correlation_id\": \"" + mCorrelationId + "\"}";
        } else {
            data += "}";
        }
        return data;
    }

    private String getCorrelationId(Activity activity) {
        try {
            Method method = getClass().getClassLoader()
                    .loadClass("com.paypal.android.sdk.payments.PayPalConfiguration")
                    .getMethod("getClientMetadataId", Context.class);
            return (String) method.invoke(null, activity);
        } catch (Exception ignored) {
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy