Java SDK just gives generic error message

I am using Square Java SDK to develop my application. I am using OAuth to obtain access tokens for sellers and make requests on behalf of sellers. When I try to create a payment request on behalf of a seller using the OAuth token of the test seller account, I get a generic API Exception and it only prints HTTP NOT OK. It is hard to debug what exactly is causing this as the error message is just NOT OK and nothing else. I am using the default test account that’s assigned, which I assume has PAYMENTS_WRITE permission for me, I have also tried with another test I created with the PAYMENTS_WRITE grant type, but I am still getting the same error. Refer to the following lines of code that I am using to rest:

SquareClient client = new SquareClient.Builder()
                        .environment(Environment.SANDBOX)
                        .customUrl("https://connect.squareupsandbox.com")
                        .bearerAuthCredentials(new BearerAuthModel
                         .Builder("SELLER_OAUTH_TOKEN").build())
                    .build();
Money amountMoney = new Money(100L, "USD");
PaymentsApi paymentsApi = client.getPaymentsApi();
CreatePaymentRequest createPaymentRequest = new CreatePaymentRequest
                .Builder("cnon:card-nonce-ok", UUID)
                  .autocomplete(false)
                  .amountMoney(amountMoney)
                  .customerId("CUSTOMER_ID")
            .build();
CreatePaymentResponse createPaymentResponse = paymentsApi.createPayment(createPaymentRequest);

All I am getting is an API exception with the message HTTP NOT OK, is there a way I could look deep into this exception and find out the root cause?

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Cash App Payments
Make your First API Call
Take Payments

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The HTTP NOT OK message is a generic error message that the Square SDK throws when it encounters an HTTP status code that is not 200 OK. To get more detailed information about the error, you can catch the ApiException and call its getErrors() method. This will return a list of Error objects that contain more detailed information about what went wrong. Each Error object has a getDetail() method that returns a human-readable description of the error, and a getCode() method that returns a machine-readable code for the error. Here’s how you can modify your code to print out more detailed error information:

try {
    CreatePaymentResponse createPaymentResponse = paymentsApi.createPayment(createPaymentRequest);
} catch (ApiException e) {
    List<Error> errors = e.getErrors();
    for (Error error : errors) {
        System.out.println("Error code: " + error.getCode());
        System.out.println("Error detail: " + error.getDetail());
    }
}

This should give you more information about what’s going wrong with your CreatePayment request.

This answer was reviewed by @josh-square.