Skip to content

Instantly share code, notes, and snippets.

@CeoFred
Created November 12, 2024 12:11
Show Gist options
  • Save CeoFred/729e91430473a5e9bb8af41f5e177f57 to your computer and use it in GitHub Desktop.
Save CeoFred/729e91430473a5e9bb8af41f5e177f57 to your computer and use it in GitHub Desktop.
Generate paystack webhook signature using golang for testing purposes
package main
import (
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"fmt"
)
func createSignature(payload []byte, secretKey string) string {
mac := hmac.New(sha512.New, []byte(secretKey))
mac.Write(payload)
return hex.EncodeToString(mac.Sum(nil))
}
func main() {
jsonString := `{
"event": "charge.success",
"data": {
"id": 302961,
"domain": "live",
"status": "success",
"reference": "018d0d84-b061-77ba-8610-365e16f2e10f--018d0d84-b066-75aa-b2c9-6f37bf4bb7f1",
"amount": 10000,
"message": null,
"gateway_response": "Approved by Financial Institution",
"paid_at": "2016-09-30T21:10:19.000Z",
"created_at": "2016-09-30T21:09:56.000Z",
"channel": "card",
"currency": "NGN",
"ip_address": "41.242.49.37",
"metadata": 0,
"log": {
"time_spent": 16,
"attempts": 1,
"authentication": "pin",
"errors": 0,
"success": false,
"mobile": false,
"input": [],
"channel": null,
"history": [
{
"type": "input",
"message": "Filled these fields: card number, card expiry, card cvv",
"time": 15
},
{
"type": "action",
"message": "Attempted to pay",
"time": 15
},
{
"type": "auth",
"message": "Authentication Required: pin",
"time": 16
}
]
},
"fees": null,
"customer": {
"id": 68324,
"first_name": "BoJack",
"last_name": "Horseman",
"email": "[email protected]",
"customer_code": "CUS_qo38as2hpsgk2r0",
"phone": null,
"metadata": null,
"risk_action": "default"
},
"authorization": {
"authorization_code": "AUTH_f5rnfq9p",
"bin": "539999",
"last4": "8877",
"exp_month": "08",
"exp_year": "2020",
"card_type": "mastercard DEBIT",
"bank": "Guaranty Trust Bank",
"country_code": "NG",
"brand": "mastercard",
"account_name": "BoJack Horseman"
},
"plan": {}
}
}`
// Convert JSON string to []byte
jsonBytes := []byte(jsonString)
// Corrected: Call createSignature with jsonBytes
signature := createSignature(jsonBytes, "sk_test_")
// Print the generated signature
fmt.Println("Generated Signature:", signature)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment