#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import tornado.web
from methods.common import *
from methods.db import *
import traceback
import logging
import uuid
import requests
import json
import xmltodict
import time
import pymysql
import datetime
import random
from hashlib import md5
#数据库
MYSQL = dict(
host='127.0.0.1', user='root', passwd='123456', db='kbe', charset="utf8mb4"
)
logger = logging.getLogger(__name__)
conn = pymysql.connect(**MYSQL)
cur_dict = conn.cursor(pymysql.cursors.DictCursor)
cur = conn.cursor()
from hashlib import md5
from requests.packages import urllib3
urllib3.disable_warnings()
###############################################
############# 微信支付配置 #################
###############################################
WeChaPayConfig = {
# 微信支付APP_ID
'APPID' : 'wx91f04ffbf8a23431',
# 微信支付MCH_ID 【登录账号】
'MCHID' : '1535411231',
# 微信支付sign_type
'SIGNTYPE' : 'MD5',
# 服务器IP地址
'SPBILLCREATEIP' : '32.23.11.34',
# 微信支付用途
'BODY' : '费用充值',
# 微信KEY值 【API密钥】
'KEY' : 'ZiwcVpWomDqixQdhRgm5FpBKNXqwasde',
# 微信统一下单URL
'UNIFIEDORDERURL' : 'https://api.mch.weixin.qq.com/pay/unifiedorder',
# 微信查询订单URL
'QUERYORDERURL' : 'https://api.mch.weixin.qq.com/pay/orderquery',
# 微信支付回调API
'CALLBACKAPI' : 'http://xxxx.com/weixinpay_rollback/',
}
def make_payment_info(notify_url=None, out_trade_no=None, total_fee=None,device_info = 'Phone'):
order_info = {'appid': WEIXIN_APP_ID,
'mch_id': WEIXIN_MCH_ID,
'device_info': device_info,
'nonce_str': '',
'sign_type': WEIXIN_SIGN_TYPE,
'body': WEIXIN_BODY,
'out_trade_no': str(out_trade_no),
'total_fee': total_fee,
'spbill_create_ip': WEIXIN_SPBILL_CREATE_IP,
'notify_url': notify_url,
'trade_type': 'APP'}
return order_info
def make_payment_request_wx(notify_url, out_trade_no, total_fee,device_info):
"""
微信统一下单,并返回客户端数据
:param notify_url: 回调地址
:param out_trade_no: 订单编号
:param total_fee: 充值金额
:param device_info:设备信息
:return: app所需结果数据
"""
def generate_call_app_data(params_dict, prepay_id):
"""
客户端APP的数据参数包装
"""
request_order_info = {'appid': params_dict['appid'],
'partnerid': params_dict['mch_id'],
'prepayid': prepay_id,
'package': 'Sign=WXPay',
'noncestr': generate_nonce_str(),
'timestamp': str(int(time.time()))}
request_order_info['sign'] = generate_sign(request_order_info)
return request_order_info
def generate_sign(params):
"""
生成md5签名的参数
"""
if 'sign' in params:
params.pop('sign')
src = '&'.join(['%s=%s' % (k, v) for k, v in sorted(params.items())]) + '&key=%s' % WEIXIN_KEY
return md5(src.encode('utf-8')).hexdigest().upper()
def generate_nonce_str():
"""
生成随机字符串
"""
return str(uuid.uuid4()).replace('-', '')
def generate_request_data(params_dict):
"""
生成统一下单请求所需要提交的数据
"""
params_dict['nonce_str'] = generate_nonce_str()
params_dict['sign'] = generate_sign(params_dict)
return xmltodict.unparse({'xml': params_dict}, pretty=True, full_document=False).encode('utf-8')
def make_payment_request(params_dict, unified_order_url):
"""
生成返回给客户端APP的数据参数
"""
data = generate_request_data(params_dict)
headers = {'Content-Type': 'application/xml'}
res = requests.post(unified_order_url, data=data, headers=headers,verify=False)
if res.status_code == 200:
result = json.loads(json.dumps(xmltodict.parse(res.content)))
if result['xml']['return_code'] == 'SUCCESS':
prepay_id = result['xml']['prepay_id']
return generate_call_app_data(params_dict, prepay_id), result['xml']
else:
return result['xml']['return_msg'], None
print('http错误码:::::::{0}'.format(str(res)))
return None, None
if float(total_fee) < 0.01:
raise Exception('充值金额不能小于0.01')
payment_info = make_payment_info(notify_url=notify_url, out_trade_no=out_trade_no, total_fee=total_fee,device_info=device_info)
res, info = make_payment_request(payment_info, WEIXIN_UNIFIED_ORDER_URL)
print('微信支付 res={0} info={1}'.format(res,info))
return res, info
def create_order_number():
"""
生成订单号
:return:
"""
date = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
# 生成4为随机数作为订单号的一部分
random_str = str(random.randint(1, 9999))
random_str = random_str.rjust(4, '0')
rtn = '%s%s' % (date, random_str)
return rtn
def weixin_create_order(money,UserId,itemId):
"""
【API】: 创建订单,供商户app调用
"""
#金额,浮点型
#设备信息:pid:tm
device_info = str(UserId)
res = {
'code': 1,
'msg': 'error'
}
try:
price = money#0.01 # 0.99元,微信的单位为分,需要转为分
out_trade_no = create_order_number()
order_info, info = make_payment_request_wx(WEIXIN_CALLBACK_API, out_trade_no, int(float(price) * 100),device_info)
if order_info and info:
print('产生订单返回=======')
print(info)
info['total_amount'] = int(float(price) * 100)
if info['result_code'] == "SUCCESS":
order_info['out_trade_no'] = out_trade_no
res['order_info'] = order_info
if isinstance(order_info,dict):
order_info['pid'] = UserId
order_info['itemId'] = itemId
#待微信服务器校验后再行入库
print('客户端返回')
print(order_info)
#入库
WeiChatPayAdd(int(UserId), out_trade_no,WEIXIN_MCH_ID,WEIXIN_APP_ID)
return order_info
# 调用统一创建订单接口失败
else:
res['msg'] = info['result_code']
elif order_info:
res['msg'] = order_info
res['code'] = -1
else:
res['code'] = -2
return None
except Exception as e:
# traceback.print_exc()
print(e)
return None
# finally:
# return json.dumps(res)
class WeChatOrderHandler(tornado.web.RequestHandler):
def post(self):
"""客户端发来的请求微信订单"""
def pro(recv):
if recv:
if recv.__contains__('UserId'): #玩家id
userId = str(recv.pop('UserId')[0], encoding='utf-8')
if recv.__contains__('money'): #金额
money = str(recv.get('money')[0], encoding='utf-8')
if recv.__contains__('itemId'): #商品
itemId = str(recv.get('itemId')[0],encoding='utf-8')
print('微信支付参数 pid={0},,money={1}'.format(userId, money))
#这里的post必须是同步阻塞,这里可以存入更多的设备信息在weixin_create_order的第三个参数里
response = weixin_create_order(money,userId,itemId)
# """返回到客户端"""
if response=='':
print('错误=====')
else:
#返回到客户端(必须是异步post请求)
requests.post('http://localhost:30041/wechat', data=response)
self.write('Success')
recv1 = self.request.arguments
from threading import Timer
# 指定1秒后执行
t = Timer(1.0, pro,(recv1,))
t.start()