Esempio n. 1
0
    def set_login_password(self, data):
        form = LoginPasswordForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        token = form.data["token"]
        cust = mc.MemSession.get_customer(token)
        if not cust:
            return -1, "invalid session", {}
        cust_id = cust.cust_id
        # step 1, 检查账户是否存在
        cust = customer.Customer.on_find_customer_by_id(cust_id)
        if not cust:
            return -1, "no permission", {}

        old_password = utils.hash_password(form.data["old_password"])
        new_password = utils.hash_password(form.data["new_password"])
        if cust.login_password == old_password:
            status = customer.Customer.on_set_login_password(
                cust_id, new_password)
            if status == 0:
                return 0, "success", {}
            else:
                return -1, "failed", {}
        return -1, "incorrect old password", {}
Esempio n. 2
0
    def register_new_customer(self, data):
        form = RegisterForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        # 验证码
        captcha_text = form.data["captcha"]
        cid = form.data["device_id"]
        if not self._check_captcha(cid, captcha_text):
            return -1, "wrong captcha code", {}
        mc.MemCaptchaSet.del_captcha(cid)

        email = form.data["email"]
        if not self._can_register(email):
            return -1, "user already exists", {}

        company_id = form.data["company_id"]
        cust_type = form.data["cust_type"]
        login_password = utils.hash_password(form.data['password'])

        cust = customer.Customer.on_add_new_customer(company_id, cust_type,
                                                     email, login_password)
        if not cust:
            return -1, "failed", {}
        wallet_name = str(uuid.uuid4())
        wallet = customer_wallet.Wallet.on_add_new_wallet(
            cust.cust_id, wallet_name)
        if not wallet:
            logging.error("create wallet failed: <{}>".format(email))
        mailer.send_register_welcome_email(email)
        return 0, "success", {}
Esempio n. 3
0
    def reset_trade_password(self, data):
        form = ResetTradePasswordForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        token = form.data["token"]
        session = mc.MemSession.get_customer(token)
        if not session:
            return -1, "invalid session", {}

        # 检查验证码
        verify_code = form.data["verify_code"]
        status = session.check_trade_password_verify_code(verify_code)
        if not status:
            return -1, "incorrect code", {}
        session.set_trade_password_verify_code("")

        cust_id = session.cust_id
        # step 1, 检查账户是否存在
        cust = customer.Customer.on_find_customer_by_id(cust_id)
        if not cust:
            return -1, "no permission", {}

        new_password = utils.hash_password(form.data["new_password"])
        status = customer.Customer.on_set_trade_password(cust_id, new_password)
        if status != 0:
            return -1, "failed", {}
        return 0, "success", {}
Esempio n. 4
0
    def reset_login_password(self, data):
        form = ResetLoginPasswordForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        email = form.data["email"]
        verify_code = form.data["verify_code"]
        new_password = utils.hash_password(form.data["new_password"])

        # 检查验证码
        code = mc.MemVerifyCodeSet.get_code(email)
        if not code:
            return -1, "incorrect code", {}
        status = code["text"] == verify_code
        if not status:
            return -1, "incorrect code", {}
        mc.MemVerifyCodeSet.set_code(email, "")

        # step 1, 检查账户是否存在
        cust = customer.Customer.on_find_customer_by_email(email)
        if not cust:
            return -1, "no permission", {}

        status = customer.Customer.on_set_login_password(
            cust.cust_id, new_password)
        if status != 0:
            return -1, "failed", {}
        return 0, "success", {}
Esempio n. 5
0
    def register_new_withdraw(self, data):
        form = RegisterNewWithdrawForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        token = form.data["token"]
        cust = mc.MemSession.get_customer(token)
        if not cust:
            return -1, "invalid session", {}
        cust_id = cust.cust_id
        # step 1, 检查账户是否存在
        cust = customer.Customer.on_find_customer_by_id(cust_id)
        if not cust:
            return -1, "no permission", {}

        trade_password = utils.hash_password(form.data["trade_password"])
        if trade_password != cust.trade_password:
            return -1, "incorrect password", {}

        withdraw_type = cust.cust_type
        card_id = form.data["card_id"]
        amount = form.data["amount"]
        currency = form.data["currency"]
        company_id = cust.company_id
        note = form.data["note"]
        fee_proportion = 0.03
        fee_amount = 100.00

        can_withdraw = False
        for card in cust.wallet.cards:
            if card.card_id == card_id:
                can_withdraw = True
                break
        if not can_withdraw:
            return -1, "invalid bank card ", {}

        _withdraw = withdraw.Withdraw.on_register_new_withdraw(
            withdraw_type, cust_id, card_id, amount, currency, company_id,
            fee_proportion, fee_amount, note)
        if not _withdraw:
            return -1, "failed", {}
        result = _withdraw.api_to_dict()

        # 客户银行卡信息
        _card = customer_card.Card.on_find_card_by_id(card_id)
        card_info = _card.to_dict()
        country_info = utils.find_country_by_code(
            card_info["bank_country_code"])
        card_info["country_info"] = country_info
        result["card_info"] = card_info
        if result["bm_payment_photo"]:
            result["bm_payment_photo"] = [
                "/w/" + x for x in result["bm_payment_photo"].split(",")
            ]
        return 0, "success", result
Esempio n. 6
0
    def login(self, data):
        form = LoginForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        email = form.data["email"]
        password = form.data['password']
        _customer = customer.Customer.on_find_customer_by_email(email)
        if not self._can_do_login(_customer):
            return -1, "no such a user", {}

        if _customer.login_password == utils.hash_password(password):
            cust_id, cust_type = _customer.cust_id, _customer.cust_type
            token = utils.gen_access_token()
            session = mc.CustomerSession(cust_id, cust_type)
            mc.MemSession.set_customer(token, session)
            result = {"token": token}
            return 0, "success", result
        return -1, "incorrect password", {}
Esempio n. 7
0
    def register_new_transfer(self, data):
        form = RegisterNewTransferForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        token = form.data["token"]
        cust = mc.MemSession.get_customer(token)
        if not cust:
            return -1, "invalid session", {}
        # 检查账户是否存在
        cust_id = cust.cust_id
        cust = customer.Customer.on_find_customer_by_id(cust_id)
        if not cust:
            return -1, "no permission", {}

        trade_password = utils.hash_password(form.data["trade_password"])
        if trade_password != cust.trade_password:
            return -1, "incorrect password", {}

        company_id = cust.company_id
        transfer_type = cust.cust_type
        amount = form.data["amount"]
        currency = form.data["currency"]
        note = form.data["note"]

        dst_contact_id = form.data["dst_contact_id"]
        dst_bank_card_name = form.data["dst_bank_card_name"]
        dst_bank_card_number = form.data["dst_bank_card_number"]
        dst_bank_swift_code = form.data["dst_bank_swift_code"]
        dst_bank_name = form.data["dst_bank_name"]
        dst_bank_city = form.data["dst_bank_city"]
        dst_bank_country = form.data["dst_bank_country"]
        dst_bank_address = form.data["dst_bank_address"]

        _country = utils.find_country_by_name(dst_bank_country)
        if not _country:
            return -1, "invalid country", {}
        dst_bank_country_code = _country["alpha_2"]

        # 检查联系人是否正确
        can_do_transfer = False
        if dst_contact_id:
            _contact = customer_contact.Contact.on_find_contact_by_id(
                dst_contact_id)
            if _contact and _contact.cust_id == cust_id:
                can_do_transfer = True
        if not can_do_transfer:
            return -1, "invalid contact", {}

        # TODO: 检查银行卡信息

        # TODO: 检查交易合法性

        # TODO: 扫描件
        upload_business_contract_filelist = []
        upload_invoice_filelist = []
        upload_receipt_form_filelist = []
        upload_path = const.TRANSFER_FILE_PATH
        for upload_type in ("upload_business_contract", "upload_invoice",
                            "upload_receipt_form"):
            file_metas = self.request.files.get(upload_type, None)
            if not file_metas:
                return -1, "failed: no image files", {}

            for meta in file_metas:
                # meta = file_metas[0]
                filename = str(uuid.uuid4())
                file_path = os.path.join(upload_path, filename)

                try:
                    with open(file_path, "wb") as fp:
                        fp.write(meta["body"])
                    img_type = imghdr.what(file_path)
                    if not img_type:
                        os.remove(file_path)
                        return -1, "failed: no image files", {}
                    filename = "{}.{}".format(filename, img_type)
                    new_file_path = "{}".format(
                        os.path.join(const.TRANSFER_FILE_PATH, filename))
                    os.rename(file_path, new_file_path)
                except Exception as why:
                    logging.error(why)
                    return -1, "failed: something wrong on server", {}
                if upload_type == "upload_business_contract":
                    upload_business_contract_filelist.append(filename)
                elif upload_type == "upload_invoice":
                    upload_invoice_filelist.append(filename)
                else:
                    upload_receipt_form_filelist.append(filename)
        upload_business_contract = ",".join(upload_business_contract_filelist)
        upload_invoice = ",".join(upload_invoice_filelist)
        upload_receipt_form = ",".join(upload_receipt_form_filelist)
        fee_proportion = 0.038
        fee_amount = 14089.00
        _transfer = transfer.Transfer.on_register_new_transfer(
            company_id, transfer_type, cust_id, amount, currency, note,
            dst_contact_id, dst_bank_card_name, dst_bank_card_number,
            dst_bank_swift_code, dst_bank_name, dst_bank_country_code,
            dst_bank_city, dst_bank_address, upload_business_contract,
            upload_invoice, upload_receipt_form, fee_proportion, fee_amount)
        if not _transfer:
            return -1, "failed", {}
        contact_info = {}
        if _transfer.dst_contact_id:
            _contact = customer_contact.Contact.on_find_contact_by_id(
                _transfer.dst_contact_id)
            if _contact:
                contact_info = _contact.to_dict()
        dst_bank_country_info = utils.find_country_by_code(
            _transfer.dst_bank_country_code)

        result = _transfer.api_to_dict()
        result["contact_info"] = contact_info
        result["dst_bank_country_info"] = dst_bank_country_info
        if result["upload_business_contract"]:
            result["upload_business_contract"] = [
                "/t/" + i
                for i in result["upload_business_contract"].split(",")
            ]
        if result["upload_invoice"]:
            result["upload_invoice"] = [
                "/t/" + i for i in result["upload_invoice"].split(",")
            ]
        if result["upload_receipt_form"]:
            result["upload_receipt_form"] = [
                "/t/" + i for i in result["upload_receipt_form"].split(",")
            ]
        if result["dst_bm_payment_photo"]:
            result["dst_bm_payment_photo"] = [
                "/t/" + i for i in result["dst_bm_payment_photo"].split(",")
            ]
        return 0, "success", result
Esempio n. 8
0
def init_database():
    password = utils.hash_password("123456")
    print "=================== Init database begin ==================="

    # 1. 创建角色
    role.Role.on_add_new_role(role_name=const.ROLE_SYSTEM,
                              description="System Role",
                              access_urls="")
    role.Role.on_add_new_role(role_name=const.ROLE_ADMIN,
                              description="Administrator Role",
                              access_urls="")
    role.Role.on_add_new_role(role_name=const.ROLE_AGENT,
                              description="Agent Role",
                              access_urls="")
    role.Role.on_add_new_role(role_name=const.ROLE_FINANCE,
                              description="Finance Role",
                              access_urls="")

    system_role = role.Role.on_find_role_by_name(role_name=const.ROLE_SYSTEM)
    admin_role = role.Role.on_find_role_by_name(role_name=const.ROLE_ADMIN)
    agent_role = role.Role.on_find_role_by_name(role_name=const.ROLE_AGENT)
    finance_role = role.Role.on_find_role_by_name(role_name=const.ROLE_FINANCE)
    if None in (admin_role, agent_role, finance_role, system_role):
        print "init database failed: can't create default roles"
        return

    # 2. 创建公司
    c_china = utils.find_country_by_name("China")
    c_hongkong = utils.find_country_by_name("Hong Kong")

    company.Company.on_add_new_company(u"BlockMov (China)", c_china["alpha_2"],
                                       u"Shanghai", u"深圳怡景大厦-东座32号",
                                       u"Asia/Shanghai", u"USD", u"")
    company.Company.on_add_new_company(u"BlockMov (Hong Kong)",
                                       c_hongkong["alpha_2"], u"Hong Kong",
                                       u"34 Jordan Rd, Yau Ma Tei, 香港",
                                       u"Asia/Hong_Kong", u"USD", u"")

    china_com = company.Company.on_find_company_by_name(u"BlockMov (China)")
    hk_com = company.Company.on_find_company_by_name(u"BlockMov (Hong Kong)")

    # 设置为正常营业状态
    company.Company.on_set_state(china_com.company_id, "00")
    company.Company.on_set_state(hk_com.company_id, "00")

    # 设置营业时间
    opening_hours = datetime.time(9, 0)
    closing_hours = datetime.time(9, 0)
    company.Company.on_set_business_hours(china_com.company_id, opening_hours,
                                          closing_hours)
    company.Company.on_set_business_hours(hk_com.company_id, opening_hours,
                                          closing_hours)

    # 添加公司银行卡
    card_name = u"BlockMov"
    card_number = u"6229-8888-8888-8888"
    bank_swift_code = u"HSMBHKHHXXX"
    bank_name = u"HSBC GLOBAL ASSET MANAGEMENT HOLDINGS (BAHAMAS) LIMITED"
    # bank_country_code = c_hongkong["alpha_2"]
    # bank_city = "Hong Kong"
    bank_address = u"HSBC MAIN BUILDING FLOOR 22 1 QUEEN'S ROAD CENTRAL, 香港旺角弥敦道673号"
    currency = u"USD"
    amount = decimal.Decimal("9867100889892998982.00")

    company_card.BankCard.on_add_new_bank_card(hk_com.company_id, card_name,
                                               card_number, bank_swift_code,
                                               bank_name, bank_address,
                                               currency, amount)

    # 3. 创建BM用户
    user.User.on_add_new_user("system", "*****@*****.**", password,
                              "138999999999", "Chinese", system_role.role_id,
                              hk_com.company_id)
    user.User.on_add_new_user("admin", "*****@*****.**", password,
                              "138999999999", "Chinese", admin_role.role_id,
                              hk_com.company_id)
    user.User.on_add_new_user("finance", "*****@*****.**", password,
                              "138999999999", "Chinese", finance_role.role_id,
                              hk_com.company_id)
    user.User.on_add_new_user("agent", "*****@*****.**", password,
                              "138999999999", "Chinese", agent_role.role_id,
                              hk_com.company_id)

    user.User.on_add_new_user("admin_hk", "*****@*****.**", password,
                              "138999999999", "English", admin_role.role_id,
                              hk_com.company_id)
    user.User.on_add_new_user("finance_hk", "*****@*****.**",
                              password, "138999999999", "English",
                              finance_role.role_id, hk_com.company_id)
    user.User.on_add_new_user("agent_hk", "*****@*****.**", password,
                              "138999999999", "English", agent_role.role_id,
                              hk_com.company_id)

    # 8. 添加收费标准
    fee_method_id_list = []
    src_time_begin = datetime.datetime.time(
        datetime.datetime(2018, 9, 9, hour=9, minute=0))
    src_time_end = datetime.datetime.time(
        datetime.datetime(2018, 9, 9, hour=18, minute=0))
    dst_time_begin = datetime.datetime.time(
        datetime.datetime(2018, 9, 9, hour=11, minute=0))
    dst_time_end = datetime.datetime.time(
        datetime.datetime(2018, 9, 9, hour=15, minute=0))

    for c_name in ("Hong Kong", "Congo", "United States", "United Kingdom",
                   "Germany", "United Arab Emirates"):
        c_country = utils.find_country_by_name(c_name)
        m1 = fee_method.FeeMethod.on_add_new_fee_method(
            hk_com.company_id, "00", hk_com.country, c_country["alpha_2"],
            "1,2,3,4,5", src_time_begin, src_time_end, dst_time_begin,
            dst_time_end, 30, 240)
        fee_method_id_list.append(m1.fee_method_id)

    for m in fee_method_id_list:
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 0.0, 100000.00, 0.03,
                                                   100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 100000.0, 200000.00,
                                                   0.03, 100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 200000.0, 300000.00,
                                                   0.03, 100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 300000.0, 400000.00,
                                                   0.03, 100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 400000.0, 500000.00,
                                                   0.03, 100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 500000.0, 1000000.00,
                                                   0.03, 100, 5000000.0)
        fee_detail.FeeDetail.on_add_new_fee_detail(m, 1000000.0, 20000000.00,
                                                   0.03, 100, 5000000.0)

    # 4. 创建客户
    customer.Customer.on_add_new_customer(hk_com.company_id, "00",
                                          "*****@*****.**", password)

    demo = customer.Customer.on_find_customer_by_email("*****@*****.**")

    # 5. 创建钱包
    for u in (demo, ):
        wallet_name = str(uuid.uuid4())
        if not u.wallet:
            wallet = customer_wallet.Wallet.on_add_new_wallet(
                u.cust_id, wallet_name)
            print "create new wallet: %s" % (wallet.to_dict())

    demo = customer.Customer.on_find_customer_by_email("*****@*****.**")
    # 6. 创建银行卡
    for u in (demo, ):
        wallet_id = u.wallet.wallet_id
        bank_card_name = u"Demo Account"
        bank_card_number = u"6229-4688-8888-8888"
        bank_swift_code = u"HSMBHKHHXXX"
        bank_name = u"HSBC GLOBAL ASSET MANAGEMENT HOLDINGS (BAHAMAS) LIMITED"
        bank_country_code = c_hongkong["alpha_2"]
        bank_city = "Hong Kong"
        bank_address = u"HSBC MAIN BUILDING FLOOR 22 1 QUEEN'S ROAD CENTRAL, 香港旺角弥敦道673号"
        customer_card.Card.on_add_new_card(wallet_id, bank_card_name,
                                           bank_card_number, bank_swift_code,
                                           bank_name, bank_country_code,
                                           bank_city, bank_address)

    # 7. 添加联系人
    for u in (demo, ):
        contact_name = u"Li Lei"
        bank_card_name = u"Li Lei"
        bank_card_number = u"6225-0000-0000-0000"
        bank_swift_code = u"SCSEHKH1XXX"
        bank_name = u"Standard Chartered Bank"
        bank_country_code = c_hongkong["alpha_2"]
        bank_city = u"Hong Kong"
        bank_address = u"BANK OF CHINA TOWER FLOOR 23 1 GARDEN ROAD CENTRAL HONG KONG"
        _contact = customer_contact.Contact.on_add_new_contact(
            u.cust_id, contact_name, bank_card_name, bank_card_number,
            bank_swift_code, bank_name, bank_country_code, bank_city,
            bank_address)

    # 测试消息历史
    for u in (demo, ):
        for i in xrange(10):
            customer_message.CustomerMessage.on_add_new_message(
                message_type="deposit",
                message_content=u"您充值了$5000USD",
                cust_id=u.cust_id)
            customer_message.CustomerMessage.on_add_new_message(
                message_type="withdraw",
                message_content=u"您提现了$5000USD",
                cust_id=u.cust_id)
            customer_message.CustomerMessage.on_add_new_message(
                message_type="transfer",
                message_content=u"转账成功",
                cust_id=u.cust_id)
            customer_message.CustomerMessage.on_add_new_message(
                message_type="payment",
                message_content=u"您在京东商场消费了3000元",
                cust_id=u.cust_id)
            customer_message.CustomerMessage.on_add_new_message(
                message_type="notify",
                message_content=u"此处是通知",
                cust_id=u.cust_id)

    print "=================== Init database end ==================="
Esempio n. 9
0
    def register_new_deposit(self, data):
        form = RegisterNewDepositForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        token = form.data["token"]
        cust = mc.MemSession.get_customer(token)
        if not cust:
            return -1, "invalid session", {}
        cust_id = cust.cust_id

        # step 1, 检查账户是否存在
        cust = customer.Customer.on_find_customer_by_id(cust_id)
        if not cust:
            return -1, "no permission", {}

        # step 1, 处理图片资料
        upload_path = const.DEPOSIT_FILE_PATH
        file_metas = self.request.files.get("credence_photo", None)
        if not file_metas:
            return -1, "failed: no image files", {}

        filelist = []
        for meta in file_metas:
            # meta = file_metas[0]
            filename = str(uuid.uuid4())
            file_path = os.path.join(upload_path, filename)

            try:
                with open(file_path, "wb") as fp:
                    fp.write(meta["body"])
                img_type = imghdr.what(file_path)
                if not img_type:
                    os.remove(file_path)
                    return -1, "failed: no image files", {}
                filename = "{}.{}".format(filename, img_type)
                new_file_path = "{}".format(
                    os.path.join(const.DEPOSIT_FILE_PATH, filename))
                os.rename(file_path, new_file_path)
                filelist.append(filename)
            except Exception as why:
                logging.error(why)
                return -1, "failed: something wrong on server", {}

        # step 2, 处理文字资料
        form = RegisterNewDepositForm.from_json(data)
        if not form.validate():
            return -1, str(form.errors), {}

        trade_password = utils.hash_password(form.data["trade_password"])
        if trade_password != cust.trade_password:
            return -1, "incorrect password", {}

        # step 4, 创建充值单
        cust_type = cust.cust_type
        cust_first_name = cust.cust_first_name
        cust_last_name = cust.cust_last_name

        bank_name = form.data["bank_name"]
        swift_code = form.data["swift_code"]
        bank_card_name = form.data["bank_card_name"]
        bank_card_number = form.data["bank_card_number"]
        amount = form.data["amount"]
        currency = form.data["currency"]
        payment_date = form.data["payment_date"]
        credence_photo = ",".join(filelist)
        note = form.data["note"]

        company_id = cust.company_id
        company_name = cust.company.company_name

        bm_card_id = form.data["bm_card_id"]
        bm_card_name = form.data["bm_card_name"]
        bm_card_number = form.data["bm_card_number"]
        bm_bank_swift_code = form.data["bm_bank_swift_code"]
        bm_bank_name = form.data["bm_bank_name"]
        bm_bank_address = form.data["bm_bank_address"]

        _deposit = deposit.Deposit.on_register_new_deposit(
            cust_id, cust_type, cust_first_name, cust_last_name, bank_name,
            swift_code, bank_card_name, bank_card_number, amount, currency,
            payment_date, credence_photo, company_id, company_name, bm_card_id,
            bm_card_name, bm_card_number, bm_bank_swift_code, bm_bank_name,
            bm_bank_address, note)
        if not _deposit:
            logging.info("register new deposit failed")
            return -1, "failed", {}
        result = _deposit.api_to_dict()
        if result["credence_photo"]:
            result["credence_photo"] = [
                "/d/" + x for x in result["credence_photo"].split(",")
            ]
        return 0, "success", result