|
@@ -2,7 +2,6 @@ package gopay
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/xml"
|
|
"encoding/xml"
|
|
|
- "errors"
|
|
|
|
|
"github.com/parnurzeal/gorequest"
|
|
"github.com/parnurzeal/gorequest"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -10,46 +9,53 @@ type weChatClient struct {
|
|
|
AppId string
|
|
AppId string
|
|
|
MchId string
|
|
MchId string
|
|
|
secretKey string
|
|
secretKey string
|
|
|
- Params *WeChatPayParams
|
|
|
|
|
isProd bool
|
|
isProd bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//初始化微信客户端
|
|
//初始化微信客户端
|
|
|
// appId:应用ID
|
|
// appId:应用ID
|
|
|
// mchID:商户ID
|
|
// mchID:商户ID
|
|
|
|
|
+// secretKey:Key值
|
|
|
// isProd:是否是正式环境
|
|
// isProd:是否是正式环境
|
|
|
-// secretKey:key,(当isProd为true时,此参数必传;false时,此参数为空)
|
|
|
|
|
-func NewWeChatClient(appId, mchId string, isProd bool, secretKey ...string) *weChatClient {
|
|
|
|
|
|
|
+func NewWeChatClient(appId, mchId, secretKey string, isProd bool) *weChatClient {
|
|
|
client := new(weChatClient)
|
|
client := new(weChatClient)
|
|
|
client.AppId = appId
|
|
client.AppId = appId
|
|
|
client.MchId = mchId
|
|
client.MchId = mchId
|
|
|
|
|
+ client.secretKey = secretKey
|
|
|
client.isProd = isProd
|
|
client.isProd = isProd
|
|
|
- if isProd && len(secretKey) > 0 {
|
|
|
|
|
- client.secretKey = secretKey[0]
|
|
|
|
|
- }
|
|
|
|
|
return client
|
|
return client
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//统一下单
|
|
//统一下单
|
|
|
func (this weChatClient) UnifiedOrder(param *WeChatPayParams) (wxRsp *weChatPayResponse, err error) {
|
|
func (this weChatClient) UnifiedOrder(param *WeChatPayParams) (wxRsp *weChatPayResponse, err error) {
|
|
|
- this.Params = param
|
|
|
|
|
- //fmt.Println("reqs:", this.ReqParam)
|
|
|
|
|
- var sign string
|
|
|
|
|
var reqs requestBody
|
|
var reqs requestBody
|
|
|
- if this.isProd {
|
|
|
|
|
- sign, reqs = getSignAndRequestBody(this.AppId, this.MchId, this.secretKey, this.Params)
|
|
|
|
|
|
|
+ var sign string
|
|
|
|
|
+ //生成下单请求参数
|
|
|
|
|
+ if !this.isProd {
|
|
|
|
|
+ //沙箱环境
|
|
|
|
|
+ param.TotalFee = 101
|
|
|
|
|
+ param.SignType = WX_SignType_MD5
|
|
|
|
|
+ reqs = param.getRequestBody(this.AppId, this.MchId, param)
|
|
|
|
|
+ key, err := param.getSanBoxSignKey(this.MchId, param.NonceStr, this.secretKey, param.SignType)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ sign = getSign(key, param.SignType, reqs)
|
|
|
} else {
|
|
} else {
|
|
|
- return nil, errors.New("暂不支持沙箱测试")
|
|
|
|
|
- //getSanBoxSignKey(this.Appid, this.MchId, this.Params)
|
|
|
|
|
|
|
+ reqs = param.getRequestBody(this.AppId, this.MchId, param)
|
|
|
|
|
+ //计算Sign
|
|
|
|
|
+ sign = getSign(this.secretKey, param.SignType, reqs)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
reqs.Set("sign", sign)
|
|
reqs.Set("sign", sign)
|
|
|
|
|
|
|
|
reqXML := generateXml(reqs)
|
|
reqXML := generateXml(reqs)
|
|
|
|
|
+ //fmt.Println("req:::", reqXML)
|
|
|
agent := gorequest.New()
|
|
agent := gorequest.New()
|
|
|
if this.isProd {
|
|
if this.isProd {
|
|
|
- agent.Post(wX_PayUrl)
|
|
|
|
|
|
|
+ agent.Post(wxURL_unifiedOrder)
|
|
|
} else {
|
|
} else {
|
|
|
- agent.Post(wX_PayUrl_SanBox)
|
|
|
|
|
|
|
+ agent.Post(wxURL_sanbox_unifiedOrder)
|
|
|
}
|
|
}
|
|
|
agent.Type("xml")
|
|
agent.Type("xml")
|
|
|
agent.SendString(reqXML)
|
|
agent.SendString(reqXML)
|