feat: update prepay id

This commit is contained in:
yanghao05
2025-04-14 20:48:50 +08:00
parent e3cee91758
commit 69e3dd1070
5 changed files with 122 additions and 10 deletions

View File

@@ -53,7 +53,15 @@ func (c *Client) GetClient() *wechat.ClientV3 {
return c.payClient
}
func (c *Client) V3TransactionJsapi(ctx context.Context, bm *BodyMap) (*wechat.PrepayRsp, error) {
type PrepayData struct {
AppID string `json:"app_id"`
PrepayID string `json:"prepay_id"`
}
func (c *Client) V3TransactionJsapi(ctx context.Context, f func(*BodyMap)) (*PrepayData, error) {
bm := NewBodyMap(c.config)
f(bm)
resp, err := c.payClient.V3TransactionJsapi(ctx, bm.bm)
if err != nil {
return nil, err
@@ -63,7 +71,10 @@ func (c *Client) V3TransactionJsapi(ctx context.Context, bm *BodyMap) (*wechat.P
return nil, errors.New(resp.Error)
}
return resp, nil
return &PrepayData{
AppID: c.config.AppID,
PrepayID: resp.Response.PrepayId,
}, nil
}
func (c *Client) BodyMap() *BodyMap { return NewBodyMap(c.config) }
@@ -74,10 +85,10 @@ type BodyMap struct {
func NewBodyMap(c *w.Config) *BodyMap {
bm := make(gopay.BodyMap)
bm.Set("sp_appid", c.AppID).
Set("sp_mchid", c.Pay.MchID).
bm.Set("appid", c.AppID).
Set("mchid", c.Pay.MchID).
Set("notify_url", c.Pay.NotifyURL).
Set("amount", func(bm gopay.BodyMap) {
SetBodyMap("amount", func(bm gopay.BodyMap) {
bm.Set("total", 1).
Set("currency", "CNY")
})
@@ -121,7 +132,7 @@ func (b *BodyMap) Amount(total int, currency string) *BodyMap {
// Payer
func (b *BodyMap) Payer(spOpenId string) *BodyMap {
return b.SetBodyMap("payer", func(bm gopay.BodyMap) {
bm.Set("sp_openid", spOpenId)
bm.Set("openid", spOpenId)
})
}

View File

@@ -0,0 +1,52 @@
package wepay
import (
"context"
"fmt"
"testing"
"time"
"quyun/app/service/testx"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite"
"go.ipao.vip/atom/contracts"
"go.uber.org/dig"
)
type WePayInjectParams struct {
dig.In
Initials []contracts.Initial `group:"initials"`
Client *Client
}
type WePayTestSuite struct {
suite.Suite
WePayInjectParams
}
func Test_WePay(t *testing.T) {
providers := testx.Default().With(Provide)
testx.Serve(providers, t, func(params WePayInjectParams) {
suite.Run(t, &WePayTestSuite{WePayInjectParams: params})
})
}
func (s *WePayTestSuite) Test_PrePay() {
Convey("get prepay", s.T(), func() {
Convey("prepay", func() {
resp, err := s.Client.V3TransactionJsapi(context.Background(), func(bm *BodyMap) {
bm.
OutTradeNo(fmt.Sprintf("test_trade_no_%d", time.Now().Unix())).
Description("Test transaction").
Payer("o5Bzk644x3LOMJsKSZRlqWin74IU")
})
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
s.T().Logf("prepay response: %+v", resp)
})
})
}