Search in sources :

Example 36 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class UnionPay method enroll.

/**
 * Enrolls a Union Pay card. Only call this method if the card needs to be enrolled. Check {@link
 * UnionPay#fetchCapabilities(BraintreeFragment, String)} if your card needs to be enrolled.
 * <p/>
 * On completion, returns a enrollmentId to
 * {@link com.braintreepayments.api.interfaces.UnionPayListener#onSmsCodeSent(String, boolean)}
 * This enrollmentId needs to be applied to {@link UnionPayCardBuilder} along with the SMS code
 * collected from the merchant before invoking {@link UnionPay#tokenize(BraintreeFragment, UnionPayCardBuilder)}
 * <p/>
 * On error, an exception will be passed back to
 * {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 *
 * @param fragment {@link BraintreeFragment}
 * @param unionPayCardBuilder {@link UnionPayCardBuilder}
 */
public static void enroll(final BraintreeFragment fragment, final UnionPayCardBuilder unionPayCardBuilder) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            UnionPayConfiguration unionPayConfiguration = configuration.getUnionPay();
            if (!unionPayConfiguration.isEnabled()) {
                fragment.postCallback(new ConfigurationException("UnionPay is not enabled"));
                return;
            }
            try {
                JSONObject enrollmentPayloadJson = unionPayCardBuilder.buildEnrollment();
                fragment.getHttpClient().post(UNIONPAY_ENROLLMENT_PATH, enrollmentPayloadJson.toString(), new HttpResponseCallback() {

                    @Override
                    public void success(String responseBody) {
                        try {
                            JSONObject response = new JSONObject(responseBody);
                            String enrollmentId = response.getString(UNIONPAY_ENROLLMENT_ID_KEY);
                            boolean smsCodeRequired = response.getBoolean(UNIONPAY_SMS_REQUIRED_KEY);
                            fragment.postUnionPayCallback(enrollmentId, smsCodeRequired);
                            fragment.sendAnalyticsEvent("union-pay.enrollment-succeeded");
                        } catch (JSONException e) {
                            failure(e);
                        }
                    }

                    @Override
                    public void failure(Exception exception) {
                        fragment.postCallback(exception);
                        fragment.sendAnalyticsEvent("union-pay.enrollment-failed");
                    }
                });
            } catch (JSONException exception) {
                fragment.postCallback(exception);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Example 37 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class BraintreeFragment method sendAnalyticsEvent.

public void sendAnalyticsEvent(final String eventFragment) {
    final AnalyticsEvent request = new AnalyticsEvent(mContext, getSessionId(), mIntegrationType, eventFragment);
    waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (configuration.getAnalytics().isEnabled()) {
                mAnalyticsDatabase.addEvent(request);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) AnalyticsEvent(com.braintreepayments.api.internal.AnalyticsEvent) Configuration(com.braintreepayments.api.models.Configuration)

Aggregations

ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)37 Configuration (com.braintreepayments.api.models.Configuration)36 Test (org.junit.Test)21 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)15 JSONException (org.json.JSONException)12 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)10 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)10 ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 BraintreeFragmentTestUtils.getFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration)4 BraintreeFragmentTestUtils.getMockFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)4 BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)4 JSONObject (org.json.JSONObject)4 Bundle (android.os.Bundle)3 Intent (android.content.Intent)2 BraintreeResponseListener (com.braintreepayments.api.interfaces.BraintreeResponseListener)2 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)2 Uri (android.net.Uri)1