Beispiel #1
0
def callBack():

    # 返回给微信使用
    result_data = {
        'return_code':'SUCCESS',
        'return_msg':'OK'
    }
    header = {'content-Type':'application/xml'}

    # 对微信下单成功后的返回给我们的信息(xml)进行验证
    config_mina = app.config['MINA_APP']
    target_wechat = WeChatService(merchant_key=config_mina['paykey'])

    # 取出微信给我们的xml信息,并转换成dict数据格式
    callback_data = target_wechat.xml_to_dict(request.data)
    app.logger.info(callback_data)

    # 取出sign值(签名),再生成一次sign值,做比较,防止伪造 ******************************
    sign = callback_data['sign']
    callback_data.pop('sign')
    gene_sign = target_wechat.create_sign(callback_data)
    app.logger.info(gene_sign)

    # 两次sign值做比较,如果测试官网的回调函数代码,这个步骤就要省略
    # if sign != gene_sign:
    #     result_data['return_code'] = result_data['return_msg'] = 'FAIL'
    #     return target_wechat.dict_to_xml(result_data),header

    # 对比金钱的数量是否正确 ****************************

    # 取出订单号查询对应的订单信息
    order_sn = callback_data['out_trade_no']
    pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
    app.logger.info('牛逼',pay_order_info)
    if not pay_order_info:
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    if int(pay_order_info.total_price * 100) != int(callback_data['total_fee']):
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    # 微信会有间隔的返回数据,如果订单的状态已经置为1,则直接返回
    if pay_order_info.status == 1:
        return target_wechat.dict_to_xml(result_data), header

    # 下单成功(微信支付回调成功的操作:pay_order的状态置为1,其他状态值也要修改)
    target_pay = PayService()
    target_pay.orderSuccess(pay_order_id=pay_order_info.id,params={'pay_sn':callback_data['transaction_id']})

    # 将微信回调的信息存表
    target_pay.addPayCallbackData(pay_order_id=pay_order_info.id,data=request.data)

    return target_wechat.dict_to_xml(result_data),header
Beispiel #2
0
def orderCallback():
    result_data = {'return_code': 'SUCCESS', 'return_msg': 'OK'}

    header = {'Content-Type': 'application/xml'}
    config_mina = app.config['MINA_APP']
    target_wechat = WeChatService(merchant_key=config_mina['paykey'])
    callback_data = target_wechat.xml_to_dict(request.data)

    app.logger.info(callback_data)

    sign = callback_data['sign']
    callback_data.pop('sign')

    gene_sign = target_wechat.create_sign(callback_data)

    app.logger.info(gene_sign)

    if sign != gene_sign:
        result_data['return_code'] = result_data['return_msg'] = "FAIL"
        return target_wechat.dict_to_xml(result_data), header

    if callback_data['result_code'] != 'SUCCESS':
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

        order_sn = callback_data['out_trade_no']
        pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
        if not pay_order_info:
            result_data['return_code'] = result_data['return_msg'] = 'FAIL'
            return target_wechat.dict_to_xml(result_data), header

        if int(pay_order_info.total_price * 100) != int(
                callback_data['total_price']):
            result_data['return_code'] = result_data['return_msg'] = 'FAIL'
            return target_wechat.dict_to_xml(result_data), header

    if pay_order_info.status == 1:
        return target_wechat.dict_to_xml(result_data), header

    target_pay = PayService()
    target_pay.orderSuccess(pay_order_id=pay_order_info.id,
                            params={'pay_sn': callback_data['transaction_id']})

    # 将微信回调的结果放入记录表
    target_pay.addPayCallbackData(pay_order_id=pay_order_info.id,
                                  data=request.data)

    return target_wechat.dict_to_xml(result_data), header
Beispiel #3
0
def orderCallback():
    """
	如何模拟调用此函数   用 postman 将数据放入 post数据中
	https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8   小程序回调返回的数据 xml格式
	:return:
	"""
    result_data = {'return_code': 'SUCCESS', 'return_msg': 'OK'}
    header = {'Content-Type': 'application/xml'}
    config_mina = app.config['MINA_APP']
    target_wechat = WeChatService(merchant_key=config_mina['paykey'])
    callback_data = target_wechat.xml_to_dict(
        request.data)  # request.data 整个post请求的所有数据
    app.logger.info(callback_data)
    sign = callback_data['sign']
    callback_data.pop('sign')
    # 检测这个签名是否和对应的一样
    gene_sign = target_wechat.create_sign(callback_data)
    app.logger.info(gene_sign)
    if sign != gene_sign:
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header
    if callback_data['result_code'] != 'SUCCESS':
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    order_sn = callback_data['out_trade_no']
    pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
    if not pay_order_info:
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    if int(pay_order_info.total_price * 100) != int(
            callback_data['total_fee']):
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    if pay_order_info.status == 1:
        return target_wechat.dict_to_xml(result_data), header

    target_pay = PayService()
    # 处理parorder 更新状态值
    target_pay.orderSuccess(pay_order_id=pay_order_info.id,
                            params={"pay_sn": callback_data['transaction_id']})
    # 回调结果放入记录表里
    target_pay.addPayCallbackData(pay_order_id=pay_order_info.id,
                                  data=request.data)
    return target_wechat.dict_to_xml(result_data), header
Beispiel #4
0
def orderCallback():
    """
    <xml>
      <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
      <attach><![CDATA[支付测试]]></attach>
      <bank_type><![CDATA[CFT]]></bank_type>
      <fee_type><![CDATA[CNY]]></fee_type>
      <is_subscribe><![CDATA[Y]]></is_subscribe>
      <mch_id><![CDATA[10000100]]></mch_id>
      <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
      <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
      <out_trade_no><![CDATA[1409811653]]></out_trade_no>
      <result_code><![CDATA[SUCCESS]]></result_code>
      <return_code><![CDATA[SUCCESS]]></return_code>
      <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
      <time_end><![CDATA[20140903131540]]></time_end>
      <total_fee>1</total_fee>
    <coupon_fee><![CDATA[10]]></coupon_fee>
    <coupon_count><![CDATA[1]]></coupon_count>
    <coupon_type><![CDATA[CASH]]></coupon_type>
    <coupon_id><![CDATA[10000]]></coupon_id>
    <coupon_fee><![CDATA[100]]></coupon_fee>
      <trade_type><![CDATA[JSAPI]]></trade_type>
      <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
    </xml>
    """
    result_data = {'return_code': 'SUCCESS', 'return_msg': 'OK'}
    header = {'Content-Type': 'application/xml'}
    config_mina = app.config['MINA_APP']
    target_wechat = WeChatService(merchant_key=config_mina['paykey'])
    callback_data = target_wechat.xml_to_dict(request.data)
    app.logger.info(callback_data)
    sign = callback_data['sign']
    callback_data.pop('sign')
    gene_sign = target_wechat.create_sign(callback_data)
    app.logger.info(gene_sign)
    if sign != gene_sign:
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header
    if callback_data['result_code'] != 'SUCCESS':
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    order_sn = callback_data['out_trade_no']
    pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
    if not pay_order_info:
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    if int(pay_order_info.total_price * 100) != int(
            callback_data['total_fee']):
        result_data['return_code'] = result_data['return_msg'] = 'FAIL'
        return target_wechat.dict_to_xml(result_data), header

    if pay_order_info.status == 1:
        return target_wechat.dict_to_xml(result_data), header

    target_pay = PayService()
    target_pay.orderSuccess(pay_order_id=pay_order_info.id,
                            params={"pay_sn": callback_data['transaction_id']})
    # 将微信回调的结果放入记录表
    target_pay.addPayCallbackData(pay_order_id=pay_order_info.id,
                                  data=request.data)
    return target_wechat.dict_to_xml(result_data), header