use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class Ideal method startPayment.
/**
* Initiates the payment flow by opening a browser where the customer can authenticate with their bank.
*
* @param fragment {@link BraintreeFragment}
* @param builder {@link IdealRequest} with the payment details.
* @param listener {@link BraintreeResponseListener} the callback to which the {@link IdealResult} will be sent
* with a status of `PENDING` before the flow starts. This result contains the iDEAL payment ID.
*/
public static void startPayment(final BraintreeFragment fragment, final IdealRequest builder, final BraintreeResponseListener<IdealResult> listener) {
fragment.waitForConfiguration(new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
fragment.sendAnalyticsEvent("ideal.start-payment.selected");
Exception configException = checkIdealEnabled(configuration);
if (configException != null) {
fragment.postCallback(configException);
fragment.sendAnalyticsEvent("ideal.start-payment.invalid-configuration");
return;
}
String redirectUrl = URI.create(configuration.getIdealConfiguration().getAssetsUrl() + ASSET_SERVER_REDIRECT_PATH + fragment.getReturnUrlScheme() + "://").toString();
fragment.getBraintreeApiHttpClient().post("/ideal-payments", builder.build(redirectUrl, configuration.getIdealConfiguration().getRouteId()), new HttpResponseCallback() {
@Override
public void success(String responseBody) {
try {
IdealResult idealResult = IdealResult.fromJson(responseBody);
BraintreeSharedPreferences.putString(fragment.getApplicationContext(), IDEAL_RESULT_ID, idealResult.getId());
if (listener != null) {
listener.onResponse(idealResult);
}
JSONObject responseJson = new JSONObject(responseBody);
String approvalUrl = responseJson.getJSONObject("data").getString("approval_url");
fragment.browserSwitch(BraintreeRequestCodes.IDEAL, approvalUrl);
fragment.sendAnalyticsEvent("ideal.webswitch.initiate.succeeded");
} catch (JSONException jsonException) {
failure(jsonException);
}
}
@Override
public void failure(Exception exception) {
fragment.sendAnalyticsEvent("ideal.webswitch.initiate.failed");
fragment.postCallback(exception);
}
});
}
});
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class TokenizationClient method tokenizeGraphQL.
private static void tokenizeGraphQL(final BraintreeFragment fragment, final CardBuilder cardBuilder, final PaymentMethodNonceCallback callback) {
fragment.sendAnalyticsEvent("card.graphql.tokenization.started");
String payload;
try {
payload = cardBuilder.buildGraphQL(fragment.getApplicationContext(), fragment.getAuthorization());
} catch (BraintreeException e) {
callback.failure(e);
return;
}
fragment.getGraphQLHttpClient().post(payload, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
try {
callback.success(parsePaymentMethodNonces(responseBody, cardBuilder.getResponsePaymentMethodType()));
fragment.sendAnalyticsEvent("card.graphql.tokenization.success");
} catch (JSONException e) {
callback.failure(e);
}
}
@Override
public void failure(Exception exception) {
fragment.sendAnalyticsEvent("card.graphql.tokenization.failure");
callback.failure(exception);
}
});
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class AmericanExpress method getRewardsBalance.
/**
* Gets the rewards balance associated with a Braintree nonce. Only for American Express cards.
*
* @param fragment the {@link BraintreeFragment} This fragment will also be responsible
* for handling callbacks to it's listeners
* @param nonce A nonce representing a card that will be used to look up the rewards balance
* @param currencyIsoCode The currencyIsoCode to use. Example: 'USD'
*/
public static void getRewardsBalance(final BraintreeFragment fragment, final String nonce, final String currencyIsoCode) {
fragment.waitForConfiguration(new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
String getRewardsBalanceUrl = Uri.parse(AMEX_REWARDS_BALANCE_PATH).buildUpon().appendQueryParameter("paymentMethodNonce", nonce).appendQueryParameter("currencyIsoCode", currencyIsoCode).build().toString();
fragment.sendAnalyticsEvent("amex.rewards-balance.start");
fragment.getHttpClient().get(getRewardsBalanceUrl, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fragment.sendAnalyticsEvent("amex.rewards-balance.success");
try {
fragment.postAmericanExpressCallback(AmericanExpressRewardsBalance.fromJson(responseBody));
} catch (JSONException e) {
fragment.sendAnalyticsEvent("amex.rewards-balance.parse.failed");
fragment.postCallback(e);
}
}
@Override
public void failure(Exception exception) {
fragment.postCallback(exception);
fragment.sendAnalyticsEvent("amex.rewards-balance.error");
}
});
}
});
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class ConfigurationManager method getConfiguration.
static void getConfiguration(final BraintreeFragment fragment, @NonNull final ConfigurationListener listener, @NonNull final BraintreeResponseListener<Exception> errorListener) {
final String configUrl = Uri.parse(fragment.getAuthorization().getConfigUrl()).buildUpon().appendQueryParameter("configVersion", "3").build().toString();
Configuration cachedConfig = getCachedConfiguration(fragment.getApplicationContext(), configUrl + fragment.getAuthorization().getBearer());
if (cachedConfig != null) {
listener.onConfigurationFetched(cachedConfig);
} else {
sFetchingConfiguration = true;
fragment.getHttpClient().get(configUrl, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
try {
Configuration configuration = Configuration.fromJson(responseBody);
cacheConfiguration(fragment.getApplicationContext(), configUrl + fragment.getAuthorization().getBearer(), configuration);
sFetchingConfiguration = false;
listener.onConfigurationFetched(configuration);
} catch (final JSONException e) {
sFetchingConfiguration = false;
errorListener.onResponse(e);
}
}
@Override
public void failure(final Exception exception) {
sFetchingConfiguration = false;
errorListener.onResponse(exception);
}
});
}
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class HttpClientTest method postsErrorWhenPathIsNull.
@Test(timeout = 1000)
public void postsErrorWhenPathIsNull() throws InterruptedException {
HttpClient httpClient = new HttpClient();
final CountDownLatch countDownLatch = new CountDownLatch(2);
httpClient.get(null, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(IllegalArgumentException.class, exception.getClass());
assertEquals("Path cannot be null", exception.getMessage());
countDownLatch.countDown();
}
});
httpClient.post(null, "{}", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(IllegalArgumentException.class, exception.getClass());
assertEquals("Path cannot be null", exception.getMessage());
countDownLatch.countDown();
}
});
countDownLatch.await();
}
Aggregations