com.payu.sdk.api.Payments Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-client Show documentation
Show all versions of api-client Show documentation
A fresh implementation of the PayU API Client for Android
The newest version!
package com.payu.sdk.api;
import com.payu.sdk.api.exceptions.AuthenticationException;
import com.payu.sdk.api.exceptions.HttpException;
import com.payu.sdk.api.exceptions.NetworkException;
import com.payu.sdk.api.exceptions.PayUException;
import com.payu.sdk.api.model.Bank;
import com.payu.sdk.api.model.PaymentMethods;
import com.payu.sdk.api.model.payments.PaymentsBanksResponse;
import com.payu.sdk.api.model.payments.PaymentsMethodsResponse;
import com.payu.sdk.api.model.request.Command;
import com.payu.sdk.api.model.request.Request;
import com.payu.sdk.api.model.response.Response;
import com.payu.sdk.api.model.response.ResponseCode;
import java.util.List;
import static com.payu.sdk.api.util.CallbackUtils.checkCallback;
import static com.payu.sdk.api.util.ResponseUtils.validateResponse;
public final class Payments {
private final RawApi rawApi;
private final RequestCreator requestCreator;
Payments(RawApi rawApi, RequestCreator requestCreator) {
this.rawApi = rawApi;
this.requestCreator = requestCreator;
}
void doPing(Callback callback) {
checkCallback(callback);
try {
callback.success(doPing());
} catch (HttpException e) {
callback.httpError(e);
} catch (AuthenticationException e) {
callback.authenticationError(e);
} catch (PayUException e) {
callback.callApiError(e);
} catch (NetworkException e) {
callback.networkError(e);
}
}
void getPaymentMethods(Callback> callback) {
checkCallback(callback);
try {
callback.success(getPaymentMethods());
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (PayUException e) {
callback.callApiError(e);
} catch (HttpException e) {
callback.httpError(e);
} catch (NetworkException e) {
callback.networkError(e);
}
}
void getListPSEBanks(Callback> callback) {
checkCallback(callback);
try {
callback.success(getListPSEBanks());
} catch (HttpException e) {
callback.httpError(e);
} catch (PayUException e) {
callback.callApiError(e);
} catch (NetworkException e) {
callback.networkError(e);
} catch (AuthenticationException e) {
callback.authenticationError(e);
}
}
private boolean doPing()
throws HttpException, PayUException, NetworkException, AuthenticationException {
Request pingRequest = requestCreator.buildRequest(Command.PING);
Response pingResponse = rawApi.paymentServices().ping(pingRequest);
validateResponse(pingResponse);
return ResponseCode.SUCCESS.equals(pingResponse.getCode());
}
private List getPaymentMethods()
throws AuthenticationException, PayUException, HttpException, NetworkException {
Request paymentMethodRequest = requestCreator.buildRequest(Command.GET_PAYMENT_METHODS);
PaymentsMethodsResponse paymentsMethodsResponse =
rawApi.paymentServices().getPaymentMethods(paymentMethodRequest);
validateResponse(paymentsMethodsResponse);
return paymentsMethodsResponse.getPaymentMethods();
}
private List getListPSEBanks()
throws HttpException, PayUException, NetworkException, AuthenticationException {
PaymentsBanksResponse paymentsBanksResponse =
rawApi.paymentServices().getListBank(requestCreator.buildPaymentBankRequestByPSE());
validateResponse(paymentsBanksResponse);
return paymentsBanksResponse.getBanks();
}
}