feat: add online payment management and checkout
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 09:26:40 +08:00
parent 5353a4a128
commit dc2efcde08
41 changed files with 4855 additions and 21 deletions

View File

@@ -24,6 +24,8 @@ type orderRecord struct {
ProductName string `json:"product_name"`
AmountCents uint64 `json:"amount_cents"`
Currency string `json:"currency"`
PaymentChannel *string `json:"payment_channel"`
PaymentStatus string `json:"payment_status"`
Status string `json:"status"`
PaymentReference *string `json:"payment_reference"`
PaidAt *time.Time `json:"paid_at"`
@@ -32,12 +34,12 @@ type orderRecord struct {
UpdatedAt time.Time `json:"updated_at"`
}
const orderSelect = `SELECT o.id,o.order_no,o.customer_id,c.name,o.product_id,p.name,o.amount_cents,o.currency,o.status,o.payment_reference,o.paid_at,o.issued_license_id,o.created_at,o.updated_at FROM offline_orders o JOIN customers c ON c.id=o.customer_id JOIN products p ON p.id=o.product_id`
const orderSelect = `SELECT o.id,o.order_no,o.customer_id,c.name,o.product_id,p.name,o.amount_cents,o.currency,o.payment_channel,o.payment_status,o.status,o.payment_reference,o.paid_at,o.issued_license_id,o.created_at,o.updated_at FROM offline_orders o JOIN customers c ON c.id=o.customer_id JOIN products p ON p.id=o.product_id`
type orderScanner interface{ Scan(...any) error }
func scanOrder(s orderScanner) (o orderRecord, err error) {
err = s.Scan(&o.ID, &o.OrderNo, &o.CustomerID, &o.CustomerName, &o.ProductID, &o.ProductName, &o.AmountCents, &o.Currency, &o.Status, &o.PaymentReference, &o.PaidAt, &o.LicenseID, &o.CreatedAt, &o.UpdatedAt)
err = s.Scan(&o.ID, &o.OrderNo, &o.CustomerID, &o.CustomerName, &o.ProductID, &o.ProductName, &o.AmountCents, &o.Currency, &o.PaymentChannel, &o.PaymentStatus, &o.Status, &o.PaymentReference, &o.PaidAt, &o.LicenseID, &o.CreatedAt, &o.UpdatedAt)
return
}
func (a *BusinessAPI) listOrders(c *gin.Context) {
@@ -256,7 +258,7 @@ func (a *BusinessAPI) writeOrder(c *gin.Context, operation string) {
}
id = uint64(n)
case "confirm-payment":
if o.Status != "pending_payment" {
if o.Status != "pending_payment" || o.PaymentChannel != nil {
apiError(c, 409, "invalid_state", "只有待收款订单可确认收款")
return
}
@@ -264,7 +266,7 @@ func (a *BusinessAPI) writeOrder(c *gin.Context, operation string) {
apiError(c, 400, "amount_mismatch", "实际收款必须等于订单金额")
return
}
_, err = tx.ExecContext(c, "UPDATE offline_orders SET status='paid',payment_reference=?,paid_at=?,paid_by=? WHERE id=?", in.PaymentReference, time.Now().UTC(), actor, id)
_, err = tx.ExecContext(c, "UPDATE offline_orders SET status='paid',payment_status='paid',payment_reference=?,paid_at=?,paid_by=? WHERE id=?", in.PaymentReference, time.Now().UTC(), actor, id)
if err != nil {
fail()
return