(c *gin.Context)
| 75 | } |
| 76 | |
| 77 | func RequestEpay(c *gin.Context) { |
| 78 | var req EpayRequest |
| 79 | err := c.ShouldBindJSON(&req) |
| 80 | if err != nil { |
| 81 | c.JSON(200, gin.H{"message": "error", "data": "参数错误"}) |
| 82 | return |
| 83 | } |
| 84 | if req.Amount < getMinTopup() { |
| 85 | c.JSON(200, gin.H{"message": "error", "data": fmt.Sprintf("充值数量不能小于 %d", getMinTopup())}) |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | id := c.GetInt("id") |
| 90 | group, err := model.GetUserGroup(id, true) |
| 91 | if err != nil { |
| 92 | c.JSON(200, gin.H{"message": "error", "data": "获取用户分组失败"}) |
| 93 | return |
| 94 | } |
| 95 | payMoney := getPayMoney(req.Amount, group) |
| 96 | if payMoney < 0.01 { |
| 97 | c.JSON(200, gin.H{"message": "error", "data": "充值金额过低"}) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | if !setting.ContainsPayMethod(req.PaymentMethod) { |
| 102 | c.JSON(200, gin.H{"message": "error", "data": "支付方式不存在"}) |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | callBackAddress := service.GetCallbackAddress() |
| 107 | returnUrl, _ := url.Parse(setting.ServerAddress + "/console/log") |
| 108 | notifyUrl, _ := url.Parse(callBackAddress + "/api/user/epay/notify") |
| 109 | tradeNo := fmt.Sprintf("%s%d", common.GetRandomString(6), time.Now().Unix()) |
| 110 | tradeNo = fmt.Sprintf("USR%dNO%s", id, tradeNo) |
| 111 | client := GetEpayClient() |
| 112 | if client == nil { |
| 113 | c.JSON(200, gin.H{"message": "error", "data": "当前管理员未配置支付信息"}) |
| 114 | return |
| 115 | } |
| 116 | uri, params, err := client.Purchase(&epay.PurchaseArgs{ |
| 117 | Type: req.PaymentMethod, |
| 118 | ServiceTradeNo: tradeNo, |
| 119 | Name: fmt.Sprintf("TUC%d", req.Amount), |
| 120 | Money: strconv.FormatFloat(payMoney, 'f', 2, 64), |
| 121 | Device: epay.PC, |
| 122 | NotifyUrl: notifyUrl, |
| 123 | ReturnUrl: returnUrl, |
| 124 | }) |
| 125 | if err != nil { |
| 126 | c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"}) |
| 127 | return |
| 128 | } |
| 129 | amount := req.Amount |
| 130 | if !common.DisplayInCurrencyEnabled { |
| 131 | dAmount := decimal.NewFromInt(int64(amount)) |
| 132 | dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) |
| 133 | amount = dAmount.Div(dQuotaPerUnit).IntPart() |
| 134 | } |
nothing calls this directly
no test coverage detected