-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
服务商小程序支付增加PrepayWithRequestPayment接口 (#164)
- Loading branch information
1 parent
af7cd03
commit 0a19d46
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
services/partnerpayments/jsapi/api_jsapi_request_payment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |