支付宝转账

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
}
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 {
return nil, fmt.Errorf("请求异常msg:" + err.Error())
}
@ -70,7 +70,7 @@ func (s *AlipayRedPackService) Query(ctx context.Context, request *proto.QueryRe
return nil, err
}
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 {
return nil, fmt.Errorf("请求异常msg:" + err.Error())
}

View File

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

View File

@ -1,7 +1,6 @@
package po
import (
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/BaseSystem/plugin/proto"
"plugins/alipay_redpack/internal/vo"
@ -13,15 +12,6 @@ type PayeeInfo struct {
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 {
OutBizNo string `validate:"required" json:"out_biz_no"`
TransAmount string `validate:"required" json:"trans_amount"`
@ -29,8 +19,8 @@ type OrderReq struct {
BizScene string `validate:"required" json:"biz_scene"`
OrderTitle string `json:"order_title"`
Remark string `json:"remark"`
PayeeInfo *PayeeInfo `json:"payee_info"`
BusinessParams string `json:"business_params"`
PayeeInfo *PayeeInfo `validate:"required" json:"payee_info"`
BusinessParams string `validate:"required" json:"business_params"`
}
type OrderResponse struct {

View File

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

View File

@ -25,6 +25,13 @@ func isValidPhoneNumber(phoneNumber string) bool {
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) {
var strToBeSigned strings.Builder
uv := url.Values{}