Skip to main content

Payment Request

Code samples to generate Signature hash while initiating payment, Use the generate signature in Init-Payment API

package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/url"
"strconv"
)
type RequestObj struct {
ClientKey string
Currency string
Amount float64
MerchantOrderId string
SuccessUrl string
FailureUrl string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
// Create a url.Values map and add the necessary parameters
params := make(url.Values)
params.Add("client_key", requestObj.ClientKey)
params.Add("currency", requestObj.Currency)
params.Add("amount", strconv.FormatFloat(requestObj.Amount, 'f', -1, 64))
params.Add("merchant_order_id", requestObj.MerchantOrderId)
params.Add("success_url", requestObj.SuccessUrl)
params.Add("failure_url", requestObj.FailureUrl)
// Encode the parameters
data := params.Encode()
// fmt.Println("data is:", data)
// Create the HMAC hash using SHA-256
secret := []byte(portOneSecret)
message := []byte(data)
hash := hmac.New(sha256.New, secret)
hash.Write(message)
// Convert the hash to a base64 string
hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
return hashValue
}
func main() {
portOneKey := "PORTONE_KEY"
portOneSecret := "PORTONE_SECRET"
// The unique merchant order reference generated by the merchant
merchantOrderId := ""
requestObj := RequestObj{
ClientKey: portOneKey,
Currency: "USD",
Amount: 100.00,
MerchantOrderId: merchantOrderId,
SuccessUrl: "https://checkout.portone.cloud/success.html",
FailureUrl: "https://checkout.portone.cloud/failure.html",
}
// Generate the signature
signature := GenerateSignature(requestObj, portOneSecret)
// Output the signature
fmt.Println("Signature is:", signature)
}