wechat-ipay 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/code_quality.yml +35 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.rubocop.yml +21 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +15 -0
- data/README.md +92 -0
- data/Rakefile +19 -0
- data/lib/wechat-pay/direct.rb +371 -0
- data/lib/wechat-pay/ecommerce/applyment.rb +195 -0
- data/lib/wechat-pay/ecommerce/balance.rb +114 -0
- data/lib/wechat-pay/ecommerce/bill.rb +91 -0
- data/lib/wechat-pay/ecommerce/combine_order.rb +133 -0
- data/lib/wechat-pay/ecommerce/order.rb +135 -0
- data/lib/wechat-pay/ecommerce/profitsharing.rb +230 -0
- data/lib/wechat-pay/ecommerce/refund.rb +135 -0
- data/lib/wechat-pay/ecommerce/subsidies.rb +84 -0
- data/lib/wechat-pay/ecommerce/withdraw.rb +154 -0
- data/lib/wechat-pay/ecommerce.rb +93 -0
- data/lib/wechat-pay/helper.rb +33 -0
- data/lib/wechat-pay/sign.rb +245 -0
- data/lib/wechat-pay/version.rb +5 -0
- data/lib/wechat-pay.rb +44 -0
- data/wechat-ipay.gemspec +27 -0
- metadata +135 -0
@@ -0,0 +1,230 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
# 分账相关
|
5
|
+
module Ecommerce
|
6
|
+
class << self
|
7
|
+
REQUEST_PROFITSHARING_FIELDS = %i[out_trade_no transaction_id sub_mchid out_order_no receivers finish].freeze # :nodoc:
|
8
|
+
#
|
9
|
+
# 分账请求
|
10
|
+
#
|
11
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_1.shtml
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# ``` ruby
|
16
|
+
# params = {"out_trade_no"=>"P202104306585", "transaction_id"=>"4323400972202104301286330188", "sub_mchid"=>"160000", "out_order_no"=>"N202104307987", "finish"=>true, "receivers"=>[{"type"=>"MERCHANT_ID", "receiver_account"=>"1607189890", "amount"=>1, "description"=>"平台抽成", "receiver_name"=>"CXOO5SF5sylMhSWjUBHQ6dBN0BTdrGExiziO8OEnJEG/nAa7gw6JTbsFQVhUbXD2er07Gcvt7qsLg7wYEe6iqNKbHHRWvChVVKWcKSyvfMOcRa95lxUkVn2+YdMmQ/Rt2h+xN7HMFMVPh9Py2c3sxnv1hZSraTEBWp577NOVwfSKiDTOAnbLtVtLbJndZ2N/bRXzW/gpbQV6TnnsrKPJ+NQ64kCedaYoO0XvEK1JavJju4kUAw/TnJ78jBMwj0gx2kfrsAgtwGrIGhrqhGcGHwwwPPDk5lS/iVaKpSdMvxOHN/9mrAqgqmvBg9uHRKE4sUqkZWuaiAFvYF9/5sLgjQ=="}]}
|
17
|
+
# WechatPay::Ecommerce.request_profitsharing(params)
|
18
|
+
# ```
|
19
|
+
def request_profitsharing(params)
|
20
|
+
url = '/v3/ecommerce/profitsharing/orders'
|
21
|
+
method = 'POST'
|
22
|
+
params = {
|
23
|
+
appid: WechatPay.app_id
|
24
|
+
}.merge(params)
|
25
|
+
|
26
|
+
payload_json = params.to_json
|
27
|
+
|
28
|
+
make_request(
|
29
|
+
method: method,
|
30
|
+
path: url,
|
31
|
+
for_sign: payload_json,
|
32
|
+
payload: payload_json,
|
33
|
+
extra_headers: {
|
34
|
+
'Wechatpay-Serial' => WechatPay.platform_serial_no
|
35
|
+
}
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
QUERY_PROFITSHARING_FIELDS = %i[out_order_no transaction_id sub_mchid].freeze # :nodoc:
|
40
|
+
#
|
41
|
+
# 分账结果查询
|
42
|
+
#
|
43
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_2.shtml
|
44
|
+
#
|
45
|
+
# Example:
|
46
|
+
#
|
47
|
+
# ``` ruby
|
48
|
+
# WechatPay::Ecommerce.query_profitsharing(out_order_no: 'N202104288345', sub_mchid: '16000000', transaction_id: '4200001048202104280183691118')
|
49
|
+
# ```
|
50
|
+
#
|
51
|
+
def query_profitsharing(params)
|
52
|
+
method = 'GET'
|
53
|
+
query = build_query(params)
|
54
|
+
path = '/v3/ecommerce/profitsharing/orders'
|
55
|
+
url = "#{path}?#{query}"
|
56
|
+
|
57
|
+
make_request(
|
58
|
+
path: url,
|
59
|
+
method: method,
|
60
|
+
extra_headers: {
|
61
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
62
|
+
}
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
RETURN_PROFITSHARING_FIELDS = %i[sub_mchid order_id out_order_no out_return_no return_mchid amount description].freeze # :nodoc:
|
67
|
+
#
|
68
|
+
# 请求分账回退
|
69
|
+
#
|
70
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_3.shtml
|
71
|
+
#
|
72
|
+
# ``` ruby
|
73
|
+
# WechatPay::Ecommerce.return_profitsharing(out_order_no: 'P202104306585', sub_mchid: '16000000', out_return_no: 'R20210430223', return_mchid: '180000', amount: 1, description: '分账回退')
|
74
|
+
# WechatPay::Ecommerce.return_profitsharing(order_id: '3008450740201411110007820472', sub_mchid: '16000000', out_return_no: 'R20210430223', return_mchid: '180000', amount: 1, description: '分账回退')
|
75
|
+
# ```
|
76
|
+
def return_profitsharing(params)
|
77
|
+
url = '/v3/ecommerce/profitsharing/returnorders'
|
78
|
+
method = 'POST'
|
79
|
+
|
80
|
+
payload_json = params.to_json
|
81
|
+
|
82
|
+
make_request(
|
83
|
+
method: method,
|
84
|
+
path: url,
|
85
|
+
for_sign: payload_json,
|
86
|
+
payload: payload_json
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
QUERY_RETURN_PROFITSHARING_FIELDS = %i[sub_mchid order_id out_order_no out_return_no].freeze # :nodoc:
|
91
|
+
#
|
92
|
+
# 分账回退结果查询
|
93
|
+
#
|
94
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_4.shtml
|
95
|
+
#
|
96
|
+
# Example:
|
97
|
+
#
|
98
|
+
# ``` ruby
|
99
|
+
# WechatPay::Ecommerce.query_return_profitsharing(sub_mchid: '1608747309', out_order_no: 'P202104306585', out_return_no: 'R202105023455')
|
100
|
+
# WechatPay::Ecommerce.query_return_profitsharing(sub_mchid: '1608747309', order_id: '3008450740201411110007820472', out_return_no: 'R202105023455')
|
101
|
+
# ```
|
102
|
+
def query_return_profitsharing(params)
|
103
|
+
method = 'GET'
|
104
|
+
query = build_query(params)
|
105
|
+
path = '/v3/ecommerce/profitsharing/returnorders'
|
106
|
+
url = "#{path}?#{query}"
|
107
|
+
|
108
|
+
make_request(
|
109
|
+
path: url,
|
110
|
+
method: method,
|
111
|
+
extra_headers: {
|
112
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
113
|
+
}
|
114
|
+
)
|
115
|
+
end
|
116
|
+
|
117
|
+
FINISH_PROFITSHARING_FIELDS = %i[transaction_id sub_mchid out_order_no description].freeze # :nodoc:
|
118
|
+
#
|
119
|
+
# 完结分账
|
120
|
+
#
|
121
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_5.shtml
|
122
|
+
#
|
123
|
+
# Example:
|
124
|
+
#
|
125
|
+
# ``` ruby
|
126
|
+
# WechatPay::Ecommerce.finish_profitsharing(sub_mchid: '160000', out_order_no: 'P202104303106', transaction_id: '4323400972202104305131070133', description: '直接打款到二级商户不分账').bod
|
127
|
+
# ```
|
128
|
+
#
|
129
|
+
def finish_profitsharing(params)
|
130
|
+
url = '/v3/ecommerce/profitsharing/finish-order'
|
131
|
+
method = 'POST'
|
132
|
+
|
133
|
+
payload_json = params.to_json
|
134
|
+
|
135
|
+
make_request(
|
136
|
+
method: method,
|
137
|
+
path: url,
|
138
|
+
for_sign: payload_json,
|
139
|
+
payload: payload_json
|
140
|
+
)
|
141
|
+
end
|
142
|
+
|
143
|
+
QUERY_PROFITSHARING_AMOUNT_FIELDS = %i[transaction_id].freeze # :nodoc:
|
144
|
+
#
|
145
|
+
# 查询订单剩余待分金额
|
146
|
+
#
|
147
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_9.shtml
|
148
|
+
#
|
149
|
+
# Example:
|
150
|
+
#
|
151
|
+
# ``` ruby
|
152
|
+
# WechatPay::Ecommerce.query_profitsharing_amount({ transaction_id: '4323400972202104301286330188' })
|
153
|
+
# ```
|
154
|
+
#
|
155
|
+
def query_profitsharing_amount(params)
|
156
|
+
method = 'GET'
|
157
|
+
transaction_id = params.delete(:transaction_id)
|
158
|
+
url = "/v3/ecommerce/profitsharing/orders/#{transaction_id}/amounts"
|
159
|
+
|
160
|
+
make_request(
|
161
|
+
path: url,
|
162
|
+
method: method,
|
163
|
+
extra_headers: {
|
164
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
165
|
+
}
|
166
|
+
)
|
167
|
+
end
|
168
|
+
|
169
|
+
ADD_PROFITSHARING_RECEIVERS_FIELDS = %i[type account name relation_type].freeze # :nodoc:
|
170
|
+
#
|
171
|
+
# 添加分账接收方
|
172
|
+
#
|
173
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_7.shtml
|
174
|
+
#
|
175
|
+
# Example:
|
176
|
+
#
|
177
|
+
# ``` ruby
|
178
|
+
# WechatPay::Ecommerce.add_profitsharing_receivers(type: 'PERSONAL_OPENID', account: 'oly6s5cLmmVzzr8iPyI6mJj7qG2s', name: 'Lan', relation_type: 'DISTRIBUTOR').body
|
179
|
+
# ```
|
180
|
+
#
|
181
|
+
def add_profitsharing_receivers(params)
|
182
|
+
url = '/v3/ecommerce/profitsharing/receivers/add'
|
183
|
+
method = 'POST'
|
184
|
+
|
185
|
+
params = {
|
186
|
+
appid: WechatPay.app_id
|
187
|
+
}.merge(params)
|
188
|
+
|
189
|
+
payload_json = params.to_json
|
190
|
+
|
191
|
+
make_request(
|
192
|
+
method: method,
|
193
|
+
path: url,
|
194
|
+
for_sign: payload_json,
|
195
|
+
payload: payload_json
|
196
|
+
)
|
197
|
+
end
|
198
|
+
|
199
|
+
DELETE_PROFITSHARING_RECEIVERS_FIELDS = %i[type account].freeze # :nodoc:
|
200
|
+
#
|
201
|
+
# 删除分账接收方
|
202
|
+
#
|
203
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_8.shtml
|
204
|
+
#
|
205
|
+
# Example:
|
206
|
+
#
|
207
|
+
# ``` ruby
|
208
|
+
# WechatPay::Ecommerce.remove_profitsharing_receivers(type: 'PERSONAL_OPENID', account: 'oly6s5cLmmVzzr8iPyI6mJj7qG2s', name: 'Lan', relation_type: 'DISTRIBUTOR').body
|
209
|
+
# ```
|
210
|
+
#
|
211
|
+
def delete_profitsharing_receivers(params)
|
212
|
+
url = '/v3/ecommerce/profitsharing/receivers/delete'
|
213
|
+
method = 'POST'
|
214
|
+
|
215
|
+
params = {
|
216
|
+
appid: WechatPay.app_id
|
217
|
+
}.merge(params)
|
218
|
+
|
219
|
+
payload_json = params.to_json
|
220
|
+
|
221
|
+
make_request(
|
222
|
+
method: method,
|
223
|
+
path: url,
|
224
|
+
for_sign: payload_json,
|
225
|
+
payload: payload_json
|
226
|
+
)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
# 退款相关
|
5
|
+
module Ecommerce
|
6
|
+
INVOKE_REFUND_FIELDS = %i[sub_mchid out_trade_no total refund out_refund_no].freeze # :nodoc:
|
7
|
+
#
|
8
|
+
# 退款申请
|
9
|
+
#
|
10
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_1.shtml
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# ``` ruby
|
15
|
+
# WechatPay::Ecommerce.invoke_refund(sub_mchid: '1600000', transaction_id: '4323400972202104305131070170', total: 1, refund: 1, description: '退款', out_refund_no: 'R10000') # by transaction_id
|
16
|
+
# WechatPay::Ecommerce.invoke_refund(sub_mchid: '1608977559', total: 1, refund: 1, description: '退款', out_trade_no: 'N202104302474', out_refund_no: 'R10000') # by out_trade_no
|
17
|
+
# ```
|
18
|
+
def self.invoke_refund(params)
|
19
|
+
url = '/v3/ecommerce/refunds/apply'
|
20
|
+
method = 'POST'
|
21
|
+
amount = {
|
22
|
+
refund: params.delete(:refund),
|
23
|
+
total: params.delete(:total),
|
24
|
+
currency: 'CNY'
|
25
|
+
}
|
26
|
+
|
27
|
+
params = {
|
28
|
+
amount: amount,
|
29
|
+
sp_appid: WechatPay.app_id
|
30
|
+
}.merge(params)
|
31
|
+
|
32
|
+
make_request(
|
33
|
+
path: url,
|
34
|
+
method: method,
|
35
|
+
for_sign: params.to_json,
|
36
|
+
payload: params.to_json
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
QUERY_REFUND_FIELDS = %i[sub_mchid refund_id out_refund_no].freeze # :nodoc:
|
41
|
+
#
|
42
|
+
# 退款查询
|
43
|
+
#
|
44
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_2.shtml
|
45
|
+
#
|
46
|
+
# Example:
|
47
|
+
#
|
48
|
+
# ``` ruby
|
49
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', out_refund_no: 'AFS202104302474')
|
50
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', refund_id: '50000000382019052709732678859')
|
51
|
+
# ```
|
52
|
+
#
|
53
|
+
def self.query_refund(params)
|
54
|
+
if params[:refund_id]
|
55
|
+
params.delete(:out_refund_no)
|
56
|
+
refund_id = params.delete(:refund_id)
|
57
|
+
path = "/v3/ecommerce/refunds/id/#{refund_id}"
|
58
|
+
else
|
59
|
+
params.delete(:refund_id)
|
60
|
+
out_refund_no = params.delete(:out_refund_no)
|
61
|
+
path = "/v3/ecommerce/refunds/out-refund-no/#{out_refund_no}"
|
62
|
+
end
|
63
|
+
|
64
|
+
params = params.merge({
|
65
|
+
sp_mchid: WechatPay.mch_id
|
66
|
+
})
|
67
|
+
|
68
|
+
method = 'GET'
|
69
|
+
query = build_query(params)
|
70
|
+
url = "#{path}?#{query}"
|
71
|
+
|
72
|
+
make_request(
|
73
|
+
method: method,
|
74
|
+
path: url,
|
75
|
+
extra_headers: {
|
76
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
77
|
+
}
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
RETURN_ADVANCE_REFUND_FIELDS = %i[refund_id sub_mchid].freeze # :nodoc:
|
82
|
+
#
|
83
|
+
# 垫付退款回补
|
84
|
+
#
|
85
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_4.shtml
|
86
|
+
#
|
87
|
+
# Example:
|
88
|
+
#
|
89
|
+
# ``` ruby
|
90
|
+
# WechatPay::Ecommerce.return_advance_refund(refund_id: '50300908092021043008398036516', sub_mchid: '160000')
|
91
|
+
# ```
|
92
|
+
#
|
93
|
+
def self.return_advance_refund(params)
|
94
|
+
refund_id = params.delete(:refund_id)
|
95
|
+
url = "/v3/ecommerce/refunds/#{refund_id}/return-advance"
|
96
|
+
method = 'POST'
|
97
|
+
|
98
|
+
make_request(
|
99
|
+
path: url,
|
100
|
+
method: method,
|
101
|
+
for_sign: params.to_json,
|
102
|
+
payload: params.to_json
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
QUERY_RETURN_ADVANCE_REFUND_FIELDS = %i[sub_mchid refund_id].freeze # :nodoc:
|
107
|
+
#
|
108
|
+
# 退款查询
|
109
|
+
#
|
110
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_2.shtml
|
111
|
+
#
|
112
|
+
# Example:
|
113
|
+
#
|
114
|
+
# ``` ruby
|
115
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', out_refund_no: 'AFS202104302474')
|
116
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', refund_id: '50000000382019052709732678859')
|
117
|
+
# ```
|
118
|
+
#
|
119
|
+
def self.query_return_advance_refund(params)
|
120
|
+
refund_id = params.delete(:refund_id)
|
121
|
+
path = "/v3/ecommerce/refunds/#{refund_id}/return-advance"
|
122
|
+
method = 'GET'
|
123
|
+
query = build_query(params)
|
124
|
+
url = "#{path}?#{query}"
|
125
|
+
|
126
|
+
make_request(
|
127
|
+
method: method,
|
128
|
+
path: url,
|
129
|
+
extra_headers: {
|
130
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
131
|
+
}
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
# 补差相关
|
5
|
+
module Ecommerce
|
6
|
+
REQUEST_SUBSIDIES_FIELDS = %i[sub_mchid transaction_id amount description].freeze # :nodoc:
|
7
|
+
#
|
8
|
+
# 请求补差
|
9
|
+
#
|
10
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_5_1.shtml
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# ``` ruby
|
15
|
+
# WechatPay::Ecommerce.request_subsidies(sub_mchid: '16000000', transaction_id: '4323400972202104305131070170', amount: 1, description: '订单补差')
|
16
|
+
# ```
|
17
|
+
#
|
18
|
+
def self.request_subsidies(params)
|
19
|
+
url = '/v3/ecommerce/subsidies/create'
|
20
|
+
method = 'POST'
|
21
|
+
|
22
|
+
payload_json = params.to_json
|
23
|
+
|
24
|
+
make_request(
|
25
|
+
method: method,
|
26
|
+
path: url,
|
27
|
+
for_sign: payload_json,
|
28
|
+
payload: payload_json
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
RETURN_SUBSIDIES_FIELDS = %i[sub_mchid transaction_id amount description out_order_no].freeze # :nodoc:
|
33
|
+
#
|
34
|
+
# 补差回退
|
35
|
+
#
|
36
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_5_2.shtml
|
37
|
+
#
|
38
|
+
# Example:
|
39
|
+
#
|
40
|
+
# ``` ruby
|
41
|
+
# WechatPay::Ecommerce.return_subsidies(sub_mchid: '16000000', transaction_id: '4323400972202104305131070170', amount: 1, description: '订单补差', out_order_no: 'P103333')
|
42
|
+
# ```
|
43
|
+
|
44
|
+
def self.return_subsidies(params)
|
45
|
+
url = '/v3/ecommerce/subsidies/return'
|
46
|
+
method = 'POST'
|
47
|
+
|
48
|
+
payload_json = params.to_json
|
49
|
+
|
50
|
+
make_request(
|
51
|
+
method: method,
|
52
|
+
path: url,
|
53
|
+
for_sign: payload_json,
|
54
|
+
payload: payload_json
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
CANCEL_SUBSIDIES_FIELDS = %i[sub_mchid transaction_id description].freeze # :nodoc:
|
59
|
+
#
|
60
|
+
# 取消补差
|
61
|
+
#
|
62
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_5_3.shtml
|
63
|
+
#
|
64
|
+
# Example:
|
65
|
+
#
|
66
|
+
# ``` ruby
|
67
|
+
# WechatPay::Ecommerce.return_subsidies(sub_mchid: '1600000', transaction_id: '4323400972202104305131070170', amount: 1, description: '订单补差', out_order_no: 'P103333')
|
68
|
+
# ```
|
69
|
+
#
|
70
|
+
def self.cancel_subsidies(params)
|
71
|
+
url = '/v3/ecommerce/subsidies/cancel'
|
72
|
+
method = 'POST'
|
73
|
+
|
74
|
+
payload_json = params.to_json
|
75
|
+
|
76
|
+
make_request(
|
77
|
+
method: method,
|
78
|
+
path: url,
|
79
|
+
for_sign: payload_json,
|
80
|
+
payload: payload_json
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
# 提现相关
|
5
|
+
module Ecommerce
|
6
|
+
WITHDRAW_FIELDS = %i[sub_mchid out_request_no amount].freeze # :nodoc:
|
7
|
+
#
|
8
|
+
# 二级商户提现
|
9
|
+
#
|
10
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_8_2.shtml
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# ``` ruby
|
15
|
+
# WechatPay::Ecommerce.withdraw(sub_mchid: '160000', out_request_no: 'P10000', amount: 1)
|
16
|
+
# ```
|
17
|
+
#
|
18
|
+
def self.withdraw(params)
|
19
|
+
url = '/v3/ecommerce/fund/withdraw'
|
20
|
+
method = 'POST'
|
21
|
+
|
22
|
+
make_request(
|
23
|
+
method: method,
|
24
|
+
path: url,
|
25
|
+
for_sign: params.to_json,
|
26
|
+
payload: params.to_json
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
QUERY_WITHDRAW_FIELDS = %i[withdraw_id out_request_no sub_mchid].freeze # :nodoc:
|
31
|
+
#
|
32
|
+
# 二级商户提现查询
|
33
|
+
#
|
34
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_8_3.shtml
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
#
|
38
|
+
# ``` ruby
|
39
|
+
# WechatPay::Ecommerce.query_withdraw(withdraw_id: '335556', sub_mchid: '160000')
|
40
|
+
# WechatPay::Ecommerce.query_withdraw(out_request_no: 'P1000', sub_mchid: '160000')
|
41
|
+
# ```
|
42
|
+
#
|
43
|
+
def self.query_withdraw(params)
|
44
|
+
if params[:withdraw_id]
|
45
|
+
params.delete(:out_request_no)
|
46
|
+
withdraw_id = params.delete(:withdraw_id)
|
47
|
+
path = "/v3/ecommerce/fund/withdraw/#{withdraw_id}"
|
48
|
+
else
|
49
|
+
params.delete(:withdraw_id)
|
50
|
+
out_request_no = params.delete(:out_request_no)
|
51
|
+
path = "/v3/ecommerce/fund/withdraw/out-request-no/#{out_request_no}"
|
52
|
+
end
|
53
|
+
|
54
|
+
method = 'GET'
|
55
|
+
query = build_query(params)
|
56
|
+
url = "#{path}?#{query}"
|
57
|
+
|
58
|
+
make_request(
|
59
|
+
method: method,
|
60
|
+
path: url,
|
61
|
+
extra_headers: {
|
62
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
63
|
+
}
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
PLATFORM_WITHDRAW_FIELDS = %i[out_request_no amount account_type].freeze # :nodoc:
|
68
|
+
#
|
69
|
+
# 电商平台提现
|
70
|
+
#
|
71
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_8_5.shtml
|
72
|
+
#
|
73
|
+
# Example:
|
74
|
+
#
|
75
|
+
# ``` ruby
|
76
|
+
# WechatPay::Ecommerce.platform_withdraw(out_request_no: 'P10000', amount: 1, account_type: 'BASIC')
|
77
|
+
# WechatPay::Ecommerce.platform_withdraw(out_request_no: 'P10000', amount: 1, account_type: 'FEES')
|
78
|
+
# ```
|
79
|
+
#
|
80
|
+
def self.platform_withdraw(params)
|
81
|
+
url = '/v3/merchant/fund/withdraw'
|
82
|
+
method = 'POST'
|
83
|
+
|
84
|
+
make_request(
|
85
|
+
method: method,
|
86
|
+
path: url,
|
87
|
+
for_sign: params.to_json,
|
88
|
+
payload: params.to_json
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
QUERY_PLATFORM_WITHDRAW_FIELDS = %i[withdraw_id out_request_no].freeze # :nodoc:
|
93
|
+
#
|
94
|
+
# 商户平台提现查询
|
95
|
+
#
|
96
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_8_6.shtml
|
97
|
+
#
|
98
|
+
# Example:
|
99
|
+
#
|
100
|
+
# ``` ruby
|
101
|
+
# WechatPay::Ecommerce.query_platform_withdraw(out_request_no: 'P1000')
|
102
|
+
# WechatPay::Ecommerce.query_platform_withdraw(withdraw_id: '12313153')
|
103
|
+
# ```
|
104
|
+
#
|
105
|
+
def self.query_platform_withdraw(params)
|
106
|
+
if params[:withdraw_id]
|
107
|
+
params.delete(:out_request_no)
|
108
|
+
withdraw_id = params.delete(:withdraw_id)
|
109
|
+
url = "/v3/merchant/fund/withdraw/withdraw-id/#{withdraw_id}"
|
110
|
+
else
|
111
|
+
params.delete(:withdraw_id)
|
112
|
+
out_request_no = params.delete(:out_request_no)
|
113
|
+
url = "/v3/merchant/fund/withdraw/out-request-no/#{out_request_no}"
|
114
|
+
end
|
115
|
+
|
116
|
+
method = 'GET'
|
117
|
+
|
118
|
+
make_request(
|
119
|
+
method: method,
|
120
|
+
path: url,
|
121
|
+
extra_headers: {
|
122
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
123
|
+
}
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
DOWNLOAD_EXCEPTION_WITHDRAW_FILE = %i[bill_type bill_date].freeze # :nodoc:
|
128
|
+
#
|
129
|
+
# 按日下载提现异常文件
|
130
|
+
#
|
131
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_8_4.shtml
|
132
|
+
#
|
133
|
+
# ``` ruby
|
134
|
+
# WechatPay::Ecommerce.download_exception_withdraw_file(bill_type: 'NO_SUCC', bill_date: '2021-05-10')
|
135
|
+
# ```
|
136
|
+
def self.download_exception_withdraw_file(params)
|
137
|
+
bill_type = params.delete(:bill_type)
|
138
|
+
path = "/v3/merchant/fund/withdraw/bill-type/#{bill_type}"
|
139
|
+
|
140
|
+
method = 'GET'
|
141
|
+
|
142
|
+
query = build_query(params)
|
143
|
+
url = "#{path}?#{query}"
|
144
|
+
|
145
|
+
make_request(
|
146
|
+
method: method,
|
147
|
+
path: url,
|
148
|
+
extra_headers: {
|
149
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
150
|
+
}
|
151
|
+
)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'wechat-pay/helper'
|
5
|
+
require 'wechat-pay/ecommerce/withdraw'
|
6
|
+
require 'wechat-pay/ecommerce/balance'
|
7
|
+
require 'wechat-pay/ecommerce/applyment'
|
8
|
+
require 'wechat-pay/ecommerce/order'
|
9
|
+
require 'wechat-pay/ecommerce/combine_order'
|
10
|
+
require 'wechat-pay/ecommerce/profitsharing'
|
11
|
+
require 'wechat-pay/ecommerce/subsidies'
|
12
|
+
require 'wechat-pay/ecommerce/refund'
|
13
|
+
require 'wechat-pay/ecommerce/bill'
|
14
|
+
|
15
|
+
module WechatPay
|
16
|
+
# # 服务商相关接口封装(电商平台,服务商有许多接口共用)
|
17
|
+
# 文档: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter3_3_3.shtml
|
18
|
+
#
|
19
|
+
# PS: 电商收付通的所有接口已经封装完毕,有需要再补充。
|
20
|
+
#
|
21
|
+
module Ecommerce
|
22
|
+
include WechatPayHelper
|
23
|
+
|
24
|
+
# 视频上传
|
25
|
+
#
|
26
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter2_1_2.shtml
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
#
|
30
|
+
# ``` ruby
|
31
|
+
# WechatPay::Ecommerce.media_video_upload(File.open('Your Video'))
|
32
|
+
# ```
|
33
|
+
#
|
34
|
+
def self.media_video_upload(video)
|
35
|
+
url = '/v3/merchant/media/video_upload'
|
36
|
+
method = 'POST'
|
37
|
+
meta = {
|
38
|
+
filename: video.to_path,
|
39
|
+
sha256: Digest::SHA256.hexdigest(video.read)
|
40
|
+
}
|
41
|
+
|
42
|
+
video.rewind
|
43
|
+
payload = {
|
44
|
+
meta: meta.to_json,
|
45
|
+
file: video
|
46
|
+
}
|
47
|
+
|
48
|
+
make_request(
|
49
|
+
method: method,
|
50
|
+
path: url,
|
51
|
+
for_sign: meta.to_json,
|
52
|
+
payload: payload,
|
53
|
+
extra_headers: {
|
54
|
+
'Content-Type' => nil
|
55
|
+
}
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
# 图片上传
|
60
|
+
#
|
61
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter2_1_1.shtml
|
62
|
+
#
|
63
|
+
# Example:
|
64
|
+
#
|
65
|
+
# ``` ruby
|
66
|
+
# WechatPay::Ecommerce.media_upload(File.open('Your Image'))
|
67
|
+
# ```
|
68
|
+
def self.media_upload(image)
|
69
|
+
url = '/v3/merchant/media/upload'
|
70
|
+
method = 'POST'
|
71
|
+
meta = {
|
72
|
+
filename: image.to_path,
|
73
|
+
sha256: Digest::SHA256.hexdigest(image.read)
|
74
|
+
}
|
75
|
+
|
76
|
+
image.rewind
|
77
|
+
payload = {
|
78
|
+
meta: meta.to_json,
|
79
|
+
file: image
|
80
|
+
}
|
81
|
+
|
82
|
+
make_request(
|
83
|
+
method: method,
|
84
|
+
path: url,
|
85
|
+
for_sign: meta.to_json,
|
86
|
+
payload: payload,
|
87
|
+
extra_headers: {
|
88
|
+
'Content-Type' => nil # Pass nil to remove the Content-Type
|
89
|
+
}
|
90
|
+
)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|