Files
pay/internal/gateway/product.go
T

42 lines
1.1 KiB
Go

package gateway
import (
"errors"
"gorm.io/gorm"
"github.com/wangjia/pay/internal/model"
"github.com/wangjia/pay/internal/money"
)
// DBProductResolver resolves a SKU (product biz_code) against the products table.
// v1 Product.Price is a "元" string; we parse it into int64 minor units for the
// given settlement currency. 加币种维度到 product 是 P3+ 的事;P2 用单一默认币种。
type DBProductResolver struct {
db *gorm.DB
currency string
}
func NewDBProductResolver(db *gorm.DB, currency string) *DBProductResolver {
if currency == "" {
currency = "CNY"
}
return &DBProductResolver{db: db, currency: currency}
}
func (r *DBProductResolver) Resolve(sku string) (int64, string, string, string, error) {
var p model.Product
err := r.db.Where("biz_code = ? AND active = ?", sku, true).First(&p).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, "", "", "", ErrProductNotFound
}
if err != nil {
return 0, "", "", "", err
}
minor, err := money.Parse(p.Price, r.currency)
if err != nil {
return 0, "", "", "", err
}
return minor, r.currency, p.Name, p.BizCode, nil
}