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 orderPay():
	resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
	member_info = g.member_info
	req = request.values
	order_sn = req['order_sn'] if 'order_sn' in req else ''
	pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
	target_pay = PayService()
	target_pay.orderSuccess(pay_order_id=pay_order_info.id, params={"pay_sn": ""})
	target_pay.addPayCallbackData(pay_order_id=pay_order_info.id, data=request.data)
	# pay_order_info = PayOrder.query.filter_by( order_sn = order_sn,member_id = member_info.id ).first()
	# if not pay_order_info:
	# 	resp['code'] = -1
	# 	resp['msg'] = "系统繁忙。请稍后再试~~"
	# 	return jsonify(resp)
	# # 用户open_id
	# oauth_bind_info = OauthMemberBind.query.filter_by( member_id =  member_info.id ).first()
	# if not oauth_bind_info:
	# 	resp['code'] = -1
	# 	resp['msg'] = "系统繁忙。请稍后再试~~"
	# 	return jsonify(resp)
	#
	# config_mina = app.config['MINA_APP']
	# notify_url = app.config['APP']['domain'] + config_mina['callback_url']
	#
	# target_wechat = WeChatService( merchant_key=config_mina['paykey'] )
	#
	# data = {
	# 	'appid': config_mina['appid'],
	# 	'mch_id': config_mina['mch_id'],
	# 	'nonce_str': target_wechat.get_nonce_str(),
	# 	'body': '吃吃吃吃',  # 商品描述
	# 	'out_trade_no': pay_order_info.order_sn,  # 商户订单号
	# 	'total_fee': int( pay_order_info.total_price * 100 ),  # 单位是分
	# 	'notify_url': notify_url,
	# 	'trade_type': "JSAPI",
	# 	'openid': oauth_bind_info.openid
	# }
	#
	# pay_info = target_wechat.get_pay_info( pay_data=data)
	#
	# # 保存prepay_id为了后面发模板消息
	# pay_order_info.prepay_id = pay_info['prepay_id']
	# db.session.add( pay_order_info )
	# db.session.commit()
	#
	# resp['data']['pay_info'] = pay_info
	return jsonify(resp)
Beispiel #5
0
def callback():
    logging.info('进入回调函数')
    result_data = {'return_code': 'SUCCESS', 'return_msg': 'OK'}

    header = {'Content-Type': 'application/xml'}
    config_mina = app.config['MINA_APP']

    logging.info(request.data)

    target_wechat = WeChatService(merchant_key=config_mina['paykey'])
    # print(request.data)

    callback_data = target_wechat.xml_to_dict(request.data)

    sign = callback_data['sign']
    callback_data.pop('sign')
    gene_sign = target_wechat.create_sign(callback_data)

    if sign != gene_sign:
        # logging.info('sing!=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()
    if not pay_order_info:
        # logging.info('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:
        # logging.info('pay_order_info.status == 1')
        return target_wechat.dict_to_xml(result_data), header

    target_pay = PayService()
    target_pay.orderSuccess(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 #6
0
def orderOps():
    resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
    req = request.values
    order_id = req['id'] if 'id' in req else 0
    act = req['act'] if 'act' in req else ''

    # pay_order_info = PayOrder.query.filter_by( id = id ).first()
    # if not pay_order_info:
    #     resp['code'] = -1
    #     resp['msg'] = "系统繁忙。请稍后再试~~"
    #     return jsonify(resp)

    app.logger.info("test 1")
    target = PayService()

    if act == "take":
        # 确认取餐
        result = target.confirmOrder(pay_order_id=order_id)

    if act == "express":
        # 确认到账——进行sale和成功回调
        # pay_order_info.express_status = -6  # -6
        # pay_order_info.updated_time = getCurrentDate()
        # db.session.add( pay_order_info )

        # 修改新增售卖记录 和 销售量
        result = target.addPayCallbackData(pay_order_id=order_id,
                                           type='pay',
                                           data='审核确认')
        app.logger.info("result is1 {}".format(result))

    if act == "cancel":
        # 取消订单——归还库存
        result = target.addPayCallbackData(pay_order_id=order_id,
                                           type='cancel',
                                           data='审核取消')
    if not result:
        app.logger.info("result is 2{}".format(result))

        resp['code'] = -1
        resp['msg'] = "系统繁忙。请稍后再试~~"
        return jsonify(resp)

    return jsonify(resp)
Beispiel #7
0
def orderCallback():
    """模拟支付"""
    resp = {'code': 0, 'msg': '支付成功', 'data': {}}
    req = request.values
    order_sn = req['order_sn']
    pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first()
    #模拟第三方支付流水信息,pay_sn
    transaction_id = "1004400740201409030005092168"
    target_pay = PayService()
    order_suc = target_pay.orderSuccess(pay_order_id=pay_order_info.id, params={"pay_sn": transaction_id})
    # 将微信回掉结果放入记录表
    callback = target_pay.addPayCallbackData(pay_order_id=pay_order_info.id, data=pay_order_info)
    if order_suc and callback:
        resp['code'] = 200

    return jsonify(resp)
Beispiel #8
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