Nhảy tới nội dung

Payment Response

Code samples to generate and verify signature hash for the data received as query parameters in redirection after payment completion.

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