# QuataPay in Go Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```go package quatapay import ( "bytes" "encoding/json" "fmt" "net/http" "os" "strings" "time" ) const base = "https://api.quatapay.com/api/v1" type Payment struct{ CheckoutURL, PaymentID string } // idempotencyKey: one per checkout attempt, reused on retries of that attempt. func CreatePayment(orderID string, amountXAF int64, idempotencyKey string) (*Payment, error) { key := os.Getenv("QUATAPAY_API_KEY") path := "/gateway/payments" if strings.HasPrefix(key, "qpay_test_") { path = "/gateway/sandbox/payments" } body, _ := json.Marshal(map[string]any{ "amount": amountXAF, // integer XAF "currency": "XAF", "customer_reference": orderID, "return_url": "https://your-site.example/pay/return?order=" + orderID, "cancel_url": "https://your-site.example/pay/cancel?order=" + orderID, }) req, err := http.NewRequest("POST", base+path, bytes.NewReader(body)) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+key) req.Header.Set("Content-Type", "application/json") req.Header.Set("Idempotency-Key", idempotencyKey) res, err := (&http.Client{Timeout: 30 * time.Second}).Do(req) if err != nil { return nil, err } defer res.Body.Close() var out map[string]any if err := json.NewDecoder(res.Body).Decode(&out); err != nil { return nil, err } data, _ := out["data"].(map[string]any) pay, _ := data["payment"].(map[string]any) if res.StatusCode < 200 || res.StatusCode > 299 { msg, _ := out["error"].(string) code, _ := out["code"].(string) return nil, fmt.Errorf("quatapay %d %s: %s", res.StatusCode, code, msg) } url, _ := data["checkout_url"].(string) id, _ := pay["id"].(string) return &Payment{CheckoutURL: url, PaymentID: id}, nil } ``` ## Verify a webhook ```go package quatapay import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "strconv" "time" ) func ValidSignature(secret string, rawBody []byte, timestamp, signature string) bool { ts, err := strconv.ParseInt(timestamp, 10, 64) if err != nil || timestamp == "" || signature == "" { return false } if d := time.Now().Unix() - ts; d > 300 || d < -300 { return false } mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(timestamp + ".")) mac.Write(rawBody) // the bytes exactly as received: io.ReadAll(r.Body) before any decoding expected := "sha256=" + hex.EncodeToString(mac.Sum(nil)) return hmac.Equal([]byte(expected), []byte(signature)) } ```