mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-11-29 15:31:53 +00:00
Premium checkout: CoinPay hosted session with URL + QR in the SSH terminal (#156)
Some checks failed
CI / build (push) Has been cancelled
deploy / deploy (push) Has been cancelled
test / test (push) Has been cancelled
Some checks failed
CI / build (push) Has been cancelled
deploy / deploy (push) Has been cancelled
test / test (push) Has been cancelled
join@ now creates a CoinPay checkout session (POST /checkout/sessions) instead of a single-chain ETH payment: the buyer picks any coin the business accepts or PayPal. The session URL and a half-block QR code of it are shown in the terminal, and the session is polled (GET /checkout/sessions/:id) until paid, Enter, disconnect or expiry. property=bbs.profullstack.com, account_id=handle, customer_email=the verified email (asked for if none is stored), metadata carries pubkey fingerprint, user id and the HMAC ref. Legacy payment ids still verify. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8fe601b296
commit
0499cb60f0
7 changed files with 333 additions and 181 deletions
|
|
@ -1104,48 +1104,93 @@ func (a *app) offerPremium(s ssh.Session, in *bufio.Reader, u *store.User) {
|
||||||
}, "\n"))
|
}, "\n"))
|
||||||
|
|
||||||
// Explicit opt-in. Anything but yes leaves with no payment created.
|
// Explicit opt-in. Anything but yes leaves with no payment created.
|
||||||
wish.Print(s, "\n Become a Founding member now? Type \"yes\" for a payment address [no]: ")
|
wish.Print(s, "\n Become a Founding member now? Type \"yes\" for a checkout link [no]: ")
|
||||||
line, err := readLine(s, in)
|
line, err := readLine(s, in)
|
||||||
if err != nil || !isYes(line) {
|
if err != nil || !isYes(line) {
|
||||||
wish.Print(s, "\n No problem — you're a free member. Want it later? Re-run: ssh join@"+a.host+"\n\n")
|
wish.Print(s, "\n No problem, you're a free member. Want it later? Re-run: ssh join@"+a.host+"\n\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ref := payments.PremiumReference(u.PubKeyFP)
|
// CoinPay needs a receipt email. join@ verifies one first, but ask if a
|
||||||
c, ok, err := payments.CreatePremiumCharge(ref)
|
// legacy account has none on file.
|
||||||
if !ok || err != nil {
|
email := strings.TrimSpace(u.Email)
|
||||||
|
for tries := 0; !validEmail(email) && tries < 3; tries++ {
|
||||||
|
wish.Print(s, "\n Email for your receipt: ")
|
||||||
|
line, err := readLine(s, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("create premium charge", "err", err)
|
return
|
||||||
}
|
}
|
||||||
wish.Print(s, "\n Payment is temporarily unavailable — please try again shortly.\n\n")
|
email = strings.TrimSpace(line)
|
||||||
|
}
|
||||||
|
if !validEmail(email) {
|
||||||
|
wish.Print(s, "\n A valid email is needed for checkout. Re-run: ssh join@"+a.host+"\n\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Remember the payment id so a later connect can confirm settlement.
|
|
||||||
if err := a.st.SetPremiumPayment(u.ID, c.ID); err != nil {
|
ses, ok, err := payments.CreatePremiumSession(payments.Buyer{
|
||||||
log.Error("store premium payment id", "err", err)
|
AccountID: u.Name,
|
||||||
}
|
Email: email,
|
||||||
amount := "$" + payments.PremiumAmount() + " " + payments.PremiumCurrency()
|
PubKeyFP: u.PubKeyFP,
|
||||||
if c.CryptoAmount != "" {
|
UserID: u.ID,
|
||||||
cur := c.Currency
|
}, "https://"+a.host+"/~"+u.Name)
|
||||||
if cur == "" {
|
if !ok || err != nil {
|
||||||
cur = strings.ToUpper(payments.PremiumBlockchain())
|
if err != nil {
|
||||||
|
log.Error("create premium checkout session", "err", err)
|
||||||
}
|
}
|
||||||
amount += " (≈ " + c.CryptoAmount + " " + cur + ")"
|
wish.Print(s, "\n Payment is temporarily unavailable, please try again shortly.\n\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Remember the session id so a later connect can confirm settlement.
|
||||||
|
if err := a.st.SetPremiumPayment(u.ID, ses.ID); err != nil {
|
||||||
|
log.Error("store premium checkout session id", "err", err)
|
||||||
}
|
}
|
||||||
lines := []string{
|
lines := []string{
|
||||||
"",
|
"",
|
||||||
" amount " + amount,
|
" amount $" + payments.PremiumAmount() + " " + payments.PremiumCurrency() + " (pay with any major coin or PayPal)",
|
||||||
" send to " + c.Address,
|
" checkout " + ses.URL,
|
||||||
}
|
|
||||||
if c.QR != "" {
|
|
||||||
lines = append(lines, " qr "+c.QR)
|
|
||||||
}
|
|
||||||
lines = append(lines,
|
|
||||||
"",
|
"",
|
||||||
" Perks unlock once payment confirms — then re-run: ssh join@"+a.host,
|
" Open the link, or scan it with your phone:",
|
||||||
"",
|
"",
|
||||||
)
|
}
|
||||||
wish.Println(s, strings.Join(lines, "\n"))
|
wish.Println(s, strings.Join(lines, "\n"))
|
||||||
|
if q := payments.TerminalQR(ses.URL, " "); q != "" {
|
||||||
|
wish.Print(s, strings.ReplaceAll(q, "\n", "\r\n"))
|
||||||
|
}
|
||||||
|
wish.Print(s, "\r\n Waiting for payment here (press Enter to leave; perks also unlock on your next connect)...\r\n")
|
||||||
|
|
||||||
|
if a.waitForPremium(s, in, u, ses.ID) {
|
||||||
|
wish.Println(s, "\n Payment received. Welcome, Founding member!")
|
||||||
|
a.showPremiumWelcome(s, *u)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wish.Print(s, "\n Once payment confirms, re-run: ssh join@"+a.host+"\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// waitForPremium polls the checkout session until it is paid, the member
|
||||||
|
// presses Enter, the session disconnects, or the session would have expired.
|
||||||
|
func (a *app) waitForPremium(s ssh.Session, in *bufio.Reader, u *store.User, sessionID string) bool {
|
||||||
|
left := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
_, _ = readLine(s, in)
|
||||||
|
close(left)
|
||||||
|
}()
|
||||||
|
tick := time.NewTicker(5 * time.Second)
|
||||||
|
defer tick.Stop()
|
||||||
|
deadline := time.After(60 * time.Minute)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-left:
|
||||||
|
return false
|
||||||
|
case <-s.Context().Done():
|
||||||
|
return false
|
||||||
|
case <-deadline:
|
||||||
|
return false
|
||||||
|
case <-tick.C:
|
||||||
|
if a.ensurePremium(u) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isYes reports whether a prompt line is an affirmative opt-in.
|
// isYes reports whether a prompt line is an affirmative opt-in.
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -20,6 +20,7 @@ require (
|
||||||
golang.org/x/crypto v0.58.0
|
golang.org/x/crypto v0.58.0
|
||||||
golang.org/x/term v0.47.0
|
golang.org/x/term v0.47.0
|
||||||
modernc.org/sqlite v1.60.1
|
modernc.org/sqlite v1.60.1
|
||||||
|
rsc.io/qr v0.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -372,3 +372,5 @@ modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
|
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
|
||||||
|
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,11 @@
|
||||||
// AGENTBBS_COINPAY_API_URL API base (default https://coinpayportal.com/api)
|
// AGENTBBS_COINPAY_API_URL API base (default https://coinpayportal.com/api)
|
||||||
// AGENTBBS_PREMIUM_AMOUNT fiat amount (default 99)
|
// AGENTBBS_PREMIUM_AMOUNT fiat amount (default 99)
|
||||||
// AGENTBBS_PREMIUM_CURRENCY fiat currency (default USD)
|
// AGENTBBS_PREMIUM_CURRENCY fiat currency (default USD)
|
||||||
// AGENTBBS_PREMIUM_BLOCKCHAIN settlement chain (default eth)
|
// AGENTBBS_COINPAY_PROPERTY site hostname on the session (default bbs.profullstack.com)
|
||||||
|
//
|
||||||
|
// Checkout is a CoinPay hosted session (POST /checkout/sessions): the buyer
|
||||||
|
// opens its URL (shown with a QR code in the SSH terminal), picks any coin the
|
||||||
|
// business accepts or PayPal, and we poll GET /checkout/sessions/:id for paid.
|
||||||
//
|
//
|
||||||
// Operators can also grant pod time manually: `agentbbs grant-pod <user> N`.
|
// Operators can also grant pod time manually: `agentbbs grant-pod <user> N`.
|
||||||
package payments
|
package payments
|
||||||
|
|
@ -27,6 +31,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -43,9 +48,8 @@ const FoundingCap = "1,000"
|
||||||
|
|
||||||
// Premium charge parameters — overridable via env so the offer can change
|
// Premium charge parameters — overridable via env so the offer can change
|
||||||
// without a rebuild.
|
// without a rebuild.
|
||||||
func PremiumAmount() string { return envOr("AGENTBBS_PREMIUM_AMOUNT", "99") }
|
func PremiumAmount() string { return envOr("AGENTBBS_PREMIUM_AMOUNT", "99") }
|
||||||
func PremiumCurrency() string { return envOr("AGENTBBS_PREMIUM_CURRENCY", "USD") }
|
func PremiumCurrency() string { return envOr("AGENTBBS_PREMIUM_CURRENCY", "USD") }
|
||||||
func PremiumBlockchain() string { return envOr("AGENTBBS_PREMIUM_BLOCKCHAIN", "eth") }
|
|
||||||
|
|
||||||
// MerchantID is the CoinPay merchant/business id payments are created under.
|
// MerchantID is the CoinPay merchant/business id payments are created under.
|
||||||
func MerchantID() string { return os.Getenv("AGENTBBS_COINPAY_MERCHANT_ID") }
|
func MerchantID() string { return os.Getenv("AGENTBBS_COINPAY_MERCHANT_ID") }
|
||||||
|
|
@ -55,8 +59,9 @@ func apiBase() string {
|
||||||
return trimSlash(envOr("AGENTBBS_COINPAY_API_URL", "https://coinpayportal.com/api"))
|
return trimSlash(envOr("AGENTBBS_COINPAY_API_URL", "https://coinpayportal.com/api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIConfigured reports whether live CoinPay calls can be made.
|
// APIConfigured reports whether live CoinPay calls can be made. The business
|
||||||
func APIConfigured() bool { return apiKey() != "" && MerchantID() != "" }
|
// API key identifies the merchant, so a hosted checkout needs only the key.
|
||||||
|
func APIConfigured() bool { return apiKey() != "" }
|
||||||
|
|
||||||
func envOr(k, def string) string {
|
func envOr(k, def string) string {
|
||||||
if v := os.Getenv(k); v != "" {
|
if v := os.Getenv(k); v != "" {
|
||||||
|
|
@ -72,19 +77,6 @@ func trimSlash(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Charge is a created CoinPay payment a user must fund: a unique deposit
|
|
||||||
// address, the crypto amount, the fiat amount it settles, and the payment id
|
|
||||||
// (store it to verify settlement later).
|
|
||||||
type Charge struct {
|
|
||||||
Address string
|
|
||||||
CryptoAmount string
|
|
||||||
Currency string
|
|
||||||
FiatAmount string
|
|
||||||
FiatCurrency string
|
|
||||||
ID string
|
|
||||||
QR string
|
|
||||||
}
|
|
||||||
|
|
||||||
// PremiumReference derives a stable memo for a user's lifetime membership from
|
// PremiumReference derives a stable memo for a user's lifetime membership from
|
||||||
// their key fingerprint; sent as payment metadata for reconciliation.
|
// their key fingerprint; sent as payment metadata for reconciliation.
|
||||||
func PremiumReference(pubkeyFP string) string { return Reference("premium", pubkeyFP) }
|
func PremiumReference(pubkeyFP string) string { return Reference("premium", pubkeyFP) }
|
||||||
|
|
@ -96,48 +88,60 @@ func Reference(plan, pubkeyFP string) string {
|
||||||
return "abbs-" + plan + "-" + hex.EncodeToString(mac.Sum(nil))[:12]
|
return "abbs-" + plan + "-" + hex.EncodeToString(mac.Sum(nil))[:12]
|
||||||
}
|
}
|
||||||
|
|
||||||
// flexStr decodes a JSON value that may arrive as either a string or a number
|
// Property is the site hostname CoinPay records on the checkout session and
|
||||||
// into a string. CoinPay returns crypto_amount as a bare JSON number (e.g.
|
// echoes on every webhook, so the shared business can tell BBS sales apart.
|
||||||
// 0.0031), but has sent it quoted in the past — accept both so a representation
|
func Property() string { return envOr("AGENTBBS_COINPAY_PROPERTY", "bbs.profullstack.com") }
|
||||||
// change on their side can't break the charge again.
|
|
||||||
type flexStr string
|
|
||||||
|
|
||||||
func (f *flexStr) UnmarshalJSON(b []byte) error {
|
// Buyer identifies who a premium checkout session is for.
|
||||||
b = bytes.TrimSpace(b)
|
type Buyer struct {
|
||||||
if len(b) == 0 || string(b) == "null" {
|
AccountID string // stable account handle (CoinPay account_id)
|
||||||
*f = ""
|
Email string // verified member email (CoinPay customer_email)
|
||||||
return nil
|
PubKeyFP string // SSH key fingerprint (metadata only)
|
||||||
|
UserID int64 // local store id (metadata only)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Session is a created CoinPay hosted checkout session: the buyer opens URL,
|
||||||
|
// picks any coin the business accepts or PayPal, and pays there.
|
||||||
|
type Session struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
ExpiresAt string `json:"expires_at"`
|
||||||
|
Methods []string `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionBody builds the POST /checkout/sessions request body. Exposed for tests.
|
||||||
|
func SessionBody(b Buyer, successURL string) map[string]any {
|
||||||
|
amount := json.Number(PremiumAmount())
|
||||||
|
if f, err := strconv.ParseFloat(PremiumAmount(), 64); err == nil {
|
||||||
|
amount = json.Number(strconv.FormatFloat(f, 'f', -1, 64))
|
||||||
}
|
}
|
||||||
if b[0] == '"' {
|
body := map[string]any{
|
||||||
var s string
|
"amount": amount,
|
||||||
if err := json.Unmarshal(b, &s); err != nil {
|
"currency": PremiumCurrency(),
|
||||||
return err
|
"description": "AgentBBS Founding Lifetime membership",
|
||||||
}
|
"customer_email": b.Email,
|
||||||
*f = flexStr(s)
|
"account_id": b.AccountID,
|
||||||
return nil
|
"property": Property(),
|
||||||
|
"expires_in_minutes": 60,
|
||||||
|
"metadata": map[string]any{
|
||||||
|
"ref": PremiumReference(b.PubKeyFP),
|
||||||
|
"plan": "premium_lifetime",
|
||||||
|
"handle": b.AccountID,
|
||||||
|
"pubkey_fp": b.PubKeyFP,
|
||||||
|
"user_id": strconv.FormatInt(b.UserID, 10),
|
||||||
|
"site": Property(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
*f = flexStr(b) // number (or other scalar) — keep its literal text
|
if successURL != "" {
|
||||||
return nil
|
body["success_url"] = successURL
|
||||||
|
body["cancel_url"] = successURL
|
||||||
|
}
|
||||||
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
// coinpayPayment is the (subset of the) CoinPay payment object, returned
|
// apiDo performs an authenticated CoinPay API call and returns the raw body.
|
||||||
// wrapped as {"payment": {…}}.
|
func apiDo(ctx context.Context, method, path string, body any) ([]byte, error) {
|
||||||
type coinpayPayment struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Address string `json:"payment_address"`
|
|
||||||
CryptoAmount flexStr `json:"crypto_amount"`
|
|
||||||
CryptoCurr string `json:"crypto_currency"`
|
|
||||||
QR string `json:"qr_code"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type paymentEnvelope struct {
|
|
||||||
Payment coinpayPayment `json:"payment"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// coinpayDo performs an authenticated CoinPay API call and decodes the
|
|
||||||
// {"payment":{…}} envelope.
|
|
||||||
func coinpayDo(ctx context.Context, method, path string, body any) (*paymentEnvelope, error) {
|
|
||||||
var rdr io.Reader
|
var rdr io.Reader
|
||||||
if body != nil {
|
if body != nil {
|
||||||
b, err := json.Marshal(body)
|
b, err := json.Marshal(body)
|
||||||
|
|
@ -152,6 +156,7 @@ func coinpayDo(ctx context.Context, method, path string, body any) (*paymentEnve
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", "Bearer "+apiKey())
|
req.Header.Set("Authorization", "Bearer "+apiKey())
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -162,57 +167,90 @@ func coinpayDo(ctx context.Context, method, path string, body any) (*paymentEnve
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
return nil, fmt.Errorf("coinpay %s %s: %s: %s", method, path, resp.Status, bytesTrim(raw))
|
return nil, fmt.Errorf("coinpay %s %s: %s: %s", method, path, resp.Status, bytesTrim(raw))
|
||||||
}
|
}
|
||||||
var env paymentEnvelope
|
return raw, nil
|
||||||
if err := json.Unmarshal(raw, &env); err != nil {
|
|
||||||
return nil, fmt.Errorf("coinpay %s %s: bad response: %w", method, path, err)
|
|
||||||
}
|
|
||||||
return &env, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bytesTrim(b []byte) string { return string(bytes.TrimSpace(b)) }
|
func bytesTrim(b []byte) string { return string(bytes.TrimSpace(b)) }
|
||||||
|
|
||||||
// CreatePremiumCharge creates a CoinPay payment for the lifetime membership and
|
// CreatePremiumSession creates a hosted CoinPay checkout session for the
|
||||||
// returns the deposit address, amount, and payment id (store the id to verify
|
// lifetime membership. created is false when CoinPay isn't configured. Store
|
||||||
// settlement later). created is false when CoinPay isn't configured.
|
// the session id (cs_...) to verify settlement later.
|
||||||
func CreatePremiumCharge(ref string) (Charge, bool, error) {
|
func CreatePremiumSession(b Buyer, successURL string) (Session, bool, error) {
|
||||||
if !APIConfigured() {
|
if !APIConfigured() {
|
||||||
return Charge{}, false, nil
|
return Session{}, false, nil
|
||||||
}
|
}
|
||||||
amount := json.Number(PremiumAmount())
|
if b.Email == "" || b.AccountID == "" {
|
||||||
if f, err := strconv.ParseFloat(PremiumAmount(), 64); err == nil {
|
return Session{}, false, fmt.Errorf("coinpay: checkout needs an email and an account id")
|
||||||
amount = json.Number(strconv.FormatFloat(f, 'f', -1, 64))
|
|
||||||
}
|
|
||||||
body := map[string]any{
|
|
||||||
"business_id": MerchantID(),
|
|
||||||
"amount": amount,
|
|
||||||
"currency": PremiumCurrency(),
|
|
||||||
"blockchain": toUpper(PremiumBlockchain()),
|
|
||||||
"description": "AgentBBS Premium membership (lifetime)",
|
|
||||||
"metadata": map[string]string{"ref": ref},
|
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
env, err := coinpayDo(ctx, http.MethodPost, "/payments/create", body)
|
raw, err := apiDo(ctx, http.MethodPost, "/checkout/sessions", SessionBody(b, successURL))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Charge{}, false, err
|
return Session{}, false, err
|
||||||
}
|
}
|
||||||
p := env.Payment
|
var out struct {
|
||||||
if p.Address == "" {
|
Session
|
||||||
return Charge{}, false, fmt.Errorf("coinpay: payment created without an address")
|
Methods []json.RawMessage `json:"methods"`
|
||||||
}
|
}
|
||||||
return Charge{
|
if err := json.Unmarshal(raw, &out); err != nil {
|
||||||
Address: p.Address,
|
return Session{}, false, fmt.Errorf("coinpay: bad session response: %w", err)
|
||||||
CryptoAmount: string(p.CryptoAmount),
|
}
|
||||||
Currency: p.CryptoCurr,
|
if out.ID == "" || out.URL == "" {
|
||||||
FiatAmount: PremiumAmount(),
|
return Session{}, false, fmt.Errorf("coinpay: session created without an id/url: %s", bytesTrim(raw))
|
||||||
FiatCurrency: PremiumCurrency(),
|
}
|
||||||
ID: p.ID,
|
ses := out.Session
|
||||||
QR: p.QR,
|
for _, m := range out.Methods {
|
||||||
}, true, nil
|
var code string
|
||||||
|
if json.Unmarshal(m, &code) == nil {
|
||||||
|
ses.Methods = append(ses.Methods, code)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var obj map[string]any
|
||||||
|
if json.Unmarshal(m, &obj) == nil {
|
||||||
|
for _, k := range []string{"code", "method", "id", "type"} {
|
||||||
|
if v, ok := obj[k].(string); ok && v != "" {
|
||||||
|
ses.Methods = append(ses.Methods, v)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ses, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// paidStatus reports whether a CoinPay session/payment status means settled.
|
||||||
|
func paidStatus(st string) bool {
|
||||||
|
switch st {
|
||||||
|
case "paid", "complete", "completed", "confirmed", "forwarded":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionStatus fetches a checkout session's status.
|
||||||
|
func SessionStatus(ctx context.Context, id string) (string, error) {
|
||||||
|
raw, err := apiDo(ctx, http.MethodGet, "/checkout/sessions/"+url.PathEscape(id), nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var out struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Session *struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
} `json:"session"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &out); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if out.Status == "" && out.Session != nil {
|
||||||
|
out.Status = out.Session.Status
|
||||||
|
}
|
||||||
|
return out.Status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyPremium reports whether a created premium payment has settled. payID is
|
// VerifyPremium reports whether a created premium payment has settled. payID is
|
||||||
// the CoinPay payment id from CreatePremiumCharge. checked is false when we
|
// the CoinPay checkout session id (cs_...) from CreatePremiumSession, or a legacy
|
||||||
|
// payment id from before hosted checkout. checked is false when we
|
||||||
// could not reach CoinPay (caller falls back to store state); paid is true on a
|
// could not reach CoinPay (caller falls back to store state); paid is true on a
|
||||||
// confirmed/forwarded status.
|
// confirmed/forwarded status.
|
||||||
func VerifyPremium(payID string) (paid bool, checked bool) {
|
func VerifyPremium(payID string) (paid bool, checked bool) {
|
||||||
|
|
@ -221,24 +259,25 @@ func VerifyPremium(payID string) (paid bool, checked bool) {
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
env, err := coinpayDo(ctx, http.MethodGet, "/payments/"+url.PathEscape(payID), nil)
|
if strings.HasPrefix(payID, "cs_") {
|
||||||
if err != nil {
|
st, err := SessionStatus(ctx, payID)
|
||||||
return false, false // network/API error — fall back to store state
|
if err != nil {
|
||||||
}
|
return false, false // network/API error: fall back to store state
|
||||||
switch env.Payment.Status {
|
|
||||||
case "confirmed", "forwarded", "completed", "paid":
|
|
||||||
return true, true
|
|
||||||
default:
|
|
||||||
return false, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func toUpper(s string) string {
|
|
||||||
b := []byte(s)
|
|
||||||
for i, c := range b {
|
|
||||||
if c >= 'a' && c <= 'z' {
|
|
||||||
b[i] = c - 32
|
|
||||||
}
|
}
|
||||||
|
return paidStatus(st), true
|
||||||
}
|
}
|
||||||
return string(b)
|
// Legacy single-chain payment ids from before hosted checkout.
|
||||||
|
raw, err := apiDo(ctx, http.MethodGet, "/payments/"+url.PathEscape(payID), nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
var env struct {
|
||||||
|
Payment struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
} `json:"payment"`
|
||||||
|
}
|
||||||
|
if json.Unmarshal(raw, &env) != nil {
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
return paidStatus(env.Payment.Status), true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,25 +5,27 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateAndVerifyPremium(t *testing.T) {
|
func TestCreateSessionAndVerifyPaid(t *testing.T) {
|
||||||
var gotAuth, gotBlockchain, gotBusiness string
|
var gotAuth string
|
||||||
|
var body map[string]any
|
||||||
|
status := "open"
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
gotAuth = r.Header.Get("Authorization")
|
gotAuth = r.Header.Get("Authorization")
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
switch {
|
switch {
|
||||||
case r.Method == http.MethodPost && r.URL.Path == "/payments/create":
|
case r.Method == http.MethodPost && r.URL.Path == "/checkout/sessions":
|
||||||
var body map[string]any
|
|
||||||
b, _ := io.ReadAll(r.Body)
|
b, _ := io.ReadAll(r.Body)
|
||||||
_ = json.Unmarshal(b, &body)
|
_ = json.Unmarshal(b, &body)
|
||||||
gotBlockchain, _ = body["blockchain"].(string)
|
w.WriteHeader(http.StatusCreated)
|
||||||
gotBusiness, _ = body["business_id"].(string)
|
_, _ = w.Write([]byte(`{"success":true,"id":"cs_abc","url":"https://coinpayportal.com/checkout/cs_abc","status":"open","methods":["BTC","ETH",{"code":"PAYPAL"}]}`))
|
||||||
// crypto_amount comes back as a bare JSON number from the live API.
|
case r.Method == http.MethodGet && r.URL.Path == "/checkout/sessions/cs_abc":
|
||||||
_, _ = w.Write([]byte(`{"payment":{"id":"pay_1","payment_address":"0xABC","crypto_amount":0.0031,"crypto_currency":"ETH","status":"pending"}}`))
|
_, _ = w.Write([]byte(`{"success":true,"session":{"id":"cs_abc","status":"` + status + `"}}`))
|
||||||
case r.Method == http.MethodGet && r.URL.Path == "/payments/pay_1":
|
case r.Method == http.MethodGet && r.URL.Path == "/payments/pay_old":
|
||||||
_, _ = w.Write([]byte(`{"payment":{"id":"pay_1","status":"confirmed"}}`))
|
_, _ = w.Write([]byte(`{"payment":{"id":"pay_old","status":"forwarded"}}`))
|
||||||
default:
|
default:
|
||||||
http.Error(w, "not found", http.StatusNotFound)
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
@ -31,64 +33,82 @@ func TestCreateAndVerifyPremium(t *testing.T) {
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
t.Setenv("AGENTBBS_COINPAY_API_URL", srv.URL)
|
t.Setenv("AGENTBBS_COINPAY_API_URL", srv.URL)
|
||||||
t.Setenv("COINPAY_API_KEY", "cp_test_key")
|
t.Setenv("COINPAY_API_KEY", "cp_live_test")
|
||||||
t.Setenv("AGENTBBS_COINPAY_MERCHANT_ID", "biz_42")
|
t.Setenv("AGENTBBS_COINPAY_MERCHANT_ID", "")
|
||||||
|
|
||||||
if !APIConfigured() {
|
if !APIConfigured() {
|
||||||
t.Fatal("APIConfigured should be true with key + merchant id")
|
t.Fatal("a business key alone must configure hosted checkout")
|
||||||
}
|
}
|
||||||
|
ses, ok, err := CreatePremiumSession(Buyer{AccountID: "alice", Email: "a@example.com", PubKeyFP: "SHA256:xyz", UserID: 7}, "https://bbs.profullstack.com/~alice")
|
||||||
c, ok, err := CreatePremiumCharge("abbs-premium-deadbeef")
|
|
||||||
if err != nil || !ok {
|
if err != nil || !ok {
|
||||||
t.Fatalf("create: ok=%v err=%v", ok, err)
|
t.Fatalf("create: ok=%v err=%v", ok, err)
|
||||||
}
|
}
|
||||||
if c.Address != "0xABC" || c.ID != "pay_1" || c.CryptoAmount != "0.0031" {
|
if ses.ID != "cs_abc" || !strings.HasSuffix(ses.URL, "/checkout/cs_abc") {
|
||||||
t.Fatalf("charge fields wrong: %+v", c)
|
t.Fatalf("session fields wrong: %+v", ses)
|
||||||
}
|
}
|
||||||
if gotAuth != "Bearer cp_test_key" {
|
if len(ses.Methods) != 3 || ses.Methods[2] != "PAYPAL" {
|
||||||
|
t.Fatalf("methods = %v", ses.Methods)
|
||||||
|
}
|
||||||
|
if gotAuth != "Bearer cp_live_test" {
|
||||||
t.Fatalf("auth header = %q", gotAuth)
|
t.Fatalf("auth header = %q", gotAuth)
|
||||||
}
|
}
|
||||||
if gotBlockchain != "ETH" {
|
for k, want := range map[string]any{
|
||||||
t.Fatalf("blockchain not upper-cased: %q", gotBlockchain)
|
"customer_email": "a@example.com", "account_id": "alice", "property": "bbs.profullstack.com",
|
||||||
|
"currency": "USD", "amount": float64(99), "expires_in_minutes": float64(60),
|
||||||
|
"success_url": "https://bbs.profullstack.com/~alice",
|
||||||
|
} {
|
||||||
|
if body[k] != want {
|
||||||
|
t.Errorf("body[%s] = %v, want %v", k, body[k], want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if gotBusiness != "biz_42" {
|
if _, has := body["blockchain"]; has {
|
||||||
t.Fatalf("business_id = %q", gotBusiness)
|
t.Error("hosted checkout must not pin a blockchain")
|
||||||
|
}
|
||||||
|
meta, _ := body["metadata"].(map[string]any)
|
||||||
|
if meta["pubkey_fp"] != "SHA256:xyz" || meta["user_id"] != "7" || meta["ref"] != PremiumReference("SHA256:xyz") {
|
||||||
|
t.Errorf("metadata = %v", meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
if paid, checked := VerifyPremium("pay_1"); !checked || !paid {
|
if paid, checked := VerifyPremium("cs_abc"); !checked || paid {
|
||||||
t.Fatalf("verify confirmed: paid=%v checked=%v", paid, checked)
|
t.Fatalf("open session: paid=%v checked=%v", paid, checked)
|
||||||
|
}
|
||||||
|
status = "paid"
|
||||||
|
if paid, checked := VerifyPremium("cs_abc"); !checked || !paid {
|
||||||
|
t.Fatalf("paid session: paid=%v checked=%v", paid, checked)
|
||||||
|
}
|
||||||
|
// A legacy single-chain payment stored before hosted checkout still settles.
|
||||||
|
if paid, checked := VerifyPremium("pay_old"); !checked || !paid {
|
||||||
|
t.Fatalf("legacy payment: paid=%v checked=%v", paid, checked)
|
||||||
}
|
}
|
||||||
// Empty id is a clean "not checked".
|
|
||||||
if _, checked := VerifyPremium(""); checked {
|
if _, checked := VerifyPremium(""); checked {
|
||||||
t.Fatal("empty payID must not be checked")
|
t.Fatal("empty id must not be checked")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateSessionNeedsEmail(t *testing.T) {
|
||||||
|
t.Setenv("COINPAY_API_KEY", "cp_live_test")
|
||||||
|
if _, ok, err := CreatePremiumSession(Buyer{AccountID: "alice"}, ""); ok || err == nil {
|
||||||
|
t.Fatalf("missing email: ok=%v err=%v", ok, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotConfigured(t *testing.T) {
|
func TestNotConfigured(t *testing.T) {
|
||||||
t.Setenv("COINPAY_API_KEY", "")
|
t.Setenv("COINPAY_API_KEY", "")
|
||||||
t.Setenv("AGENTBBS_COINPAY_MERCHANT_ID", "")
|
|
||||||
if APIConfigured() {
|
if APIConfigured() {
|
||||||
t.Fatal("must not be configured without key/merchant")
|
t.Fatal("must not be configured without a key")
|
||||||
}
|
}
|
||||||
if _, ok, err := CreatePremiumCharge("ref"); ok || err != nil {
|
if _, ok, err := CreatePremiumSession(Buyer{AccountID: "a", Email: "a@b.co"}, ""); ok || err != nil {
|
||||||
t.Fatalf("unconfigured create: ok=%v err=%v", ok, err)
|
t.Fatalf("unconfigured create: ok=%v err=%v", ok, err)
|
||||||
}
|
}
|
||||||
if _, checked := VerifyPremium("pay_1"); checked {
|
if _, checked := VerifyPremium("cs_abc"); checked {
|
||||||
t.Fatal("unconfigured verify must not be checked")
|
t.Fatal("unconfigured verify must not be checked")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlexStrDecodesNumberOrString(t *testing.T) {
|
func TestTerminalQR(t *testing.T) {
|
||||||
for _, raw := range []string{
|
q := TerminalQR("https://coinpayportal.com/checkout/cs_abc", " ")
|
||||||
`{"payment":{"crypto_amount":0.0031}}`, // live API: bare number
|
lines := strings.Split(strings.TrimRight(q, "\n"), "\n")
|
||||||
`{"payment":{"crypto_amount":"0.0031"}}`, // legacy: quoted string
|
if len(lines) < 10 || !strings.HasPrefix(lines[0], " █") {
|
||||||
} {
|
t.Fatalf("unexpected QR:\n%s", q)
|
||||||
var env paymentEnvelope
|
|
||||||
if err := json.Unmarshal([]byte(raw), &env); err != nil {
|
|
||||||
t.Fatalf("unmarshal %s: %v", raw, err)
|
|
||||||
}
|
|
||||||
if got := string(env.Payment.CryptoAmount); got != "0.0031" {
|
|
||||||
t.Fatalf("crypto_amount from %s = %q, want 0.0031", raw, got)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
internal/payments/qr.go
Normal file
45
internal/payments/qr.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
package payments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"rsc.io/qr"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TerminalQR renders text as a QR code made of Unicode half blocks, two module
|
||||||
|
// rows per text line, with a quiet zone. Light modules are drawn as blocks so it
|
||||||
|
// scans as dark-on-light on the usual light-on-dark terminal. Each line is
|
||||||
|
// prefixed with indent. Returns "" if the text cannot be encoded.
|
||||||
|
func TerminalQR(text, indent string) string {
|
||||||
|
code, err := qr.Encode(text, qr.L)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
const quiet = 2
|
||||||
|
n := code.Size
|
||||||
|
light := func(x, y int) bool {
|
||||||
|
if x < 0 || y < 0 || x >= n || y >= n {
|
||||||
|
return true // quiet zone
|
||||||
|
}
|
||||||
|
return !code.Black(x, y)
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
for y := -quiet; y < n+quiet; y += 2 {
|
||||||
|
b.WriteString(indent)
|
||||||
|
for x := -quiet; x < n+quiet; x++ {
|
||||||
|
top, bot := light(x, y), light(x, y+1)
|
||||||
|
switch {
|
||||||
|
case top && bot:
|
||||||
|
b.WriteString("█")
|
||||||
|
case top:
|
||||||
|
b.WriteString("▀")
|
||||||
|
case bot:
|
||||||
|
b.WriteString("▄")
|
||||||
|
default:
|
||||||
|
b.WriteString(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
6
setup.sh
6
setup.sh
|
|
@ -429,8 +429,8 @@ AGENTBBS_FILES_WEB_ADDR=${FILES_WEB_ADDR}
|
||||||
# Offered at join@.
|
# Offered at join@.
|
||||||
|
|
||||||
# Premium payments hit the CoinPay REST API directly (no coinpay CLI needed):
|
# Premium payments hit the CoinPay REST API directly (no coinpay CLI needed):
|
||||||
# join@ creates a charge and shows the amount + deposit address; a later connect
|
# join@ creates a hosted checkout session (any coin or PayPal) and shows its URL
|
||||||
# verifies settlement by payment id. COINPAY_API_KEY + the merchant id are
|
# + a QR code, then polls it; a later connect also verifies by session id. COINPAY_API_KEY + the merchant id are
|
||||||
# injected by the CI deploy from GitHub secrets (COINPAY_API_KEY /
|
# injected by the CI deploy from GitHub secrets (COINPAY_API_KEY /
|
||||||
# COINPAY_MERCHANT_ID); set them here directly when provisioning by hand:
|
# COINPAY_MERCHANT_ID); set them here directly when provisioning by hand:
|
||||||
# COINPAY_API_KEY=cp_live_xxx
|
# COINPAY_API_KEY=cp_live_xxx
|
||||||
|
|
@ -438,7 +438,7 @@ AGENTBBS_FILES_WEB_ADDR=${FILES_WEB_ADDR}
|
||||||
# AGENTBBS_COINPAY_API_URL=https://coinpayportal.com/api
|
# AGENTBBS_COINPAY_API_URL=https://coinpayportal.com/api
|
||||||
# AGENTBBS_PREMIUM_AMOUNT=10
|
# AGENTBBS_PREMIUM_AMOUNT=10
|
||||||
# AGENTBBS_PREMIUM_CURRENCY=USD
|
# AGENTBBS_PREMIUM_CURRENCY=USD
|
||||||
# AGENTBBS_PREMIUM_BLOCKCHAIN=eth
|
# AGENTBBS_COINPAY_PROPERTY=bbs.profullstack.com
|
||||||
|
|
||||||
# Member email (free for every verified member). Addresses are <name>@${DOMAIN}
|
# Member email (free for every verified member). Addresses are <name>@${DOMAIN}
|
||||||
# (the address domain), while the Mailu server lives on the mail host below.
|
# (the address domain), while the Mailu server lives on the mail host below.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue