diff --git a/go.mod b/go.mod index 1685023..021f2f9 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/agiledragon/gomonkey v2.0.2+incompatible - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.1 ) diff --git a/go.sum b/go.sum index a7af7c5..ef1e5c6 100644 --- a/go.sum +++ b/go.sum @@ -7,9 +7,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/services/partnerpayments/jsapi/api_jsapi_request_payment.go b/services/partnerpayments/jsapi/api_jsapi_request_payment.go new file mode 100644 index 0000000..c2195c2 --- /dev/null +++ b/services/partnerpayments/jsapi/api_jsapi_request_payment.go @@ -0,0 +1,97 @@ +package jsapi + +import ( + "context" + "fmt" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/utils" + "strconv" + "time" +) + +type PrepayWithRequestPaymentResponse struct { + // 预支付交易会话标识 + PrepayId *string `json:"prepay_id"` // revive:disable-line:var-naming + // 应用ID + Appid *string `json:"appid"` + // 时间戳 + TimeStamp *string `json:"timeStamp"` + // 随机字符串 + NonceStr *string `json:"nonceStr"` + // 订单详情扩展字符串 + Package *string `json:"package"` + // 签名方式 + SignType *string `json:"signType"` + // 签名 + PaySign *string `json:"paySign"` +} + +// PrepayWithRequestPayment Jsapi支付下单,并返回调起支付的请求参数 +func (a *JsapiApiService) PrepayWithRequestPayment( + ctx context.Context, req PrepayRequest, requestPaymentAppid string, +) (resp *PrepayWithRequestPaymentResponse, result *core.APIResult, err error) { + prepayResp, result, err := a.Prepay(ctx, req) + if err != nil { + return nil, result, err + } + + resp = new(PrepayWithRequestPaymentResponse) + resp.PrepayId = prepayResp.PrepayId + resp.SignType = core.String("RSA") + resp.Appid = &requestPaymentAppid + resp.TimeStamp = core.String(strconv.FormatInt(time.Now().Unix(), 10)) + nonce, err := utils.GenerateNonce() + if err != nil { + return nil, nil, fmt.Errorf("generate request for payment err:%s", err.Error()) + } + resp.NonceStr = core.String(nonce) + resp.Package = core.String("prepay_id=" + *prepayResp.PrepayId) + message := fmt.Sprintf("%s\n%s\n%s\n%s\n", *resp.Appid, *resp.TimeStamp, *resp.NonceStr, *resp.Package) + signatureResult, err := a.Client.Sign(ctx, message) + if err != nil { + return nil, nil, fmt.Errorf("generate sign for payment err:%s", err.Error()) + } + resp.PaySign = core.String(signatureResult.Signature) + return resp, result, nil +} + +func (o PrepayWithRequestPaymentResponse) String() string { + var ret string + if o.PrepayId == nil { + ret += "PrepayId:, " + } else { + ret += fmt.Sprintf("PrepayId:%v, ", *o.PrepayId) + } + if o.Appid == nil { + ret += "Appid:, " + } else { + ret += fmt.Sprintf("Appid:%v, ", *o.Appid) + } + if o.TimeStamp == nil { + ret += "TimeStamp:, " + } else { + ret += fmt.Sprintf("TimeStamp:%v, ", *o.TimeStamp) + } + if o.NonceStr == nil { + ret += "NonceStr:, " + } else { + ret += fmt.Sprintf("NonceStr:%v, ", *o.NonceStr) + } + if o.Package == nil { + ret += "Package:, " + } else { + ret += fmt.Sprintf("Package:%v, ", *o.Package) + } + if o.SignType == nil { + ret += "SignType:" + } else { + ret += fmt.Sprintf("SignType:%v", *o.SignType) + } + if o.PaySign == nil { + ret += "PaySign:" + } else { + ret += fmt.Sprintf("PaySign:%v", *o.PaySign) + } + + return fmt.Sprintf("PrepayResponse{%s}", ret) +}