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)
})
}