※ Lagom

Golang

If you want to verify Lagom micro-payments directly from your Golang server, you can use the following snippet.

It comes with no requirement for third-party dependencies. You can explore the whole file on our integration-example repository.

func lagomVerify(req *http.Request, page string, amount int) error {
	// extract callback params from URL, decode and parse
	lguid := req.URL.Query().Get("lguid")
	lgts := req.URL.Query().Get("lgts")
	lgsig := req.URL.Query().Get("lgsig")
	lgid := req.URL.Query().Get("lgid")
	lgamt := req.URL.Query().Get("lgamt")

	// verify timestamp freshness
	ts, err := strconv.Atoi(lgts)
	if err != nil || int(time.Now().UTC().Unix()) > ts+10 {
		return fmt.Errorf("This link has expired")
	}

	// check amount and page
	lgamtint, err := strconv.Atoi(lgamt)
	if err != nil || lgamtint != amount {
		return fmt.Errorf("This link is not valid")
	}

	// verify signature with pre shared secret
	mac := hmac.New(sha256.New, []byte(SECRET))
	mac.Write([]byte(lguid + lgid + lgts + page + lgamt))
	ret := mac.Sum(nil)
	if lgsig != fmt.Sprintf("%x", ret) {
		return fmt.Errorf("This link is not valid")
	}

	return nil
}