Beispiel #1
0
def get_user_bankcard(user_id, bankcard_id):
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    account_user_id = user_map.account_user_id

    bc = dba.query_bankcard_by_id(bankcard_id)
    if bc is None or bc.user_id != account_user_id:
        return response.bad_request(
            msg='user [{0}] has no bankcard [{1}]'.format(
                user_id, bankcard_id))
    return response.success(data=bc.to_dict())
Beispiel #2
0
def query_bin(card_no):
    try:
        card_bin = bankcard.query_bin(card_no)
        return response.success(**(card_bin.to_dict()))
    except Exception as e:
        logger.exception(e)
        return response.bad_request(msg=e.message)
Beispiel #3
0
def draw_cheque(user_id):
    data = request.values
    channel = request.channel
    order_id = data.get('order_id')
    amount = data['amount']
    valid_seconds = data.get('valid_seconds', 30)
    cheque_type = data.get('cheque_type', ChequeType.INSTANT)
    info = data.get('info', '')
    client_notify_url = data.get('notify_url', '')

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    from_id = user_map.account_user_id

    try:
        cheque_record = cheque.draw_cheque(channel, from_id, amount, order_id,
                                           valid_seconds, cheque_type, info,
                                           client_notify_url)
        return response.success(sn=cheque_record.sn,
                                cash_token=cheque_record.cash_token,
                                amount=cheque_record.amount)
    except Exception as e:
        logger.exception(e)
        return response.fail(code=1, msg=e.message)
Beispiel #4
0
def prepare_params(tx, payment_scene, vas_name, extra_params=None):
    from api_x.zyt.user_mapping.auth import vas_payment_is_enabled
    from api_x.zyt.evas.payment import params

    # TODO: log to db.
    logger.info("[PAYMENT PARAMS] {0}, {1}, {2}, {3}, {4}".format(
        payment_scene, vas_name, tx.type, tx.sn, extra_params))

    if tx.state != PaymentTxState.CREATED:
        return response.processed()

    payment_entity = gen_payment_entity(tx)
    if payment_entity is None:
        return response.bad_request()

    # 目前暂时用来控制zyt余额支付仅限于lvye_pay_site.
    if not vas_payment_is_enabled(payment_entity, vas_name):
        return response.refused(msg='[{0}] is not allowed for [{1}]'.format(
            vas_name, tx.channel_name))

    try:
        payment_type, prepared_params = params.prepare(
            payment_scene, vas_name, payment_entity, extra_params=extra_params)
        return response.success(params=prepared_params,
                                vas_name=vas_name,
                                payment_type=payment_type)
    except Exception as e:
        logger.exception(e)
        return response.fail(msg=e.message)
Beispiel #5
0
def get_account_user(user_id):
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(msg='user not exists: [{0}]'.format(user_id))

    return response.success(account_user_id=user_map.account_user_id)
Beispiel #6
0
def query_bin(card_no):
    try:
        bankcard_bin = bankcard.query_bin_cache(card_no)
        card_bin_info = bankcard_bin.to_dict()
        return response.success(data=card_bin_info)
    except Exception as e:
        logger.exception(e)
        return response.bad_request(msg=e.message)
Beispiel #7
0
def withdraw(user_id):
    data = request.values
    channel = request.channel
    order_id = data.get('order_id')
    bankcard_id = data['bankcard_id']
    amount = data['amount']
    client_notify_url = data.get('notify_url', '')

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    from_user_id = user_map.account_user_id

    try:
        amount_value = Decimal(amount)
    except Exception as e:
        logger.error('bad amount value: [{0}]: {1}'.format(amount, e.message))
        return response.bad_request(
            msg='amount value error: [{0}]'.format(amount))

    fee = calc_user_withdraw_fee(from_user_id, amount_value)

    try:
        bankcard = dba.query_bankcard_by_id(bankcard_id)
        if bankcard is None:
            return response.fail(
                msg='bankcard not found: [{0}]'.format(bankcard_id))
        if bankcard.user_id != from_user_id:
            return response.fail(
                msg='bankcard [{0}] is not bound to user [{1}]'.format(
                    bankcard_id, from_user_id))

        withdraw_record = apply_to_withdraw(channel, order_id, from_user_id,
                                            bankcard, amount_value, fee,
                                            client_notify_url, data)
        log_user_withdraw(from_user_id, withdraw_record.sn, bankcard_id,
                          withdraw_record.amount,
                          withdraw_record.actual_amount, withdraw_record.fee)
        return response.success(sn=withdraw_record.sn,
                                actual_amount=withdraw_record.actual_amount,
                                fee=withdraw_record.fee)
    except Exception as e:
        logger.exception(e)
        return response.fail(code=1, msg=e.message)
Beispiel #8
0
 def wrapper(*args, **kwargs):
     try:
         referrer = request.referrer
         parts = urlparse.urlparse(referrer)
         if parts.netloc in netlocs:
             return f(*args, **kwargs)
     except Exception as e:
         logger.exception(e)
     if ex_callback:
         return ex_callback(*args, **kwargs)
     return response.bad_request()
Beispiel #9
0
def confirm_guarantee_payment():
    from api_x.utils import response
    data = request.params
    channel = request.channel
    order_id = data['order_id']

    try:
        pay.confirm_payment(channel, order_id)
        return response.success()
    except Exception as e:
        logger.exception(e)
        return response.bad_request(msg=e.message)
Beispiel #10
0
def list_user_bankcards(user_id):
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    account_user_id = user_map.account_user_id

    bankcards = dba.query_all_bankcards(account_user_id)
    bankcards = [bc.to_dict() for bc in bankcards]
    return response.success(data=bankcards)
Beispiel #11
0
def query_withdraw(user_id, sn):
    data = request.values
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    from_user_id = user_map.account_user_id

    tx = get_tx_by_sn(sn)
    if tx is None:
        return response.not_found(
            msg="withdraw tx not exits: [sn: {0}]".format(sn))
    withdraw_record = tx.record

    if withdraw_record.from_user_id != from_user_id:
        return response.bad_request('user [{0}] has no withdraw [{1}]'.format(
            user_id, sn))

    if tx.state == WithdrawTxState.SUCCESS:
        code = 0
    elif tx.state == WithdrawTxState.FAILED:
        code = 1
    else:
        code = 2

    params = {
        'code': code,
        'user_id': user_id,
        'sn': tx.sn,
        'amount': withdraw_record.amount,
        'actual_amount': withdraw_record.actual_amount,
        'fee': withdraw_record.fee
    }

    return response.success(data=params)
Beispiel #12
0
def cancel_cheque(user_id):
    data = request.values
    channel = request.channel
    sn = data['sn']

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))

    try:
        cheque.cancel_cheque(channel, user_map.account_user_id, sn)
        return response.success()
    except Exception as e:
        logger.exception(e)
        return response.fail(code=1, msg=e.message)
Beispiel #13
0
def query_user_balance(user_id):
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    account_user_id = user_map.account_user_id

    balance = account_user.get_user_cash_balance(account_user_id)
    return response.success(
        data={
            'total': balance.total,
            'available': balance.available,
            'frozen': balance.frozen
        })
Beispiel #14
0
    def wrapper(*args, **kwargs):
        try:
            params = request.view_args
            hashed_sn = params.get('sn')
            tx, expire_hash = Transaction.get_tx_from_hashed_sn(hashed_sn)
            if tx is None:
                # 没找到
                return response.not_found()
            if not tx.check_expire_hash(expire_hash):
                # 过期
                return response.expired(msg='expired, please retry request pay.')

            return f(tx, *args, **kwargs)
        except Exception as e:
            logger.exception(e)
        # 异常
        return response.bad_request()
Beispiel #15
0
def list_transactions(user_id):
    data = request.args
    channel = request.channel
    role = data.get('role')
    page_no = int(data.get('page_no', 1))
    page_size = int(data.get('page_size', 20))
    q = data.get('q')
    vas_name = data.get('vas_name')

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(msg='user not exists: [{0}]'.format(user_id))
    account_user_id = user_map.account_user_id

    total_num, utxs = query_user_transactions(account_user_id, role, page_no, page_size, q, vas_name)
    txs = [_get_tx(utx) for utx in utxs]

    return response.success(data={'total': total_num, 'txs': txs})
Beispiel #16
0
def cash_cheque(user_id):
    data = request.values
    channel = request.channel
    cash_token = data['cash_token']

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    to_id = user_map.account_user_id

    try:
        cheque_record = cheque.cash_cheque(channel, to_id, cash_token)
        return response.success(sn=cheque_record.sn,
                                amount=cheque_record.amount)
    except Exception as e:
        logger.exception(e)
        return response.fail(code=1, msg=e.message)
Beispiel #17
0
        def wrapper(*args, **kwargs):
            try:
                params = {}
                params.update(request.values.items())
                params.update(request.view_args)

                logger.info("[{0}] {1}: {2}".format(entry_path, request.url, params))
                # check perm
                channel_name = params.get('channel_name')
                if channel_name is None:
                    return response.fail(msg='channel_name is needed.')

                channel = get_channel_by_name(channel_name)
                if channel is None:
                    msg = 'channel not exits: [{0}]'.format(channel_name)
                    return response.fail(msg=msg)

                try:
                    _verify_channel_perm(channel, entry_path)
                except EntryAuthError as e:
                    return response.refused(msg=e.message)

                # verify sign
                sign_type = params['sign_type']
                # 这里的主要作用是验签, 只需要channel_pub_key或md5_key
                try:
                    signer = Signer('key', 'sign', channel.md5_key, None, channel.public_key)
                    if not signer.verify(params, sign_type):
                        logger.warn('sign verify error: [{0}]'.format(params))
                        msg = 'sign error.'
                        return response.refused(msg=msg)
                except Exception as e:
                    logger.exception(e)
                    msg = 'sign error.'
                    return response.refused(msg=msg)
            except Exception as e:
                logger.exception(e)
                return response.bad_request(msg=e.message)

            logger.info("[{0}] verify done.".format(entry_path))
            request.__dict__['channel'] = channel
            request.__dict__['params'] = params
            return f(*args, **kwargs)
Beispiel #18
0
def query_info(tx, payment_scene, extra_params=None):
    from api_x.zyt.evas.payment import infos

    # TODO: log to db.
    logger.info("[PAYMENT INFO] {0}, {1}, {2}, {3}".format(
        payment_scene, tx.type, tx.sn, extra_params))
    payment_entity = gen_payment_entity(tx)
    if payment_entity is None:
        return response.bad_request()

    try:
        data, evases, vas_infos = infos.prepare(payment_scene,
                                                payment_entity,
                                                extra_params=extra_params)
        return response.success(info=data,
                                activated_evas=evases,
                                vas_infos=vas_infos)
    except Exception as e:
        logger.exception(e)
        return response.fail(msg=e.message)
Beispiel #19
0
def bind_bankcard(user_id):
    data = request.params
    channel = request.channel
    card_no = data['card_no']
    acct_name = data['account_name']
    is_corporate_account = to_bool(data['is_corporate_account'])
    province_code = data['province_code']
    city_code = data['city_code']
    brabank_name = data['branch_bank_name']

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))
    account_user_id = user_map.account_user_id

    bankcard_id = bankcard.bind_bankcard(account_user_id, card_no, acct_name,
                                         is_corporate_account, province_code,
                                         city_code, brabank_name)
    return response.success(id=bankcard_id)
Beispiel #20
0
def list_cheque(user_id):
    data = request.values
    channel = request.channel

    user_map = channel.get_user_map(user_id)
    if user_map is None:
        return response.bad_request(
            msg='user not exists: [{0}]'.format(user_id))

    try:
        cheque_records = cheque.list_cheque(channel, user_map.account_user_id)
        return response.success(cheques=[{
            'sn': cr.sn,
            'info': cr.info,
            'type': cr.type,
            'amount': cr.amount,
            'cash_token': cr.cash_token,
            'expired_at': cr.expired_at,
            'created_on': cr.created_on
        } for cr in cheque_records])
    except Exception as e:
        logger.exception(e)
        return response.fail(code=1, msg=e.message)