支付宝优化

This commit is contained in:
lwl0608 2024-02-18 23:24:52 +08:00
parent 1ce92ce589
commit ef6538528d
7 changed files with 48 additions and 20 deletions

View File

@ -8,8 +8,8 @@ type ConfigSt struct {
MixMysql *configcenter.MysqlConfig `json:"mix_mysql" yaml:"mix_mysql"` // mix mysql MixMysql *configcenter.MysqlConfig `json:"mix_mysql" yaml:"mix_mysql"` // mix mysql
Crypto *configcenter.CryptoConfig `json:"crypto" yaml:"crypto"` // 账号相关加密 Crypto *configcenter.CryptoConfig `json:"crypto" yaml:"crypto"` // 账号相关加密
Wxpay *configcenter.WxpayClientConfig `json:"wxpay" yaml:"wxpay"` // 微信支付 Wxpay *configcenter.WxpayClientConfig `json:"wxpay" yaml:"wxpay"` // 微信支付
Alipay *configcenter.AlipayClientConfig `json:"alipay" yaml:"alipay"` // 支付宝 Alipay *configcenter.AlipayClientConfig `json:"alipay" yaml:"alipay"` // 支付宝 心意到了
AlipayMYTS *configcenter.AlipayClientConfig `json:"alipay_myts" yaml:"alipay_myts"` // 支付宝 AlipayMYTS *configcenter.AlipayClientConfig `json:"alipay_myts" yaml:"alipay_myts"` // 支付宝 觅缘天使
Apollo *configcenter.ApolloConfig `json:"apollo" yaml:"apollo"` // Apollo Apollo *configcenter.ApolloConfig `json:"apollo" yaml:"apollo"` // Apollo
Media *configcenter.MediaConfig `json:"media" yaml:"media"` // 媒体配置 Media *configcenter.MediaConfig `json:"media" yaml:"media"` // 媒体配置
Dysmsapi *configcenter.DysmsapiConfig `json:"dysmsapi" yaml:"dysmsapi"` // 阿里云短信服务 Dysmsapi *configcenter.DysmsapiConfig `json:"dysmsapi" yaml:"dysmsapi"` // 阿里云短信服务

View File

@ -12,7 +12,7 @@ import (
func AlipayCallback(ctx *gin.Context) { func AlipayCallback(ctx *gin.Context) {
//req, _ := ctx.GetRawData() //req, _ := ctx.GetRawData()
//logger.Info("_AlipayCallback req: %v", string(req)) //logger.Info("_AlipayCallback req: %v", string(req))
bm, err := alipaycli.GetDefaultAlipayClient().ParseNotify(ctx.Request) bm, err := alipaycli.GetAlipayClient().ParseNotify(ctx.Request)
if err != nil { if err != nil {
logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(bm), err) logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(bm), err)
return return

View File

@ -113,20 +113,20 @@ func NewMysql(cfg *conf.ConfigSt) (mysql *Mysql, err error) {
func (m *Mysql) CreateOrder(ctx *gin.Context, tx *sqlx.Tx, order *dbstruct.Order) error { func (m *Mysql) CreateOrder(ctx *gin.Context, tx *sqlx.Tx, order *dbstruct.Order) error {
var err error var err error
sqlStr := "insert into " + TableOrder + sqlStr := "insert into " + TableOrder +
" (id, mid, uid, product_id, pay_type, pay_amount, " + " (id, mid, uid, product_id, pay_type, pay_amount, oid1, oid2, oid3, " +
" out_order_id, receipt_data, coins, order_status, order_from, " + " out_order_id, receipt_data, coins, order_status, order_from, " +
" ct, ut, ext, b_did, b_ver, b_osver, b_dt, b_ch, b_model, b_nt, ip) " + " ct, ut, ext, b_did, b_ver, b_osver, b_dt, b_ch, b_model, b_nt, ip) " +
" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
if tx != nil { if tx != nil {
_, err = tx.ExecContext(ctx, sqlStr, _, err = tx.ExecContext(ctx, sqlStr,
order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetOid1(), order.GetOid2(), order.GetOid3(),
order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(), order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(),
order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(), order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(),
) )
} else { } else {
db := m.getDBVas() db := m.getDBVas()
_, err = db.ExecContext(ctx, sqlStr, _, err = db.ExecContext(ctx, sqlStr,
order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetOid1(), order.GetOid2(), order.GetOid3(),
order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(), order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(),
order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(), order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(),
) )

View File

@ -168,7 +168,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data
case vasproto.PayTypeOp: case vasproto.PayTypeOp:
case vasproto.PayTypeAlipay: case vasproto.PayTypeAlipay:
alipayCli := alipaycli.GetDefaultAlipayClient() alipayCli := alipaycli.GetAlipayClient()
appPayParam := &alipaycli.AppPayParam{ appPayParam := &alipaycli.AppPayParam{
OutTradeNo: orderId, OutTradeNo: orderId,
Subject: product.Subject, Subject: product.Subject,
@ -182,7 +182,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data
} }
req.Oid3 = alipayCli.AppId req.Oid3 = alipayCli.AppId
case vasproto.PayTypeAlipayH5: case vasproto.PayTypeAlipayH5:
alipayCli := alipaycli.GetDefaultAlipayClient() alipayCli := alipaycli.GetAlipayClient()
wapPayParam := &alipaycli.WapPayParam{ wapPayParam := &alipaycli.WapPayParam{
OutTradeNo: orderId, OutTradeNo: orderId,
Subject: product.Subject, Subject: product.Subject,
@ -1945,7 +1945,7 @@ func (v *Vas) WithdrawApply(ctx *gin.Context, req *vasproto.WithdrawApplyReq) (t
AlipayLoginId: alipayId, AlipayLoginId: alipayId,
AlipayName: alipayName, AlipayName: alipayName,
} }
transferResp, err = alipaycli.GetDefaultAlipayClient().UniTransfer(ctx, transferParam) transferResp, err = alipaycli.GetAlipayClient().UniTransfer(ctx, transferParam)
if err != nil { if err != nil {
logger.Error("UniTransfer fail, param: %v, err: %v", util.ToJson(transferParam), err) logger.Error("UniTransfer fail, param: %v, err: %v", util.ToJson(transferParam), err)
return return
@ -2083,7 +2083,7 @@ func (v *Vas) DealOneOrder(ctx *gin.Context, orderId string) (err error) {
// 支付宝查询订单 // 支付宝查询订单
switch order.GetPayType() { switch order.GetPayType() {
case vasproto.PayTypeAlipayH5: case vasproto.PayTypeAlipayH5:
alipayCli := alipaycli.GetDefaultAlipayClient() alipayCli := alipaycli.GetAlipayClient()
var alipayResp *alipay.TradeQueryResponse var alipayResp *alipay.TradeQueryResponse
alipayResp, err = alipayCli.QueryOrder(ctx, &alipaycli.QueryOrderParam{ alipayResp, err = alipayCli.QueryOrder(ctx, &alipaycli.QueryOrderParam{
OutTradeNo: orderId, OutTradeNo: orderId,

View File

@ -43,6 +43,7 @@ import (
"service/bizcommon/util" "service/bizcommon/util"
"service/dbstruct" "service/dbstruct"
"service/library/apollo" "service/library/apollo"
"service/library/configcenter"
"service/library/contentaudit/imageaudit" "service/library/contentaudit/imageaudit"
"service/library/contentaudit/textaudit" "service/library/contentaudit/textaudit"
"service/library/logger" "service/library/logger"
@ -128,7 +129,10 @@ func (s *Service) Init(c any) (err error) {
s.defaultMelody = melody.New() s.defaultMelody = melody.New()
err = alipaycli.Init(cfg.AlipayMYTS) err = alipaycli.InitMulti([]*configcenter.AlipayClientConfig{
cfg.Alipay,
cfg.AlipayMYTS,
})
if err != nil { if err != nil {
logger.Error("alipaycli.Init fail, cfg: %v, err: %v", util.ToJson(cfg.Alipay), err) logger.Error("alipaycli.Init fail, cfg: %v, err: %v", util.ToJson(cfg.Alipay), err)
return return

View File

@ -2,6 +2,7 @@ package alipaycli
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/go-pay/gopay" "github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/alipay"
@ -16,17 +17,39 @@ const (
DefaultOrderTimeoutSeconds = 900 // 默认订单超时时间,单位: s DefaultOrderTimeoutSeconds = 900 // 默认订单超时时间,单位: s
) )
var defaultAlipayClient *AlipayClient const (
AppIdXinYiDaoLe = "2021004115647165" // 心意到了
AppIdMiYuanTianShi = "2021004135664261" // 觅缘天使
)
func GetDefaultAlipayClient() *AlipayClient { var allAlipayClients = map[string]*AlipayClient{}
return defaultAlipayClient
func GetAlipayClient() *AlipayClient {
return allAlipayClients[AppIdXinYiDaoLe]
} }
type AlipayClient struct { type AlipayClient struct {
*alipay.Client *alipay.Client
alipayPublicCertPath string
} }
func Init(cfg *configcenter.AlipayClientConfig) (err error) { func InitMulti(cfgList []*configcenter.AlipayClientConfig) (err error) {
for _, cfg := range cfgList {
var cli *AlipayClient
cli, err = NewAlipayClient(cfg)
if err != nil {
return
}
if cli == nil {
err = errors.New("NewAlipayClient fail")
return
}
allAlipayClients[cli.AppId] = cli
}
return
}
func NewAlipayClient(cfg *configcenter.AlipayClientConfig) (ret *AlipayClient, err error) {
alipayCli, err := alipay.NewClient(cfg.Appid, cfg.PrivateKey, true) alipayCli, err := alipay.NewClient(cfg.Appid, cfg.PrivateKey, true)
if err != nil { if err != nil {
logger.Error("NewClient fail, cfg: %v, err: %v", util.ToJson(cfg), err) logger.Error("NewClient fail, cfg: %v, err: %v", util.ToJson(cfg), err)
@ -44,8 +67,9 @@ func Init(cfg *configcenter.AlipayClientConfig) (err error) {
return return
} }
defaultAlipayClient = &AlipayClient{ ret = &AlipayClient{
Client: alipayCli, Client: alipayCli,
alipayPublicCertPath: cfg.AlipayPublicCertPath,
} }
return return
} }
@ -61,7 +85,7 @@ func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err
logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp)) logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp))
// 验签 // 验签
ok, err := alipay.VerifySignWithCert("/app/wishpal-ironfan/etc/mix/alipaycert_myts/alipayCertPublicKey_RSA2.crt", notifyTmp) ok, err := alipay.VerifySignWithCert(c.alipayPublicCertPath, notifyTmp)
if !ok { if !ok {
logger.Error("VerifySign fail, not ok, bm: %v", util.ToJson(notifyTmp)) logger.Error("VerifySign fail, not ok, bm: %v", util.ToJson(notifyTmp))
return return

View File

@ -27,7 +27,7 @@ func TestMain(m *testing.M) {
} }
func TestAlipayClient_WapPay(t *testing.T) { func TestAlipayClient_WapPay(t *testing.T) {
cli := GetDefaultAlipayClient() cli := GetAlipayClient()
paramStr, err := cli.WapPay(context.Background(), &WapPayParam{ paramStr, err := cli.WapPay(context.Background(), &WapPayParam{
OutTradeNo: idgenerator.GenOrderId(), OutTradeNo: idgenerator.GenOrderId(),
Subject: "哈哈", Subject: "哈哈",
@ -42,7 +42,7 @@ func TestAlipayClient_WapPay(t *testing.T) {
} }
func TestAlipayClient_QueryOrder(t *testing.T) { func TestAlipayClient_QueryOrder(t *testing.T) {
cli := GetDefaultAlipayClient() cli := GetAlipayClient()
resp, err := cli.QueryOrder(context.Background(), &QueryOrderParam{ resp, err := cli.QueryOrder(context.Background(), &QueryOrderParam{
OutTradeNo: "1750539072771731456", OutTradeNo: "1750539072771731456",
}) })