• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 398, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            python服務(wù)器 實現(xiàn)app微信支付:支付異步通知

            # -*- coding: utf-8 -*-
            import tornado.httpserver
            import tornado.ioloop
            import tornado.options
            import tornado.web
            import pymysql
            import json
            import xmltodict
            from hashlib import md5
            from tornado.options import define, options
            ####################微信支付異步回調(diào)消息########################
            WEIXIN_KEY = '8mdRLb1yeesYssfasdfsadfassdaV'
            def generate_sign(params):
                """
                生成md5簽名的參數(shù)
                """
                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 validate_sign(resp_dict):
                """
                驗證微信返回的簽名
                """
                if 'sign' not in resp_dict:
                    return False
                wx_sign = resp_dict['sign']
                sign = generate_sign(resp_dict)
                if sign == wx_sign:
                    return True
                return False
            def handle_wx_response_xml(params):
                """
                處理微信支付返回的xml格式數(shù)據(jù)
                """
                try:
                    resp_dict = xmltodict.parse(params)['xml']
                    if not resp_dict or len(resp_dict) < 1:
                        print('resp_dict is zero+++++++++')
                        return None
                    return_code = resp_dict.get('return_code')
                    if return_code == 'SUCCESS':  # 僅僅判斷通信標(biāo)識成功,非交易標(biāo)識成功,交易需判斷result_code
                        if validate_sign(resp_dict):
                            print('驗證成功!!!')
                            return resp_dict
                   
                except Exception as e:
                    print(e)
                    return None
                return None
            def weixinpay_call_back(request):
                """
                微信支付回調(diào)
                :param request: 回調(diào)參數(shù)
                :return:
                """
                args = str(request.body,'utf-8')
                if args is None:
                    return None
                print(args)
                # 驗證平臺簽名
                resp_dict = handle_wx_response_xml(args)
                if resp_dict is None:
                    print('簽名驗證失敗!!!')
                    return None
                return resp_dict
            def weixinpay_response_xml(params):
                """
                生成交易成功返回信息
                """
                def generate_response_data(resp_dict):
                    """
                    字典轉(zhuǎn)xml
                    """
                    return xmltodict.unparse({'xml': resp_dict}, pretty=True, full_document=False).encode('utf-8')
                return_info = {
                    'return_code': params,
                    'return_msg': 'OK'
                }
                return generate_response_data(return_info)
            def weixinpay_sucess_db(dicts):
                """微信支付成功后數(shù)據(jù)庫日志操作"""
                if isinstance(dicts,dict):
                    trade_status = dicts['result_code']  # 業(yè)務(wù)結(jié)果  SUCCESS/FAIL
                    trade_no = dicts['out_trade_no']  # 商戶訂單號
                    if trade_status == "SUCCESS":
                        appid = dicts['appid']  # 應(yīng)用ID
                        bank_type = dicts['bank_type']  # 付款銀行
                        cash_fee = dicts['cash_fee']  # 現(xiàn)金支付金額(分)
                        device_info = dicts['device_info']  # 微信支付分配的終端設(shè)備號
                        fee_type = dicts['fee_type']  # 貨幣種類
                        gmt_create = dicts['time_end']  # 支付完成時間
                        total_amount = int(dicts['total_fee']) / 100  # 總金額(單位由分轉(zhuǎn)元)
                        trade_type = dicts['trade_type']  # 交易類型
                        out_trade_no = dicts['transaction_id']  # 微信支付訂單號
                        seller_id = dicts['mch_id']  # 商戶號
                        openid = dicts['openid']  # 用戶標(biāo)識
                        update_sql = ''' update weixin_trade set trade_status='{trade_status}', appid='{appid}', ''' + \
                        '''seller_id='{seller_id}', openid='{openid}', total_amount='{total_amount}',''' +  \
                        '''out_trade_no='{out_trade_no}', gmt_create='{gmt_create}', '''+ \
                        '''device_info='{device_info}', trade_type='{trade_type}', bank_type='{bank_type}', ''' + \
                        '''fee_type='{fee_type}', cash_fee='{cash_fee}' where trade_no='{trade_no}' '''
                        update_sql = update_sql.format(
                            trade_status=trade_status,
                            appid=appid,
                            seller_id=seller_id,
                            openid=openid,
                            total_amount=total_amount,
                            out_trade_no=out_trade_no,
                            gmt_create=gmt_create,
                            device_info=device_info,
                            trade_type=trade_type,
                            bank_type=bank_type,
                            fee_type=fee_type,
                            cash_fee=cash_fee,
                            trade_no=trade_no)
                        print(update_sql)
                        #寫數(shù)據(jù)庫
                        
            def weixin_rollback(request):
                """
                【API】: 微信寶支付結(jié)果回調(diào)接口,供微信服務(wù)端調(diào)用
                """
                try:
                    # 支付異步回調(diào)驗證
                    data = weixinpay_call_back(request)
                    if data:
                        print('微信支付返回====={0}'.format(data))
                        res = "success"
                        trade_status = data['result_code']  # 業(yè)務(wù)結(jié)果  SUCCESS/FAIL
                        out_trade_no = data['out_trade_no']  # 商戶訂單號
                        if trade_status == "SUCCESS":
                            weixinpay_sucess_db(data)
                            device_info = data['device_info']  # 微信支付分配的終端設(shè)備號
                            # 如果狀態(tài)是支付成功則發(fā)放物品
                            hall = KBEngine.globalData["Halls"]
                            if hall:
                                player_data = hall.GetPlayerByDbId(int(device_info))
                                if player_data:
                                    player = player_data.entityCall
                                    if player:
                                        print('微信發(fā)放獎品到客戶端,訂單號======%s' % out_trade_no)
                                        player.GmBkCmd({'cmd':'PayReward','Order_id':out_trade_no})
                                    else:
                                        print('找不到玩家++++++++')
                        else:
                            res = "error: pay failed! "
                            status = 0
                            err_code = data['err_code']  # 錯誤代碼
                            err_code_des = data['err_code_des']  # 錯誤代碼描述
                    else:
                        res = "error: verify failed! "
                except Exception as e:
                    print(e)
                    res='err:exception==='
                finally:
                    return weixinpay_response_xml(res)
            class WeChatPayNotify(tornado.web.RequestHandler):
                """微信支付回調(diào)"""
                def post(self):
                    """
                    【API】: 微信寶支付結(jié)果回調(diào)接口,供微信服務(wù)端調(diào)用
                    """
                    self.write(weixin_rollback(self.request))
            class WeChatPay(tornado.web.RequestHandler):
                def post(self):
                    to_client={}
                    for k,v in self.request.arguments.items():
                        to_client[k] = str(v[0],encoding='utf-8')
                    pid = to_client.pop('pid')
                    if isinstance(pid,str):
                        pid = int(pid)
                    self.write('')

            posted on 2020-12-05 15:34 Benjamin 閱讀(332) 評論(0)  編輯 收藏 引用 所屬分類: python

            91久久精品无码一区二区毛片| 久久久久久久久66精品片| 久久精品国产只有精品2020| 国内精品伊人久久久久网站| 国产69精品久久久久久人妻精品| 99久久婷婷国产综合亚洲| 久久国产免费直播| 99精品国产免费久久久久久下载| 久久午夜羞羞影院免费观看| 久久久久亚洲AV无码专区桃色 | 中文字幕亚洲综合久久| 久久国产成人午夜AV影院| 午夜精品久久久久久毛片| 久久久久久一区国产精品| 国产91色综合久久免费| 精品久久久久成人码免费动漫| 国产欧美久久久精品| 久久国产色av免费看| 深夜久久AAAAA级毛片免费看| 久久91精品久久91综合| 久久国产精品一国产精品金尊| 久久亚洲中文字幕精品一区四 | 日本五月天婷久久网站| 国产精品99久久久久久宅男 | 精品综合久久久久久98| 亚洲国产精品一区二区三区久久| 2020最新久久久视精品爱 | 久久这里只精品99re66| 国产精品内射久久久久欢欢| 精品久久久久久国产| 91精品国产91久久综合| 精品久久久久久无码专区 | 91精品国产综合久久四虎久久无码一级| 伊人久久大香线蕉亚洲| 久久久噜噜噜久久中文字幕色伊伊| 久久久久久久久久久免费精品| 国产精品99久久久久久宅男| 久久精品国产精品亚洲人人| 国产成人久久精品麻豆一区| 久久se精品一区精品二区国产| 伊人久久大香线焦综合四虎|