Class: WechatPayment::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat_payment/client.rb

Constant Summary collapse

GATEWAY_URL =
'https://api.mch.weixin.qq.com'.freeze
ORDER_REQUIRED_FIELD =
[:out_trade_no, :spbill_create_ip, :body, :total_fee, :openid]
REFUND_REQUIRED_PARAMS =
[:total_fee, :refund_fee, :out_trade_no, :out_refund_no]
GENERATE_JS_PAY_REQ_REQUIRED_FIELDS =
[:prepayid, :noncestr]
INVOKE_UNIFIEDORDER_REQUIRED_FIELDS =
[:body, :out_trade_no, :total_fee, :spbill_create_ip, :notify_url, :trade_type]
INVOKE_REFUND_REQUIRED_FIELDS =
[:out_refund_no, :total_fee, :refund_fee, :op_user_id]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt_refund_notify(decrypted_data) ⇒ Object

解密微信退款回调信息

result = Hash.from_xml(request.body.read)

data = WechatPayment::Service.decrypt_refund_notify(result)



202
203
204
205
206
207
208
# File 'lib/wechat_payment/client.rb', line 202

def self.decrypt_refund_notify(decrypted_data)
  aes = OpenSSL::Cipher::AES.new('256-ECB')
  aes.decrypt
  aes.key = Digest::MD5.hexdigest(WechatPayment.key)
  result = aes.update(Base64.decode64(decrypted_data)) + aes.final
  Hash.from_xml(result)["root"]
end

.gen_js_pay_payload(order_result, options = {}) ⇒ Object

生成下单成功后返回给前端拉起微信支付的数据结构



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wechat_payment/client.rb', line 108

def self.gen_js_pay_payload(order_result, options = {})
  payment_params = {
    appId: WechatPayment.sub_appid || WechatPayment.appid,
    package: "prepay_id=#{order_result["prepay_id"]}",
    key: options.delete(:key) || WechatPayment.key,
    nonceStr: SecureRandom.hex(16),
    timeStamp: Time.now.to_i.to_s,
    signType: 'MD5'
  }

  payment_params[:paySign] = WechatPayment::Sign.generate(payment_params)

  payment_params
end

.generate_js_pay_req(params, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wechat_payment/client.rb', line 124

def self.generate_js_pay_req(params, options = {})
  check_required_options(params, GENERATE_JS_PAY_REQ_REQUIRED_FIELDS)

  params = {
    appId: options.delete(:appid) || WechatPayment.appid,
    package: "prepay_id=#{params.delete(:prepayid)}",
    key: options.delete(:key) || WechatPayment.key,
    nonceStr: params.delete(:noncestr),
    timeStamp: Time.now.to_i.to_s,
    signType: 'MD5'
  }.merge(params)

  params[:paySign] = WechatPayment::Sign.generate(params)
  params
end

.handle_payment_notify(notify_data) ⇒ Object

处理支付回调



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wechat_payment/client.rb', line 68

def self.handle_payment_notify(notify_data)
  if !WechatPayment::Sign.verify?(notify_data)
    message = "回调签名验证失败"
    WechatPayment::PaymentLogger.error { { message: , error: notify_data }.to_json }
    WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :validate_sign_failed)
  end

  result = WechatPayment::InvokeResult.new(notify_data)
  if result.success?
    message = "支付执行成功"
    log_content = { message:, callback: notify_data }.to_json
    WechatPayment::PaymentLogger.info { log_content }
    WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :payment_exec_success)
  else
    message = "支付执行失败"
    log_content = { message:, callback: notify_data }.to_json
    WechatPayment::PaymentLogger.error { log_content }
    WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :payment_exec_failed)
  end
end

.handle_refund_notify(encrypted_notify_data) ⇒ Object

处理退款回调



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wechat_payment/client.rb', line 90

def self.handle_refund_notify(encrypted_notify_data)
  notify_data = decrypt_refund_notify(encrypted_notify_data)

  result = WechatPayment::InvokeResult.new(notify_data)
  if result.success?
    message = "退款执行成功"
    log_content = { message:, callback: notify_data }.to_json
    WechatPayment::RefundLogger.info { log_content }
    WechatPayment::SuccessResult.new(data: notify_data, message:, message_kind: :refund_exec_success)
  else
    message = "退款执行失败"
    log_content = { message:, callback: notify_data}.to_json
    WechatPayment::RefundLogger.error { log_content }
    WechatPayment::FailureResult.new(error: notify_data, message:, message_kind: :refund_exec_failed)
  end
end

Instance Method Details

#check_required_options(options, names) ⇒ Object



215
216
217
218
219
# File 'lib/wechat_payment/client.rb', line 215

def check_required_options(options, names)
  names.each do |name|
    warn("WechatPayment Warn: missing required option: #{name}") unless options.has_key?(name)
  end
end

#invoke_refund(params, options = {}) {|result| ... } ⇒ Object

out_trade_no 和 transaction_id 是二选一(必填)

Yields:

  • (result)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/wechat_payment/client.rb', line 165

def invoke_refund(params, options = {})
  params = {
    appid: options.delete(:appid) || WechatPayment.appid,
    mch_id: options.delete(:mch_id) || WechatPayment.mch_id,
    key: options.delete(:key) || WechatPayment.key,
    nonce_str: SecureRandom.uuid.tr('-', ''),
  }.merge(params)

  params[:op_user_id] ||= params[:mch_id]

  check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)

  if ([:out_trade_no, :transaction_id] & params.keys) == []
    warn("WechatPayment Warn: missing required option: out_trade_no or transaction_id must have one")
  end

  options = {
    cert: options.delete(:apiclient_cert) || WechatPayment.apiclient_cert,
    key: options.delete(:apiclient_key) || WechatPayment.apiclient_key,
    verify_mode: OpenSSL::SSL::VERIFY_NONE
  }.merge(options)

  result = WechatPayment::InvokeResult.new(
    Hash.from_xml(
      invoke_remote("/secapi/pay/refund", make_payload(params), options)
    )
  )

  yield result if block_given?
  result
end

#invoke_remote(url, payload, options = {}) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/wechat_payment/client.rb', line 221

def invoke_remote(url, payload, options = {})
  uri = URI("#{GATEWAY_URL}#{url}")

  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/xml'

  options = {
    use_ssl: true
  }.merge(options)

  res = Net::HTTP.start(uri.hostname, uri.port, **options) do |http|
    http.use_ssl = true
    http.request(req, payload)
  end

  res.body
end

#invoke_unifiedorder(params, options = {}) {|result| ... } ⇒ Object

Yields:

  • (result)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/wechat_payment/client.rb', line 141

def invoke_unifiedorder(params, options = {})
  params = {
    appid: options.delete(:appid) || WechatPayment.appid,
    mch_id: options.delete(:mch_id) || WechatPayment.mch_id,
    key: options.delete(:key) || WechatPayment.key,
    nonce_str: SecureRandom.uuid.tr('-', '')
  }.merge(params)

  check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)

  result = WechatPayment::InvokeResult.new(
    Hash.from_xml(
      invoke_remote("/pay/unifiedorder", make_payload(params), options)
    )
  )

  yield result if block_given?

  result
end

#make_payload(params, sign_type = WechatPayment::Sign::SIGN_TYPE_MD5) ⇒ Object



210
211
212
213
# File 'lib/wechat_payment/client.rb', line 210

def make_payload(params, sign_type = WechatPayment::Sign::SIGN_TYPE_MD5)
  sign = WechatPayment::Sign.generate(params, sign_type)
  "<xml>#{params.except(:key).sort.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
end

#order(order_params) ⇒ Object

下单



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wechat_payment/client.rb', line 8

def order(order_params)
  check_required_key!(order_params, ORDER_REQUIRED_FIELD)

  order_params.merge!(**WechatPayment.as_payment_params,
                      trade_type: :JSAPI,
                      notify_url: payment_notify_url)

  # 如果是服务商模式(根据参数中有没有 sub_appid 判断),就把 openid 换成 sub_openid
  if order_params[:sub_appid]
    order_params[:sub_openid] = order_params.delete(:openid)
  end

  order_result = invoke_unifiedorder(order_params)

  if order_result.success?
    message = "发起支付成功"
    log_content = { message:, params: order_params, result: order_result }.to_json
    WechatPayment::PaymentLogger.info { log_content }
    WechatPayment::SuccessResult.new(data: order_result, message:, message_kind: :payment_apply_success)
  else
    message = "发起支付失败"
    log_content = { message: , params: order_params, result: order_result }.to_json
    WechatPayment::PaymentLogger.error { log_content }
    WechatPayment::FailureResult.new(error: order_result, message:, message_kind: :payment_apply_failed)
  end
end

#payment_notify_urlObject

支付回调地址



36
37
38
# File 'lib/wechat_payment/client.rb', line 36

def payment_notify_url
  ENV["WECHAT_PAYMENT_NOTIFY_URL"] || "#{WechatPayment.host}/wechat_payment/callback/payment"
end

#refund(refund_params) ⇒ Object

退款



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wechat_payment/client.rb', line 42

def refund(refund_params)
  check_required_key!(refund_params, REFUND_REQUIRED_PARAMS)

  refund_params.merge!(**WechatPayment.as_payment_params, notify_url: refund_notify_url)

  refund_result = invoke_refund(refund_params.to_options)

  if refund_result.success?
    message = '发起退款成功'
    log_content = { message:, params: refund_params, result: refund_result}.to_json
    WechatPayment::RefundLogger.info { log_content }
    WechatPayment::SuccessResult.new(data: refund_result, message:, message_kind: :refund_apply_success)
  else
    message = "发起退款失败"
    log_content = { message:, params: refund_params, result: refund_result }.to_json
    WechatPayment::RefundLogger.error { log_content }
    WechatPayment::FailureResult.new(error: refund_result, message:, message_kind: :refund_apply_failed)
  end
end

#refund_notify_urlObject

退款回调地址



63
64
65
# File 'lib/wechat_payment/client.rb', line 63

def refund_notify_url
  ENV["WECHAT_REFUND_NOTIFY_URL"] || "#{WechatPayment.host}/wechat_payment/callback/refund"
end