Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

服务商小程序支付增加PrepayWithRequestPayment接口 #164

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
97 changes: 97 additions & 0 deletions services/partnerpayments/jsapi/api_jsapi_request_payment.go
Original file line number Diff line number Diff line change
@@ -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:<nil>, "
} else {
ret += fmt.Sprintf("PrepayId:%v, ", *o.PrepayId)
}
if o.Appid == nil {
ret += "Appid:<nil>, "
} else {
ret += fmt.Sprintf("Appid:%v, ", *o.Appid)
}
if o.TimeStamp == nil {
ret += "TimeStamp:<nil>, "
} else {
ret += fmt.Sprintf("TimeStamp:%v, ", *o.TimeStamp)
}
if o.NonceStr == nil {
ret += "NonceStr:<nil>, "
} else {
ret += fmt.Sprintf("NonceStr:%v, ", *o.NonceStr)
}
if o.Package == nil {
ret += "Package:<nil>, "
} else {
ret += fmt.Sprintf("Package:%v, ", *o.Package)
}
if o.SignType == nil {
ret += "SignType:<nil>"
} else {
ret += fmt.Sprintf("SignType:%v", *o.SignType)
}
if o.PaySign == nil {
ret += "PaySign:<nil>"
} else {
ret += fmt.Sprintf("PaySign:%v", *o.PaySign)
}

return fmt.Sprintf("PrepayResponse{%s}", ret)
}