wechat_payment 2.0.13 → 2.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f80eae6a4f7482e85fb59eb39fabf6a4d48b44c75020030f3df92e714af92dd
4
- data.tar.gz: f93e93c6aa13ad0bc7f175a21e0ae0042466f85e1add3ecfdc66d6f4d4945857
3
+ metadata.gz: d7869db809862db9e0d242d723f3ab309ce374b2f4e802e356f0191691b68ca8
4
+ data.tar.gz: f25452d31d64ce7f95c6351da70a58ebf8dd80a200ad3695a28e90ec1782613c
5
5
  SHA512:
6
- metadata.gz: d6a462950e6339c60ffe909b9e18e344e21fc26e0235510651269f8dbb099ddcef604bc0611023d0c36ab9e82e9a8d2dc4f5bb75dd2999a4ed3096308ff08cec
7
- data.tar.gz: f9a7f2d102f2bd681a3b408ffb4111a8e68c7ebca95472e8ed0487f299ccf4f1d432fdd5333a0a940553a13b4d35bcaf8ba9cacae3dcd237dbab0326fe2131e7
6
+ metadata.gz: 934dadb407e4d8befa5dbf6912d05f17e00f67fc22d9351df69848ff9b354e310dbea9a6577ad1091e92ab45428ee84f296248adf2bf7fa3f986f9673fa6aae6
7
+ data.tar.gz: 2f599903670717b7f8d35c6974087c6b3130a6417d44d28a045a617a611fc15a28f7d4357ce8f9512bffafcf9aeddb9a49090ccc6ed150cf19da65538256c676
@@ -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: 支付回调签名验证失败
@@ -16,4 +16,7 @@ WechatPayment.setup do |config|
16
16
  config.sub_appid = "wx8f9f912623456789"
17
17
  config.sub_mch_id = "1234911291"
18
18
  config.sub_app_secret = "88888231e2f3a21152d163f61b99999"
19
+
20
+ # 生成的订单编号前缀
21
+ # config.order_no_prefix = "wx_"
19
22
  end
@@ -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
- payment_logger.info("{params: #{order_params}, result: #{order_result}}")
42
- WechatPayment::ServiceResult.new(success: true, data: order_result.with_indifferent_access)
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
- payment_logger.error("{params: #{order_params}, result: #{order_result}}")
45
- WechatPayment::ServiceResult.new(success: false, error: order_result.with_indifferent_access)
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
- refund_logger.info "{params: #{refund_params}, result: #{refund_result}"
65
- WechatPayment::ServiceResult.new(success: true, data: refund_result)
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
- refund_logger.error "{params: #{refund_params}, result: #{refund_result}"
68
- WechatPayment::ServiceResult.new(success: false, error: refund_result)
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
- payment_logger.error("{msg: 签名验证失败, error: #{notify_data}}")
81
- WechatPayment::ServiceResult.new(success: false, error: notify_data, message: "回调签名验证失败")
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
- payment_logger.info("{callback: #{notify_data}}")
88
- WechatPayment::ServiceResult.new(success: true, data: notify_data, message: "支付执行成功", )
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
- payment_logger.error("{callback: #{notify_data}}")
91
- WechatPayment::ServiceResult.new(success: false, error: notify_data, message: "支付执行失败", error_type: :payment_exec_failed)
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
- refund_logger.info "退款执行成功{callback: #{notify_data}}"
102
- WechatPayment::ServiceResult.new(success: true, data: notify_data)
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
- refund_logger.error "退款执行失败: {callback: #{notify_data}}"
105
- WechatPayment::ServiceResult.new(success:false, error: notify_data, message: "退款回调失败")
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
- warn("WechatPayment Warn: missing required option: out_trade_no or transaction_id must have one") if ([:out_trade_no, :transaction_id] & params.keys) == []
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
- return WechatPayment::ServiceResult.new(success: false,
46
- error: user_goods.errors,
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
- error_type: :create_user_goods_failed)
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
- unless payment_order.save
59
- return WechatPayment::ServiceResult.new(success: false, error: user_goods.errors, message: "支付订单创建失败")
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
@@ -3,5 +3,6 @@ module WechatPayment
3
3
  isolate_namespace WechatPayment
4
4
 
5
5
  config.autoload_paths << "#{config.root}/lib"
6
+ config.i18n.default_locale = "zh-CN"
6
7
  end
7
8
  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
@@ -0,0 +1,7 @@
1
+ class WechatPayment::PaymentLogger < WechatPayment::Logger
2
+
3
+ def self.tags
4
+ ["WechatPayment", "Pay"]
5
+ end
6
+
7
+ end
@@ -0,0 +1,6 @@
1
+
2
+ class WechatPayment::RefundLogger < WechatPayment::Logger
3
+ def self.tags
4
+ ["WechatPayment", "Refund"]
5
+ end
6
+ end
@@ -4,27 +4,22 @@ module WechatPayment
4
4
  :error,
5
5
  :data,
6
6
  :message,
7
- :message_type,
8
- :error_type
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
- message_type: nil,
14
- data: nil,
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 get_message_type
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
- error_type: error_type
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
@@ -1,3 +1,3 @@
1
1
  module WechatPayment
2
- VERSION = '2.0.13'
2
+ VERSION = '2.1.1'
3
3
  end
@@ -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.13
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-22 00:00:00.000000000 Z
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.3
129
+ rubygems_version: 3.3.7
124
130
  signing_key:
125
131
  specification_version: 4
126
132
  summary: Summary of WechatPayment.