diff --git a/openpay-android-test/src/mx/openpay/android/test/OpenpayTest.java b/openpay-android-test/src/mx/openpay/android/test/OpenpayTest.java index 16cc246..43ce86d 100644 --- a/openpay-android-test/src/mx/openpay/android/test/OpenpayTest.java +++ b/openpay-android-test/src/mx/openpay/android/test/OpenpayTest.java @@ -69,6 +69,14 @@ public void createToken() throws InterruptedException { this.signal.await(); } + @Test + public void createToken_ExpirationYearError() throws InterruptedException { + Card card = this.getCard(); + card.expirationYear(12); + this.openpay.createToken(card, new LocalOperationCallBack()); + this.signal.await(); + } + private Address getAddres() { return new Address().city("Mexico").countryCode("MX").line1("Calle de la felicidad").line2("Numero 20") .line3("Col ensueƱo").postalCode("76900").state("Mexico"); diff --git a/openpay-android/src/mx/openpay/android/services/BaseService.java b/openpay-android/src/mx/openpay/android/services/BaseService.java index 7eb0b27..3de6241 100644 --- a/openpay-android/src/mx/openpay/android/services/BaseService.java +++ b/openpay-android/src/mx/openpay/android/services/BaseService.java @@ -40,11 +40,14 @@ * */ public abstract class BaseService { - public final static JsonFactory JSON_FACTORY = new JacksonFactory(); + private final static JsonFactory JSON_FACTORY = new JacksonFactory(); private final static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport(); private final static String EMPTY_PASSWORD = ""; private final static String API_VERSION = "v1"; private final static String URL_SEPARATOR = "/"; + private static final String AGENT = "openpay-android/"; + + private static final int DEFAULT_CONNECTION_TIMEOUT = 60000; protected String baseUrl; protected String merchantId; @@ -60,15 +63,7 @@ public BaseService(final String baseUrl, final String merchantId, final String a } public HttpRequestFactory getRequestFactory() { - return HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { - @Override - public void initialize(final HttpRequest request) { - request.setParser(new JsonObjectParser(JSON_FACTORY)); - request.getHeaders().setBasicAuthentication(BaseService.this.apiKey, EMPTY_PASSWORD); - request.getHeaders().setUserAgent("OpenPay-"); - request.setUnsuccessfulResponseHandler(new ServiceUnsuccessfulResponseHandler()); - } - }); + return HTTP_TRANSPORT.createRequestFactory(new OpenpayHttpRequestInitializer()); } public GenericUrl getGenericUrl(final String resourceUrl) { @@ -97,15 +92,34 @@ public T post(final String resourceUrl, final V data) throws OpenpayServiceExcep } } -} + private class OpenpayHttpRequestInitializer implements HttpRequestInitializer { + @Override + public void initialize(final HttpRequest request) { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + request.getHeaders().setBasicAuthentication(BaseService.this.apiKey, EMPTY_PASSWORD); + String version = this.getClass().getPackage().getImplementationVersion(); + if (version == null) { + version = "1.0.1-UNKNOWN"; + } + request.setSuppressUserAgentSuffix(true); + request.getHeaders().setUserAgent(AGENT + version); + request.setUnsuccessfulResponseHandler(new ServiceUnsuccessfulResponseHandler()); + request.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT); + request.setReadTimeout(DEFAULT_CONNECTION_TIMEOUT); + } -class ServiceUnsuccessfulResponseHandler implements HttpUnsuccessfulResponseHandler { - @Override - public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean arg2) - throws IOException { - OpenpayServiceException exception = new JsonObjectParser(BaseService.JSON_FACTORY).parseAndClose( - response.getContent(), response.getContentCharset(), OpenpayServiceException.class); - throw exception; + private class ServiceUnsuccessfulResponseHandler implements HttpUnsuccessfulResponseHandler { + @Override + public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean arg2) + throws IOException { + OpenpayServiceException exception = new JsonObjectParser(BaseService.JSON_FACTORY).parseAndClose( + response.getContent(), response.getContentCharset(), OpenpayServiceException.class); + throw exception; + } + + } } } + +