Ejemplo n.º 1
0
 def _validated(self, value):
     try:
         division = Division.search(value)
     except ValueError:
         raise ValidationError('请选择正确的银行卡开户城市')
     if not division.is_prefecture:
         raise ValidationError(u'请选择正确的银行卡开户城市')
Ejemplo n.º 2
0
def test_history_data():
    get(522401, year=2010) == Division(522401, '毕节市', 2010)

    with raises(ValueError) as error:
        get(522401)
    assert error.value.args[0] == '522401 is not valid division code'

    with raises(ValueError) as error:
        get(110101, 2000)
    assert error.value.args[0].startswith('year must be in')
Ejemplo n.º 3
0
 def _validate(cls, user_id, division_id, street):
     if not Account.get(user_id):
         raise ValueError('user not found')
     if not Division.search(division_id):
         raise ValueError('administrative division not found')
     # street should be unicode instead of bytes
     if not street:
         raise ValueError('street information too short')
     if len(unicode(street)) > 80:
         raise ValueError('street information too long')
Ejemplo n.º 4
0
def test_hashable():
    division_set = set([
        Division(110101, '东城区'),
        Division(110000, '北京市'),
        Division(110101, '东城区'),
        Division(110101, '东城区', 2006),
    ])
    assert division_set == set([
        Division(110101, '东城区'),
        Division(110000, '北京市'),
        Division(110101, '东城区', 2006),
    ])
Ejemplo n.º 5
0
def bind_bankcard():
    """绑定新的银行卡或更新已有银行卡.
    :query partner: 可选参数, 按合作方支持情况限制返回结果. 目前可为:

                    - ``"zw"`` 指旺 (攒钱助手)
                    - ``"xm"`` 指旺 (攒钱助手)
                    - ``"zs"`` 中山证券 (零钱包)


    :request: :class:`.BankCardRequestSchema`
    :response: :class:`.BankCardSchema`
    :reqheader Authorization: OAuth 2.0 Bearer Token
    :status 200: 绑定或更新成功
    :status 403: 绑定被拒
    """
    bankcard_schema = BankCardSchema(strict=True)
    bankcard_request_schema = BankCardRequestSchema(strict=True)
    partner = request.args.get('partner', type=Partner)
    data = bankcard_request_schema.load(request.get_json(force=True)).data
    result = {
        'mobile_phone': data['mobile_phone'],
        'card_number': data['bankcard_number'],
        'bank_id': data['bank_id'],
        'city_id': data['division_id'],
        'province_id': Division.search(data['division_id']).province.code,
        'local_bank_name': data.get('local_bank_name'),
    }
    #: 针对零钱包,若用户已经绑定银行卡,只返回已绑定卡
    if partner is Partner.zs:
        bankcard_list = [
            bankcard for bankcard in g.bankcard_manager.get_all()
            if is_bound_bankcard(bankcard, zhongshan)
        ]
        if bankcard_list:
            abort(403, u'零钱包暂时只支持绑定一张银行卡, 详情见通知中心')
    try:
        bankcard = g.bankcard_manager.create_or_update(**result)
        inject_bankcard_amount_limit(partner, [bankcard])
    except (BankConflictError, CardConflictError) as e:
        return abort(403, unicode(e))

    return jsonify(success=True, data=bankcard_schema.dump(bankcard).data)
Ejemplo n.º 6
0
def test_searching(code, name, year):
    division = Division.search(code)
    assert division.name == name
    assert division.year == year
Ejemplo n.º 7
0
def test_comparable():
    assert get(110101) == Division(110101, '东城区')
    assert get(110101) != Division(110000, '北京市')
    assert get(110101, year=2006) != Division(110101, '东城区')
Ejemplo n.º 8
0
def test_searching(code, name, year):
    division = Division.search(code)
    assert division.name == name
    assert division.year == year
Ejemplo n.º 9
0
 def division(self):
     return Division.search(self.division_id)
Ejemplo n.º 10
0
 def process_formdata(self, valuelist):
     if valuelist:
         self.data = Division.search(valuelist[0])
         if not self.data:
             raise ValueError(self._error)
Ejemplo n.º 11
0
 def validate_district(self, field):
     try:
         Division.search(field.data)
     except ValueError:
         raise ValidationError('地区不存在')
Ejemplo n.º 12
0
def get_division(code, year=None):
    try:
        return Division.get(code, year)
    except ValueError:
        return
Ejemplo n.º 13
0
def all_division(year=None):
    for code in data.get(year, []):
        if unicode(code) in excluded_division_codes:
            continue  # restricted to China Mainland
        yield Division.get(code, year)
Ejemplo n.º 14
0
 def province(self):
     return Division.search(self.province_id)
Ejemplo n.º 15
0
 def prefecture(self):
     return Division.search(self.city_id)
Ejemplo n.º 16
0
def subscribe_product(user,
                      product,
                      bankcard,
                      order_amount,
                      pay_amount,
                      due_date,
                      wrapped_product=None,
                      coupon=None,
                      pocket_deduction_amount=0):
    """申购产品"""
    # 检查礼券是否可用、返现账户抵扣是否可用、订单是否可建
    if coupon:
        coupon.check_before_use(wrapped_product or product, order_amount)
    if pocket_deduction_amount > 0:
        FirewoodWorkflow(user.id_).check_deduction_enjoyable(
            wrapped_product or product, order_amount, pocket_deduction_amount)
    wrapped_product_id = wrapped_product.id_ if wrapped_product else None
    ZhiwangOrder.check_before_adding(user.id_, bankcard.id_,
                                     product.product_id, order_amount,
                                     wrapped_product_id)

    # 获取订单优惠信息并检查合法性
    hike_list = collect_profit_hikes(user, coupon, pocket_deduction_amount,
                                     wrapped_product)
    rate_bonus = max([h.annual_rate
                      for h in hike_list]) if hike_list else Decimal('0')
    deduction_bonus = sum([h.deduction_amount for h in hike_list])
    assert rate_bonus < Decimal('5.0')  # 指旺最高加息限制
    assert order_amount - deduction_bonus == pay_amount

    try:
        # 向指旺发起申购请求
        response = zhiwang.order_apply_with_coupon(
            ZhiwangAccount.get_by_local(user.id_).zhiwang_id,  # 用户的指旺ID
            product.product_id,  # 用户认购的产品
            rate_bonus,  # 加息值
            order_amount,  # 订单金额
            pay_amount,  # 实际支付金额
            bankcard.card_number,  # 银行卡号
            int(bankcard.bank.zwlib_id),  # 银行ID
            bankcard.mobile_phone,  # 银行预留手机号
            due_date.strftime('%Y-%m-%d')
            if due_date else None,  # TODO: 认购产品到期日
            Division.get(bankcard.province_id, year=2006).name,  # 银行卡开卡省份
            Division.get(bankcard.city_id, year=2006).name)  # 银行卡开卡市
    except RemoteError as e:
        err_msg = e.args[1]
        err_msg = ZWLIB_ERROR_MAPPING.get(err_msg, err_msg)
        raise SubscribeProductError(u'申购产品失败: %s' % err_msg)

    assert pay_amount == response.pay_amount

    # 创建订单
    order = ZhiwangOrder.add(user.id_, product.product_id, bankcard.id_,
                             order_amount, response.pay_amount,
                             response.expect_interest,
                             response.interest_start_date,
                             response.interest_end_date, response.order_code,
                             response.pay_code, wrapped_product_id)
    # 创建优惠记录
    for hike in hike_list:
        # FIXME: the operation of hikes should be managed in one session
        Hike.add(user.id_, order.id_, hike.kind, hike.annual_rate,
                 hike.deduction_amount)
    # 订单预绑定礼券
    if coupon:
        CouponUsageRecord.add(coupon, user, provider_zhiwang, order)
    # 创建抵扣使用记录
    if pocket_deduction_amount > 0:
        FirewoodBurning.add(user, pocket_deduction_amount,
                            FirewoodBurning.Kind.deduction, provider_zhiwang,
                            order.id_)
    return order