Files
zeekay 1943f1c093 Hanzo Commerce
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans,
usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0.

Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
2026-09-23 21:52:16 -07:00

50 lines
1.2 KiB
Go

package recaptcha
import (
"context"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/hanzoai/commerce/log"
"github.com/hanzoai/commerce/util/json"
)
type RecaptchaResponse struct {
Success bool `json:"success"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
func Challenge(ctx context.Context, privateKey, response string) bool {
// log.Warn("Captcha:\n\n%s\n\n%s\n\n%s", privateKey, response, ctx)
client := &http.Client{Timeout: 55 * time.Second}
r := RecaptchaResponse{}
resp, err := client.PostForm("https://www.google.com/recaptcha/api/siteverify",
url.Values{
"secret": {privateKey},
"response": {response},
// "remoteip": {remoteIp},
})
if err != nil {
log.Error("Captcha post error: %s", err, ctx)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
log.Info("Captcha %s", body, ctx)
if err != nil {
log.Error("Read error: could not read body: %s", err, ctx)
return false
}
err = json.Unmarshal(body, &r)
log.Info("Captcha %v", json.Encode(r), ctx)
if err != nil {
log.Error("Read error: got invalid JSON: %s", err, ctx)
return false
}
return r.Success
}