stripe / stripe-java

Java library for the Stripe API.
https://stripe.com
MIT License
827 stars 361 forks source link

ApiResource.GSON.fromJson(payload, Event.class) throws java.lang.classNoClassDefFoundError #1903

Closed andra-popa closed 1 month ago

andra-popa commented 1 month ago

Describe the bug

I have added the stripe dependency in the pom file as described in the documentation

            <dependency>
                <groupId>com.stripe</groupId>
                <artifactId>stripe-java</artifactId>
                <version>27.0.0</version>
            </dependency>

The codebase compiles just fine, I have set up my endpoint and I definitely receive Stripe events in it. The Json looks correct. This is the payload value seen in the debugger (triggered using stripe trigger payment_intent.succeeded):

{
  "id": "evt_3QCNqy2nnSX6rIgJ0aTZYppe",
  "object": "event",
  "api_version": "2023-10-16",
  "created": 1729525749,
  "data": {
    "object": {
      "id": "ch_3QCNqy2nnSX6rIgJ0U5meQMY",
      "object": "charge",
      "amount": 2000,
      "amount_captured": 2000,
      "amount_refunded": 0,
      "application": null,
      "application_fee": null,
      "application_fee_amount": null,
      "balance_transaction": "txn_3QCNqy2nnSX6rIgJ03fC2uYT",
      "billing_details": {
        "address": {
          "city": null,
          "country": null,
          "line1": null,
          "line2": null,
          "postal_code": null,
          "state": null
        },
        "email": null,
        "name": null,
        "phone": null
      },
      "calculated_statement_descriptor": "GRAVITY SKETCH INC.",
      "captured": true,
      "created": 1729525749,
      "currency": "usd",
      "customer": null,
      "description": "(created by Stripe CLI)",
      "destination": null,
      "dispute": null,
      "disputed": false,
      "failure_balance_transaction": null,
      "failure_code": null,
      "failure_message": null,
      "fraud_details": {
      },
      "invoice": null,
      "livemode": false,
      "metadata": {
      },
      "on_behalf_of": null,
      "order": null,
      "outcome": {
        "network_status": "approved_by_network",
        "reason": null,
        "risk_level": "normal",
        "risk_score": 55,
        "seller_message": "Payment complete.",
        "type": "authorized"
      },
      "paid": true,
      "payment_intent": "pi_3QCNqy2nnSX6rIgJ0TANaBqv",
      "payment_method": "pm_1QCNqy2nnSX6rIgJjogSvASF",
      "payment_method_details": {
        "card": {
          "amount_authorized": 2000,
          "authorization_code": null,
          "brand": "visa",
          "checks": {
            "address_line1_check": null,
            "address_postal_code_check": null,
            "cvc_check": "pass"
          },
          "country": "US",
          "exp_month": 10,
          "exp_year": 2025,
          "extended_authorization": {
            "status": "disabled"
          },
          "fingerprint": "X8SB2T0Y9n6XRxsM",
          "funding": "credit",
          "incremental_authorization": {
            "status": "unavailable"
          },
          "installments": null,
          "last4": "4242",
          "mandate": null,
          "multicapture": {
            "status": "unavailable"
          },
          "network": "visa",
          "network_token": {
            "used": false
          },
          "overcapture": {
            "maximum_amount_capturable": 2000,
            "status": "unavailable"
          },
          "three_d_secure": null,
          "wallet": null
        },
        "type": "card"
      },
      "radar_options": {
      },
      "receipt_email": null,
      "receipt_number": null,
      "receipt_url": "https://pay.stripe.com/receipts/payment/CAcaFwoVYWNjdF8xUTZEUlkybm5TWDZySWdKKPXv2bgGMgZGsPD28mE6LBbZjUvoVGkBRbYmtUzLXPC3Fq3IGwLAJZHz7Hkm1kRPtHivr8_9crVGQTXA",
      "refunded": false,
      "review": null,
      "shipping": {
        "address": {
          "city": "San Francisco",
          "country": "US",
          "line1": "510 Townsend St",
          "line2": null,
          "postal_code": "94103",
          "state": "CA"
        },
        "carrier": null,
        "name": "Jenny Rosen",
        "phone": null,
        "tracking_number": null
      },
      "source": null,
      "source_transfer": null,
      "statement_descriptor": null,
      "statement_descriptor_suffix": null,
      "status": "succeeded",
      "transfer_data": null,
      "transfer_group": null
    }
  },
  "livemode": false,
  "pending_webhooks": 2,
  "request": {
    "id": "req_BhFyKQ16D3ypdF",
    "idempotency_key": "7b8a1319-f3b9-486a-ae63-155b4f586dbe"
  },
  "type": "charge.succeeded"
}

But at runtime, the fromJson method doesn't even get called cause I get NoClassDefFoundError:

image

The local Stripe listener gets HTTP 500

is it possible I did not set the library up correctly? Note that the compiler finds the class just fine. Can I get some help with this?

To Reproduce

  1. Set up a local endpoint in a java server
  2. Start a local stripe listener
  3. trigger events using stripe trigger payment_intent.succeeded
  4. place a breakpoint on ApiResource.GSON.fromJson(payload, Event.class); and observe the behaviour

Expected behavior

ApiResource.GSON.fromJson(payload, Event.class); gets called and it parses the json into an Event class

Code snippets

This is my resource:

@Path("/stripe/events")
public class StripeEventsResource {
    public StripeEventsResource() {}

    @POST
    @Timed
    @ResponseMetered
    public Response processRequest(byte[] in, @Context HttpServletRequest httpServletRequest) {
        String payload = new String(in);
        Event event = null;
        try {
            event = ApiResource.GSON.fromJson(payload, Event.class); // I don't get past this line

            event = Webhook.constructEvent(
                payload, httpServletRequest.getHeader("Stripe-Signature"), "whsec_662cc1572cea897f91bec3d1268a01b1f624ea417d6e3b17c62c063502528694"
            );
            return Response.status(Response.Status.OK).build();
        } catch (Exception e) {
            // I don't see any of these logs and the debugger does not get here
            System.out.println("======== internal server error. message: " + e.getMessage());
            System.out.println("======== internal server error. type: " + e.getClass());
            System.out.println("======== internal server error. cause: " + e.getCause());

            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
        }
    }
}

OS

macOS

Java version

Java 21

stripe-java version

27.0.0

API version

2024-09-30

Additional context

No response

andra-popa commented 1 month ago

update: it doesn't work with this gson version:

2.8.9

I have updated to 2.10.1 and now it works fine