wechat-pay 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,264 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WechatPay
4
+ module Ecommerce
5
+ class<<self
6
+ INVOKE_COMBINE_TRANSACTIONS_IN_NATIVE_FIELDS = %i[combine_out_trade_no combine_payer_info sub_orders notify_url].freeze # :nodoc:
7
+ #
8
+ # native合单
9
+ #
10
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_5.shtml
11
+ #
12
+ # Example:
13
+ #
14
+ # ``` ruby
15
+ # params = {
16
+ # combine_out_trade_no: 'combine_out_trade_no',
17
+ # combine_payer_info: {
18
+ # openid: 'client open id'
19
+ # },
20
+ # sub_orders: [
21
+ # {
22
+ # mchid: 'mchid',
23
+ # sub_mchid: 'sub mchid',
24
+ # attach: 'attach',
25
+ # amount: {
26
+ # total_amount: 100,
27
+ # currency: 'CNY'
28
+ # },
29
+ # out_trade_no: 'out_trade_no',
30
+ # description: 'description'
31
+ # }
32
+ # ],
33
+ # notify_url: 'the_url'
34
+ # }
35
+ #
36
+ # WechatPay::Ecommerce.invoke_combine_transactions_in_native(params)
37
+ # ```
38
+ def invoke_combine_transactions_in_native(params)
39
+ combine_transactions_method_by_suffix('native', params)
40
+ end
41
+
42
+ INVOKE_COMBINE_TRANSACTIONS_IN_H5_FIELDS = %i[combine_out_trade_no combine_payer_info sub_orders notify_url].freeze # :nodoc:
43
+ #
44
+ # h5合单
45
+ #
46
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_2.shtml
47
+ #
48
+ # Example:
49
+ #
50
+ # ``` ruby
51
+ # params = {
52
+ # combine_out_trade_no: 'combine_out_trade_no',
53
+ # combine_payer_info: {
54
+ # openid: 'client open id'
55
+ # },
56
+ # sub_orders: [
57
+ # {
58
+ # mchid: 'mchid',
59
+ # sub_mchid: 'sub mchid',
60
+ # attach: 'attach',
61
+ # amount: {
62
+ # total_amount: 100,
63
+ # currency: 'CNY'
64
+ # },
65
+ # out_trade_no: 'out_trade_no',
66
+ # description: 'description'
67
+ # }
68
+ # ],
69
+ # notify_url: 'the_url'
70
+ # }
71
+ #
72
+ # WechatPay::Ecommerce.invoke_combine_transactions_in_h5(params)
73
+ # ```
74
+ def invoke_combine_transactions_in_h5(params)
75
+ combine_transactions_method_by_suffix('h5', params)
76
+ end
77
+
78
+ INVOKE_COMBINE_TRANSACTIONS_IN_APP_FIELDS = %i[combine_out_trade_no combine_payer_info sub_orders notify_url].freeze # :nodoc:
79
+ #
80
+ # app合单
81
+ #
82
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_1.shtml
83
+ #
84
+ # Example:
85
+ #
86
+ # ``` ruby
87
+ # params = {
88
+ # combine_out_trade_no: 'combine_out_trade_no',
89
+ # combine_payer_info: {
90
+ # openid: 'client open id'
91
+ # },
92
+ # sub_orders: [
93
+ # {
94
+ # mchid: 'mchid',
95
+ # sub_mchid: 'sub mchid',
96
+ # attach: 'attach',
97
+ # amount: {
98
+ # total_amount: 100,
99
+ # currency: 'CNY'
100
+ # },
101
+ # out_trade_no: 'out_trade_no',
102
+ # description: 'description'
103
+ # }
104
+ # ],
105
+ # notify_url: 'the_url'
106
+ # }
107
+ #
108
+ # WechatPay::Ecommerce.invoke_combine_transactions_in_app(params)
109
+ # ```
110
+ def invoke_combine_transactions_in_app(params)
111
+ combine_transactions_method_by_suffix('app', params)
112
+ end
113
+
114
+ INVOKE_COMBINE_TRANSACTIONS_IN_JS_FIELDS = %i[combine_out_trade_no combine_payer_info sub_orders notify_url].freeze # :nodoc:
115
+ #
116
+ # js合单
117
+ #
118
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_3.shtml
119
+ #
120
+ # Example:
121
+ #
122
+ # ``` ruby
123
+ # params = {
124
+ # combine_out_trade_no: 'combine_out_trade_no',
125
+ # combine_payer_info: {
126
+ # openid: 'client open id'
127
+ # },
128
+ # sub_orders: [
129
+ # {
130
+ # mchid: 'mchid',
131
+ # sub_mchid: 'sub mchid',
132
+ # attach: 'attach',
133
+ # amount: {
134
+ # total_amount: 100,
135
+ # currency: 'CNY'
136
+ # },
137
+ # out_trade_no: 'out_trade_no',
138
+ # description: 'description'
139
+ # }
140
+ # ],
141
+ # notify_url: 'the_url'
142
+ # }
143
+ #
144
+ # WechatPay::Ecommerce.invoke_combine_transactions_in_js(params)
145
+ # ```
146
+ def invoke_combine_transactions_in_js(params)
147
+ combine_transactions_method_by_suffix('jsapi', params)
148
+ end
149
+
150
+ INVOKE_COMBINE_TRANSACTIONS_IN_MINIPROGRAM_FIELDS = %i[combine_out_trade_no combine_payer_info sub_orders notify_url].freeze # :nodoc:
151
+ #
152
+ # 小程序合单
153
+ #
154
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_4.shtml
155
+ #
156
+ # Example:
157
+ #
158
+ # ``` ruby
159
+ # params = {
160
+ # combine_out_trade_no: 'combine_out_trade_no',
161
+ # combine_payer_info: {
162
+ # openid: 'client open id'
163
+ # },
164
+ # sub_orders: [
165
+ # {
166
+ # mchid: 'mchid',
167
+ # sub_mchid: 'sub mchid',
168
+ # attach: 'attach',
169
+ # amount: {
170
+ # total_amount: 100,
171
+ # currency: 'CNY'
172
+ # },
173
+ # out_trade_no: 'out_trade_no',
174
+ # description: 'description'
175
+ # }
176
+ # ],
177
+ # notify_url: 'the_url'
178
+ # }
179
+ #
180
+ # WechatPay::Ecommerce.invoke_combine_transactions_in_miniprogram(params)
181
+ # ```
182
+ def invoke_combine_transactions_in_miniprogram(params)
183
+ combine_transactions_method_by_suffix('jsapi', params)
184
+ end
185
+
186
+ QUERY_COMBINE_ORDER_FIELDS = %i[combine_out_trade_no].freeze # :nodoc:
187
+ #
188
+ # 合单查询
189
+ #
190
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_11.shtml
191
+ #
192
+ # ``` ruby
193
+ # WechatPay::Ecommerce.query_order(combine_out_trade_no: 'C202104302474')
194
+ # ```
195
+ #
196
+ def query_combine_order(params)
197
+ combine_out_trade_no = params.delete(:combine_out_trade_no)
198
+
199
+ url = "/v3/combine-transactions/out-trade-no/#{combine_out_trade_no}"
200
+
201
+ method = 'GET'
202
+
203
+ make_request(
204
+ method: method,
205
+ path: url,
206
+ extra_headers: {
207
+ 'Content-Type' => 'application/x-www-form-urlencoded'
208
+ }
209
+ )
210
+ end
211
+
212
+ CLOSE_COMBINE_ORDER_FIELDS = %i[combine_out_trade_no sub_orders].freeze # :nodoc:
213
+ #
214
+ # 关闭合单
215
+ #
216
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_11.shtml
217
+ #
218
+ # ``` ruby
219
+ # WechatPay::Ecommerce.close_combine_order(combine_out_trade_no: 'C202104302474')
220
+ # ```
221
+ def close_combine_order(params)
222
+ combine_out_trade_no = params.delete(:combine_out_trade_no)
223
+
224
+ url = "/v3/combine-transactions/out-trade-no/#{combine_out_trade_no}/close"
225
+
226
+ payload = {
227
+ combine_appid: WechatPay.app_id
228
+ }.merge(params)
229
+
230
+ payload_json = payload.to_json
231
+
232
+ method = 'POST'
233
+
234
+ make_request(
235
+ method: method,
236
+ for_sign: payload_json,
237
+ payload: payload_json,
238
+ path: url
239
+ )
240
+ end
241
+
242
+ private
243
+
244
+ def combine_transactions_method_by_suffix(suffix, params)
245
+ url = "/v3/combine-transactions/#{suffix}"
246
+ method = 'POST'
247
+
248
+ params = params.merge({
249
+ combine_mchid: WechatPay.mch_id,
250
+ combine_appid: WechatPay.app_id
251
+ })
252
+
253
+ payload_json = params.to_json
254
+
255
+ make_request(
256
+ method: method,
257
+ path: url,
258
+ for_sign: payload_json,
259
+ payload: payload_json
260
+ )
261
+ end
262
+ end
263
+ end
264
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WechatPay
4
+ module Ecommerce
5
+ class << self
6
+ INVOKE_TRANSACTIONS_IN_JS_FIELDS = %i[sub_mchid description out_trade_no notify_url amount].freeze # :nodoc:
7
+ #
8
+ # js下单
9
+ #
10
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_2.shtml
11
+ #
12
+ # Example:
13
+ #
14
+ # ```
15
+ # params = {
16
+ # description: 'pay',
17
+ # out_trade_no: 'Order Number',
18
+ # payer: {
19
+ # sp_openid: 'wechat open id'
20
+ # },
21
+ # amount: {
22
+ # total: 10
23
+ # },
24
+ # sub_mchid: 'Your sub mchid',
25
+ # notify_url: 'the url'
26
+ # }
27
+ #
28
+ # WechatPay::Ecommerce.invoke_transactions_in_app(params)
29
+ # ```
30
+ def invoke_transactions_in_js(params)
31
+ transactions_method_by_suffix('jsapi', params)
32
+ end
33
+
34
+ INVOKE_TRANSACTIONS_IN_MINIPROGRAM_FIELDS = %i[sub_mchid description out_trade_no notify_url amount].freeze # :nodoc:
35
+ #
36
+ # 小程序下单
37
+ #
38
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_3.shtml
39
+ #
40
+ # Example:
41
+ #
42
+ # ```
43
+ # params = {
44
+ # description: 'pay',
45
+ # out_trade_no: 'Order Number',
46
+ # payer: {
47
+ # sp_openid: 'wechat open id'
48
+ # },
49
+ # amount: {
50
+ # total: 10
51
+ # },
52
+ # sub_mchid: 'Your sub mchid',
53
+ # notify_url: 'the url'
54
+ # }
55
+ #
56
+ # WechatPay::Ecommerce.invoke_transactions_in_miniprogram(params)
57
+ # ```
58
+ def invoke_transactions_in_miniprogram(params)
59
+ transactions_method_by_suffix('jsapi', params)
60
+ end
61
+
62
+ INVOKE_TRANSACTIONS_IN_APP_FIELDS = %i[sub_mchid description out_trade_no notify_url amount].freeze # :nodoc:
63
+ #
64
+ # App下单
65
+ #
66
+ # https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_1.shtml
67
+ #
68
+ # Example:
69
+ #
70
+ # ```
71
+ # params = {
72
+ # description: 'pay',
73
+ # out_trade_no: 'Order Number',
74
+ # payer: {
75
+ # sp_openid: 'wechat open id'
76
+ # },
77
+ # amount: {
78
+ # total: 10
79
+ # },
80
+ # sub_mchid: 'Your sub mchid',
81
+ # notify_url: 'the url'
82
+ # }
83
+ #
84
+ # WechatPay::Ecommerce.invoke_transactions_in_miniprogram(params)
85
+ # ```
86
+ def invoke_transactions_in_app(params)
87
+ transactions_method_by_suffix('app', params)
88
+ end
89
+
90
+ INVOKE_TRANSACTIONS_IN_H5_FIELDS = %i[sub_mchid description out_trade_no notify_url amount].freeze # :nodoc:
91
+ #
92
+ # h5下单
93
+ #
94
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_4.shtml
95
+ #
96
+ # Example:
97
+ #
98
+ # ``` ruby
99
+ # params = {
100
+ # description: 'pay',
101
+ # out_trade_no: 'Order Number',
102
+ # payer: {
103
+ # sp_openid: 'wechat open id'
104
+ # },
105
+ # amount: {
106
+ # total: 10
107
+ # },
108
+ # sub_mchid: 'Your sub mchid',
109
+ # notify_url: 'the url'
110
+ # }
111
+ #
112
+ # WechatPay::Ecommerce.invoke_transactions_in_h5(params)
113
+ # ```
114
+ def invoke_transactions_in_h5(params)
115
+ transactions_method_by_suffix('h5', params)
116
+ end
117
+
118
+ QUERY_ORDER_FIELDS = %i[sub_mchid out_trade_no transaction_id].freeze # :nodoc:
119
+ #
120
+ # 订单查询
121
+ #
122
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_5.shtml
123
+ #
124
+ # ``` ruby
125
+ # WechatPay::Ecommerce.query_order(sub_mchid: '16000008', transaction_id: '4323400972202104305133344444') # by transaction_id
126
+ # WechatPay::Ecommerce.query_order(sub_mchid: '16000008', out_trade_no: 'N202104302474') # by out_trade_no
127
+ # ```
128
+ #
129
+ def query_order(params)
130
+ if params[:transaction_id]
131
+ params.delete(:out_trade_no)
132
+ transaction_id = params.delete(:transaction_id)
133
+ path = "/v3/pay/partner/transactions/id/#{transaction_id}"
134
+ else
135
+ params.delete(:transaction_id)
136
+ out_trade_no = params.delete(:out_trade_no)
137
+ path = "/v3/pay/partner/transactions/out-trade-no/#{out_trade_no}"
138
+ end
139
+
140
+ params = params.merge({
141
+ sp_mchid: WechatPay.mch_id
142
+ })
143
+
144
+ method = 'GET'
145
+ query = build_query(params)
146
+ url = "#{path}?#{query}"
147
+
148
+ make_request(
149
+ method: method,
150
+ path: url,
151
+ extra_headers: {
152
+ 'Content-Type' => 'application/x-www-form-urlencoded'
153
+ }
154
+ )
155
+ end
156
+
157
+ CLOSE_ORDER_FIELDS = %i[sub_mchid out_trade_no].freeze # :nodoc:
158
+ #
159
+ # 关闭订单
160
+ #
161
+ # Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_6.shtml
162
+ #
163
+ # ``` ruby
164
+ # WechatPay::Ecommerce.close_order(sub_mchid: '16000008', out_trade_no: 'N3344445')
165
+ # ```
166
+ #
167
+ def close_order(params)
168
+ out_trade_no = params.delete(:out_trade_no)
169
+ url = "/v3/pay/partner/transactions/out-trade-no/#{out_trade_no}/close"
170
+ params = params.merge({
171
+ sp_mchid: WechatPay.mch_id
172
+ })
173
+
174
+ method = 'POST'
175
+
176
+ make_request(
177
+ method: method,
178
+ path: url,
179
+ for_sign: params.to_json,
180
+ payload: params.to_json
181
+ )
182
+ end
183
+
184
+ private
185
+
186
+ def transactions_method_by_suffix(suffix, params)
187
+ url = "/v3/pay/partner/transactions/#{suffix}"
188
+ method = 'POST'
189
+
190
+ params = params.merge({
191
+ sp_appid: WechatPay.app_id,
192
+ sp_mchid: WechatPay.mch_id
193
+ })
194
+
195
+ payload_json = params.to_json
196
+
197
+ make_request(
198
+ method: method,
199
+ path: url,
200
+ for_sign: payload_json,
201
+ payload: payload_json
202
+ )
203
+ end
204
+ end
205
+ end
206
+ end