支付宝转账

This commit is contained in:
李子铭 2024-08-23 17:00:32 +08:00
parent 1c0a06f612
commit d64c0d0e33
5 changed files with 26 additions and 35 deletions

View File

@ -44,7 +44,7 @@ func (s *AlipayRedPackService) Order(ctx context.Context, request *proto.OrderRe
return nil, err return nil, err
} }
var response po.OrderResp var response po.OrderResp
err = requests.URL(c.BaseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx) err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx)
if err != nil { if err != nil {
return nil, fmt.Errorf("请求异常msg:" + err.Error()) return nil, fmt.Errorf("请求异常msg:" + err.Error())
} }
@ -70,7 +70,7 @@ func (s *AlipayRedPackService) Query(ctx context.Context, request *proto.QueryRe
return nil, err return nil, err
} }
var response po.QueryResp var response po.QueryResp
err = requests.URL(c.BaseUri).BodyForm(uv).ToJSON(&response).Fetch(ctx) err = requests.URL(baseUri).BodyForm(uv).ToJSON(&response).Fetch(ctx)
if err != nil { if err != nil {
return nil, fmt.Errorf("请求异常msg:" + err.Error()) return nil, fmt.Errorf("请求异常msg:" + err.Error())
} }

View File

@ -26,7 +26,7 @@ func config() []byte {
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
t.Run("TestConfig", func(t *testing.T) { t.Run("TestConfig", func(t *testing.T) {
c := config() c := config()
fmt.Printf("%+s\n", string(c)) fmt.Printf("%s\n", string(c))
assert.NotEmpty(t, c) assert.NotEmpty(t, c)
}) })
} }

View File

@ -1,7 +1,6 @@
package po package po
import ( import (
"encoding/json"
"fmt" "fmt"
"gitea.cdlsxd.cn/BaseSystem/plugin/proto" "gitea.cdlsxd.cn/BaseSystem/plugin/proto"
"plugins/alipay_redpack/internal/vo" "plugins/alipay_redpack/internal/vo"
@ -13,15 +12,6 @@ type PayeeInfo struct {
Name string `json:"name"` Name string `json:"name"`
} }
type BusinessParams struct {
SubBizScene string `json:"sub_biz_scene"`
}
func (b *BusinessParams) ToString() string {
s, _ := json.Marshal(b)
return string(s)
}
type OrderReq struct { type OrderReq struct {
OutBizNo string `validate:"required" json:"out_biz_no"` OutBizNo string `validate:"required" json:"out_biz_no"`
TransAmount string `validate:"required" json:"trans_amount"` TransAmount string `validate:"required" json:"trans_amount"`
@ -29,8 +19,8 @@ type OrderReq struct {
BizScene string `validate:"required" json:"biz_scene"` BizScene string `validate:"required" json:"biz_scene"`
OrderTitle string `json:"order_title"` OrderTitle string `json:"order_title"`
Remark string `json:"remark"` Remark string `json:"remark"`
PayeeInfo *PayeeInfo `json:"payee_info"` PayeeInfo *PayeeInfo `validate:"required" json:"payee_info"`
BusinessParams string `json:"business_params"` BusinessParams string `validate:"required" json:"business_params"`
} }
type OrderResponse struct { type OrderResponse struct {

View File

@ -13,7 +13,6 @@ import (
type Config struct { type Config struct {
AppId string `json:"app_id"` AppId string `json:"app_id"`
BaseUri string `json:"base_uri"`
Prk string `json:"prk"` // 私钥 Prk string `json:"prk"` // 私钥
PukPath string `json:"npk"` // 验签公钥 PukPath string `json:"npk"` // 验签公钥
@ -27,9 +26,6 @@ func transConfig(config []byte) (*Config, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if c.BaseUri == "" {
c.BaseUri = baseUri
}
return &c, nil return &c, nil
} }
@ -61,27 +57,28 @@ func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Produ
Wishing string `json:"wishing"` Wishing string `json:"wishing"`
} }
var productExtra ProductExtra var productExtra ProductExtra
if product.Extra != nil { if product.Extra == nil {
err := json.Unmarshal(product.Extra, &productExtra) return nil, fmt.Errorf("product extra is nil")
if err != nil { }
return nil, fmt.Errorf("product extra json unmarshal error: %v", err) err := json.Unmarshal(product.Extra, &productExtra)
} if err != nil {
return nil, fmt.Errorf("product extra json unmarshal error: %v", err)
} }
payeeInfo := po.PayeeInfo{ payeeInfo := po.PayeeInfo{
Identity: order.Account, Identity: order.Account,
IdentityType: "ALIPAY_USER_ID", IdentityType: "ALIPAY_USER_ID",
} }
if isValidPhoneNumber(order.Account) { if isValidPhoneNumber(order.Account) || isEmailValid(order.Account) {
type OrderExtra struct { type OrderExtra struct {
Name string `json:"name"` Name string `json:"name"`
} }
if order.Extra == nil {
return nil, fmt.Errorf("order extra is nil")
}
var orderExtra OrderExtra var orderExtra OrderExtra
if product.Extra != nil { err = json.Unmarshal(order.Extra, &orderExtra)
err := json.Unmarshal(order.Extra, &orderExtra) if err != nil {
if err != nil { return nil, fmt.Errorf("order extra json unmarshal error: %v", err)
return nil, fmt.Errorf("order extra json unmarshal error: %v", err)
}
} }
if orderExtra.Name == "" { if orderExtra.Name == "" {
return nil, fmt.Errorf("姓名当identity_type=ALIPAY_LOGON_ID时本字段必填") return nil, fmt.Errorf("姓名当identity_type=ALIPAY_LOGON_ID时本字段必填")
@ -89,9 +86,6 @@ func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Produ
payeeInfo.IdentityType = "ALIPAY_LOGON_ID" payeeInfo.IdentityType = "ALIPAY_LOGON_ID"
payeeInfo.Name = orderExtra.Name payeeInfo.Name = orderExtra.Name
} }
businessParams := &po.BusinessParams{
SubBizScene: "REDPACKET",
}
o := &po.OrderReq{ o := &po.OrderReq{
OutBizNo: order.OrderNo, OutBizNo: order.OrderNo,
@ -101,7 +95,7 @@ func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Produ
OrderTitle: productExtra.Wishing, OrderTitle: productExtra.Wishing,
Remark: productExtra.Wishing, Remark: productExtra.Wishing,
PayeeInfo: &payeeInfo, PayeeInfo: &payeeInfo,
BusinessParams: businessParams.ToString(), BusinessParams: `{"sub_biz_scene":"REDPACKET"}`,
} }
return o, nil return o, nil
} }

View File

@ -25,6 +25,13 @@ func isValidPhoneNumber(phoneNumber string) bool {
return regexp.MustCompile(phoneRegex).MatchString(phoneNumber) 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)
}
func req(config *Config, req *po.Param) (url.Values, error) { func req(config *Config, req *po.Param) (url.Values, error) {
var strToBeSigned strings.Builder var strToBeSigned strings.Builder
uv := url.Values{} uv := url.Values{}