コード例 #1
0
 def get(self, request, args):
     user = self._get_current_user(request)
     if not user:
         return self.send_error(status_code=status.HTTP_401_UNAUTHORIZED,
                                error_message={"error_text": "用户未登录"})
     shop_id = args.get("shop_id")
     shop = get_shop_by_shop_id(shop_id)
     if not shop:
         return self.send_fail(error_text="店铺不存在")
     shop = get_shop_by_shop_id(shop_id)
     super_admin_data = get_user_by_id_interface(shop.super_admin.id)
     shop.super_admin_data = super_admin_data
     serializer = SuperShopSerializer(shop)
     return self.send_success(data=serializer.data)
コード例 #2
0
 def post(self, request, args):
     shop = get_shop_by_shop_id(args.get("shop_id"))
     if not shop:
         return self.send_fail(error_text="店铺不存在")
     payment_status = args.get("payment_status")
     if shop.pay_active == payment_status:
         text = (
             "正在审核中" if payment_status == ShopPayActive.CHECKING else
             "已通过审核" if payment_status == ShopPayActive.YES else "已拒绝审核")
         return self.send_fail(error_text="该店铺%s, 请不要重复操作" % text)
     shop.pay_active = payment_status
     # 创建paychannel
     if shop.pay_active == ShopPayActive.YES:
         pay_channel_info = {
             "smerchant_no": args.get("lc_merchant_no"),
             "terminal_id1": args.get("lc_terminal_id"),
             "access_token": args.get("lc_access_token"),
             "channel_type": ShopPayChannelType.LCSW,
         }
         serializer = ShopPayChannelSerializer(data=pay_channel_info,
                                               context={"shop": shop})
         if serializer.is_valid():
             return self.send_error(error_message=serializer.errors,
                                    status_code=status.HTTP_400_BAD_REQUEST)
         serializer.save()
     return self.send_success()
コード例 #3
0
 def put(self, request, args):
     shop = get_shop_by_shop_id(args.pop("shop_id"))
     if not shop:
         return self.send_fail(error_text="店铺不存在")
     serializer = SuperShopVerifySerializer(shop, data=args)
     if not serializer.is_valid():
         return self.send_error(error_message=serializer.errors,
                                status_code=status.HTTP_400_BAD_REQUEST)
     serializer.save()
     return self.send_success()
コード例 #4
0
 def process_request(self, request):
     # 从请求体中获取shop_code进行查询
     shop_code = request.GET.get("shop_code")
     shop = None
     if shop_code:
         shop = get_shop_by_shop_code(shop_code)
     wsc_shop_id = request.COOKIES.get("wsc_shop_id")
     # 从cookie中获取shop_id进行查询
     if wsc_shop_id and not shop:
         shop = get_shop_by_shop_id(int(wsc_shop_id))
     request.shop = shop
コード例 #5
0
 def post(self, request, args):
     # 还是检验一下是否登录
     user = self._get_current_user(request)
     if not user:
         return self.send_error(status_code=status.HTTP_401_UNAUTHORIZED,
                                error_message={"error_text": "用户未登录"})
     shop_id = args.get("shop_id")
     shop = get_shop_by_shop_id(shop_id)
     # 设置cookies时使用
     request.shop = shop
     if not shop:
         return self.send_fail(error_text="商铺id有误或商铺不存在")
     return self.send_success()
コード例 #6
0
def print_order(order: Order, user_id: int = 0):
    """
    订单打印
    :param order:
    :param user_id:
    :return:
    """
    shop_id = order.shop.id
    shop = get_shop_by_shop_id(shop_id)
    receipt_config = get_receipt_by_shop_id(shop_id)
    printer = ylyPrinter()
    template = jinja2.Template(ORDER_TPL_58)
    body = template.render(
        order=order,
        print_time=make_aware(
            datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S"),
        shop=shop,
        receipt_config=receipt_config,
    )
    printer_config = get_printer_by_shop_id(shop_id)
    if not printer_config:
        return False, "请先添加打印机"
    partner = "1693"  # 用户ID
    apikey = "664466347d04d1089a3d373ac3b6d985af65d78e"  # API密钥
    timenow = str(int(time.time()))  # 当前时间戳
    machine_code = printer_config.code  # 打印机终端号 520
    mkey = printer_config.key  # 打印机密钥 110110
    if machine_code and mkey:
        sign = "{}machine_code{}partner{}time{}{}".format(
            apikey, machine_code, partner, timenow, mkey)
        sign = hashlib.md5(sign.encode("utf-8")).hexdigest().upper()
    else:
        return False, "打印机配置错误"
    data = {
        "partner": partner,
        "machine_code": machine_code,
        "content": body,
        "time": timenow,
        "sign": sign,
    }
    success, msg = printer.send_request(data, receipt_config.copies)
    if success and user_id >= 0:
        log_info = {
            "order_num": order.order_num,
            "shop_id": order.shop.id,
            "operator_id": user_id,
            "operate_type": OrderLogType.PRINT,
        }
        create_order_log(log_info)
    return success, msg
コード例 #7
0
def handle_lcsw_callback(res_dict: dict):
    """
    处理利楚回调
    :param res_dict:
    :return:
    """
    # 业务结果检查
    if res_dict["return_code"] == "02":
        raise ValueError("LcCallBackFail", res_dict["return_msg"])
    # 附加信息检查
    if res_dict["attach"] != "SENGUOPRODUCT":
        raise ValueError("LcCallBackFail", "附加信息有误")
    # 订单有效性检查
    num = res_dict["terminal_trace"]
    order = get_order_by_num_for_update(num)
    if not order:
        raise ValueError("LcCallBackFail", "订单不存在: {}".format(num))
    elif order.status != OrderStatus.UNPAID:
        raise ValueError("LcCallBackFail", "订单状态错误: {}".format(order.status))
    # 店铺检查及验签
    shop_id, _ = NumGenerator.decode(num)
    shop = get_shop_by_shop_id(shop_id)
    if not shop:
        raise ValueError("LcCallBackFail", "找不到对应的店铺")
    success, pay_channel = get_pay_channel_by_shop_id(shop_id)
    if not success:
        raise ValueError("LcCallBackFail", pay_channel)
    key_sign = res_dict["key_sign"]
    str_sign = (LcswPay.getStrForSignOfTradeNotice(res_dict) +
                "&access_token=%s" % pay_channel.access_token)
    if key_sign != hashlib.md5(str_sign.encode("utf-8")).hexdigest().lower():
        raise ValueError("LcCallBackFail", "签名有误")
    # 检查业务结果:01成功 02失败
    result_code = res_dict["result_code"]
    if result_code == "02":
        raise ValueError("LcCallBackFail", res_dict["return_msg"])

    # TODO: 考虑是否进行回调的幂等检查
    create_order_transaction(
        order.id,
        res_dict["out_trade_no"],
        res_dict["receipt_fee"],
        res_dict["channel_trade_no"],
    )
    return True, order
コード例 #8
0
 def initialize_request(self, request, *args, **kwargs):
     request = super().initialize_request(request, *args, **kwargs)
     try:
         wsc_shop_id = request.get_signed_cookie(
             "wsc_shop_id",
             salt="hzh_wsc_shop_id",
         )
     except Exception as e:
         wsc_shop_id = 0
     # 从cookie中获取shop_id进行查询
     shop = get_shop_by_shop_id(int(wsc_shop_id))
     self.current_shop = shop
     current_staff = None
     if shop and self.current_user:
         current_staff = get_staff_by_user_id_and_shop_id(
             self.current_user.id, self.current_shop.id)
     self.current_staff = current_staff
     return request
コード例 #9
0
ファイル: services.py プロジェクト: hzh595395786/wsc_django
def _create_order_address(address_info: dict, shop_id: int,
                          delivery_method: int):
    """
    创建订单地址
    :param address_info:
    :param shop_id:
    :param delivery_method:
    :return:
    """
    order_address = OrderAddress(**address_info)
    # 自提订单更新成店铺地址
    if delivery_method == OrderDeliveryMethod.CUSTOMER_PICK:
        shop = get_shop_by_shop_id(shop_id)
        order_address.province = shop.shop_province
        order_address.city = shop.shop_city
        order_address.county = shop.shop_county
        order_address.address = shop.shop_address
    order_address.save()
    return order_address
コード例 #10
0
def get_wx_jsApi_pay(order: Order, wx_openid: str):
    """
    公众号支付参数获取
    :param order:
    :param wx_openid:
    :return:
    """
    shop = get_shop_by_shop_id(order.shop.id)
    success, pay_channel = get_pay_channel_by_shop_id(order.shop.id)
    if not success:
        return False, pay_channel
    body = "{}-订单号-{}".format(shop.shop_name, order.order_num)
    notify_url = "{}/payment/lcsw/callback/order/".format(LCSW_CALLBACK_HOST)
    parameters = LcswPay.getJspayParas(
        order.order_num,
        wx_openid,
        order.create_time.strftime("%Y%m%d%H%M%S"),
        int(round(order.total_amount_net * 100)),
        body,
        notify_url,
        pay_channel.smerchant_no,
        pay_channel.terminal_id1,
        pay_channel.access_token,
    )

    try:
        r = requests.post(
            LCSW_HANDLE_HOST + "/pay/100/jspay",
            data=json.dumps(parameters),
            verify=False,
            headers={"content-type": "application/json"},
            timeout=(1, 5),
        )
        res_dict = json.loads(r.text)
    except BaseException:
        return False, "微信支付预下单失败:接口超时或返回异常(LC)"

        # 响应码:01成功 ,02失败,响应码仅代表通信状态,不代表业务结果
    if res_dict["return_code"] == "02":
        return (
            False,
            "微信支付通信失败:{msg}".format(msg=res_dict["return_msg"]),
        )

    key_sign = res_dict["key_sign"]
    str_sign = LcswPay.getStrForSignOfJspayRet(res_dict)
    if key_sign != hashlib.md5(str_sign.encode("utf-8")).hexdigest().lower():
        return False, "微信支付校验失败:签名错误(LC)"

    # 业务结果:01成功 02失败
    result_code = res_dict["result_code"]
    if result_code == "02":
        return (False, "微信支付业务失败:{msg}".format(msg=res_dict["return_msg"]))

    renderPayParams = {
        "appId": res_dict["appId"],
        "timeStamp": res_dict["timeStamp"],
        "nonceStr": res_dict["nonceStr"],
        "package": res_dict["package_str"],
        "signType": res_dict["signType"],
        "paySign": res_dict["paySign"],
    }
    return True, renderPayParams