Search in sources :

Example 1 with UnionPayConfiguration

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

Aggregations

ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)1 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)1 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)1 Configuration (com.braintreepayments.api.models.Configuration)1 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1