wechat_payment 2.0.14 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/zh-CN.yml +15 -0
- data/lib/wechat_payment/client.rb +40 -61
- data/lib/wechat_payment/concern/goods.rb +17 -8
- data/lib/wechat_payment/engine.rb +1 -0
- data/lib/wechat_payment/failure_result.rb +7 -0
- data/lib/wechat_payment/logger.rb +18 -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
- metadata +9 -4
- data/lib/wechat_payment/r_logger.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17e53d537ae37b1fcf5d6186579c35314f46dfa6a1c26a879b1935061c5864ea
|
4
|
+
data.tar.gz: bd1b81430ef7ac61d870ac013dd48b6ecad2fc1813a894f2a1b2b7a504914e6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e97fb758a760ba3d34e08b6a79820c233a1d70d7dd7d324bf8b59660cc27942bee800c26a5b95514e102e5dc325c945bc89cf954c53b3412f2e96cf74102b69e
|
7
|
+
data.tar.gz: 4eb5a89a9e5b25a23bb6530f2ac42eba6f04d631b46e0a055d70635efaa5ef6f78d153e4bd34df5bcf055c4107c02241f0d49b896dd4afb467bbb627ef1fbb1b
|
@@ -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,15 @@ 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
|
+
log_content = { message:, params: order_params, result: order_result }.to_json
|
25
|
+
WechatPayment::PaymentLogger.info { log_content }
|
26
|
+
WechatPayment::SuccessResult.new(data: order_result, message:, message_kind: :payment_apply_success)
|
43
27
|
else
|
44
|
-
|
45
|
-
|
28
|
+
message = "发起支付失败"
|
29
|
+
log_content = { message: , params: order_params, result: order_result }.to_json
|
30
|
+
WechatPayment::PaymentLogger.error { log_content }
|
31
|
+
WechatPayment::FailureResult.new(error: order_result, message:, message_kind: :payment_apply_failed)
|
46
32
|
end
|
47
33
|
end
|
48
34
|
|
@@ -61,11 +47,15 @@ module WechatPayment
|
|
61
47
|
refund_result = invoke_refund(refund_params.to_options)
|
62
48
|
|
63
49
|
if refund_result.success?
|
64
|
-
|
65
|
-
|
50
|
+
message = '发起退款成功'
|
51
|
+
log_content = { message:, params: refund_params, result: refund_result}.to_json
|
52
|
+
WechatPayment::RefundLogger.info { log_content }
|
53
|
+
WechatPayment::SuccessResult.new(data: refund_result, message:, message_kind: :refund_apply_success)
|
66
54
|
else
|
67
|
-
|
68
|
-
|
55
|
+
message = "发起退款失败"
|
56
|
+
log_content = { message:, params: refund_params, result: refund_result }.to_json
|
57
|
+
WechatPayment::RefundLogger.error { log_content }
|
58
|
+
WechatPayment::FailureResult.new(error: refund_result, message:, message_kind: :refund_apply_failed)
|
69
59
|
end
|
70
60
|
end
|
71
61
|
|
@@ -77,18 +67,22 @@ module WechatPayment
|
|
77
67
|
# 处理支付回调
|
78
68
|
def self.handle_payment_notify(notify_data)
|
79
69
|
if !WechatPayment::Sign.verify?(notify_data)
|
80
|
-
|
81
|
-
WechatPayment::
|
70
|
+
message = "回调签名验证失败"
|
71
|
+
WechatPayment::PaymentLogger.error { { message: , error: notify_data }.to_json }
|
72
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :validate_sign_failed)
|
82
73
|
end
|
83
74
|
|
84
75
|
result = WechatPayment::InvokeResult.new(notify_data)
|
85
|
-
|
86
76
|
if result.success?
|
87
|
-
|
88
|
-
|
77
|
+
message = "支付执行成功"
|
78
|
+
log_content = { message:, callback: notify_data }.to_json
|
79
|
+
WechatPayment::PaymentLogger.info { log_content }
|
80
|
+
WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :payment_exec_success)
|
89
81
|
else
|
90
|
-
|
91
|
-
|
82
|
+
message = "支付执行失败"
|
83
|
+
log_content = { message:, callback: notify_data }.to_json
|
84
|
+
WechatPayment::PaymentLogger.error { log_content }
|
85
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :payment_exec_failed)
|
92
86
|
end
|
93
87
|
end
|
94
88
|
|
@@ -98,11 +92,15 @@ module WechatPayment
|
|
98
92
|
|
99
93
|
result = WechatPayment::InvokeResult.new(notify_data)
|
100
94
|
if result.success?
|
101
|
-
|
102
|
-
|
95
|
+
message = "退款执行成功"
|
96
|
+
log_content = { message:, callback: notify_data }.to_json
|
97
|
+
WechatPayment::RefundLogger.info { log_content }
|
98
|
+
WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :refund_exec_success)
|
103
99
|
else
|
104
|
-
|
105
|
-
|
100
|
+
message = "退款执行失败"
|
101
|
+
log_content = { message:, callback: notify_data}.to_json
|
102
|
+
WechatPayment::RefundLogger.error { log_content }
|
103
|
+
WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :refund_exec_failed)
|
106
104
|
end
|
107
105
|
end
|
108
106
|
|
@@ -139,7 +137,6 @@ module WechatPayment
|
|
139
137
|
params
|
140
138
|
end
|
141
139
|
|
142
|
-
|
143
140
|
INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = [:body, :out_trade_no, :total_fee, :spbill_create_ip, :notify_url, :trade_type]
|
144
141
|
def invoke_unifiedorder(params, options = {})
|
145
142
|
params = {
|
@@ -176,7 +173,10 @@ module WechatPayment
|
|
176
173
|
params[:op_user_id] ||= params[:mch_id]
|
177
174
|
|
178
175
|
check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
|
179
|
-
|
176
|
+
|
177
|
+
if ([:out_trade_no, :transaction_id] & params.keys) == []
|
178
|
+
warn("WechatPayment Warn: missing required option: out_trade_no or transaction_id must have one")
|
179
|
+
end
|
180
180
|
|
181
181
|
options = {
|
182
182
|
cert: options.delete(:apiclient_cert) || WechatPayment.apiclient_cert,
|
@@ -191,7 +191,6 @@ module WechatPayment
|
|
191
191
|
)
|
192
192
|
|
193
193
|
yield result if block_given?
|
194
|
-
|
195
194
|
result
|
196
195
|
end
|
197
196
|
|
@@ -237,7 +236,6 @@ module WechatPayment
|
|
237
236
|
res.body
|
238
237
|
end
|
239
238
|
|
240
|
-
|
241
239
|
private
|
242
240
|
|
243
241
|
# 判断 hash 是否缺少 key
|
@@ -248,24 +246,5 @@ module WechatPayment
|
|
248
246
|
raise WechatPayment::MissingKeyError.new("Parameter missing keys: #{lack_of_keys}")
|
249
247
|
end
|
250
248
|
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
249
|
end
|
271
|
-
end
|
250
|
+
end
|
@@ -42,10 +42,15 @@ module WechatPayment
|
|
42
42
|
)
|
43
43
|
|
44
44
|
unless user_goods.save
|
45
|
-
|
46
|
-
|
45
|
+
log_content = {
|
46
|
+
message: "商品中间表 #{user_goods_model.table_name} 插入数据失败",
|
47
|
+
error: user_goods.errors.full_messages
|
48
|
+
}.to_json
|
49
|
+
|
50
|
+
WechatPayment::Logger.info { log_content }
|
51
|
+
return WechatPayment::FailureResult.new(error: user_goods.error,
|
47
52
|
message: "商品中间表 #{user_goods_model.table_name} 插入数据失败",
|
48
|
-
|
53
|
+
message_kind: :create_user_goods_failed)
|
49
54
|
end
|
50
55
|
|
51
56
|
payment_order = user_goods.payment_orders.new(
|
@@ -55,11 +60,15 @@ module WechatPayment
|
|
55
60
|
customer: user
|
56
61
|
)
|
57
62
|
|
58
|
-
|
59
|
-
|
63
|
+
if payment_order.save
|
64
|
+
message = "支付订单创建成功"
|
65
|
+
WechatPayment::SuccessResult.new(data: payment_order, message:, message_kind: :create_payment_order_success)
|
66
|
+
else
|
67
|
+
message = "支付订单创建失败"
|
68
|
+
log_content = { message:, error: payment_order.errors.full_messages }.to_json
|
69
|
+
WechatPayment::Logger.error { log_content }
|
70
|
+
WechatPayment::FailureResult.new(error: user_goods.error, message:, message_kind: :create_payment_order_failed)
|
60
71
|
end
|
61
|
-
|
62
|
-
WechatPayment::ServiceResult.new(success: true, data: payment_order, message: "支付订单创建成功")
|
63
72
|
end
|
64
73
|
|
65
74
|
# 重新支付,应用场景是: 用户取消了支付后,使用最后一张订单进行支付
|
@@ -92,4 +101,4 @@ module WechatPayment
|
|
92
101
|
end
|
93
102
|
end
|
94
103
|
end
|
95
|
-
end
|
104
|
+
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,18 @@
|
|
1
|
+
|
2
|
+
class WechatPayment::Logger
|
3
|
+
def self.tags
|
4
|
+
["WechatPayment"]
|
5
|
+
end
|
6
|
+
|
7
|
+
%w{ info error warn fatal }.each do |level|
|
8
|
+
define_singleton_method level do |content = "", &block|
|
9
|
+
Rails.logger.tagged *self.tags do
|
10
|
+
if block.present?
|
11
|
+
Rails.logger.send(level, &block)
|
12
|
+
else
|
13
|
+
Rails.logger.send(level, content)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
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 || {}).with_indifferent_access
|
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
|
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.
|
4
|
+
version: 2.1.2
|
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-17 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,15 @@ 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
|
97
|
-
- lib/wechat_payment/
|
100
|
+
- lib/wechat_payment/payment_logger.rb
|
101
|
+
- lib/wechat_payment/refund_logger.rb
|
98
102
|
- lib/wechat_payment/service_result.rb
|
99
103
|
- lib/wechat_payment/sign.rb
|
104
|
+
- lib/wechat_payment/success_result.rb
|
100
105
|
- lib/wechat_payment/version.rb
|
101
106
|
homepage: http://dev.com
|
102
107
|
licenses:
|
@@ -120,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
125
|
- !ruby/object:Gem::Version
|
121
126
|
version: '0'
|
122
127
|
requirements: []
|
123
|
-
rubygems_version: 3.3.
|
128
|
+
rubygems_version: 3.3.7
|
124
129
|
signing_key:
|
125
130
|
specification_version: 4
|
126
131
|
summary: Summary of WechatPayment.
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module WechatPayment
|
2
|
-
class RLogger
|
3
|
-
def initialize
|
4
|
-
raise Exceptions::InitializeDenied.new("please use 'ILogger.make' instead of 'ILogger.new'")
|
5
|
-
end
|
6
|
-
|
7
|
-
class << self
|
8
|
-
def make(log_file)
|
9
|
-
@logger ||= {}
|
10
|
-
|
11
|
-
log_file_name = if log_file.class.in? [String, Symbol]
|
12
|
-
log_file_name = log_file.to_sym
|
13
|
-
|
14
|
-
unless log_file_name.to_s.end_with? ".log"
|
15
|
-
log_file_name = "#{log_file_name}.log"
|
16
|
-
end
|
17
|
-
|
18
|
-
"#{root_path}/#{log_file_name}"
|
19
|
-
elsif log_file.respond_to? :to_path
|
20
|
-
log_file.to_path
|
21
|
-
else
|
22
|
-
raise Exceptions::UnsupportdParamType.new("log file parameter only support 'File' or 'String' Type.")
|
23
|
-
end
|
24
|
-
|
25
|
-
# 如果已经存在日志对象,则返回已有的日志对象
|
26
|
-
@logger[log_file_name] ||= ::Logger.new(log_file_name)
|
27
|
-
end
|
28
|
-
|
29
|
-
def root_path
|
30
|
-
@root ||= "#{Rails.root}/log"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|