OneSpan Sign How To: Create a Callback Event Notification Listener in Go

Haris Haidary,

In a previous blog, I went over how to create and send a package using the Go programming language. Staying in the same vein, I will show you how to create a simple callback event notification listener in Go.

The Code

If you don’t already have Go installed, you can download it from the official website, here. In order to receive callback event notifications, you will need a publicly accessible URL to hit and work. Your localhost server on its own will not. Ngrok is a very simple and easy to use tool that creates a secure tunnel on your local machine along with a public URL you can use for browsing your local site. This saves you the trouble of deploying your web application. The complete example code is available on our Developer Community Code Share. Let's get started. In your favorite text editor, create a new file named "listener.go" and save it in a location of your choice. You can go ahead and copy the code below. I will go over it down below.

package main

import (
	"io"
	"log"
	"net/http"
	"os"
)

func handleEventNotification(w http.ResponseWriter, r *http.Request) {
	_, err := io.Copy(os.Stdout, r.Body)
	if err != nil {
		log.Println(err)
		return
	}
}

func main() {
    log.Println("Starting server...")
	http.HandleFunc("/event", handleEventNotification)
	log.Fatal(http.ListenAndServe(":4567", nil))
}

In the main() function, the http requests to the path /event are handled by the http.HandleFunc library using the handleEventNotification function.

The http.ListenAndServe line starts a new webserver on localhost:4567. It is important to note that http.ListenAndServe can return an error, so it wrapped around log.Fatal() in such situations.

In my example, I simply copy the JSON content to stdout using the io.Copy method (i.e. print the JSON payload to the terminal console).

Running Your Code

Open your command prompt and change the current directory to the location where you saved your "listener.go" file. Then, enter the following lines:

go build listener.go
listener.exe

Then, in a new command prompt window, change the current directory to the location when you saved your ngrok executable file and enter the following line:

ngrok http 4567 -host-header="localhost:4567"

Finally, login to your OneSpan Sign account and browse to the Admin page. Enter in the "Callback URL" field the URL to your callback listener (i.e. http://f3e93555.ngrok.io/event) and register for the "transaction complete" event. Finally, create, send, and complete a test package. You should be able to view the JSON sent by OneSpan Sign printed in the command prompt window.

1 2

If you have questions regarding this blog or anything else concerning integrating OneSpan Sign into your application, visit the developer community forums: https://developer.OneSpanSign.com. That's it from me. Thank you for reading! If you found this post helpful, please share it on Facebook, Twitter, or LinkedIn.

Haris Haidary
Junior Technical Evangelist
LinkedIn | Twitter

OneSpan Developer Community

OneSpan Developer Community

Join the OneSpan Developer Community! Forums, blogs, documentation, SDK downloads, and more.

Join Today