feat: support refund

This commit is contained in:
Rogee
2025-05-06 21:16:23 +08:00
parent 533c9b70af
commit 811ed3a41f
6 changed files with 101 additions and 12 deletions

View File

@@ -83,7 +83,7 @@ func (pay *PrepayData) PaySignOfJSAPI() (*wechat.JSAPIPayParams, error) {
}
func (c *Client) Refund(ctx context.Context, f func(*BodyMap)) (*wechat.RefundOrderResponse, error) {
bm := NewBodyMap(c.config)
bm := NewRefundBodyMap(c.config)
f(bm)
resp, err := c.payClient.V3Refund(ctx, bm.bm)
@@ -91,6 +91,11 @@ func (c *Client) Refund(ctx context.Context, f func(*BodyMap)) (*wechat.RefundOr
return nil, err
}
if resp.Code != wechat.Success {
log.Errorf("WePay Refund error: %s", resp.Error)
return nil, errors.New(resp.Error)
}
return resp.Response, nil
}
@@ -184,6 +189,14 @@ type BodyMap struct {
bm gopay.BodyMap
}
func NewRefundBodyMap(c *w.Config) *BodyMap {
bm := make(gopay.BodyMap)
bm.Set("notify_url", c.Pay.NotifyURL)
return &BodyMap{
bm: bm,
}
}
func NewBodyMap(c *w.Config) *BodyMap {
bm := make(gopay.BodyMap)
bm.Set("appid", c.AppID).
@@ -253,13 +266,17 @@ func (b *BodyMap) CNYRefundAmount(total, refund int64) *BodyMap {
return b.RefundAmount(total, refund, CNY)
}
type RefundGoodsInfo struct {
MerchantGoodsID string `json:"merchant_goods_id"`
GoodsName string `json:"goods_name"`
RefundQuantity int64 `json:"refund_quantity"`
RefundAmount int64 `json:"refund_amount"`
UnitPrice int64 `json:"unit_price"`
}
// RefundGoodsInfo
func (b *BodyMap) RefundGoodsInfo(name string) *BodyMap {
return b.Set("goods_detail", []map[string]any{
{
"goods_name": name,
},
})
func (b *BodyMap) RefundGoods(goods []RefundGoodsInfo) *BodyMap {
return b.Set("goods_detail", goods)
}
// Amount
@@ -275,6 +292,19 @@ func (b *BodyMap) CNYAmount(total int64) *BodyMap {
return b.Amount(total, CNY)
}
type GoodsInfo struct {
MerchantGoodsID string `json:"merchant_goods_id"`
GoodsName string `json:"goods_name"`
Quantity int64 `json:"quantity"`
UnitPrice int64 `json:"unit_price"`
}
func (b *BodyMap) Detail(goods []GoodsInfo) *BodyMap {
return b.SetBodyMap("detail", func(bm gopay.BodyMap) {
bm.Set("goods_detail", goods)
})
}
// Payer
func (b *BodyMap) Payer(spOpenId string) *BodyMap {
return b.SetBodyMap("payer", func(bm gopay.BodyMap) {