(referenceId string, customerId string)
| 52 | } |
| 53 | |
| 54 | func 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 | } |
no test coverage detected