def post_order_payment(): """订单支付""" request_param = "" try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) order = request.json.get("order") pay_method = request.json.get("channel") # 余额 支付宝 微信 if not order: return response(msg="Bad Request: Miss params: 'order'.", code=1, status=400) if pay_method not in ["微信", "支付宝", "余额"]: return response(msg="Bad Request: Params 'pay_method' is error.", code=1, status=400) total_amount = 0 cursor = manage.client["order"].find({"order": order, "state": 1}) n = 0 for doc in cursor: total_amount += doc["price"] if n == 0: goods_id = doc["works_id"] n += 1 doc = manage.client["user"].find_one({"uid": user_id}) balance = doc.get("balance") # 生成交易信息 trade_id = str(int(time.time() * 1000)) + str( int(time.clock() * 1000000)) condition = { "trade_id": trade_id, "type": "balance" if pay_method == "余额" else ("alipay" if pay_method == "支付宝" else "wxpay"), "trade_amount": total_amount, "goods_id": goods_id, "state": 1, "order": order, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) } manage.client["trade"].insert(condition) # 余额支付 if pay_method == "余额": trade_data = { "trade_id": trade_id, "balance": balance, "trade_amount": total_amount } import json trade_str = json.dumps(trade_data) request_param = trade_str # 支付宝支付 if pay_method == "支付宝": alipay = AliPay(order, str(total_amount)) request_param = alipay.generate_request_param( order, str(total_amount)) # 微信支付 if pay_method == "微信": wechatpay = WechatPay(order, total_amount * 100) prepay_id = wechatpay.wechat_payment_request() if not prepay_id: return response(msg="请求微信失败", code=1) request_param = wechatpay.generate_app_call_data(prepay_id) return response(data=request_param) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def post_top_up(): """余额充值""" request_param = "" try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) channel = request.json.get("channel") # 支付宝 微信 total_amount = request.json.get("total_amount") if pay_method not in ["微信", "支付宝", "余额"]: return response(msg="Bad Request: Params 'pay_method' is error.", code=1, status=400) if not total_amount: return response(msg="Bad Request: Miss params: 'total_amount'.", code=1, status=400) if total_amount < 0 or type(total_amount) != float: return response(msg="Bad Request: Parmas 'total_amount' is error.", code=1, status=400) # order = str(int(time.time() * 1000)) + str(int(time.clock() * 1000000)) # # 创建充值订单 # condition = { # "order": order, "user_id": user_id, "channel": channel, "amount": total_amount, "state": 0, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) # } # manage.client["recharge_records"].insert(condition) # 生成交易id trade_id = str(int(time.time() * 1000)) + str( int(time.clock() * 1000000)) condition = { "trade_id": trade_id, "type": "alipay" if channel == "支付宝" else "wxpay", "trade_amount": total_amount, "goods_id": "", "state": 1, "order": "", "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) } manage.client["trade"].insert(condition) # 支付宝支付 if pay_method == "支付宝": alipay = AliPay(order, total_amount) request_param = alipay.generate_request_param() # 微信支付 if pay_method == "微信": wechatpay = WechatPay(order, total_amount * 100) prepay_id = wechatpay.wechat_payment_request() if not prepay_id: return response(msg="请求微信失败", code=1) request_param = wechatpay.generate_app_call_data(prepay_id) return response(data=request_param) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)