50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
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.WechatAppId).
|
|
Set("sp_mchid", client.conf.WechatMechID).
|
|
Set("sub_mchid", client.conf.WechatSubMechID).
|
|
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.WechatAppId, resp.Response.PrepayId)
|
|
}
|