From 1bc256fc19e9557f910aa92b9831c9ac897152f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Mon, 26 Aug 2024 13:46:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D=E8=BD=AC=E8=B4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alipay_redpack/internal/alipay_redpack.go | 3 ++- .../internal/alipay_redpack_test.go | 4 ++-- plugins/alipay_redpack/internal/po/order.go | 11 ++++++----- plugins/alipay_redpack/internal/transform.go | 2 +- plugins/alipay_redpack/internal/util.go | 19 +++---------------- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/plugins/alipay_redpack/internal/alipay_redpack.go b/plugins/alipay_redpack/internal/alipay_redpack.go index 7435081..229f008 100644 --- a/plugins/alipay_redpack/internal/alipay_redpack.go +++ b/plugins/alipay_redpack/internal/alipay_redpack.go @@ -20,6 +20,7 @@ const ( const baseUri = "https://openapi.alipay.com/gateway.do" const ( + // 支持幂等请求 orderMethod = "alipay.fund.trans.uni.transfer" queryMethod = "alipay.fund.trans.common.query" ) @@ -44,7 +45,7 @@ func (s *AlipayRedPackService) Order(ctx context.Context, request *proto.OrderRe return nil, err } var response po.OrderResp - err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx) + err = requests.URL(baseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx) if err != nil { return nil, fmt.Errorf("请求异常,msg:" + err.Error()) } diff --git a/plugins/alipay_redpack/internal/alipay_redpack_test.go b/plugins/alipay_redpack/internal/alipay_redpack_test.go index 96da3bb..64babd1 100644 --- a/plugins/alipay_redpack/internal/alipay_redpack_test.go +++ b/plugins/alipay_redpack/internal/alipay_redpack_test.go @@ -35,7 +35,7 @@ func TestOrder(t *testing.T) { request := &proto.OrderRequest{ Config: config(), Order: &proto.OrderRequest_Order{ - OrderNo: "lsxd202306071545141533", + OrderNo: "lsxd202406071545141533", Account: "18666173766", Quantity: 1, Amount: 0.01, @@ -44,7 +44,7 @@ func TestOrder(t *testing.T) { Product: &proto.OrderRequest_Product{ ProductNo: "", Price: 0.01, - Extra: []byte(`{"wishing":"祝福语"}`), + Extra: []byte(`{"wishing":"zhufuyu"}`), }, } t.Run("TestOrder", func(t *testing.T) { diff --git a/plugins/alipay_redpack/internal/po/order.go b/plugins/alipay_redpack/internal/po/order.go index b898197..0302cc3 100644 --- a/plugins/alipay_redpack/internal/po/order.go +++ b/plugins/alipay_redpack/internal/po/order.go @@ -14,7 +14,7 @@ type PayeeInfo struct { type OrderReq struct { OutBizNo string `validate:"required" json:"out_biz_no"` - TransAmount string `validate:"required" json:"trans_amount"` + TransAmount float32 `validate:"required" json:"trans_amount"` ProductCode string `validate:"required" json:"product_code"` BizScene string `validate:"required" json:"biz_scene"` OrderTitle string `json:"order_title"` @@ -33,13 +33,14 @@ type OrderResponse struct { OrderId string `json:"order_id"` PayFundOrderId string `json:"pay_fund_order_id"` Status string `json:"status"` - TransDate int32 `json:"trans_date"` - SettleSerialNo int32 `json:"settle_serial_no"` + TransDate string `json:"trans_date"` + SettleSerialNo int32 `json:"settle_serial_no"` // 清算机构流水号 } type OrderResp struct { - Response OrderResponse `json:"alipay_fund_trans_uni_transfer_response"` - Sign string `json:"sign"` + Response OrderResponse `json:"alipay_fund_trans_uni_transfer_response"` + AlipayCertSn string `json:"alipay_cert_sn"` + Sign string `json:"sign"` } func (o *OrderResp) GetMsg() string { diff --git a/plugins/alipay_redpack/internal/transform.go b/plugins/alipay_redpack/internal/transform.go index 3032022..e488cdb 100644 --- a/plugins/alipay_redpack/internal/transform.go +++ b/plugins/alipay_redpack/internal/transform.go @@ -89,7 +89,7 @@ func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Produ o := &po.OrderReq{ OutBizNo: order.OrderNo, - TransAmount: fmt.Sprintf("%.2f", order.Amount), + TransAmount: order.Amount, ProductCode: "STD_RED_PACKET", BizScene: "DIRECT_TRANSFER", OrderTitle: productExtra.Wishing, diff --git a/plugins/alipay_redpack/internal/util.go b/plugins/alipay_redpack/internal/util.go index d95c508..55e51c8 100644 --- a/plugins/alipay_redpack/internal/util.go +++ b/plugins/alipay_redpack/internal/util.go @@ -18,17 +18,12 @@ import ( ) func isValidPhoneNumber(phoneNumber string) bool { - // 定义中国大陆手机号的正则表达式 - // 这个正则表达式匹配11位数字,以1开头,第二位为3、4、5、7、8中的一个,后面跟任意8位数字 phoneRegex := `^1[34578]\d{9}$` - // 使用正则表达式进行匹配 return regexp.MustCompile(phoneRegex).MatchString(phoneNumber) } func isEmailValid(email string) bool { - // 定义邮箱的正则表达式 var emailRegex = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) - // 使用正则表达式匹配邮箱地址 return emailRegex.MatchString(email) } @@ -37,16 +32,14 @@ func req(config *Config, req *po.Param) (url.Values, error) { uv := url.Values{} kvRows := utils.SortStructJsonTag(req) for _, kv := range kvRows { - if kv.Key == "sign" { - continue - } - if kv.Value == "" { + if kv.Key == "sign" || kv.Value == "" { continue } uv.Set(kv.Key, kv.Value) strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value)) } s := strings.TrimRight(strToBeSigned.String(), "&") + sign, err := Sign(s, []byte(utils.NewPrivate().Build(config.Prk))) if err != nil { return nil, err @@ -81,13 +74,7 @@ func Verify(n *po.Notify, publicKeyPEM string) (bool, error) { uv := url.Values{} kvRows := utils.SortStructJsonTag(n) for _, kv := range kvRows { - if kv.Key == "sign" { - continue - } - if kv.Key == "sign_type" { - continue - } - if kv.Value == "" { + if kv.Key == "sign" || kv.Key == "sign_type" || kv.Value == "" { continue } uv.Set(kv.Key, kv.Value)