微信h5支付python版

您所在的位置:网站首页 h5调用python 微信h5支付python版

微信h5支付python版

2024-07-04 21:29| 来源: 网络整理| 查看: 265

微信h5支付python版

最近闲暇,整理下微信支付,根据官方文档进行的。 官方配置文件这里就不多说了,

tornado感觉方便快捷,所以就用这个整理处理一份。经由django改编。 文档结构 在这里插入图片描述

进入、处理、回调需要配置3个url就能搞定了。

0、导包

import hashlib import uuid import re from tornado.httpserver import HTTPServer import tornado.web from tornado.ioloop import IOLoop import os import requests from urllib import request as reqt from tornado.options import define, parse_command_line, options from tornado.web import Application # md5 def get_token(md5str): m1 = hashlib.md5() m1.update(md5str.encode("utf-8")) token = m1.hexdigest() return token

1、进入r'/index', indexHandler),

class indexHandler(tornado.web.RequestHandler): def get(self): self.render("index.html")

index.html

微信Demo //用户点击跳转地址(非静默授权) 参数appid为公众号的id redirect_uri为微信回调接口 state为可携带的参数 // window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6ce0cd5c2d7b1299&redirect_uri=http://wanyou.abc6.net:22708/index2/mainServlet&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; window.location.href="/Notify_wx";

2.处理 /Notify_wx 需要准备的有,

mch_id body key host

body 要和index.html中title一值,我就在这里浪费了大半天。 金额等可以在index自行传参,后台处理

class Notify_wxHandler(tornado.web.RequestHandler): def get(self): total_fee = "20" # 金额 appid = 'appid' mch_id = 'mch_id' body = "微信Demo" key = '' host = "域名 " # 微信填写的备案域名 nonce_str = str(uuid.uuid4()).replace('-', "")[:-3] # 生成随机字符串 notify_url = 'http://{}/Notify_URL'.format(host) # 回调函数 wap_name = '腾讯充值' wap_name.encode('utf-8') scene_info = { "h5_info": {"type": "Wap", "wap_url": "http://{}/Notify_wx".format(host), "wap_name": wap_name}} out_trade_no = get_token(nonce_str) # md5 加密生成订单编号 ip = self.request.remote_ip # x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR') # print(self.request.META) # if x_forwarded_for: # spbill_create_ip = x_forwarded_for.split(',')[0] # 所以这里是真实的ip # else: # spbill_create_ip = self.request.META.get('REMOTE_ADDR') # 这里获得代理ip # 此处 为django获取ip的方法 total_amount = str(int(float(total_fee) * 100)) trade_type = 'MWEB' signA = "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&scene_info=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" % ( appid, body, mch_id, nonce_str, notify_url, out_trade_no, scene_info, ip, total_amount, trade_type) print(signA) strSignTmp = signA + "&key=" + key sign = get_token(strSignTmp).upper() # 进行MD5加密 print(sign) post_data = "" for i in (signA + "&sign=" + sign).split("&"): xml1 = i.split("=")[0] xml2 = i.split("=")[1] post_data = post_data + '' + xml2 + '' post_data = post_data + '' # 组合xml请求 print(post_data) # post_data.encode('utf-8') headers = {'Content-Type': 'binary'} # 解决post_data 中文编码问题 url = "https://api.mch.weixin.qq.com/pay/unifiedorder" res = requests.post(url, data=post_data.encode(), headers=headers, verify=False) # 提交订单信息 # res.text.encode('utf-8') print(res.text.encode('latin_1').decode('utf8')) pattern = re.compile("


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3