feat: add client self-service purchases and integration kit
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-15 17:33:52 +08:00
parent 68a2b4d892
commit cfcc9b386b
33 changed files with 2598 additions and 44 deletions

View File

@@ -0,0 +1,116 @@
package httpapi
import (
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/base64"
"errors"
"github.com/gin-gonic/gin"
"time"
)
func (a *BusinessAPI) clientPurchaseClaim(c *gin.Context) {
s, order, ok := a.lookupClientPurchase(c, true)
if !ok {
return
}
if len(a.credentialEncryptionKey) != 32 {
apiError(c, 503, "credentials_unavailable", "请配置独立实例凭据加密密钥")
return
}
tx, e := a.db.BeginTx(c, nil)
if e != nil {
purchaseFailure(c)
return
}
defer tx.Rollback()
if lockPurchaseScope(c, tx, s) != nil {
apiError(c, 401, "invalid_purchase_credential", "购买凭据无效")
return
}
var license sql.NullInt64
var state string
if tx.QueryRowContext(c, "SELECT status,issued_license_id FROM offline_orders WHERE id=? AND customer_id=? AND product_id=? FOR UPDATE", order, s.Customer, s.Product).Scan(&state, &license) != nil {
purchaseFailure(c)
return
}
if state != "issued" || !license.Valid {
apiError(c, 409, "purchase_not_paid", "订单尚未完成支付及授权")
return
}
var paymentState string
if tx.QueryRowContext(c, "SELECT status FROM payment_orders WHERE order_id=? FOR UPDATE", order).Scan(&paymentState) != nil || paymentState != "paid" {
apiError(c, 409, "purchase_not_paid", "订单尚未完成支付及授权")
return
}
var licenseKey, licenseState string
var expires time.Time
if tx.QueryRowContext(c, "SELECT license_key,status,expires_at FROM licenses WHERE id=? AND customer_id=? AND product_id=? FOR UPDATE", license.Int64, s.Customer, s.Product).Scan(&licenseKey, &licenseState, &expires) != nil || licenseState != "active" || !time.Now().Before(expires) {
apiError(c, 409, "authorization_unavailable", "授权不可用")
return
}
var bound sql.NullString
if tx.QueryRowContext(c, "SELECT instance_id FROM client_purchases WHERE order_id=? AND credential_id=? FOR UPDATE", order, s.ID).Scan(&bound) != nil {
purchaseFailure(c)
return
}
var instance, secret string
var encrypted []byte
var instanceStatus, freezeState string
e = tx.QueryRowContext(c, "SELECT instance_id,secret_encrypted,status,freeze_state FROM license_instances WHERE license_id=? FOR UPDATE", license.Int64).Scan(&instance, &encrypted, &instanceStatus, &freezeState)
if e == nil {
if (instanceStatus != "provisioned" && instanceStatus != "activated") || freezeState != "none" {
apiError(c, 409, "authorization_unavailable", "授权实例当前不可交付")
return
}
if !bound.Valid || bound.String != instance {
apiError(c, 409, "instance_conflict", "授权已由其他流程绑定实例")
return
}
secret, e = openInstanceSecret(a.credentialEncryptionKey, instance, encrypted)
if e != nil {
apiError(c, 503, "credentials_unavailable", "凭据解密不可用")
return
}
} else if errors.Is(e, sql.ErrNoRows) {
if bound.Valid {
apiError(c, 409, "instance_conflict", "实例已被移除")
return
}
instance, e = newInstanceID()
if e != nil {
purchaseFailure(c)
return
}
raw := make([]byte, 32)
if _, e = rand.Read(raw); e != nil {
purchaseFailure(c)
return
}
secret = base64.RawURLEncoding.EncodeToString(raw)
hash := sha256.Sum256(raw)
encrypted, e = sealInstanceSecret(a.credentialEncryptionKey, instance, secret)
if e != nil {
apiError(c, 503, "credentials_unavailable", "凭据加密不可用")
return
}
_, e = tx.ExecContext(c, "INSERT INTO license_instances(instance_id,license_id,customer_id,product_id,secret_hash,created_by,secret_encrypted) VALUES(?,?,?,?,?,?,?)", instance, license.Int64, s.Customer, s.Product, hash[:], s.Actor, encrypted)
if e == nil {
_, e = tx.ExecContext(c, "UPDATE client_purchases SET instance_id=? WHERE order_id=?", instance, order)
}
if e != nil {
purchaseFailure(c)
return
}
} else {
purchaseFailure(c)
return
}
_, e = tx.ExecContext(c, "INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,'purchase.claimed','order',?,'{}')", s.Actor, order)
if e != nil || tx.Commit() != nil {
purchaseFailure(c)
return
}
c.JSON(200, gin.H{"data": gin.H{"instance_id": instance, "secret": secret, "license_key": licenseKey, "license_id": license.Int64}})
}