wechat-pay 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/set_base_envrionment.rb +8 -0
- data/lib/wechat-pay.rb +42 -0
- data/lib/wechat-pay/direct.rb +611 -0
- data/lib/wechat-pay/ecommerce.rb +95 -0
- data/lib/wechat-pay/ecommerce/applyment.rb +196 -0
- data/lib/wechat-pay/ecommerce/balance.rb +115 -0
- data/lib/wechat-pay/ecommerce/bill.rb +90 -0
- data/lib/wechat-pay/ecommerce/combine_order.rb +264 -0
- data/lib/wechat-pay/ecommerce/order.rb +206 -0
- data/lib/wechat-pay/ecommerce/profitsharing.rb +229 -0
- data/lib/wechat-pay/ecommerce/refund.rb +136 -0
- data/lib/wechat-pay/ecommerce/subsidies.rb +85 -0
- data/lib/wechat-pay/ecommerce/withdraw.rb +155 -0
- data/lib/wechat-pay/helper.rb +32 -0
- data/lib/wechat-pay/sign.rb +209 -0
- data/lib/wechat-pay/version.rb +5 -0
- metadata +133 -0
@@ -0,0 +1,229 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
module Ecommerce
|
5
|
+
class << self
|
6
|
+
REQUEST_PROFITSHARING_FIELDS = %i[out_trade_no transaction_id sub_mchid out_order_no receivers finish].freeze # :nodoc:
|
7
|
+
#
|
8
|
+
# 分账请求
|
9
|
+
#
|
10
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_1.shtml
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# ``` ruby
|
15
|
+
# 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=="}]}
|
16
|
+
# WechatPay::Ecommerce.request_profitsharing(params)
|
17
|
+
# ```
|
18
|
+
def request_profitsharing(params)
|
19
|
+
url = '/v3/ecommerce/profitsharing/orders'
|
20
|
+
method = 'POST'
|
21
|
+
params = params.merge({
|
22
|
+
appid: WechatPay.app_id
|
23
|
+
})
|
24
|
+
|
25
|
+
payload_json = params.to_json
|
26
|
+
|
27
|
+
make_request(
|
28
|
+
method: method,
|
29
|
+
path: url,
|
30
|
+
for_sign: payload_json,
|
31
|
+
payload: payload_json,
|
32
|
+
extra_headers: {
|
33
|
+
'Wechatpay-Serial' => WechatPay.platform_serial_no
|
34
|
+
}
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
QUERY_PROFITSHARING_FIELDS = %i[out_order_no transaction_id sub_mchid].freeze # :nodoc:
|
39
|
+
#
|
40
|
+
# 分账结果查询
|
41
|
+
#
|
42
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_2.shtml
|
43
|
+
#
|
44
|
+
# Example:
|
45
|
+
#
|
46
|
+
# ``` ruby
|
47
|
+
# WechatPay::Ecommerce.query_profitsharing(out_order_no: 'N202104288345', sub_mchid: '16000000', transaction_id: '4200001048202104280183691118')
|
48
|
+
# ```
|
49
|
+
#
|
50
|
+
def query_profitsharing(params)
|
51
|
+
method = 'GET'
|
52
|
+
query = build_query(params)
|
53
|
+
path = '/v3/ecommerce/profitsharing/orders'
|
54
|
+
url = "#{path}?#{query}"
|
55
|
+
|
56
|
+
make_request(
|
57
|
+
path: url,
|
58
|
+
method: method,
|
59
|
+
extra_headers: {
|
60
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
61
|
+
}
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
RETURN_PROFITSHARING_FIELDS = %i[sub_mchid order_id out_order_no out_return_no return_mchid amount description].freeze # :nodoc:
|
66
|
+
#
|
67
|
+
# 请求分账回退
|
68
|
+
#
|
69
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_3.shtml
|
70
|
+
#
|
71
|
+
# ``` ruby
|
72
|
+
# WechatPay::Ecommerce.return_profitsharing(out_order_no: 'P202104306585', sub_mchid: '16000000', out_return_no: 'R20210430223', return_mchid: '180000', amount: 1, description: '分账回退')
|
73
|
+
# WechatPay::Ecommerce.return_profitsharing(order_id: '3008450740201411110007820472', sub_mchid: '16000000', out_return_no: 'R20210430223', return_mchid: '180000', amount: 1, description: '分账回退')
|
74
|
+
# ```
|
75
|
+
def return_profitsharing(params)
|
76
|
+
url = '/v3/ecommerce/profitsharing/returnorders'
|
77
|
+
method = 'POST'
|
78
|
+
|
79
|
+
payload_json = params.to_json
|
80
|
+
|
81
|
+
make_request(
|
82
|
+
method: method,
|
83
|
+
path: url,
|
84
|
+
for_sign: payload_json,
|
85
|
+
payload: payload_json
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
QUERY_RETURN_PROFITSHARING_FIELDS = %i[sub_mchid order_id out_order_no out_return_no].freeze # :nodoc:
|
90
|
+
#
|
91
|
+
# 分账回退结果查询
|
92
|
+
#
|
93
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_4.shtml
|
94
|
+
#
|
95
|
+
# Example:
|
96
|
+
#
|
97
|
+
# ``` ruby
|
98
|
+
# WechatPay::Ecommerce.query_return_profitsharing(sub_mchid: '1608747309', out_order_no: 'P202104306585', out_return_no: 'R202105023455')
|
99
|
+
# WechatPay::Ecommerce.query_return_profitsharing(sub_mchid: '1608747309', order_id: '3008450740201411110007820472', out_return_no: 'R202105023455')
|
100
|
+
# ```
|
101
|
+
def query_return_profitsharing(params)
|
102
|
+
method = 'GET'
|
103
|
+
query = build_query(params)
|
104
|
+
path = '/v3/ecommerce/profitsharing/returnorders'
|
105
|
+
url = "#{path}?#{query}"
|
106
|
+
|
107
|
+
make_request(
|
108
|
+
path: url,
|
109
|
+
method: method,
|
110
|
+
extra_headers: {
|
111
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
112
|
+
}
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
FINISH_PROFITSHARING_FIELDS = %i[transaction_id sub_mchid out_order_no description].freeze # :nodoc:
|
117
|
+
#
|
118
|
+
# 完结分账
|
119
|
+
#
|
120
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_5.shtml
|
121
|
+
#
|
122
|
+
# Example:
|
123
|
+
#
|
124
|
+
# ``` ruby
|
125
|
+
# WechatPay::Ecommerce.finish_profitsharing(sub_mchid: '160000', out_order_no: 'P202104303106', transaction_id: '4323400972202104305131070133', description: '直接打款到二级商户不分账').bod
|
126
|
+
# ```
|
127
|
+
#
|
128
|
+
def finish_profitsharing(params)
|
129
|
+
url = '/v3/ecommerce/profitsharing/finish-order'
|
130
|
+
method = 'POST'
|
131
|
+
|
132
|
+
payload_json = params.to_json
|
133
|
+
|
134
|
+
make_request(
|
135
|
+
method: method,
|
136
|
+
path: url,
|
137
|
+
for_sign: payload_json,
|
138
|
+
payload: payload_json
|
139
|
+
)
|
140
|
+
end
|
141
|
+
|
142
|
+
QUERY_PROFITSHARING_AMOUNT_FIELDS = %i[transaction_id].freeze # :nodoc:
|
143
|
+
#
|
144
|
+
# 查询订单剩余待分金额
|
145
|
+
#
|
146
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_9.shtml
|
147
|
+
#
|
148
|
+
# Example:
|
149
|
+
#
|
150
|
+
# ``` ruby
|
151
|
+
# WechatPay::Ecommerce.query_profitsharing_amount({ transaction_id: '4323400972202104301286330188' })
|
152
|
+
# ```
|
153
|
+
#
|
154
|
+
def query_profitsharing_amount(params)
|
155
|
+
method = 'GET'
|
156
|
+
transaction_id = params.delete(:transaction_id)
|
157
|
+
url = "/v3/ecommerce/profitsharing/orders/#{transaction_id}/amounts"
|
158
|
+
|
159
|
+
make_request(
|
160
|
+
path: url,
|
161
|
+
method: method,
|
162
|
+
extra_headers: {
|
163
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
164
|
+
}
|
165
|
+
)
|
166
|
+
end
|
167
|
+
|
168
|
+
ADD_PROFITSHARING_RECEIVERS_FIELDS = %i[type account name relation_type].freeze # :nodoc:
|
169
|
+
#
|
170
|
+
# 添加分账接收方
|
171
|
+
#
|
172
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_7.shtml
|
173
|
+
#
|
174
|
+
# Example:
|
175
|
+
#
|
176
|
+
# ``` ruby
|
177
|
+
# WechatPay::Ecommerce.add_profitsharing_receivers(type: 'PERSONAL_OPENID', account: 'oly6s5cLmmVzzr8iPyI6mJj7qG2s', name: 'Lan', relation_type: 'DISTRIBUTOR').body
|
178
|
+
# ```
|
179
|
+
#
|
180
|
+
def add_profitsharing_receivers(params)
|
181
|
+
url = '/v3/ecommerce/profitsharing/receivers/add'
|
182
|
+
method = 'POST'
|
183
|
+
|
184
|
+
params = params.merge({
|
185
|
+
appid: WechatPay.app_id
|
186
|
+
})
|
187
|
+
|
188
|
+
payload_json = params.to_json
|
189
|
+
|
190
|
+
make_request(
|
191
|
+
method: method,
|
192
|
+
path: url,
|
193
|
+
for_sign: payload_json,
|
194
|
+
payload: payload_json
|
195
|
+
)
|
196
|
+
end
|
197
|
+
|
198
|
+
DELETE_PROFITSHARING_RECEIVERS_FIELDS = %i[type account].freeze # :nodoc:
|
199
|
+
#
|
200
|
+
# 删除分账接收方
|
201
|
+
#
|
202
|
+
# Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_4_8.shtml
|
203
|
+
#
|
204
|
+
# Example:
|
205
|
+
#
|
206
|
+
# ``` ruby
|
207
|
+
# WechatPay::Ecommerce.remove_profitsharing_receivers(type: 'PERSONAL_OPENID', account: 'oly6s5cLmmVzzr8iPyI6mJj7qG2s', name: 'Lan', relation_type: 'DISTRIBUTOR').body
|
208
|
+
# ```
|
209
|
+
#
|
210
|
+
def delete_profitsharing_receivers(params)
|
211
|
+
url = '/v3/ecommerce/profitsharing/receivers/delete'
|
212
|
+
method = 'POST'
|
213
|
+
|
214
|
+
params = params.merge({
|
215
|
+
appid: WechatPay.app_id
|
216
|
+
})
|
217
|
+
|
218
|
+
payload_json = params.to_json
|
219
|
+
|
220
|
+
make_request(
|
221
|
+
method: method,
|
222
|
+
path: url,
|
223
|
+
for_sign: payload_json,
|
224
|
+
payload: payload_json
|
225
|
+
)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
module Ecommerce
|
5
|
+
class << self
|
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 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 = params.merge({
|
28
|
+
amount: amount,
|
29
|
+
sp_appid: WechatPay.app_id
|
30
|
+
})
|
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: 'N202104302474') # 注意这里的out_refund_no用的是商户订单号,微信文档有误
|
50
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', refund_id: '420000103020210508005624628') # 注意这里的refund_id用的是微信交易单号transaction_id,微信文档有误
|
51
|
+
# ```
|
52
|
+
#
|
53
|
+
def query_refund(params)
|
54
|
+
if params[:refund_id]
|
55
|
+
params.delete(:out_refund_no)
|
56
|
+
refund_id = params.delete(:refund_id)
|
57
|
+
path = "/v3/pay/partner/transactions/id/#{refund_id}"
|
58
|
+
else
|
59
|
+
params.delete(:refund_id)
|
60
|
+
out_refund_no = params.delete(:out_refund_no)
|
61
|
+
path = "/v3/pay/partner/transactions/out-trade-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 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: 'N202104302474') # 注意这里的out_refund_no用的是商户订单号,微信文档有误
|
116
|
+
# WechatPay::Ecommerce.query_refund(sub_mchid: '16000000', refund_id: '420000103020210508005624628') # 注意这里的refund_id用的是微信交易单号transaction_id,微信文档有误
|
117
|
+
# ```
|
118
|
+
#
|
119
|
+
def 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
|
136
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WechatPay
|
4
|
+
module Ecommerce
|
5
|
+
class << self
|
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 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 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 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
|
85
|
+
end
|