-
Notifications
You must be signed in to change notification settings - Fork 158
/
client.go
197 lines (143 loc) · 6.07 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package profitSharing
import (
"context"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
payment "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/profitSharing/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/profitSharing/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
}
// Share Orders.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_1.shtml
func (comp *Client) Share(ctx context.Context, param *request.RequestShare) (*response.ResponseProfitSharingOrder, error) {
result := &response.ResponseProfitSharingOrder{}
if param.AppID == "" {
config := (*comp.App).GetConfig()
param.AppID = config.GetString("app_id", "")
}
//options, err := object.StructToHashMapWithTag(param,"json")
options, err := object.StructToHashMap(param)
endpoint := comp.Wrap("/v3/profitsharing/orders")
_, err = comp.Request(ctx, endpoint, nil, http.MethodPost, options, false, nil, result)
return result, err
}
// Query Profit Sharing Result.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_2.shtml
func (comp *Client) Query(ctx context.Context, transactionID string, outOrderNO string) (*response.ResponseProfitSharingOrder, error) {
result := &response.ResponseProfitSharingOrder{}
params := &object.StringMap{
"transaction_id": transactionID,
}
endpoint := comp.Wrap(fmt.Sprintf("/v3/profitsharing/orders/%s", outOrderNO))
_, err := comp.Request(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, false, nil, result)
return result, err
}
// Share Return.
// https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_7&index=8
func (comp *Client) Return(ctx context.Context, data *request.RequestShareReturn) (*response.ResponseProfitSharingReturn, error) {
result := &response.ResponseProfitSharingReturn{}
config := (*comp.App).GetConfig()
params, err := object.StructToHashMapWithXML(data)
if err != nil {
return nil, err
}
base := &object.HashMap{
"return_amount": 1,
"appid": config.GetString("app_id", ""),
"mch_id": config.GetString("mch_id", ""),
}
params = object.MergeHashMap(params, base)
endpoint := comp.Wrap("/secapi/pay/profitsharingreturn")
_, err = comp.SafeRequest(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, nil, result)
return result, err
}
// Query Return Orders Result.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_2.shtml
func (comp *Client) QueryReturn(ctx context.Context, outOrderNO string, outReturnNO string) (*response.ResponseProfitSharingReturnOrder, error) {
result := &response.ResponseProfitSharingReturnOrder{}
params := &object.StringMap{
"out_order_no": outOrderNO,
}
endpoint := comp.Wrap(fmt.Sprintf("/v3/profitsharing/return-orders/%s", outReturnNO))
_, err := comp.Request(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, false, nil, result)
return result, err
}
// UnFreeze Orders.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_5.shtml
func (comp *Client) UnfreezeOrders(ctx context.Context, transactionID string, outOrderNO string, description string) (*response.ResponseProfitSharingOrder, error) {
result := &response.ResponseProfitSharingOrder{}
options := &object.HashMap{
"transaction_id": transactionID,
"out_order_no": outOrderNO,
"description": description,
}
endpoint := comp.Wrap("/v3/profitsharing/orders/unfreeze")
_, err := comp.Request(ctx, endpoint, nil, http.MethodPost, options, false, nil, result)
return result, err
}
// Query Transaction Amounts.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_6.shtml
func (comp *Client) QueryTransactions(ctx context.Context, transactionID string) (*response.ResponseProfitSharingTransaction, error) {
result := &response.ResponseProfitSharingTransaction{}
endpoint := comp.Wrap(fmt.Sprintf("/v3/profitsharing/transactions/%s/amounts", transactionID))
_, err := comp.Request(ctx, endpoint, nil, http.MethodPost, &object.HashMap{}, false, nil, result)
return result, err
}
// Add Receiver.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_8.shtml
func (comp *Client) AddReceiver(
ctx context.Context,
receiverType string, account string, name string,
relationType string, customRelation string) (*response.ResponseProfitSharingAddReceiver, error) {
result := &response.ResponseProfitSharingAddReceiver{}
options := &object.HashMap{
"type": receiverType,
"account": account,
"name": name,
"relation_type": relationType,
"custom_relation": customRelation,
}
endpoint := comp.Wrap("/v3/profitsharing/receivers/add")
_, err := comp.Request(ctx, endpoint, nil, http.MethodPost, options, false, nil, result)
return result, err
}
// Delete Receiver.
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_9.shtml
func (comp *Client) DeleteReceiver(ctx context.Context, receiverType string, account string) (*response.ResponseProfitSharingDeleteReceiver, error) {
result := &response.ResponseProfitSharingDeleteReceiver{}
config := (*comp.App).GetConfig()
options := &object.HashMap{
"appid": config.GetString("app_id", ""),
"type": receiverType,
"account": account,
}
endpoint := comp.Wrap("/v3/profitsharing/receivers/delete")
_, err := comp.Request(ctx, endpoint, nil, http.MethodPost, options, false, nil, result)
return result, err
}
// Get Bills.
// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_11.shtml
func (comp *Client) GetBills(ctx context.Context, subMchID string, billDate string, tarType string) (*response.ResponseProfitSharingGetBills, error) {
result := &response.ResponseProfitSharingGetBills{}
params := &object.StringMap{
"sub_mchid": subMchID,
"bill_date": billDate,
"tar_type": tarType,
}
endpoint := comp.Wrap("/v3/profitsharing/bills")
_, err := comp.Request(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, false, nil, result)
return result, err
}