python flask 微信支付下单及支付结果回调处理代码笔记

前言

因为微信支付下单的时候不允许同一个订单重复下单,所以在下单之前 对订单号加了三位随机字符串 在回调时 进行截取操作

@order_api.route('/user/wx_xcx_pay/notify', methods=['POST'])
def new_notify():
    headers = request.headers
    # print('headers', headers)
    wxpay = wx_pay_func()
    result = wxpay.callback(headers, request.data)
    if result and result.get('event_type') == 'TRANSACTION.SUCCESS':
        resp = result.get('resource')
        out_trade_no = resp.get('out_trade_no')  # 本系统订单号
        transaction_id = resp.get('transaction_id')  # 微信支付订单号
        first_character = out_trade_no[0]  # 获取开头字母 确定支付类型
        order_number = out_trade_no[:-3]  # 获取实际订单号
        success_time = resp.get('success_time')  # 支付完成时间
        payer = resp.get('payer')  # 付款人信息
        amount = resp.get('amount').get('total')  # 金额 单位分
        payment_amount = Decimal(amount) / Decimal(100)  # 实际支付金额 单位元
        # 上门取 或者 上门洗
        if first_character in ['X', 'Q']:
            return order_pay_callback(order_number, amount, success_time, transaction_id, resp)
        # 充值订单
        elif first_character == 'C':
            return recharge_pay_callback(order_number, transaction_id, payment_amount)
        # 补差价订单
        elif first_character == 'B':
            return price_difference_payment_callback(order_number, transaction_id, payment_amount)
    else:
        return reg_func(200, None, '支付失败')


def load_private_key(file_name):
    # 获取当前文件的目录
    current_dir = os.path.dirname(__file__)
    # 构建完整的文件路径
    file_path = os.path.join(current_dir, file_name)

    try:
        # 安全地打开文件
        with open(file_path, 'r') as file:
            private_key = file.read()
        return private_key
    except FileNotFoundError:
        print(f"Error: The file {
     file_name} was not found in the directory 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值