33import com .braintreegateway .exceptions .BraintreeException ;
44import com .braintreegateway .exceptions .UnexpectedException ;
55import com .braintreegateway .util .Http ;
6- import com .braintreegateway .util .StringUtils ;
6+ import com .braintreegateway .util .HttpClient ;
77import com .fasterxml .jackson .jr .ob .JSON ;
8+
89import java .io .IOException ;
9- import java .io .InputStream ;
10- import java .net .HttpURLConnection ;
11- import java .net .URL ;
12- import java .nio .charset .StandardCharsets ;
10+ import java .util .HashMap ;
1311import java .util .Map ;
14- import java .util .zip .GZIPInputStream ;
12+
13+ import static com .braintreegateway .util .HttpClient .HttpResponse ;
14+ import static com .braintreegateway .util .HttpClient .Payload ;
15+ import static com .braintreegateway .util .HttpClient .RequestMethod ;
1516
1617public class ThreeDSecureGateway {
1718 private final Configuration configuration ;
@@ -28,27 +29,19 @@ public Result<ThreeDSecureLookupResponse> lookup(ThreeDSecureLookupRequest reque
2829 }
2930
3031 try {
31- URL url = new URL (configuration .getBaseURL () + configuration .getMerchantPath () +
32- "/client_api/v1/payment_methods/" + request .getNonce () + "/three_d_secure/lookup" );
33- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
34- connection .setRequestMethod ("POST" );
35- connection .addRequestProperty ("X-ApiVersion" , Configuration .apiVersion ());
36- connection .addRequestProperty ("Content-Type" , "application/json" );
37- connection .setDoOutput (true );
38- connection .getOutputStream ().write (request .toJSON ().getBytes (StandardCharsets .UTF_8 ));
39- connection .getOutputStream ().close ();
32+ Map <String , String > headers = new HashMap <>();
33+ headers .put ("X-ApiVersion" , Configuration .apiVersion ());
4034
41- boolean isError = connection .getResponseCode () != 201 ;
42- InputStream responseStream = isError ? connection .getErrorStream () : connection .getInputStream ();
43- if ("gzip" .equalsIgnoreCase (connection .getContentEncoding ())) {
44- responseStream = new GZIPInputStream (responseStream );
45- }
35+ String url = configuration .getBaseURL () + configuration .getMerchantPath () + "/client_api/v1/payment_methods/" + request .getNonce ()
36+ + "/three_d_secure/lookup" ;
4637
47- String rawResponse = StringUtils . inputStreamToString ( responseStream );
48- responseStream . close ( );
38+ HttpClient httpClient = configuration . getHttpClient ( );
39+ HttpResponse httpResponse = httpClient . request ( RequestMethod . POST , url , Payload . json ( headers , request . toJSON ()) );
4940
50- if (isError ) {
51- Http .throwExceptionIfErrorStatusCode (connection .getResponseCode (), rawResponse );
41+ int responseCode = httpResponse .getResponseCode ();
42+ String rawResponse = httpResponse .getResponseBody ();
43+ if (responseCode != 201 ) {
44+ Http .throwExceptionIfErrorStatusCode (responseCode , rawResponse );
5245 }
5346
5447 Map <String , Object > jsonResponse = JSON .std .mapFrom (rawResponse );
0 commit comments