66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package topuplogic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/smartwalle/alipay/v3"
|
|
"tianyuan-api/apps/sentinel/internal/model"
|
|
"tianyuan-api/apps/sentinel/internal/svc"
|
|
"tianyuan-api/apps/sentinel/sentinel"
|
|
"tianyuan-api/pkg/generate"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AliTopUpLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewAliTopUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliTopUpLogic {
|
|
return &AliTopUpLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AliTopUpLogic) AliTopUp(in *sentinel.AliTopUpRequest) (*sentinel.AliTopUpResponse, error) {
|
|
if in.Amount > l.svcCtx.Config.TopUp.MaxTopUpAmount {
|
|
return &sentinel.AliTopUpResponse{}, fmt.Errorf("充值金额最大不能超过%d元", l.svcCtx.Config.TopUp.MaxTopUpAmount)
|
|
}
|
|
// 使用从 svcCtx 中获取的支付宝客户端
|
|
client := l.svcCtx.AlipayClient
|
|
outTradeNo := generate.GenerateTransactionID()
|
|
totalAmount := fmt.Sprintf("%.2f", float64(in.Amount))
|
|
// 构造支付请求
|
|
var p = alipay.TradePagePay{
|
|
Trade: alipay.Trade{
|
|
Subject: "天远数据API使用额度",
|
|
OutTradeNo: outTradeNo,
|
|
TotalAmount: totalAmount,
|
|
ProductCode: "FAST_INSTANT_TRADE_PAY",
|
|
NotifyURL: "https://console.tianyuanapi.com/api/console/", // 异步回调通知地址
|
|
ReturnURL: "https://console.tianyuanapi.com/charge/recharge", // 支付成功后的跳转地址
|
|
},
|
|
}
|
|
_, inserErr := l.svcCtx.PayOrderModel.Insert(l.ctx, &model.PayOrder{
|
|
UserId: in.UserId,
|
|
OutTradeNo: outTradeNo,
|
|
Amount: float64(in.Amount),
|
|
})
|
|
if inserErr != nil {
|
|
return nil, inserErr
|
|
}
|
|
// 生成支付链接
|
|
url, err := client.TradePagePay(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &sentinel.AliTopUpResponse{
|
|
PayUrl: url.String(),
|
|
}, nil
|
|
}
|