支付宝转账

This commit is contained in:
李子铭 2024-08-23 16:42:48 +08:00
parent 0c7aeececa
commit 1c0a06f612
4 changed files with 39 additions and 13 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).BodyForm(uv).ToJSON(&response).Fetch(ctx) err = requests.URL(c.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())
} }

View File

@ -23,13 +23,23 @@ func config() []byte {
return marshal return marshal
} }
func TestConfig(t *testing.T) {
t.Run("TestConfig", func(t *testing.T) {
c := config()
fmt.Printf("%+s\n", string(c))
assert.NotEmpty(t, c)
})
}
func TestOrder(t *testing.T) { func TestOrder(t *testing.T) {
request := &proto.OrderRequest{ request := &proto.OrderRequest{
Config: config(), Config: config(),
Order: &proto.OrderRequest_Order{ Order: &proto.OrderRequest_Order{
OrderNo: "240403164049635931", OrderNo: "lsxd202306071545141533",
Account: "18666173766", Account: "18666173766",
Extra: []byte(``), Quantity: 1,
Amount: 0.01,
Extra: []byte(`{"name":"李子铭"}`),
}, },
Product: &proto.OrderRequest_Product{ Product: &proto.OrderRequest_Product{
ProductNo: "", ProductNo: "",
@ -43,7 +53,7 @@ func TestOrder(t *testing.T) {
t.Errorf("Order() error = %v", err) t.Errorf("Order() error = %v", err)
return return
} }
fmt.Printf("%+v", got) fmt.Printf("%s", got.String())
assert.Equal(t, int(proto.Status_ING), int(got.Result.Status)) assert.Equal(t, int(proto.Status_ING), int(got.Result.Status))
}) })
} }

View File

@ -10,6 +10,7 @@ import (
type PayeeInfo struct { type PayeeInfo struct {
Identity string `validate:"required" json:"identity"` Identity string `validate:"required" json:"identity"`
IdentityType string `validate:"required" json:"identity_type"` IdentityType string `validate:"required" json:"identity_type"`
Name string `json:"name"`
} }
type BusinessParams struct { type BusinessParams struct {

View File

@ -57,35 +57,50 @@ func (c *Config) paramReq(req po.Req, method string) (*po.Param, error) {
} }
func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (*po.OrderReq, error) { func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (*po.OrderReq, error) {
type Extra struct { type ProductExtra struct {
Wishing string `json:"wishing"` Wishing string `json:"wishing"`
} }
var extra Extra var productExtra ProductExtra
if product.Extra != nil { if product.Extra != nil {
err := json.Unmarshal(product.Extra, &extra) err := json.Unmarshal(product.Extra, &productExtra)
if err != nil { if err != nil {
return nil, fmt.Errorf("product extra json unmarshal error: %v", err) 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) {
type OrderExtra struct {
Name string `json:"name"`
}
var orderExtra OrderExtra
if product.Extra != nil {
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.IdentityType = "ALIPAY_LOGON_ID"
payeeInfo.Name = orderExtra.Name
} }
businessParams := &po.BusinessParams{ businessParams := &po.BusinessParams{
SubBizScene: "REDPACKET", SubBizScene: "REDPACKET",
} }
o := &po.OrderReq{ o := &po.OrderReq{
OutBizNo: order.OrderNo, OutBizNo: order.OrderNo,
TransAmount: fmt.Sprintf("%.2f", order.Amount), TransAmount: fmt.Sprintf("%.2f", order.Amount),
ProductCode: product.ProductNo, ProductCode: "STD_RED_PACKET",
BizScene: "DIRECT_TRANSFER", BizScene: "DIRECT_TRANSFER",
OrderTitle: extra.Wishing, OrderTitle: productExtra.Wishing,
Remark: extra.Wishing, Remark: productExtra.Wishing,
PayeeInfo: payeeInfo, PayeeInfo: &payeeInfo,
BusinessParams: businessParams.ToString(), BusinessParams: businessParams.ToString(),
} }
return o, nil return o, nil