-
Notifications
You must be signed in to change notification settings - Fork 155
/
client.go
158 lines (120 loc) · 4.85 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
package order
import (
"context"
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
payment "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/order/request"
"github.com/ArtisanCloud/PowerWeChat/v3/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(ctx context.Context, params *request.RequestJSAPIPrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/pay/transactions/jsapi"
_, err := comp.PayTransaction(ctx, 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(ctx context.Context, params *request.RequestAppPrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/pay/transactions/app"
_, err := comp.PayTransaction(ctx, 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(ctx context.Context, params *request.RequestH5Prepay) (*response.ResponseH5URL, error) {
result := &response.ResponseH5URL{}
endpoint := "/v3/pay/transactions/h5"
_, err := comp.PayTransaction(ctx, 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(ctx context.Context, params *request.RequestNativePrepay) (*response.ResponseCodeURL, error) {
result := &response.ResponseCodeURL{}
endpoint := "/v3/pay/transactions/native"
_, err := comp.PayTransaction(ctx, 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(ctx context.Context, params *request.RequestCombinePrepay) (*response.ResponseUnitfy, error) {
result := &response.ResponseUnitfy{}
endpoint := "/v3/combine-transactions/app"
_, err := comp.PayTransaction(ctx, endpoint, params, result)
return result, err
}
func (comp *Client) PayTransaction(ctx context.Context, 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(ctx, transactionEndpoint, nil, http.MethodPost, options, false, nil, result)
return rs, err
}
func (comp *Client) QueryByTransactionId(ctx context.Context, number string) (*response.ResponseOrder, error) {
endpoint := fmt.Sprintf("/v3/pay/transactions/id/%s", number)
return comp.Query(ctx, &object.HashMap{
"endpoint": endpoint,
})
}
func (comp *Client) QueryByOutTradeNumber(ctx context.Context, transactionID string) (*response.ResponseOrder, error) {
endpoint := fmt.Sprintf("/v3/pay/transactions/out-trade-no/%s", transactionID)
return comp.Query(ctx, &object.HashMap{
"endpoint": endpoint,
})
}
func (comp *Client) Query(ctx context.Context, 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(ctx, endpoint, &object.StringMap{
"mchid": config.GetString("mch_id", ""),
}, http.MethodGet, &object.HashMap{}, false, nil, result)
return result, err
}
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml
func (comp *Client) Close(ctx context.Context, tradeNo string) (*response2.ResponsePayment, error) {
result := &response2.ResponsePayment{}
config := (*comp.App).GetConfig()
endpoint := comp.Wrap(fmt.Sprintf("/v3/pay/transactions/out-trade-no/%s/close", tradeNo))
_, err := comp.PlainRequest(ctx, endpoint, nil, http.MethodPost, &object.HashMap{
"mchid": config.GetString("mch_id", ""),
}, false, nil, result)
return result, err
}