From c61abde23b46fbe38ffd8dd79c1218e8df885124 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Sun, 18 Feb 2024 23:47:58 +0800 Subject: [PATCH] fff --- app/mix/controller/alipay_callback.go | 2 +- app/mix/dao/mysql.go | 2 +- app/mix/service/logic/vas.go | 8 +++--- app/mix/service/service.go | 6 +--- library/payclients/alipaycli/client.go | 31 +++++++++++++++++++-- library/payclients/alipaycli/client_test.go | 22 ++++++++++++--- 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/app/mix/controller/alipay_callback.go b/app/mix/controller/alipay_callback.go index 5af4feb3..08afbe5d 100644 --- a/app/mix/controller/alipay_callback.go +++ b/app/mix/controller/alipay_callback.go @@ -12,7 +12,7 @@ import ( func AlipayCallback(ctx *gin.Context) { //req, _ := ctx.GetRawData() //logger.Info("_AlipayCallback req: %v", string(req)) - bm, err := alipaycli.GetAlipayClient().ParseNotify(ctx.Request) + bm, err := alipaycli.GetDefaultAlipayClient().ParseNotify(ctx.Request) if err != nil { logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(bm), err) return diff --git a/app/mix/dao/mysql.go b/app/mix/dao/mysql.go index c8093359..da870c62 100644 --- a/app/mix/dao/mysql.go +++ b/app/mix/dao/mysql.go @@ -116,7 +116,7 @@ func (m *Mysql) CreateOrder(ctx *gin.Context, tx *sqlx.Tx, order *dbstruct.Order " (id, mid, uid, product_id, pay_type, pay_amount, oid1, oid2, oid3, " + " 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) " + - " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " if tx != nil { _, err = tx.ExecContext(ctx, sqlStr, order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetOid1(), order.GetOid2(), order.GetOid3(), diff --git a/app/mix/service/logic/vas.go b/app/mix/service/logic/vas.go index ba901ad0..754c79ac 100644 --- a/app/mix/service/logic/vas.go +++ b/app/mix/service/logic/vas.go @@ -168,7 +168,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data case vasproto.PayTypeOp: case vasproto.PayTypeAlipay: - alipayCli := alipaycli.GetAlipayClient() + alipayCli := alipaycli.GetDefaultAlipayClient() appPayParam := &alipaycli.AppPayParam{ OutTradeNo: orderId, Subject: product.Subject, @@ -182,7 +182,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data } req.Oid3 = alipayCli.AppId case vasproto.PayTypeAlipayH5: - alipayCli := alipaycli.GetAlipayClient() + alipayCli := alipaycli.GetDefaultAlipayClient() wapPayParam := &alipaycli.WapPayParam{ OutTradeNo: orderId, Subject: product.Subject, @@ -1945,7 +1945,7 @@ func (v *Vas) WithdrawApply(ctx *gin.Context, req *vasproto.WithdrawApplyReq) (t AlipayLoginId: alipayId, AlipayName: alipayName, } - transferResp, err = alipaycli.GetAlipayClient().UniTransfer(ctx, transferParam) + transferResp, err = alipaycli.GetDefaultAlipayClient().UniTransfer(ctx, transferParam) if err != nil { logger.Error("UniTransfer fail, param: %v, err: %v", util.ToJson(transferParam), err) return @@ -2083,7 +2083,7 @@ func (v *Vas) DealOneOrder(ctx *gin.Context, orderId string) (err error) { // 支付宝查询订单 switch order.GetPayType() { case vasproto.PayTypeAlipayH5: - alipayCli := alipaycli.GetAlipayClient() + alipayCli := alipaycli.GetDefaultAlipayClient() var alipayResp *alipay.TradeQueryResponse alipayResp, err = alipayCli.QueryOrder(ctx, &alipaycli.QueryOrderParam{ OutTradeNo: orderId, diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 99facaed..ff5ec41d 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -43,7 +43,6 @@ import ( "service/bizcommon/util" "service/dbstruct" "service/library/apollo" - "service/library/configcenter" "service/library/contentaudit/imageaudit" "service/library/contentaudit/textaudit" "service/library/logger" @@ -129,10 +128,7 @@ func (s *Service) Init(c any) (err error) { s.defaultMelody = melody.New() - err = alipaycli.InitMulti([]*configcenter.AlipayClientConfig{ - cfg.Alipay, - cfg.AlipayMYTS, - }) + err = alipaycli.InitMulti(cfg.Alipay, cfg.AlipayMYTS) if err != nil { logger.Error("alipaycli.Init fail, cfg: %v, err: %v", util.ToJson(cfg.Alipay), err) return diff --git a/library/payclients/alipaycli/client.go b/library/payclients/alipaycli/client.go index 663e3466..878e1ab0 100644 --- a/library/payclients/alipaycli/client.go +++ b/library/payclients/alipaycli/client.go @@ -24,16 +24,20 @@ const ( var allAlipayClients = map[string]*AlipayClient{} -func GetAlipayClient() *AlipayClient { +func GetDefaultAlipayClient() *AlipayClient { return allAlipayClients[AppIdXinYiDaoLe] } +func GetAlipayClientByAppId(appId string) *AlipayClient { + return allAlipayClients[appId] +} + type AlipayClient struct { *alipay.Client alipayPublicCertPath string } -func InitMulti(cfgList []*configcenter.AlipayClientConfig) (err error) { +func InitMulti(cfgList ...*configcenter.AlipayClientConfig) (err error) { for _, cfg := range cfgList { var cli *AlipayClient cli, err = NewAlipayClient(cfg) @@ -197,3 +201,26 @@ func (c *AlipayClient) QueryOrder(ctx context.Context, param *QueryOrderParam) ( } return } + +// 退款申请 +type RefundOneParam struct { + OutTradeNo string // 商家订单id,我们自己的订单id + RefundAmount int64 // 退款金额,单位:分 + RefundReason string // 退款理由 +} + +func (c *AlipayClient) RefundOne(ctx context.Context, param *RefundOneParam) (resp *alipay.TradeRefundResponse, err error) { + if len(param.RefundReason) <= 0 { + param.RefundReason = "退款" + } + bm := gopay.BodyMap{ + "out_trade_no": param.OutTradeNo, + "refund_amount": fmt.Sprintf("%.2f", float64(param.RefundAmount)/100.0), + "refund_reason": param.RefundReason, + } + resp, err = c.TradeRefund(ctx, bm) + if err != nil { + return + } + return +} diff --git a/library/payclients/alipaycli/client_test.go b/library/payclients/alipaycli/client_test.go index 8b69a3d9..93582ce9 100644 --- a/library/payclients/alipaycli/client_test.go +++ b/library/payclients/alipaycli/client_test.go @@ -13,12 +13,12 @@ import ( func TestMain(m *testing.M) { cfg := new(conf.ConfigSt) - err := configcenter.LoadConfig("/Users/erwin/wishpalv2/service/etc/mix/mix-local.yaml", cfg) + err := configcenter.LoadConfig("/Users/erwin/wishpalv2/service/etc/mix/mix-prod.yaml", cfg) if err != nil { fmt.Printf("%v\n", err) } - err = Init(cfg.AlipayMYTS) + err = InitMulti(cfg.Alipay, cfg.AlipayMYTS) if err != nil { fmt.Println(err) os.Exit(1) @@ -27,7 +27,7 @@ func TestMain(m *testing.M) { } func TestAlipayClient_WapPay(t *testing.T) { - cli := GetAlipayClient() + cli := GetDefaultAlipayClient() paramStr, err := cli.WapPay(context.Background(), &WapPayParam{ OutTradeNo: idgenerator.GenOrderId(), Subject: "哈哈", @@ -42,7 +42,7 @@ func TestAlipayClient_WapPay(t *testing.T) { } func TestAlipayClient_QueryOrder(t *testing.T) { - cli := GetAlipayClient() + cli := GetDefaultAlipayClient() resp, err := cli.QueryOrder(context.Background(), &QueryOrderParam{ OutTradeNo: "1750539072771731456", }) @@ -52,3 +52,17 @@ func TestAlipayClient_QueryOrder(t *testing.T) { } fmt.Println(util.ToJson(resp)) } + +func TestAlipayClient_RefundOne(t *testing.T) { + cli := GetAlipayClientByAppId(AppIdXinYiDaoLe) + resp, err := cli.RefundOne(context.Background(), &RefundOneParam{ + OutTradeNo: "1750539072771731456", + RefundAmount: 0, + RefundReason: "测试退款", + }) + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println(util.ToJson(resp)) +}