feat: add wechat pay
This commit is contained in:
49
backend/providers/pay/wechat.go
Normal file
49
backend/providers/pay/wechat.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
)
|
||||
|
||||
// get js pay prepay id
|
||||
func (client *Client) WeChat_JSApiPayRequest(ctx context.Context, payerOpenID, orderNo, title string, price, amount int64, notifyUrl string) (*wechat.JSAPIPayParams, error) {
|
||||
expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
|
||||
// 初始化 BodyMap
|
||||
bm := make(gopay.BodyMap)
|
||||
|
||||
bm.
|
||||
Set("sp_appid", client.conf.WeChat.AppId).
|
||||
Set("sp_mchid", client.conf.WeChat.MechID).
|
||||
Set("sub_mchid", client.conf.WeChat.SubMechID).
|
||||
Set("description", title).
|
||||
Set("out_trade_no", orderNo).
|
||||
Set("time_expire", expire).
|
||||
Set("notify_url", notifyUrl).
|
||||
SetBodyMap("amount", func(bm gopay.BodyMap) {
|
||||
if amount == 0 {
|
||||
amount = 1
|
||||
}
|
||||
|
||||
bm.
|
||||
Set("total", amount).
|
||||
Set("currency", "CNY")
|
||||
}).
|
||||
SetBodyMap("payer", func(bm gopay.BodyMap) {
|
||||
bm.Set("sp_openid", payerOpenID)
|
||||
})
|
||||
|
||||
resp, err := client.WeChat.V3TransactionJsapi(ctx, bm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.Code != 0 {
|
||||
return nil, errors.New("获取预支付ID失败")
|
||||
}
|
||||
|
||||
return client.WeChat.PaySignOfJSAPI(client.conf.WeChat.AppId, resp.Response.PrepayId)
|
||||
}
|
||||
Reference in New Issue
Block a user