wechat_payment 2.0.12 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/wechat_payment/payment_orders_controller.rb +1 -1
- data/app/models/wechat_payment/payment_order.rb +1 -1
- data/config/locales/zh-CN.yml +15 -0
- data/lib/generators/wechat_payment/install/templates/initializer.rb +3 -0
- data/lib/wechat_payment/client.rb +32 -61
- data/lib/wechat_payment/concern/goods.rb +9 -8
- data/lib/wechat_payment/engine.rb +1 -0
- data/lib/wechat_payment/failure_result.rb +7 -0
- data/lib/wechat_payment/logger.rb +14 -0
- data/lib/wechat_payment/payment_logger.rb +7 -0
- data/lib/wechat_payment/refund_logger.rb +6 -0
- data/lib/wechat_payment/service_result.rb +21 -19
- data/lib/wechat_payment/success_result.rb +6 -0
- data/lib/wechat_payment/version.rb +1 -1
- data/lib/wechat_payment.rb +1 -1
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c976e72ae7aec4d893bb8682f324f69759ee2c42a62efa19d7363931798c21bb
|
4
|
+
data.tar.gz: 1069c1c332dc0253a3e57b9c3301ff293e6accbdb279791892623d09db23d361
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09eb12bc79a0dbd83e6dba6c3f2d67becd956b28665013e77ca30bfddd3ec51be79b6473b287096b7c1fe055014e532b8fb3133af146575c06190f56dbacfdcd'
|
7
|
+
data.tar.gz: 12228e2ea147aca00e1ee29e25c6d30956e4519fd98645cbec05f1de6e3057ae0d8a77ccac6eac6cae9ebb50f14bd88334f50a1e2ae43e2be6f4da3865d09612
|
@@ -28,7 +28,7 @@ module WechatPayment
|
|
28
28
|
# 生成交易编号
|
29
29
|
def gen_out_trade_no
|
30
30
|
loop do
|
31
|
-
out_trade_no = "#{Time.current.to_i}#{SecureRandom.random_number(999_999_999)}"
|
31
|
+
out_trade_no = "#{WechatPayment.order_no_prefix}#{Time.current.to_i}#{SecureRandom.random_number(999_999_999)}"
|
32
32
|
records_count = WechatPayment::PaymentOrder.where(out_trade_no: out_trade_no).count
|
33
33
|
if records_count == 0
|
34
34
|
self.out_trade_no = out_trade_no
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
"zh-CN":
|
3
|
+
wechat_payment_service_result:
|
4
|
+
message:
|
5
|
+
invoke_unifiedorder_success: 统一支付预支付成功
|
6
|
+
invoke_unifiedorder_failed: 统一支付预支付失败
|
7
|
+
payment_apply_success: 支付发起成功
|
8
|
+
payment_apply_failed: 支付发起失败
|
9
|
+
payment_exec_success: 支付执行成功
|
10
|
+
payment_exec_failed: 支付执行失败
|
11
|
+
refund_apply_success: 退款发起成功
|
12
|
+
refund_apply_failed: 退款发起失败
|
13
|
+
refund_exec_success: 退款执行成功
|
14
|
+
refund_exec_failed: 退款执行失败
|
15
|
+
validate_sign_failed: 支付回调签名验证失败
|
@@ -3,23 +3,6 @@ module WechatPayment
|
|
3
3
|
class Client
|
4
4
|
GATEWAY_URL = 'https://api.mch.weixin.qq.com'.freeze
|
5
5
|
|
6
|
-
def initialize # (merchant = WechatPayment)
|
7
|
-
# required_attrs = [:appid, :mch_id, :key, :app_secret, :cert_path]
|
8
|
-
# missing_attrs = required_attrs.reject { |attr| merchant.respond_to?(attr) }
|
9
|
-
# if missing_attrs.present?
|
10
|
-
# raise Exceptions::MerchantMissingAttr.new("Missing attributes: #{missing_attrs}, merchant target must respond to: appid, mch_id, key, appsecret, cert_path")
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# @merchant = merchant
|
14
|
-
# cert_path = Rails.root.join(merchant.cert_path)
|
15
|
-
#
|
16
|
-
# WechatPayment.appid = merchant.appid
|
17
|
-
# WechatPayment.key = merchant.key
|
18
|
-
# WechatPayment.mch_id = merchant.mch_id
|
19
|
-
# WechatPayment.appsecret = merchant.app_secret
|
20
|
-
# WechatPayment.set_apiclient_by_pkcs12(File.binread(cert_path), merchant.mch_id)
|
21
|
-
end
|
22
|
-
|
23
6
|
ORDER_REQUIRED_FIELD = [:out_trade_no, :spbill_create_ip, :body, :total_fee, :openid]
|
24
7
|
# 下单
|
25
8
|
def order(order_params)
|
@@ -37,12 +20,13 @@ module WechatPayment
|
|
37
20
|
order_result = invoke_unifiedorder(order_params)
|
38
21
|
|
39
22
|
if order_result.success?
|
40
|
-
|
41
|
-
|
42
|
-
WechatPayment::
|
23
|
+
message = "发起支付成功"
|
24
|
+
WechatPayment::PaymentLogger.info { "{ msg: '#{message}', params: #{order_params}, result: #{order_result} }" }
|
25
|
+
WechatPayment::SuccessResult.new(data: order_result, message:, message_kind: :payment_apply_success)
|
43
26
|
else
|
44
|
-
|
45
|
-
WechatPayment::
|
27
|
+
message = "发起支付失败"
|
28
|
+
WechatPayment::PaymentLogger.error { "{ msg: '#{message}', params: #{order_params}, result: #{order_result} }" }
|
29
|
+
WechatPayment::FailureResult.new(error: order_result, message:, message_kind: :payment_apply_failed)
|
46
30
|
end
|
47
31
|
end
|
48
32
|
|
@@ -61,11 +45,13 @@ module WechatPayment
|
|
61
45
|
refund_result = invoke_refund(refund_params.to_options)
|
62
46
|
|
63
47
|
if refund_result.success?
|
64
|
-
|
65
|
-
WechatPayment::
|
48
|
+
message = '发起退款成功'
|
49
|
+
WechatPayment::RefundLogger.info { "{ msg: '#{message}', params: #{refund_params}, result: #{refund_result}" }
|
50
|
+
WechatPayment::SuccessResult.new(data: refund_result, message:, message_kind: :refund_apply_success)
|
66
51
|
else
|
67
|
-
|
68
|
-
WechatPayment::
|
52
|
+
message = "发起退款失败"
|
53
|
+
WechatPayment::RefundLogger.error { "{ msg: '#{message}', params: #{refund_params}, result: #{refund_result}" }
|
54
|
+
WechatPayment::FailureResult.new(error: refund_result, message:, message_kind: :refund_apply_failed)
|
69
55
|
end
|
70
56
|
end
|
71
57
|
|
@@ -77,18 +63,20 @@ module WechatPayment
|
|
77
63
|
# 处理支付回调
|
78
64
|
def self.handle_payment_notify(notify_data)
|
79
65
|
if !WechatPayment::Sign.verify?(notify_data)
|
80
|
-
|
81
|
-
WechatPayment::
|
66
|
+
message = "回调签名验证失败"
|
67
|
+
WechatPayment::PaymentLogger.error { "{ msg: '#{message}', error: #{notify_data} }" }
|
68
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :validate_sign_failed)
|
82
69
|
end
|
83
70
|
|
84
71
|
result = WechatPayment::InvokeResult.new(notify_data)
|
85
|
-
|
86
72
|
if result.success?
|
87
|
-
|
88
|
-
WechatPayment::
|
73
|
+
message = "支付执行成功"
|
74
|
+
WechatPayment::PaymentLogger.info { "{ msg: '#{message}', callback: #{notify_data} }" }
|
75
|
+
WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :payment_exec_success)
|
89
76
|
else
|
90
|
-
|
91
|
-
WechatPayment::
|
77
|
+
message = "支付执行失败"
|
78
|
+
WechatPayment::PaymentLogger.error{ "{ msg: '#{message}', callback: #{notify_data} }" }
|
79
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :payment_exec_failed)
|
92
80
|
end
|
93
81
|
end
|
94
82
|
|
@@ -98,11 +86,13 @@ module WechatPayment
|
|
98
86
|
|
99
87
|
result = WechatPayment::InvokeResult.new(notify_data)
|
100
88
|
if result.success?
|
101
|
-
|
102
|
-
WechatPayment::
|
89
|
+
message = "退款执行成功"
|
90
|
+
WechatPayment::RefundLogger.info { "{ msg: '#{message}', callback: #{notify_data} }" }
|
91
|
+
WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :refund_exec_success)
|
103
92
|
else
|
104
|
-
|
105
|
-
WechatPayment::
|
93
|
+
message = "退款执行失败"
|
94
|
+
WechatPayment::RefundLogger.error { "{ msg: '#{message}', callback: #{notify_data} }" }
|
95
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :refund_exec_failed)
|
106
96
|
end
|
107
97
|
end
|
108
98
|
|
@@ -139,7 +129,6 @@ module WechatPayment
|
|
139
129
|
params
|
140
130
|
end
|
141
131
|
|
142
|
-
|
143
132
|
INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = [:body, :out_trade_no, :total_fee, :spbill_create_ip, :notify_url, :trade_type]
|
144
133
|
def invoke_unifiedorder(params, options = {})
|
145
134
|
params = {
|
@@ -176,7 +165,10 @@ module WechatPayment
|
|
176
165
|
params[:op_user_id] ||= params[:mch_id]
|
177
166
|
|
178
167
|
check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
|
179
|
-
|
168
|
+
|
169
|
+
if ([:out_trade_no, :transaction_id] & params.keys) == []
|
170
|
+
warn("WechatPayment Warn: missing required option: out_trade_no or transaction_id must have one")
|
171
|
+
end
|
180
172
|
|
181
173
|
options = {
|
182
174
|
cert: options.delete(:apiclient_cert) || WechatPayment.apiclient_cert,
|
@@ -191,7 +183,6 @@ module WechatPayment
|
|
191
183
|
)
|
192
184
|
|
193
185
|
yield result if block_given?
|
194
|
-
|
195
186
|
result
|
196
187
|
end
|
197
188
|
|
@@ -237,7 +228,6 @@ module WechatPayment
|
|
237
228
|
res.body
|
238
229
|
end
|
239
230
|
|
240
|
-
|
241
231
|
private
|
242
232
|
|
243
233
|
# 判断 hash 是否缺少 key
|
@@ -248,24 +238,5 @@ module WechatPayment
|
|
248
238
|
raise WechatPayment::MissingKeyError.new("Parameter missing keys: #{lack_of_keys}")
|
249
239
|
end
|
250
240
|
end
|
251
|
-
|
252
|
-
# 支付日志
|
253
|
-
def payment_logger
|
254
|
-
WechatPayment::Client.payment_logger
|
255
|
-
end
|
256
|
-
|
257
|
-
# 退款日志
|
258
|
-
def refund_logger
|
259
|
-
WechatPayment::Client.refund_logger
|
260
|
-
end
|
261
|
-
|
262
|
-
def self.payment_logger
|
263
|
-
@payment_logger ||= WechatPayment::RLogger.make("wx_payment")
|
264
|
-
end
|
265
|
-
|
266
|
-
def self.refund_logger
|
267
|
-
@refund_logger ||= WechatPayment::RLogger.make("wx_refund")
|
268
|
-
end
|
269
|
-
|
270
241
|
end
|
271
|
-
end
|
242
|
+
end
|
@@ -42,10 +42,9 @@ module WechatPayment
|
|
42
42
|
)
|
43
43
|
|
44
44
|
unless user_goods.save
|
45
|
-
return WechatPayment::
|
46
|
-
error: user_goods.errors,
|
45
|
+
return WechatPayment::FailureResult.new(error: user_goods.error,
|
47
46
|
message: "商品中间表 #{user_goods_model.table_name} 插入数据失败",
|
48
|
-
|
47
|
+
message_kind: :create_user_goods_failed)
|
49
48
|
end
|
50
49
|
|
51
50
|
payment_order = user_goods.payment_orders.new(
|
@@ -55,11 +54,13 @@ module WechatPayment
|
|
55
54
|
customer: user
|
56
55
|
)
|
57
56
|
|
58
|
-
|
59
|
-
|
57
|
+
if payment_order.save
|
58
|
+
message = "支付订单创建成功"
|
59
|
+
WechatPayment::SuccessResult.new(data: payment_order, message:, message_kind: :create_payment_order_success)
|
60
|
+
else
|
61
|
+
message = "支付订单创建失败"
|
62
|
+
WechatPayment::FailureResult.new(error: user_goods.error, message:, message_kind: :create_payment_order_failed)
|
60
63
|
end
|
61
|
-
|
62
|
-
WechatPayment::ServiceResult.new(success: true, data: payment_order, message: "支付订单创建成功")
|
63
64
|
end
|
64
65
|
|
65
66
|
# 重新支付,应用场景是: 用户取消了支付后,使用最后一张订单进行支付
|
@@ -92,4 +93,4 @@ module WechatPayment
|
|
92
93
|
end
|
93
94
|
end
|
94
95
|
end
|
95
|
-
end
|
96
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
|
2
|
+
class WechatPayment::FailureResult < WechatPayment::ServiceResult
|
3
|
+
def initialize(data: {}, error: nil, message: nil, message_kind: nil, message_type: nil)
|
4
|
+
super(success: false, data: data, error: error, message: message, message_kind: message_kind, message_type: message_type)
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
class WechatPayment::Logger
|
3
|
+
%w{ info error warn fatal }.each do |level|
|
4
|
+
define_singleton_method level do |content = "", &block|
|
5
|
+
Rails.logger.tagged *self.tags do
|
6
|
+
if block.present?
|
7
|
+
Rails.logger.send(level, &block)
|
8
|
+
else
|
9
|
+
Rails.logger.send(level, content)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -4,27 +4,22 @@ module WechatPayment
|
|
4
4
|
:error,
|
5
5
|
:data,
|
6
6
|
:message,
|
7
|
-
:
|
8
|
-
:
|
7
|
+
:message_kind,
|
8
|
+
:message_type
|
9
9
|
|
10
10
|
def initialize(success: false,
|
11
|
+
data: {},
|
11
12
|
error: nil,
|
12
13
|
message: nil,
|
13
|
-
|
14
|
-
|
15
|
-
error_type: nil)
|
16
|
-
self.success = success
|
17
|
-
|
18
|
-
self.data = data.presence || {}
|
19
|
-
|
20
|
-
if self.data.is_a? Hash
|
21
|
-
self.data = self.data.with_indifferent_access
|
22
|
-
end
|
14
|
+
message_kind: nil,
|
15
|
+
message_type: nil)
|
23
16
|
|
17
|
+
self.success = success
|
18
|
+
self.data = data
|
24
19
|
self.error = error
|
25
|
-
|
26
20
|
self.message = message
|
27
21
|
self.message_type = message_type
|
22
|
+
self.message_kind = message_kind
|
28
23
|
end
|
29
24
|
|
30
25
|
alias success? :success
|
@@ -41,9 +36,9 @@ module WechatPayment
|
|
41
36
|
yield(self) if failure?
|
42
37
|
end
|
43
38
|
|
44
|
-
def
|
45
|
-
if message_type.present?
|
46
|
-
message_type.to_sym
|
39
|
+
def message_type
|
40
|
+
if @message_type.present?
|
41
|
+
@message_type.to_sym
|
47
42
|
elsif success?
|
48
43
|
:info
|
49
44
|
else
|
@@ -51,15 +46,22 @@ module WechatPayment
|
|
51
46
|
end
|
52
47
|
end
|
53
48
|
|
49
|
+
def message_kind_prefix
|
50
|
+
"wechat_payment_"
|
51
|
+
end
|
52
|
+
|
53
|
+
def message_kind
|
54
|
+
"#{message_kind_prefix}#{@message_kind}"
|
55
|
+
end
|
56
|
+
|
54
57
|
def as_json(options = {})
|
55
|
-
# data.as_json(options)
|
56
58
|
{
|
57
59
|
success: success,
|
58
60
|
data: data,
|
59
61
|
message: message,
|
60
|
-
message_type: get_message_type,
|
61
62
|
error: error,
|
62
|
-
|
63
|
+
message_kind: message_kind,
|
64
|
+
message_type: message_type
|
63
65
|
}
|
64
66
|
end
|
65
67
|
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
|
2
|
+
class WechatPayment::SuccessResult < WechatPayment::ServiceResult
|
3
|
+
def initialize(data: {}, error: nil, message: nil, message_kind: nil, message_type: nil)
|
4
|
+
super(success: true, data: data, error: error, message: message, message_kind: message_kind, message_type: message_type)
|
5
|
+
end
|
6
|
+
end
|
data/lib/wechat_payment.rb
CHANGED
@@ -5,7 +5,7 @@ module WechatPayment
|
|
5
5
|
|
6
6
|
class << self
|
7
7
|
attr_reader :apiclient_cert, :apiclient_key
|
8
|
-
attr_accessor :appid, :app_secret, :mch_id, :sub_appid, :sub_app_secret, :sub_mch_id, :key, :cert_path, :host
|
8
|
+
attr_accessor :appid, :app_secret, :mch_id, :sub_appid, :sub_app_secret, :sub_mch_id, :key, :cert_path, :host, :order_no_prefix
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.setup
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wechat_payment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- app/models/wechat_payment/refund_order.rb
|
76
76
|
- app/services/wechat_payment/service.rb
|
77
77
|
- app/views/layouts/wechat_payment/application.html.erb
|
78
|
+
- config/locales/zh-CN.yml
|
78
79
|
- config/routes.rb
|
79
80
|
- db/migrate/20210706075217_create_wechat_payment_payment_orders.rb
|
80
81
|
- db/migrate/20210706095205_create_wechat_payment_refund_orders.rb
|
@@ -92,11 +93,16 @@ files:
|
|
92
93
|
- lib/wechat_payment/concern/user.rb
|
93
94
|
- lib/wechat_payment/concern/user_goods.rb
|
94
95
|
- lib/wechat_payment/engine.rb
|
96
|
+
- lib/wechat_payment/failure_result.rb
|
95
97
|
- lib/wechat_payment/invoke_result.rb
|
98
|
+
- lib/wechat_payment/logger.rb
|
96
99
|
- lib/wechat_payment/missing_key_error.rb
|
100
|
+
- lib/wechat_payment/payment_logger.rb
|
97
101
|
- lib/wechat_payment/r_logger.rb
|
102
|
+
- lib/wechat_payment/refund_logger.rb
|
98
103
|
- lib/wechat_payment/service_result.rb
|
99
104
|
- lib/wechat_payment/sign.rb
|
105
|
+
- lib/wechat_payment/success_result.rb
|
100
106
|
- lib/wechat_payment/version.rb
|
101
107
|
homepage: http://dev.com
|
102
108
|
licenses:
|
@@ -120,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
126
|
- !ruby/object:Gem::Version
|
121
127
|
version: '0'
|
122
128
|
requirements: []
|
123
|
-
rubygems_version: 3.3.
|
129
|
+
rubygems_version: 3.3.7
|
124
130
|
signing_key:
|
125
131
|
specification_version: 4
|
126
132
|
summary: Summary of WechatPayment.
|