Skip to content

Instantly share code, notes, and snippets.

@xifengzhu
Last active September 14, 2017 08:53
Show Gist options
  • Save xifengzhu/00a19934e38120a3683d11303fdcfc53 to your computer and use it in GitHub Desktop.
Save xifengzhu/00a19934e38120a3683d11303fdcfc53 to your computer and use it in GitHub Desktop.
微信退款成功回调通知数据解密
def refund_notify
encrypt_result = Hash.from_xml(request.body.read)["xml"]
decrypt_result = Hash.from_xml(decrypt_data(encrypt_result['req_info']))['root']
# other logic code (decrypt_result['out_refund_no'])
render :xml => {return_code: "SUCCESS"}.to_xml(root: 'xml', dasherize: false)
end
private
# https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16
def decrypt_data(encrypted_base64)
encrypted = Base64.decode64(encrypted_base64)
decipher = OpenSSL::Cipher::AES256.new(:ECB)
decipher.decrypt
decipher.key = Digest::MD5.hexdigest(Rails.application.secrets.wechat[:mch_key])
data = decipher.update(encrypted) + decipher.final
data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment