checkout / checkout-sdk-java

Checkout.com SDK for Java
MIT License
24 stars 29 forks source link

Webhook processing #441

Open andreaskraschitzer opened 2 months ago

andreaskraschitzer commented 2 months ago

It would be great if you could pass a webhook to the library, and it would take care of the key / signature validation and parsing the payload into POJOs. It seems to me that the SDK doesn't support any webhook processing.

Environment

Description

I had a look around the sdk and also the official Checkout documentation but could not find any details about what Webhooks look like or how they should be processed. This leads me to

I found the EventResponse object closely aligns with the structure of the webhook payload so used it for an initial parsing. This then allows me to determine how the data should be further parsed based on the type field.

My usecase is card and payment processing, so based on the event type "card_verified" I went ahead and tried to parse the data field to the PaymentResponse which fails at PaymentResponse.source because com.checkout.payments.response.source.ResponseSource is just an interface and jackson doesn't know at this time which impelemtation should be used.

Example code

    public HttpStatus handlePspHook(final Configuration configuration, final String path, final Map<String, String> headers, final String data) {
        // authentication check

        final EventResponse event = objectMapper.readValue(data, EventResponse.class);
        switch (event.getType()) {
            case "card_verified" -> {
                final PaymentResponse payment = objectMapper.convertValue(event.getData(), PaymentResponse.class);
                ...

I also just now realized that EventResponse is from the previous package which use I tried to avoid so far.

Question

Do I have to build my own POJOs based on the structure of EventResponse and PaymentResponse to process the webhook data or is there a better way which I have missed?

armando-rodriguez-cko commented 2 weeks ago

Hi @andreaskraschitzer, sorry for the delay, here you have:

https://github.com/checkout/checkout-sdk-java/blob/master/src/main/java/com/checkout/payments/response/source/AlternativePaymentSourceResponse.java

And here:

https://github.com/checkout/checkout-sdk-java/blob/8e73e9d0dd2d86b79f78d151d3ceac162e607230/src/main/java/com/checkout/GsonSerializer.java#L97

For the correct deserialisation