package weChat import ( "context" "errors" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/services/payments/app" "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi" ) /** JSAPI下单 APP下单 */ // JsapiRequest 请求数据-JSAPI下单 type JsapiRequest struct { AppId string `json:"appid" comment:"公众号ID"` MchId string `json:"mchid" comment:"直连商户号"` Description string `json:"description" comment:"商品描述"` OutTradeNo string `json:"out_trade_no" comment:"商户订单号"` NotifyUrl string `json:"notify_url" comment:"回调地址"` Amount JsapiRequestAmountRequest `json:"amount" comment:"订单金额"` Payer JsapiRequestPayerRequest `json:"payer" comment:"支付者"` } // JsapiRequestAmountRequest 订单金额信息 type JsapiRequestAmountRequest struct { Total int64 `json:"total" comment:"订单总金额"` Currency string `json:"currency" comment:"货币类型"` } // JsapiRequestPayerRequest 支付者信息 type JsapiRequestPayerRequest struct { OpenId string `json:"openid" comment:"openid"` } // AppRequest 请求数据-APP下单 type AppRequest struct { AppId string `json:"appid" comment:"公众号ID"` MchId string `json:"mchid" comment:"直连商户号"` Description string `json:"description" comment:"商品描述"` OutTradeNo string `json:"out_trade_no" comment:"商户订单号"` NotifyUrl string `json:"notify_url" comment:"回调地址"` Amount AppRequestAmountRequest `json:"amount" comment:"订单金额"` } // AppRequestAmountRequest 订单金额信息 type AppRequestAmountRequest struct { Total int64 `json:"total" comment:"订单总金额"` Currency string `json:"currency" comment:"货币类型"` } // GetJsapiPrepay JSAPI下单-获取jsapi预支付交易会话 func (r JsapiRequest) GetJsapiPrepay() (prepay *jsapi.PrepayWithRequestPaymentResponse, err error) { client, err := createClient() if err != nil { return nil, err } svc := jsapi.JsapiApiService{Client: client} // 得到prepay_id,以及调起支付所需的参数和签名 PrepayRequest := jsapi.PrepayRequest{ Appid: core.String(r.AppId), Mchid: core.String(r.MchId), Description: core.String(r.Description), OutTradeNo: core.String(r.OutTradeNo), NotifyUrl: core.String(r.NotifyUrl), Amount: &jsapi.Amount{ Total: core.Int64(r.Amount.Total), Currency: core.String("CNY"), }, Payer: &jsapi.Payer{ Openid: core.String(r.Payer.OpenId), }, } resp, result, err := svc.PrepayWithRequestPayment(context.Background(), PrepayRequest, ) if err != nil { return nil, err } if result.Response.StatusCode != 200 { return nil, errors.New("发起支付失败") } if resp.PrepayId == nil { return nil, errors.New("发起支付失败") } if err != nil { return nil, err } return resp, nil } // GetAppPrepay APP下单-获取app预支付交易会话 func (r AppRequest) GetAppPrepay() (prepay *app.PrepayWithRequestPaymentResponse, err error) { client, err := createClient() if err != nil { return nil, err } svc := app.AppApiService{Client: client} // 得到prepay_id,以及调起支付所需的参数和签名 resp, result, err := svc.PrepayWithRequestPayment(context.Background(), app.PrepayRequest{ Appid: core.String(r.AppId), Mchid: core.String(r.MchId), Description: core.String(r.Description), OutTradeNo: core.String(r.OutTradeNo), NotifyUrl: core.String(r.NotifyUrl), Amount: &app.Amount{ Total: core.Int64(r.Amount.Total), Currency: core.String("CNY"), }, }, ) if err != nil { return nil, err } if result.Response.StatusCode != 200 { return nil, errors.New("发起支付失败") } if resp.PrepayId == nil { return nil, errors.New("发起支付失败") } if err != nil { return nil, err } return resp, nil }