-
Notifications
You must be signed in to change notification settings - Fork 147
/
client.go
160 lines (122 loc) · 4.61 KB
/
client.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package order
import (
"errors"
"fmt"
response2 "github.com/ArtisanCloud/PowerLibs/v2/http/response"
"github.com/ArtisanCloud/PowerLibs/v2/object"
payment "github.com/ArtisanCloud/PowerWeChat/v2/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v2/src/payment/order/request"
"github.com/ArtisanCloud/PowerWeChat/v2/src/payment/order/response"
"net/http"
)
type Client struct {
*payment.BaseClient
}
func NewClient(app *payment.ApplicationPaymentInterface) (*Client, error) {
baseClient, err := payment.NewBaseClient(app)
if err != nil {
return nil, err
}
return &Client{
baseClient,
}, nil
}
// JSAPI pay transaction. 小程序支付
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
func (comp *Client) JSAPITransaction(params *request.RequestJSAPIPrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/pay/transactions/jsapi"
_, err := comp.PayTransaction(endpoint, params, result)
return result, err
}
// App pay transaction.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml
func (comp *Client) TransactionApp(params *request.RequestAppPrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/pay/transactions/app"
_, err := comp.PayTransaction(endpoint, params, result)
return result, err
}
// H5 pay transaction.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml
func (comp *Client) TransactionH5(params *request.RequestH5Prepay) (*response.ResponseH5URL, error) {
result := &response.ResponseH5URL{}
endpoint := "/v3/pay/transactions/h5"
_, err := comp.PayTransaction(endpoint, params, result)
return result, err
}
// Native pay transaction.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
func (comp *Client) TransactionNative(params *request.RequestNativePrepay) (*response.ResponseCodeURL, error) {
result := &response.ResponseCodeURL{}
endpoint := "/v3/pay/transactions/native"
_, err := comp.PayTransaction(endpoint, params, result)
return result, err
}
// 合单APP下单API
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_1.shtml
func (comp *Client) TransactionAppCombine(params *request.RequestCombinePrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/combine-transactions/app"
_, err := comp.PayTransaction(endpoint, params, result)
return result, err
}
func (comp *Client) PayTransaction(entryPoint string, params request.Prepay, result interface{}) (interface{}, error) {
config := (*comp.App).GetConfig()
if params.GetAppID() == "" {
appID := config.GetString("app_id", "")
params.SetAppID(appID)
}
if params.GetNotifyUrl() == "" {
url := config.GetString("notify_url", "")
params.SetNotifyUrl(url)
}
mchID := config.GetString("mch_id", "")
params.SetMchID(mchID)
//options, err := object.StructToHashMapWithTag(params,"json")
options, err := object.StructToHashMap(params)
if err != nil {
return nil, err
}
transactionEndpoint := comp.Wrap(entryPoint)
rs, err := comp.Request(transactionEndpoint, nil, "POST", options, false, nil, result)
return rs, err
}
func (comp *Client) QueryByTransactionId(number string) (*response.ResponseOrder, error) {
endpoint := fmt.Sprintf("/v3/pay/transactions/id/%s", number)
return comp.Query(&object.HashMap{
"endpoint": endpoint,
})
}
func (comp *Client) QueryByOutTradeNumber(transactionID string) (*response.ResponseOrder, error) {
endpoint := fmt.Sprintf("/v3/pay/transactions/out-trade-no/%s", transactionID)
return comp.Query(&object.HashMap{
"endpoint": endpoint,
})
}
func (comp *Client) Query(params *object.HashMap) (*response.ResponseOrder, error) {
result := &response.ResponseOrder{}
config := (*comp.App).GetConfig()
if (*params)["endpoint"] == nil || (*params)["endpoint"].(string) == "" {
return nil, errors.New("no query endpoint! ")
}
endpoint := (*params)["endpoint"].(string)
endpoint = comp.Wrap(endpoint)
_, err := comp.Request(endpoint, &object.StringMap{
"mchid": config.GetString("mch_id", ""),
}, "GET", nil, false, nil, result)
return result, err
}
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml
func (comp *Client) Close(tradeNo string) (*http.Response, error) {
config := (*comp.App).GetConfig()
endpoint := comp.Wrap(fmt.Sprintf("/v3/pay/transactions/out-trade-no/%s/close", tradeNo))
rs, err := comp.PlainRequest(endpoint, nil, "POST", &object.HashMap{
"mchid": config.GetString("mch_id", ""),
}, false, nil, nil)
var httpResponse *http.Response = nil
if err != nil {
httpResponse = rs.(*response2.HttpResponse).Response
}
return httpResponse, err
}