1

I call api for pin payment : https://test-api.pin.net.au/1/cards/

I try on postman , it good work.

When i apply into code android with okhttp 2.7.5

My code :

OkHttpClient httpClient = new OkHttpClient();

    RequestBody requestBody = new FormEncodingBuilder()
            .add("publishable_api_key", BuildConfig.PIN_PAYMENT_PUBLISHABLE_KEY)
            .add("number", scanResult.cardNumber)
            .add("expiry_month", scanResult.expiryMonth+"")
            .add("expiry_year", scanResult.expiryYear+"")
            .add("cvc", scanResult.cvv)
            .add("address_postcode", scanResult.postalCode)
            .add("name", name).build();


    Request request = new Request.Builder()
            .url(BuildConfig.BASE_URL_PIN_PAYMENT + Constants.API_CARD_PIN_PAYMENT)
            .post(requestBody)
            .build();

    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            view.hideLoading();
            view.showError(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            view.hideLoading();
            if (!response.isSuccessful()){
                String body = "Something went wrong.Please try again later.";
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    body = object.getString("error_description");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                view.showError(body);
            }else {
                view.hideLoading();
                Gson gson = new Gson();
                CardPinPaymentResponse paymentResponse = gson.fromJson(response.body().string() , CardPinPaymentResponse.class);
                view.getTokenCardSuccess(paymentResponse);
            }
        }

    });

But it not work and issue : javax.net.ssl.SSLException: Connection closed by peer

1 Answers1

1

When you connect to https server the OKhttp needs to know which TLS versions and cipher suites to offer. Here is the example code to connect with https

    ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)  
    .tlsVersions(TlsVersion.TLS_1_2)
    .cipherSuites(
          CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
          CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
          CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
    .build();

OkHttpClient client = new OkHttpClient.Builder() 
    .connectionSpecs(Collections.singletonList(spec))
    .build();

Good luck!!!

Liem Vo
  • 5,149
  • 2
  • 18
  • 16
  • 1
    Thanks you , but it not work , issue now : Unable to find acceptable protocols. isFallback=false, modes=[ConnectionSpec(cipherSuites=[TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_DHE_RSA_WITH_AES_128_GCM_SHA256], tlsVersions=[TLS_1_1], supportsTlsExtensions=true)], supported protocols=[SSLv3, TLSv1] – Nguyễn TrungHậu Apr 25 '17 at 03:32
  • It is only example code. You need to know which TLS version that is using on your sever then set in the client code – Liem Vo Apr 25 '17 at 04:15
  • i have the same problem, and i also try the TLS version of my server but not found any solution for kitkat, its working fine on all version greater then kitkat, if you found any solution please let me know also – Shoaib Anwar Jul 31 '17 at 08:01
  • @shoaib for me its not working for android 8.0 itself. Can you help me on this issue ? https://stackoverflow.com/questions/49946350/okhttp-android-websocket-and-jetty-server-connection-with-ssl – Firnaz Apr 27 '18 at 07:08
  • Can you look into this, if it's resolve your problem - https://stackoverflow.com/a/45853669/3448003 – Bajrang Hudda Jan 09 '20 at 11:09