Creating Custom Event Handlers

You can create a custom event handler using Java or Groovy.

Java Implementation

A custom event handler is a Java class that does the following:

  • Implements the com.matrixx.payments.events.PaymentGatewayEventHandler interface (available in the payment-gateway-sdk.jar file).
  • Has an @PaymentGateway annotation indicating to which payment gateway it applies. The annotation takes a single long argument which is used to identify the associated payment gateway by its ID.

You must include the compiled class on the classpath by saving the class/jar to /opt/mtx/ext/paymentservice.

The PaymentGatewayEventHandler defines a method for each transaction life cycle event. Default no-op implementations are included in the SDK so you only need to implement the methods you require.

The following is an example of Java implementation of the PaymentGatewayEventHandler interface that logs the event.

package com.matrixx.payments.example;
 
import com.matrixx.payments.annotation.PaymentGateway;
import com.matrixx.payments.events.PaymentGatewayEvent;
import com.matrixx.payments.events.PaymentGatewayEventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
/**
 * Example Payment Gateway Event Handler which just logs the event.
 */
@Slf4j
@Component
@PaymentGateway(1L)
public class ExamplePaymentGatewayEventHandler implements PaymentGatewayEventHandler {
 
    @Override
    public void onBeforeSendToGateway(final PaymentGatewayEvent event)
    {
        log.info("Before Send to Gateway: {}", event);
    }
 
    @Override
    public void onResponseFromGateway(final PaymentGatewayEvent event)
    {
        log.info("Response from Gateway: {}", event);
    }
}

The class has been decorated with the @Component so that it is picked up automatically by Spring if it is on the classpath (and within a sub-package of com.matrixx.payments). This also allows you to make use of Spring's auto-wiring feature to bring in any additional objects or properties.

Notice that it does not require the @Component annotation, it does not need to be in a specific package, and it supports Spring's auto-wiring functionality.

Groovy Implementation

You can create a custom event handler using Groovy. Groovy does not require the additional compilation that Java does. You must save the event handler .groovy file to /opt/mtx/ext/paymentservice.

The following example shows the Groovy equivalent of the Java implementation above.
import com.matrixx.payments.annotation.PaymentGateway;
import com.matrixx.payments.events.PaymentGatewayEvent;
import com.matrixx.payments.events.PaymentGatewayEventHandler;
 
 
/**
 * Example Payment Gateway Event Handler which just logs the event.
 */
@PaymentGateway(1L)
class ExamplePaymentGatewayEventHandler implements PaymentGatewayEventHandler {
 
    @Override
    public void onBeforeSendToGateway(final PaymentGatewayEvent event)
    {
        println "Before Send to Gateway: " + event
    }
 
    @Override
    public void onResponseFromGateway(final PaymentGatewayEvent event)
    {
        println "Response from Gateway: " + event
    }
}