MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / Recharge

Function Recharge

model/topup.go:54–100  ·  view source on GitHub ↗
(referenceId string, customerId string)

Source from the content-addressed store, hash-verified

52}
53
54func Recharge(referenceId string, customerId string) (err error) {
55 if referenceId == "" {
56 return errors.New("未提供支付单号")
57 }
58
59 var quota float64
60 topUp := &TopUp{}
61
62 refCol := "`trade_no`"
63 if common.UsingPostgreSQL {
64 refCol = `"trade_no"`
65 }
66
67 err = DB.Transaction(func(tx *gorm.DB) error {
68 err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", referenceId).First(topUp).Error
69 if err != nil {
70 return errors.New("充值订单不存在")
71 }
72
73 if topUp.Status != common.TopUpStatusPending {
74 return errors.New("充值订单状态错误")
75 }
76
77 topUp.CompleteTime = common.GetTimestamp()
78 topUp.Status = common.TopUpStatusSuccess
79 err = tx.Save(topUp).Error
80 if err != nil {
81 return err
82 }
83
84 quota = topUp.Money * common.QuotaPerUnit
85 err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Updates(map[string]interface{}{"stripe_customer": customerId, "quota": gorm.Expr("quota + ?", quota)}).Error
86 if err != nil {
87 return err
88 }
89
90 return nil
91 })
92
93 if err != nil {
94 return errors.New("充值失败," + err.Error())
95 }
96
97 RecordLog(topUp.UserId, LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v,支付金额:%d", common.FormatQuota(int(quota)), topUp.Amount))
98
99 return nil
100}

Callers 1

sessionCompletedFunction · 0.92

Calls 5

GetTimestampFunction · 0.92
FormatQuotaFunction · 0.92
RecordLogFunction · 0.85
SaveMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected