Skip to main content

Webhooks

Code samples to generate and verify signature hash for the data received via webhooks set from CHAIPort servers.

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
)
type WebhookResponseObj struct {
Currency string
Amount string
OrderRef string
MerchantOrderRef string
CountryCode string
ChannelOrderRef string
Status string
ChannelKey string
MethodName string
Signature string
}
func VerifySignature(webhookResponseObj WebhookResponseObj, secretKey string) bool {
params := make(url.Values)
params.Add("currency", webhookResponseObj.Currency)
params.Add("amount", webhookResponseObj.Amount)
params.Add("order_ref", webhookResponseObj.OrderRef)
params.Add("merchant_order_ref", webhookResponseObj.MerchantOrderRef)
params.Add("channel_order_ref", webhookResponseObj.ChannelOrderRef)
params.Add("country_code", webhookResponseObj.CountryCode)
params.Add("status", webhookResponseObj.Status)
params.Add("channel_key", webhookResponseObj.ChannelKey)
params.Add("method_name", webhookResponseObj.MethodName)
data = params.Encode()
secret := []byte(secretKey)
message := []byte(data)
hash := hmac.New(sha256.New, secret)
hash.Write(message)
hash_value := base64.StdEncodin.EncodeToString(hash.Sum(nil))
// compare this hash_value to one received in payment response
if hash_value != webhookResponseObj.Signature {
println("Hash verification failed, not from valid source")
return false
} else {
println("Hash verification succeded")
return true
}
}