-
Notifications
You must be signed in to change notification settings - Fork 0
/
pay.go
97 lines (87 loc) · 2.14 KB
/
pay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package wxgamevp
import (
"fmt"
"github.com/birjemin/wxgamevp/utils"
"github.com/spf13/cast"
"log"
)
// Pay model
type Pay struct {
OpenID string
AppID string
OfferID string
Ts int
ZoneID string
Pf string
UserIP string
Amt int
BillNo string
PayItem string
AppRemark string
AccessToken string
Secret string
HTTPRequest *utils.HTTPClient
Debug bool
}
// RespPay response
type RespPay struct {
CommonError
BillNo string `json:"bill_no"`
Balance int `json:"balance"`
UsedGenAmt int `json:"used_gen_amt"`
}
// Pay pay
func (p *Pay) Pay() (*RespPay, error) {
return p.doPay(wechatDomain)
}
// doGetBalance
func (p *Pay) doPay(domain string) (*RespPay, error) {
params := p.getQueryParams()
jsonStr, err := jsonIter.Marshal(params)
if err != nil {
log.Println("[pay]Pay, json marshal failed", err, string(jsonStr))
return nil, err
}
url := fmt.Sprintf("%s%s?access_token=%s", domain, p.getPayURI(), p.AccessToken)
// log.Println("post url: ", url)
// log.Println("post str: ", string(jsonStr))
if err := p.HTTPRequest.HTTPPostJSON(url, string(jsonStr)); err != nil {
log.Println("[pay]Pay, post failed", err)
return nil, err
}
var respPay = new(RespPay)
if err = p.HTTPRequest.GetResponseJSON(respPay); err != nil {
log.Println("[pay]Pay, response json failed", err)
return respPay, err
}
return respPay, nil
}
// getQueryParams
func (p *Pay) getQueryParams() map[string]string {
params := make(map[string]string, 12)
params["openid"] = p.OpenID
params["appid"] = p.AppID
params["offer_id"] = p.OfferID
params["ts"] = cast.ToString(p.Ts)
params["zone_id"] = p.ZoneID
params["pf"] = p.Pf
params["amt"] = cast.ToString(p.Amt)
params["bill_no"] = p.BillNo
if p.UserIP != "" {
params["user_ip"] = p.UserIP
}
if p.PayItem != "" {
params["pay_item"] = p.PayItem
}
if p.AppRemark != "" {
params["app_remark"] = p.AppRemark
}
params["sig"] = GenerateSign(p.getPayURI(), "POST", "secret", p.Secret, params)
return params
}
func (p *Pay) getPayURI() string {
if p.Debug {
return getSandboxPayURI
}
return getPayURI
}