Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Jan 20, 2019
1 parent 55b3d14 commit 1f6ffef
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 345 deletions.
112 changes: 66 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* NATIVE - Native支付
* APP - app支付
* MWEB - H5支付
* 查询订单(开发中)
* 查询订单
* 关闭订单(开发中)
* 申请退款(开发中)
* 查询退款(开发中)
Expand All @@ -34,62 +34,82 @@ $ go get github.com/iGoogle-ink/gopay

未完成

## 微信统一下单 example
## 微信

<font color='#0088ff'>注意:具体参数根据请求的不同而不同,请参考微信官方文档的参数说明!</font>

参考文档:[微信支付文档](https://pay.weixin.qq.com/wiki/doc/api/index.html)

* 初始化客户端
* 参数:AppId:应用ID
* 参数:mchID:商户ID
* 参数:secretKey:Key值
* 参数:isProd:是否正式环境

### 统一下单
```go
//正式环境
//初始化微信客户端
// appId:应用ID
// mchID:商户ID
// secretKey:Key值
// isProd:是否是正式环境
client := gopay.NewWeChatClient("wxd678efh567hg6787", "1230000109", "192006250b4c09247ec02edce69f6a2d", true)

//沙箱环境
client := gopay.NewWeChatClient("wxd678efh567hg6787", "1230000109", "192006250b4c09247ec02edce69f6a2d", false)
//初始化参数Map
body := make(gopay.BodyMap)
body.Set("nonce_str", gopay.GetRandomString(32))
body.Set("body", "测试支付")
number := gopay.GetRandomString(32)
log.Println("Number:", number)
body.Set("out_trade_no", number)
body.Set("total_fee", 1)
body.Set("spbill_create_ip", "127.0.0.1") //终端IP
body.Set("notify_url", "http://www.igoogle.ink")
body.Set("trade_type", gopay.TradeType_JsApi)
body.Set("device_info", "WEB")
body.Set("sign_type", gopay.SignType_MD5)
//body.Set("scene_info", `{"h5_info": {"type":"Wap","wap_url": "http://www.igoogle.ink","wap_name": "测试支付"}}`)
body.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu6")

//发起下单请求
wxRsp, err := client.UnifiedOrder(body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("ReturnCode:", wxRsp.ReturnCode)
fmt.Println("ReturnMsg:", wxRsp.ReturnMsg)
fmt.Println("Appid:", wxRsp.Appid)
fmt.Println("MchId:", wxRsp.MchId)
fmt.Println("DeviceInfo:", wxRsp.DeviceInfo)
fmt.Println("NonceStr:", wxRsp.NonceStr)
fmt.Println("Sign:", wxRsp.Sign)
fmt.Println("ResultCode:", wxRsp.ResultCode)
fmt.Println("ErrCode:", wxRsp.ErrCode)
fmt.Println("ErrCodeDes:", wxRsp.ErrCodeDes)
fmt.Println("PrepayId:", wxRsp.PrepayId)
fmt.Println("TradeType:", wxRsp.TradeType)
fmt.Println("CodeUrl:", wxRsp.CodeUrl)
fmt.Println("MwebUrl:", wxRsp.MwebUrl)
```

* 初始化统一下单参数
> 以下参数设置皆为必选参数,如需其他参数,请参考API文档。
>
> 参考文档:[微信支付文档](https://pay.weixin.qq.com/wiki/doc/api/index.html)
### 查询订单
```go
params := new(gopay.WeChatPayParams)
params.NonceStr = "dyUNIkNS29hvDUC1CmoF0alSdfCQGg9I"
params.Body = "支付测试"
params.OutTradeNo = "GYsadfjk4dhg3fkh3ffgnlsdkf"
params.TotalFee = 10 //单位为分,如沙箱环境,则默认为101
params.SpbillCreateIp = "127.0.0.1"
params.NotifyUrl = "http://www.igoogle.ink"
params.TradeType = gopay.WX_PayType_JsApi
params.DeviceInfo = "WEB"
params.SignType = gopay.WX_SignType_HMAC_SHA256 //如不设置此参数,默认为MD5,如沙箱环境,则默认为MD5
params.Openid = "o0Df70H2Q0fY8JXh1aFPIRyOBgu8" //JSAPI 方式时,此参数必填
```
//初始化微信客户端
// appId:应用ID
// mchID:商户ID
// secretKey:Key值
// isProd:是否是正式环境
client := gopay.NewWeChatClient("wxd678efh567hg6787", "1230000109", "192006250b4c09247ec02edce69f6a2d", true)

* 发起统一下单请求
* 参数:param:统一下单请求参数
> 请求成功后,获取下单结果
```go
wxRsp, err := client.UnifiedOrder(params)
//初始化参数结构体
body := make(gopay.BodyMap)
body.Set("out_trade_no", "CC68aTofMIwVKkVR5UruoBLFFXTAqBfv")
body.Set("nonce_str", gopay.GetRandomString(32))
body.Set("sign_type", gopay.SignType_MD5)

//请求订单查询
wxRsp, err := client.QueryOrder(body)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("ReturnCode:", wxRsp.ReturnCode)
fmt.Println("ReturnMsg:", wxRsp.ReturnMsg)
fmt.Println("Appid:", wxRsp.Appid)
fmt.Println("MchId:", wxRsp.MchId)
fmt.Println("DeviceInfo:", wxRsp.DeviceInfo)
fmt.Println("NonceStr:", wxRsp.NonceStr)
fmt.Println("Sign:", wxRsp.Sign)
fmt.Println("ResultCode:", wxRsp.ResultCode)
fmt.Println("ErrCode:", wxRsp.ErrCode)
fmt.Println("ErrCodeDes:", wxRsp.ErrCodeDes)
fmt.Println("PrepayId:", wxRsp.PrepayId)
fmt.Println("TradeType:", wxRsp.TradeType)
fmt.Println("CodeUrl:", wxRsp.CodeUrl)
fmt.Println("MwebUrl:", wxRsp.MwebUrl)
return
}
fmt.Println("Response:", wxRsp)
```

## 支付宝支付 example
Expand Down
File renamed without changes.
File renamed without changes.
28 changes: 14 additions & 14 deletions constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ const (
wx_base_url = "https://api.mch.weixin.qq.com/"
wx_sanbox_base_url = "https://api.mch.weixin.qq.com/sandboxnew/"

wxURL_unifiedOrder = wx_base_url + "pay/unifiedorder"
wxURL_orderquery = wx_base_url + "pay/orderquery"
wxURL_closeorder = wx_base_url + "pay/closeorder"
wxURL_UnifiedOrder = wx_base_url + "pay/unifiedorder"
wxURL_OrderQuery = wx_base_url + "pay/orderquery"
wxURL_CloseOrder = wx_base_url + "pay/closeorder"

wxURL_sanbox_getsignkey = wx_sanbox_base_url + "pay/getsignkey"
wxURL_sanbox_unifiedOrder = wx_sanbox_base_url + "pay/unifiedorder"
wxURL_sanbox_orderquery = wx_sanbox_base_url + "pay/orderquery"
wxURL_sanbox_closeorder = wx_sanbox_base_url + "pay/closeorder"
wxURL_SanBox_GetSignKey = wx_sanbox_base_url + "pay/getsignkey"
wxURL_SanBox_UnifiedOrder = wx_sanbox_base_url + "pay/unifiedorder"
wxURL_SanBox_OrderQuery = wx_sanbox_base_url + "pay/orderquery"
wxURL_SanBox_CloseOrder = wx_sanbox_base_url + "pay/closeorder"

//支付类型
WX_PayType_Mini = "JSAPI"
WX_PayType_JsApi = "JSAPI"
WX_PayType_App = "APP"
WX_PayType_H5 = "MWEB"
WX_PayType_Native = "NATIVE"
TradeType_Mini = "JSAPI"
TradeType_JsApi = "JSAPI"
TradeType_App = "APP"
TradeType_H5 = "MWEB"
TradeType_Native = "NATIVE"

//签名方式
WX_SignType_MD5 = "MD5"
WX_SignType_HMAC_SHA256 = "HMAC-SHA256"
SignType_MD5 = "MD5"
SignType_HMAC_SHA256 = "HMAC-SHA256"

//Debug数据
secretKey = "GFDS8j98rewnmgl45wHTt980jg543wmg"
Expand Down
88 changes: 66 additions & 22 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ package gopay

import (
"bytes"
"encoding/xml"
"errors"
"github.com/parnurzeal/gorequest"
"math/rand"
"sort"
"strconv"
"time"
)

type requestBody map[string]string
type BodyMap map[string]interface{}

//设置参数
func (w requestBody) Set(key string, value string) {
w[key] = value
func (bm BodyMap) Set(key string, value interface{}) {
bm[key] = value
}

//获取参数
func (w requestBody) Get(key string) string {
if w == nil {
func (bm BodyMap) Get(key string) string {
if bm == nil {
return ""
}
ws := w[key]
return ws
v := bm[key]
value, ok := v.(int)
if ok {
value := strconv.Itoa(value)
return value
}
return v.(string)
}

//删除参数
func (w requestBody) Remove(key string) {
delete(w, key)
func (bm BodyMap) Remove(key string) {
delete(bm, key)
}

//获取随机字符串
Expand All @@ -40,20 +50,54 @@ func GetRandomString(length int) string {
return string(result)
}

func generateXml(w requestBody) (reqXml string) {
//获取根据Key排序后的请求参数字符串
func sortSignParams(secretKey string, body BodyMap) string {
keyList := make([]string, 0)
for k := range body {
keyList = append(keyList, k)
}
sort.Strings(keyList)
buffer := new(bytes.Buffer)
buffer.WriteString("<xml>")

for k, v := range w {
buffer.WriteString("<")
for _, k := range keyList {
buffer.WriteString(k)
buffer.WriteString("><![CDATA[")
buffer.WriteString(v)
buffer.WriteString("]]></")
buffer.WriteString(k)
buffer.WriteString(">")
buffer.WriteString("=")
value, ok := body[k].(int)
if ok {
value := strconv.Itoa(value)
buffer.WriteString(value)
} else {
buffer.WriteString(body[k].(string))
}
buffer.WriteString("&")
}
buffer.WriteString("key=")
buffer.WriteString(secretKey)
return buffer.String()
}

//从微信提供的接口获取:SandboxSignkey
func getSanBoxSignKey(mchId, nonceStr, sign string) (key string, err error) {
reqs := make(BodyMap)
reqs.Set("mch_id", mchId)
reqs.Set("nonce_str", nonceStr)
reqs.Set("sign", sign)

reqXml := generateXml(reqs)
//fmt.Println("req:::", reqXml)
_, byteList, errorList := gorequest.New().
Post(wxURL_SanBox_GetSignKey).
Type("xml").
SendString(reqXml).EndBytes()
if len(errorList) > 0 {
return "", errorList[0]
}
keyResponse := new(getSignKeyResponse)
err = xml.Unmarshal(byteList, keyResponse)
if err != nil {
return "", err
}
if keyResponse.ReturnCode == "FAIL" {
return "", errors.New(keyResponse.Retmsg)
}
buffer.WriteString("</xml>")
reqXml = buffer.String()
return
return keyResponse.SandboxSignkey, nil
}
Loading

0 comments on commit 1f6ffef

Please sign in to comment.