有效天数问题,充值单回调问题

This commit is contained in:
2026-05-08 11:46:34 +08:00
parent 7c85a8cf29
commit a93acc0784
12 changed files with 402 additions and 87 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
asset "github.com/break/junhong_cmp_fiber/internal/service/asset"
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
"github.com/break/junhong_cmp_fiber/internal/service/packageprice"
"github.com/break/junhong_cmp_fiber/internal/store"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
@@ -560,10 +561,7 @@ func buildClientPackageItem(
return dto.ClientPackageItem{}, false
}
validityDays := pkg.DurationDays
if validityDays <= 0 && pkg.DurationMonths > 0 {
validityDays = pkg.DurationMonths * 30
}
validityDays := packagepkg.CalculateValidityDays(pkg.CalendarType, time.Now(), pkg.DurationMonths, pkg.DurationDays)
dataAllowance := pkg.RealDataMB
if dataAllowance <= 0 {

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
@@ -16,6 +17,7 @@ import (
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/fuiou"
"github.com/break/junhong_cmp_fiber/pkg/response"
"github.com/break/junhong_cmp_fiber/pkg/wechat"
"github.com/gofiber/fiber/v2"
@@ -300,12 +302,13 @@ func (h *ClientWalletHandler) CreateRecharge(c *fiber.Ctx) error {
rechargeNo := generateClientRechargeNo()
paymentNo := generateClientPaymentNo()
// 先初始化微信支付实例并创建预支付订单,确认支付通道可用
// 先初始化生效支付通道并创建预支付订单,确认支付通道可用
// 避免先写入充值记录后支付初始化失败,导致产生孤儿记录
payResult, err := h.createJSAPIPayOrder(
payConfig, err := h.createClientRechargePayConfig(
resolved.SkipPermissionCtx,
config,
appID,
req.AppType,
paymentNo,
"资产钱包充值",
openID,
@@ -347,7 +350,6 @@ func (h *ClientWalletHandler) CreateRecharge(c *fiber.Ctx) error {
return errors.Wrap(errors.CodeDatabaseError, err, "创建支付记录失败")
}
payConfig := buildClientRechargePayConfig(appID, payResult)
resp := &dto.ClientRechargeResponse{
Recharge: dto.ClientRechargeResult{
RechargeID: rechargeOrder.ID,
@@ -607,33 +609,107 @@ func pickAppIDByType(config *model.WechatConfig, appType string) (string, error)
}
}
// createJSAPIPayOrder 根据支付配置的提供者类型路由到对应支付通道,发起 JSAPI 预下单。
// 与 client_order.Service.newPaymentProvider 保持相同的路由逻辑,支持 wechat_v2 和 wechatv3
func (h *ClientWalletHandler) createJSAPIPayOrder(
// createClientRechargePayConfig 根据当前生效支付配置创建充值预支付订单。
// provider_type 必须显式匹配,避免富友等渠道误落到微信直连
func (h *ClientWalletHandler) createClientRechargePayConfig(
ctx context.Context,
config *model.WechatConfig,
appID, orderNo, description, openID string,
appID, appType, orderNo, description, openID string,
amount int,
) (*wechat.JSAPIPayResult, error) {
) (dto.ClientRechargePayConfig, error) {
switch config.ProviderType {
case model.ProviderTypeWechatV2:
// 微信 v2仅需 APIv2Key无需证书序列号
svc, err := wechat.NewPaymentV2ServiceFromConfig(config, appID, h.logger)
if err != nil {
return nil, errors.Wrap(errors.CodeWechatPayFailed, err, "初始化微信 v2 支付实例失败")
return dto.ClientRechargePayConfig{}, errors.Wrap(errors.CodeWechatPayFailed, err, "初始化微信 v2 支付实例失败")
}
return svc.CreateJSAPIOrder(ctx, orderNo, description, openID, amount)
default:
result, err := svc.CreateJSAPIOrder(ctx, orderNo, description, openID, amount)
if err != nil {
return dto.ClientRechargePayConfig{}, err
}
return buildClientRechargePayConfig(appID, result), nil
case model.ProviderTypeWechat:
// 微信 v3PowerWeChat需要证书序列号和 API v3 Key
cache := wechat.NewRedisCache(h.redis)
paymentApp, err := wechat.NewPaymentAppFromConfig(config, appID, cache, h.logger)
if err != nil {
return nil, errors.Wrap(errors.CodeWechatPayFailed, err, "初始化微信支付实例失败")
return dto.ClientRechargePayConfig{}, errors.Wrap(errors.CodeWechatPayFailed, err, "初始化微信支付实例失败")
}
return wechat.NewPaymentService(paymentApp, h.logger).CreateJSAPIOrder(ctx, orderNo, description, openID, amount)
result, err := wechat.NewPaymentService(paymentApp, h.logger).CreateJSAPIOrder(ctx, orderNo, description, openID, amount)
if err != nil {
return dto.ClientRechargePayConfig{}, err
}
return buildClientRechargePayConfig(appID, result), nil
case model.ProviderTypeFuiou:
return h.createFuiouRechargePayConfig(ctx, config, appID, appType, orderNo, description, openID, amount)
default:
return dto.ClientRechargePayConfig{}, errors.New(errors.CodeWechatConfigUnavailable, "不支持的支付渠道类型")
}
}
// createFuiouRechargePayConfig 使用富友支付配置创建充值预支付订单。
func (h *ClientWalletHandler) createFuiouRechargePayConfig(
ctx context.Context,
config *model.WechatConfig,
appID, appType, orderNo, description, openID string,
amount int,
) (dto.ClientRechargePayConfig, error) {
if strings.TrimSpace(config.FyInsCd) == "" ||
strings.TrimSpace(config.FyMchntCd) == "" ||
strings.TrimSpace(config.FyTermID) == "" ||
strings.TrimSpace(config.FyPrivateKey) == "" ||
strings.TrimSpace(config.FyPublicKey) == "" ||
strings.TrimSpace(config.FyAPIURL) == "" ||
strings.TrimSpace(config.FyNotifyURL) == "" ||
strings.TrimSpace(appID) == "" {
return dto.ClientRechargePayConfig{}, errors.New(errors.CodeFuiouPayFailed, "富友支付配置不完整")
}
client, err := fuiou.NewClient(
config.FyInsCd,
config.FyMchntCd,
config.FyTermID,
config.FyAPIURL,
config.FyNotifyURL,
config.FyPrivateKey,
config.FyPublicKey,
h.logger,
)
if err != nil {
return dto.ClientRechargePayConfig{}, errors.Wrap(errors.CodeFuiouPayFailed, err, "创建富友支付客户端失败")
}
tradeType := "JSAPI"
if appType == "miniapp" {
tradeType = "LETPAY"
}
termIP := fuiou.GetServerIP()
if ip, ok := ctx.Value(constants.ContextKeyIP).(string); ok && strings.TrimSpace(ip) != "" {
termIP = ip
}
resp, err := client.WxPreCreate(orderNo, strconv.Itoa(amount), description, termIP, tradeType, appID, openID)
if err != nil {
h.logger.Error("富友充值预下单失败",
zap.String("payment_no", orderNo),
zap.String("trade_type", tradeType),
zap.Error(err),
)
return dto.ClientRechargePayConfig{}, errors.Wrap(errors.CodeFuiouPayFailed, err, "富友预下单失败")
}
return dto.ClientRechargePayConfig{
AppID: resp.SdkAppid,
Timestamp: resp.SdkTimestamp,
NonceStr: resp.SdkNoncestr,
PackageVal: resp.SdkPackage,
SignType: resp.SdkSigntype,
PaySign: resp.SdkPaysign,
}, nil
}
func generateClientRechargeNo() string {
timestamp := time.Now().Format("20060102150405")
randomNum := rand.Intn(1000000)

View File

@@ -260,18 +260,25 @@ func (h *PaymentHandler) AlipayCallback(c *fiber.Ctx) error {
// FuiouPayCallback 富友支付回调(带签名验证)
// POST /api/callback/fuiou-pay
func (h *PaymentHandler) FuiouPayCallback(c *fiber.Ctx) error {
body := c.Body()
if len(body) == 0 {
body, bodySource := fuiouCallbackPayload(c)
if strings.TrimSpace(string(body)) == "" {
return errors.New(errors.CodeFuiouCallbackInvalid, "回调请求体为空")
}
ctx := c.UserContext()
c.Set("Content-Type", "text/xml; charset=gbk")
c.Set("Content-Type", "text/plain; charset=utf-8")
// 预解析订单号(不验签),用于按 payment_config_id 加载创建订单时所用的配置
preNotify, err := fuiou.ParseNotify(body)
if err != nil {
h.logger.Error("富友回调:预解析失败", zap.Error(err))
h.logger.Error("富友回调:预解析失败",
zap.Error(err),
zap.String("payload_source", bodySource),
zap.Int("raw_body_len", len(c.Body())),
zap.Int("payload_len", len(body)),
zap.String("content_type", c.Get("Content-Type")),
zap.String("query", string(c.Request().URI().QueryString())),
)
return c.Send(fuiou.BuildNotifyFailResponse("parse failed"))
}
@@ -283,6 +290,21 @@ func (h *PaymentHandler) FuiouPayCallback(c *fiber.Ctx) error {
)
return c.Send(fuiou.BuildNotifyFailResponse("payment config unavailable"))
}
if cfg.ProviderType != model.ProviderTypeFuiou ||
strings.TrimSpace(preNotify.InsCd) != strings.TrimSpace(cfg.FyInsCd) ||
strings.TrimSpace(preNotify.MchntCd) != strings.TrimSpace(cfg.FyMchntCd) {
h.logger.Error("富友回调:支付配置与回调商户不匹配",
zap.String("order_no", preNotify.MchntOrderNo),
zap.Uint("config_id", cfg.ID),
zap.String("config_name", cfg.Name),
zap.String("callback_ins_cd", preNotify.InsCd),
zap.String("config_ins_cd", cfg.FyInsCd),
zap.String("callback_mchnt_cd", preNotify.MchntCd),
zap.String("config_mchnt_cd", cfg.FyMchntCd),
zap.String("provider_type", cfg.ProviderType),
)
return c.Send(fuiou.BuildNotifyFailResponse("payment config mismatch"))
}
client, err := fuiou.NewClient(
cfg.FyInsCd,
@@ -307,7 +329,16 @@ func (h *PaymentHandler) FuiouPayCallback(c *fiber.Ctx) error {
zap.String("result_msg", notify.ResultMsg))
return c.Send(fuiou.BuildNotifySuccessResponse())
}
h.logger.Error("富友回调:验签或解析失败", zap.Error(err))
h.logger.Error("富友回调:验签或解析失败",
zap.Error(err),
zap.String("order_no", preNotify.MchntOrderNo),
zap.Uint("config_id", cfg.ID),
zap.String("config_name", cfg.Name),
zap.String("callback_ins_cd", preNotify.InsCd),
zap.String("config_ins_cd", cfg.FyInsCd),
zap.String("callback_mchnt_cd", preNotify.MchntCd),
zap.String("config_mchnt_cd", cfg.FyMchntCd),
)
return c.Send(fuiou.BuildNotifyFailResponse(err.Error()))
}
@@ -345,3 +376,14 @@ func (h *PaymentHandler) FuiouPayCallback(c *fiber.Ctx) error {
return c.Send(fuiou.BuildNotifySuccessResponse())
}
// fuiouCallbackPayload 提取富友回调载荷,兼容 body、form req 和 query req 三种来源。
func fuiouCallbackPayload(c *fiber.Ctx) ([]byte, string) {
if req := strings.TrimSpace(c.FormValue("req")); req != "" {
return []byte(req), "form:req"
}
if req := strings.TrimSpace(c.Query("req")); req != "" {
return []byte(req), "query:req"
}
return c.Body(), "body"
}

View File

@@ -150,7 +150,7 @@ type ClientPackageItem struct {
PackageType string `json:"package_type" description:"套餐类型 (formal:正式套餐, addon:加油包)"`
RetailPrice int64 `json:"retail_price" description:"生效零售价(分)"`
CostPrice int64 `json:"cost_price" description:"成本价(分)"`
ValidityDays int `json:"validity_days" description:"有效天数"`
ValidityDays int `json:"validity_days" description:"有效天数(按天套餐为折算总天数,自然月套餐按当前日期推算)"`
IsAddon bool `json:"is_addon" description:"是否加油包"`
DataAllowance int64 `json:"data_allowance" description:"流量额度"`
DataUnit string `json:"data_unit" description:"流量单位"`

View File

@@ -278,8 +278,6 @@ func (s *Service) Renew(ctx context.Context, id uint) error {
if err = tx.Model(&model.IotCard{}).Where("id = ?", card.ID).Updates(map[string]any{
"generation": card.Generation + 1,
"asset_status": 1,
"accumulated_recharge": 0,
"first_commission_paid": false,
"accumulated_recharge_by_series": "{}",
"first_recharge_triggered_by_series": "{}",
"updater": middleware.GetUserIDFromContext(ctx),
@@ -320,8 +318,6 @@ func (s *Service) Renew(ctx context.Context, id uint) error {
if err = tx.Model(&model.Device{}).Where("id = ?", device.ID).Updates(map[string]any{
"generation": device.Generation + 1,
"asset_status": 1,
"accumulated_recharge": 0,
"first_commission_paid": false,
"accumulated_recharge_by_series": "{}",
"first_recharge_triggered_by_series": "{}",
"updater": middleware.GetUserIDFromContext(ctx),

View File

@@ -10,7 +10,7 @@ import (
// calendarType: 套餐周期类型natural_month=自然月by_day=按天)
// activatedAt: 激活时间
// durationMonths: 套餐时长月数calendar_type=natural_month 时使用)
// durationDays: 套餐天数calendar_type=by_day 时使用)
// durationDays: 套餐天数calendar_type=by_day 时使用,兼容每月折算天数
// 返回:过期时间(当天 23:59:59
func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMonths, durationDays int) time.Time {
var expiryDate time.Time
@@ -31,13 +31,53 @@ func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMon
expiryDate = time.Date(targetYear, targetMonth+1, 0, 23, 59, 59, 0, activatedAt.Location())
} else {
// 按天套餐activated_at + N 天23:59:59
expiryDate = activatedAt.AddDate(0, 0, durationDays)
effectiveDays := calculateByDayValidityDays(durationMonths, durationDays)
expiryDate = activatedAt.AddDate(0, 0, effectiveDays)
expiryDate = time.Date(expiryDate.Year(), expiryDate.Month(), expiryDate.Day(), 23, 59, 59, 0, expiryDate.Location())
}
return expiryDate
}
// CalculateValidityDays 计算套餐展示有效天数。
// 按天套餐优先返回折算后的总天数;自然月套餐按 baseTime 推算到目标月月末。
func CalculateValidityDays(calendarType string, baseTime time.Time, durationMonths, durationDays int) int {
if calendarType == constants.PackageCalendarTypeNaturalMonth {
return calculateNaturalMonthValidityDays(baseTime, durationMonths)
}
return calculateByDayValidityDays(durationMonths, durationDays)
}
func calculateByDayValidityDays(durationMonths, durationDays int) int {
if durationDays <= 0 {
if durationMonths <= 0 {
return 0
}
return durationMonths * 30
}
if durationMonths > 1 && durationDays <= 31 {
return durationMonths * durationDays
}
return durationDays
}
func calculateNaturalMonthValidityDays(baseTime time.Time, durationMonths int) int {
if baseTime.IsZero() || durationMonths <= 0 {
return 0
}
expiresAt := CalculateExpiryTime(constants.PackageCalendarTypeNaturalMonth, baseTime, durationMonths, 0)
startDate := time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), 0, 0, 0, 0, baseTime.Location())
endDate := time.Date(expiresAt.Year(), expiresAt.Month(), expiresAt.Day(), 0, 0, 0, 0, expiresAt.Location())
days := int(endDate.Sub(startDate).Hours()/24) + 1
if days < 0 {
return 0
}
return days
}
// CalculateNextResetTime 计算下次流量重置时间
// dataResetCycle: 流量重置周期daily/monthly/yearly/none
// calendarType: 套餐周期类型natural_month/by_day影响月重置逻辑

View File

@@ -193,6 +193,3 @@ func (s *Service) checkForceRechargeRequirement(ctx context.Context, resourceTyp
return result, nil
}
// updateAccumulatedRechargeInTx 更新累计充值(事务内使用)
// 同时更新旧的 accumulated_recharge 字段和新的 accumulated_recharge_by_series JSON 字段

View File

@@ -204,7 +204,6 @@ func (s *Service) updateAccumulatedRechargeInTx(ctx context.Context, tx *gorm.DB
result := tx.Model(&model.IotCard{}).
Where("id = ?", resourceID).
Updates(map[string]any{
"accumulated_recharge": gorm.Expr("accumulated_recharge + ?", amount),
"accumulated_recharge_by_series": card.AccumulatedRechargeBySeriesJSON,
})
if result.Error != nil {
@@ -225,7 +224,6 @@ func (s *Service) updateAccumulatedRechargeInTx(ctx context.Context, tx *gorm.DB
result := tx.Model(&model.Device{}).
Where("id = ?", resourceID).
Updates(map[string]any{
"accumulated_recharge": gorm.Expr("accumulated_recharge + ?", amount),
"accumulated_recharge_by_series": device.AccumulatedRechargeBySeriesJSON,
})
if result.Error != nil {
@@ -400,7 +398,6 @@ func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *
}
if err := tx.Model(&model.IotCard{}).Where("id = ?", rechargeOrder.ResourceID).
Updates(map[string]any{
"first_commission_paid": true,
"first_recharge_triggered_by_series": card.FirstRechargeTriggeredBySeriesJSON,
}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新卡佣金发放状态失败")
@@ -415,7 +412,6 @@ func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *
}
if err := tx.Model(&model.Device{}).Where("id = ?", rechargeOrder.ResourceID).
Updates(map[string]any{
"first_commission_paid": true,
"first_recharge_triggered_by_series": device.FirstRechargeTriggeredBySeriesJSON,
}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新设备佣金发放状态失败")

View File

@@ -593,8 +593,6 @@ func (s *Service) resetAssetGeneration(ctx context.Context, assetType string, as
if err := tx.Model(&model.IotCard{}).Where("id = ?", card.ID).Updates(map[string]any{
"generation": card.Generation + 1,
"asset_status": 1,
"accumulated_recharge": 0,
"first_commission_paid": false,
"accumulated_recharge_by_series": "{}",
"first_recharge_triggered_by_series": "{}",
"updated_at": now,
@@ -644,8 +642,6 @@ func (s *Service) resetAssetGeneration(ctx context.Context, assetType string, as
if err := tx.Model(&model.Device{}).Where("id = ?", device.ID).Updates(map[string]any{
"generation": device.Generation + 1,
"asset_status": 1,
"accumulated_recharge": 0,
"first_commission_paid": false,
"accumulated_recharge_by_series": "{}",
"first_recharge_triggered_by_series": "{}",
"updated_at": now,