feat: payment
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 1s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 23s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 1s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 1s
CI / payment (push) Successful in 33s
CI / product (push) Successful in 0s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 18s
CI / wecom (push) Successful in 0s

This commit is contained in:
zzw
2026-10-05 15:29:19 +08:00
parent ffe4bf2fe5
commit 851348ed5c
12 changed files with 427 additions and 59 deletions
-11
View File
@@ -10,9 +10,6 @@ const (
PayTypeWechat uint8 = 1
PayTypeAlipay uint8 = 2
PayTypeBank uint8 = 4
CredentialTypeOpenid = "openid"
CredentialEnabled = 1
)
type OrderPayRow struct {
@@ -25,14 +22,6 @@ type OrderPayRow struct {
OutTradeNo string `gorm:"column:out_trade_no" json:"out_trade_no"`
}
type UserCredential struct {
Id int64 `gorm:"column:id" json:"id"`
UserId int64 `gorm:"column:user_id" json:"user_id"`
CredentialType string `gorm:"column:credential_type" json:"credential_type"`
Identifier string `gorm:"column:identifier" json:"identifier"`
Status uint8 `gorm:"column:status" json:"status"`
}
func ChannelToPayType(channel string) uint8 {
switch channel {
case "wechat":
+1 -18
View File
@@ -2,10 +2,8 @@ package logic
import (
"context"
"fmt"
"math"
"lone-services/pkg/modelbase"
pkgpay "lone-services/pkg/payment"
"lone-services/pkg/utils"
payment "lone-services/rpc/payment/pb"
@@ -80,7 +78,7 @@ func (l *PayLogic) Pay(in *payment.PayReq) (*payment.Response, error) {
switch channelCode {
case pkgpay.ChannelWechat:
openid, oErr := loadOpenID(loginUser.ID)
openid, oErr := loadOpenID(l.ctx, l.svcCtx.UserSvcName, loginUser.ID)
if oErr != nil || openid == utils.StringEmpty {
l.Errorf("pay openid: %v", oErr)
return outResponse(utils.Fail, "未绑定微信openid"), nil
@@ -101,21 +99,6 @@ func (l *PayLogic) Pay(in *payment.PayReq) (*payment.Response, error) {
return okResponse(result), nil
}
func loadOpenID(userID int64) (string, error) {
var cred dao.UserCredential
err := modelbase.DB().Table(modelbase.Prefix()+"user_credential").
Where("user_id = ? AND credential_type = ? AND status = ?",
userID, dao.CredentialTypeOpenid, dao.CredentialEnabled).
Take(&cred).Error
if err != nil {
return "", err
}
if cred.Id < utils.NumberOne {
return "", fmt.Errorf("openid credential not found")
}
return cred.Identifier, nil
}
func yuanToFen(yuan float64) int64 {
return int64(math.Round(yuan * 100))
}
@@ -0,0 +1,35 @@
package logic
import (
"context"
"fmt"
"lone-services/pkg/utils"
userpb "lone-services/rpc/user/pb"
"lone-services/services/payment/internal/svc"
)
func loadOpenID(ctx context.Context, userSvcName string, userID int64) (string, error) {
if userSvcName == utils.StringEmpty {
return utils.StringEmpty, fmt.Errorf("user service not configured")
}
if userID < utils.NumberOne {
return utils.StringEmpty, fmt.Errorf("invalid user id")
}
cli, err := svc.GetRpcClient(userSvcName)
if err != nil {
return utils.StringEmpty, err
}
info, err := userpb.NewUserClient(cli.Conn()).InternalGetUserInfo(ctx, &userpb.InternalGetUserInfoReq{
UserId: userID,
CredentialType: "openid",
})
if err != nil {
return utils.StringEmpty, err
}
if info == nil || info.Identifier == utils.StringEmpty {
return utils.StringEmpty, fmt.Errorf("openid credential not found")
}
return info.Identifier, nil
}