125 lines
4.0 KiB
Go
125 lines
4.0 KiB
Go
package front
|
||
|
||
import (
|
||
"PaymentCenter/app/utils"
|
||
"crypto"
|
||
"crypto/rsa"
|
||
"crypto/sha256"
|
||
"crypto/x509"
|
||
"encoding/base64"
|
||
"encoding/pem"
|
||
"fmt"
|
||
)
|
||
|
||
type GetWxAuthUrlRequest struct {
|
||
PayChannelId string `json:"pay_channel_id" form:"pay_channel_id" validate:"required"`
|
||
}
|
||
|
||
type GetWxAuthRequest struct {
|
||
Code string `json:"code" form:"code" `
|
||
PayChannelId string `json:"state" form:"state"`
|
||
}
|
||
|
||
// 获取微信用户信息, 微信公众号
|
||
type GetWxAuthResponse struct {
|
||
AccessToken string `json:"access_token"` //网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
|
||
ExpiresIn int `json:"expires_in"` //access_token接口调用凭证超时时间,单位(秒)
|
||
RefreshToken string `json:"refresh_token"` //用户刷新access_token
|
||
Openid string `json:"openid"` //用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
|
||
Scope string `json:"scope"` //用户授权的作用域,使用逗号(,)分隔
|
||
IsSnapshotuser int `json:"is_snapshotuser"`
|
||
Unionid string `json:"unionid"` // 用户统一标识(针对一个微信开放平台账号下的应用,同一用户的 unionid 是唯一的),只有当scope为"snsapi_userinfo"时返回
|
||
}
|
||
|
||
// 获取微信用户信息, 微信小程序
|
||
type GetWxAuthMiniResponse struct {
|
||
Openid string `json:"openid"` // 用户唯一标识
|
||
SessionKey string `json:"session_key"` //会话密钥
|
||
Unionid string `json:"unionid"` //用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回
|
||
Errcode int `json:"errcode"`
|
||
Errmsg string `json:"errmsg"`
|
||
}
|
||
|
||
func (this *GetWxAuthMiniResponse) IsSuccess() bool {
|
||
return this.Errcode == 0
|
||
}
|
||
|
||
// jsapi支付
|
||
type WxJsApiPayRequest struct {
|
||
Code string `json:"code" form:"code" `
|
||
State string `json:"state" form:"state"` // 支付中心的订单id
|
||
ClientIp string `json:"client_ip" form:"client_ip"`
|
||
}
|
||
|
||
//// jsapi支付返回
|
||
//type WxJsApiPayResponse struct {
|
||
// Url string
|
||
// ThirdMsg string
|
||
//}
|
||
|
||
// jsapi支付返回
|
||
type WxJsApiPayResponse struct {
|
||
AppId string `json:"appId"`
|
||
TimeStamp string `json:"timeStamp"`
|
||
NonceStr string `json:"nonceStr"`
|
||
Package string `json:"package"`
|
||
SignType string `json:"signType"`
|
||
PaySign string `json:"paySign"`
|
||
ThirdMsg string `json:"third_msg"`
|
||
ReturnUrl string
|
||
}
|
||
|
||
// 微信小程序支付
|
||
type WxMiniPayRequest struct {
|
||
Code string `json:"code" form:"code" `
|
||
OrderId string `json:"order_id" form:"order_id"` // 支付中心的订单id
|
||
ClientIp string `json:"client_ip" form:"client_ip"`
|
||
}
|
||
|
||
// 微信小程序支付 返回
|
||
type WxMiniPayResponse struct {
|
||
AppId string `json:"appId"`
|
||
TimeStamp string `json:"timeStamp"`
|
||
NonceStr string `json:"nonceStr"`
|
||
Package string `json:"package"`
|
||
SignType string `json:"signType"`
|
||
PaySign string `json:"paySign"`
|
||
ThirdMsg string `json:"third_msg"`
|
||
ReturnUrl string `json:"return_url"`
|
||
}
|
||
|
||
func (this *WxMiniPayResponse) SignPaySign(PrivateKey string) (err error) {
|
||
//组装被加密的字符串
|
||
targetStr := this.AppId + "\n" + this.TimeStamp + "\n" + this.NonceStr + "\n" + this.Package + "\n"
|
||
|
||
//var keypath = "商户证书私钥地址"
|
||
//key, err := ioutil.ReadFile(keypath)
|
||
var key []byte
|
||
if PrivateKey == "" {
|
||
err = fmt.Errorf("配置的私钥不存在")
|
||
return
|
||
}
|
||
key = []byte(PrivateKey)
|
||
|
||
blocks, _ := pem.Decode(key)
|
||
if blocks == nil || blocks.Type != "PRIVATE KEY" {
|
||
utils.Log(nil, "failed to decode PRIVATE KEY")
|
||
return
|
||
|
||
}
|
||
privateKey, err := x509.ParsePKCS8PrivateKey(blocks.Bytes)
|
||
|
||
h := sha256.New()
|
||
h.Write([]byte(targetStr))
|
||
digest := h.Sum(nil)
|
||
s, _ := rsa.SignPKCS1v15(nil, privateKey.(*rsa.PrivateKey), crypto.SHA256, digest)
|
||
this.PaySign = base64.StdEncoding.EncodeToString(s)
|
||
|
||
return
|
||
}
|
||
|
||
type GetWxMiniSchemeRequest struct {
|
||
PayChannelId string `json:"pay_channel_id" form:"pay_channel_id" validate:"required"`
|
||
GenerateSchemeRequest
|
||
}
|