Exemplo n.º 1
0
def confirm_payment(request):
    try:
        if request.method == 'POST':
            order_id = request.POST['order_id']
            keyword = request.POST['search_keyword']
            ordermanager.confirm_purchase_order(order_id,
                                                request.user.username)
            messages.success(request, '确认付款,交易完成')
            useraccountInfo = useraccountinfomanager.get_user_accountInfo(
                request.user, 'AXFund')
            request.session[REQ_KEY_USERACCOUNTINFO] = useraccountInfo.tojson()
            buyorders = ordermanager.search_orders(keyword, None, None)
        else:
            return HttpResponseBadRequest('确认买单只能允许POST')
        return render(
            request, 'trading/admin/transmanagement.html', {
                'useraccountInfo': useraccountInfo,
                REQ_KEY_USERNAME: request.user.username,
                'search_keyword': keyword,
                'buyorders': buyorders,
                'bootstrap_datepicker': True
            })
    except Exception as e:
        error_msg = '确认付款遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 2
0
def listusertransactions(request):
    try:
        username = request.user.username
        userId = request.user.id
        trans = ordermanager.get_user_transactions(userId, 'AXFund')
        pending_trans = []
        transactions = []
        for tran in trans:
            if tran.status == 'PENDING':
                pending_trans.append(create_trans_list_item(tran, 'AXFund'))
            else:
                transactions.append(create_trans_list_item(tran, 'AXFund'))
        accountinfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund', True)
        return render(
            request, 'trading/translist.html', {
                'pending_trans': pending_trans,
                'transactions': transactions,
                'useraccountInfo': accountinfo
            })

    except Exception as e:
        error_msg = '出售美基金遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 3
0
def sell_axfund(request):
    try:
        sitesettings = context_processor.settings(request)['settings']
        accountinfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund')
        if len(accountinfo.paymentmethods) == 0:
            messages.error(request, '请先注册支付方式再挂卖单')
            return redirect('accountinfo')
        if len(accountinfo.paymentmethods[0].account_at_provider) == 0:
            messages.error(request, '请先注册支付账号再挂卖单')
            return redirect('accountinfo')
        if request.method == 'POST':
            request_source = request.POST['request_source']
            # this indicate that the request come from submit
            # order instead of refresh a response page of previous
            # order
            if len(request_source) > 0:
                order_command = read_order_input(request)
                if order_command.total_units - accountinfo.available_balance < 0:
                    ordermanager.create_sell_order(order_command,
                                                   request.user.username)
                    messages.success(request, '您的卖单已经成功创建')
                else:
                    messages.error(request, '卖单数量不可以高于可用余额')
        sellorders, buyorders = ordermanager.get_orders_by_user(
            request.user.id)
        if request.method == 'POST':
            return redirect('sellorder')
        else:
            return render(
                request, 'trading/mysellorder.html', {
                    'sellorders': sellorders,
                    'buyorders': buyorders,
                    'settings': sitesettings,
                    'useraccountInfo': accountinfo
                })

    except Exception as e:
        error_msg = '出售美基金遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 4
0
def accountinfo(request):
    try:
        useraccountInfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund')
        weixin, weixin_payment_image, weixin_shop_assistant_image = userpaymentmethodmanager.load_weixin_info(
            request.user)
        request.session[REQ_KEY_USERACCOUNTINFO] = useraccountInfo.tojson()
        return render(
            request, 'trading/myaccount.html', {
                'useraccountInfo': useraccountInfo,
                REQ_KEY_USERNAME: request.user.username,
                'weixin': weixin,
                'weixin_payment_image': weixin_payment_image,
                'weixin_shop_assistant_image': weixin_shop_assistant_image
            })
    except Exception as e:
        error_msg = '用户主页显示遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 5
0
def show_active_sell_orders(request):
    try:
        logger.debug("get show show_active_sell_orders request")
        username = request.user.username
        status = None
        sellorders = ordermanager.get_all_open_seller_order_exclude_user(
            request.user.id)
        accountinfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund')
        return render(
            request, 'trading/purchase.html', {
                'sellorders': sellorders,
                REQ_KEY_USERNAME: username,
                'useraccountInfo': accountinfo,
                'previous_call_status': status
            })

    except Exception as e:
        error_msg = '显示现有卖单出现错误: {0}'.format(sys.exc_info()[0])
        logger.exception(e)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 6
0
def cancel_purchase(request):
    try:
        if request.method == 'POST':
            buyorderid = request.POST['order_id']
            cancelsellorder = request.POST.get('cancelsellorder', 'false')
            keyword = request.POST['search_keyword']
            purchase_order = Order.objects.get(order_id=buyorderid)
            trade_status = TRADE_STATUS_USERABANDON
            payment_status = PAYMENT_STATUS_USERABANDON
            if cancelsellorder == 'true':
                trade_status = TRADE_STATUS_BADRECEIVINGACCOUNT
                payment_status = PAYMENT_STATUS_BADRECEIVINGACCOUNT

            ordermanager.cancel_purchase_order(purchase_order, trade_status,
                                               payment_status, request.user)
            useraccountInfo = useraccountinfomanager.get_user_accountInfo(
                request.user, 'AXFund')
            request.session[REQ_KEY_USERACCOUNTINFO] = useraccountInfo.tojson()
            buyorders = ordermanager.search_orders(keyword, None, None)
            messages.success(request, '确认取消订单,交易完成')
        else:
            return HttpResponseBadRequest('撤销买单只能允许POST')

        return render(
            request, 'trading/admin/transmanagement.html', {
                'useraccountInfo': useraccountInfo,
                REQ_KEY_USERNAME: request.user.username,
                'search_keyword': keyword,
                'buyorders': buyorders,
                'bootstrap_datepicker': True
            })
    except Exception as e:
        error_msg = '用户主页显示遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 7
0
def gettrans(request):
    try:
        useraccountInfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund')
        request.session[REQ_KEY_USERACCOUNTINFO] = useraccountInfo.tojson()
        buyorders = None
        keyword = ''
        if request.method == 'POST':
            keyword = request.POST['keyword']
            buyorders = ordermanager.search_orders(keyword, None, None)

        return render(
            request, 'trading/admin/transmanagement.html', {
                'useraccountInfo': useraccountInfo,
                REQ_KEY_USERNAME: request.user.username,
                'search_keyword': keyword,
                'buyorders': buyorders,
                'bootstrap_datepicker': True
            })
    except Exception as e:
        error_msg = '用户主页显示遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 8
0
def show_purchase_input(request):
    try:
        if request.method != 'POST':
            error_msg = '购买页面不接受GET请求'
            logger.error(
                'show_purchase_input(): receive GET request from {0}'.format(
                    get_client_ip(request)))
            return errorpageview.show_error(
                request, ERR_CRITICAL_RECOVERABLE,
                '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))

        username = request.user.username
        userid = request.user.id
        useraccountInfo = useraccountinfomanager.get_user_accountInfo(
            request.user, 'AXFund')
        if len(useraccountInfo.paymentmethods) == 0:
            messages.error(request, '请先注册支付方式再购买')
            return redirect('accountinfo')
        if len(useraccountInfo.paymentmethods[0].account_at_provider) == 0:
            messages.error(request, '请先注册支付账号再购买')
            return redirect('accountinfo')
        if "owner_user_id" not in request.POST:
            messages.warning(request, "回到主页再进行操作")
            return redirect('accountinfo')
        owner_user_id = request.POST["owner_user_id"]

        reference_order_id = request.POST["reference_order_id"]
        paypal_client_id = ''
        sell_order_payment_method = ordermanager.get_and_update_sell_order_payment_methods(
            reference_order_id)
        paypal_client_id = sell_order_payment_method.client_id if sell_order_payment_method and sell_order_payment_method.client_id else ''
        payment_method = sell_order_payment_method.provider.code if sell_order_payment_method else ''
        bill_no = payment_account = payment_qrcode_url = ''
        if not sell_order_payment_method:
            api_tran = APIUserTransactionManager.get_trans_by_reference_order(
                reference_order_id)
            if api_tran is None:
                raise ValueError('ERR_API_SELLER_NO_API_RECORD')

            bill_no, payment_method, payment_account, payment_qrcode_url = parseInfo(
                api_tran)

        owner_login = request.POST["owner_login"]
        unit_price = float(request.POST["locked_in_unit_price"])
        order_sub_type = request.POST["sub_type"]
        total_units = 0
        if 'quantity' in request.POST:
            total_units = float(request.POST['quantity'])
        available_units = float(request.POST["available_units_for_purchase"])
        order_currency = request.POST['sell_order_currency']
        if order_sub_type == 'ALL_OR_NOTHING':
            total_units = available_units

        #owner_payment_methods = ordermanager.get_user_payment_methods(owner_user_id) if not sell_order_payment_method else [
        #    UserPaymentMethodView(0,
        #        0, None,
        #        # TODO: the name of the seller payment provider should come from DB
        #        '微信', '',
        #        '', "", "")]
        #for method in owner_payment_methods:
        #    print ("provider %s has image %s" % (method.provider.name, method.provider_qrcode_image))
        buyorder = OrderItem('',
                             userid,
                             username,
                             unit_price,
                             order_currency,
                             total_units,
                             0,
                             0.0,
                             'AXFund',
                             '',
                             '',
                             'BUY',
                             sub_type=order_sub_type,
                             selected_payment_provider=payment_method,
                             account_at_payment_provider=payment_account)
        return render(
            request, 'trading/input_purchase.html', {
                'username': username,
                'buyorder': buyorder,
                'owner_user_id': owner_user_id,
                'sub_type': order_sub_type,
                'reference_order_id': reference_order_id,
                'available_units_for_purchase': available_units,
                'paypal_clientId': paypal_client_id,
                'order_currency': order_currency
            })
    except ValueError as ve:
        handleValueError(ve, request)
    except Exception as e:
        error_msg = '显示买单出现错误: {0}'.format(sys.exc_info()[0])
        logger.exception(e)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))
Exemplo n.º 9
0
def create_purchase_order(request):
    try:
        logger.debug('create_purchase_order()...')
        username = request.user.username
        userid = request.user.id
        logger.info("Begin process user input for creating purchase order")
        if request.method == 'POST':
            reference_order_id = request.POST['reference_order_id']
            owner_user_id = int(request.POST["owner_user_id"])
            quantity = float(request.POST['quantity'])
            unit_price = float(request.POST['unit_price'])
            order_currency = request.POST['order_currency']
            seller_payment_provider = request.POST['seller_payment_provider']
            logger.debug(
                'create_purchase_order(): seller_payment_provider is {0}'.
                format(seller_payment_provider))
            crypto = request.POST['crypto']
            total_amount = float(request.POST['total_amount'])
            buyorder = OrderItem('', userid, username, unit_price,
                                 order_currency, quantity, 0, total_amount,
                                 crypto, '', '', 'BUY')
            buyorderid = None
            try:
                buyorderid = ordermanager.create_purchase_order(
                    buyorder, reference_order_id, seller_payment_provider,
                    username)
            except ValueError as ve:
                if ve.args[0] == 'SELLORDER_NOT_OPEN':
                    messages.error(request, '卖单暂时被锁定,请稍后再试')
                elif ve.args[0] == 'BUY_EXCEED_AVAILABLE_UNITS':
                    messages.error(request, '购买数量超过卖单余额,请按撤销键然后再试')
                else:
                    raise
                #owner_payment_methods = ordermanager.get_user_payment_methods(owner_user_id)
                #useraccountInfo = useraccountinfomanager.get_user_accountInfo(request.user,'AXFund')
                return redirect('purchase')
            if buyorderid is None:
                raise ValueError('Failed to get purchase order id')

            if settings.PAYMENT_API_STATUS['heepay'] == 'auto':
                # read the sitsettings
                sitesettings = context_processor.settings(request)['settings']
                json_response = send_payment_request(sitesettings,
                                                     seller_payment_provider,
                                                     buyorder.order_id,
                                                     total_amount)
                if json_response and json_response['return_code'] == 'SUCCESS':
                    ordermanager.post_open_payment_order(
                        buyorderid, 'heepay', json_response['hy_bill_no'],
                        json_response['hy_url'], username)

                    qrcode_file = generate_payment_qrcode(
                        'heepay', json_response, settings.MEDIA_ROOT)
                    return render(
                        request, 'trading/purchase_heepay_qrcode.html', {
                            'total_units': quantity,
                            'unit_price': unit_price,
                            'total_amount': total_amount,
                            'heepay_qrcode_file': qrcode_file
                        })
                elif json_response and json_response[
                        'return_msg'] == HEEPAY_ERR_NONEXIST_RECEIVE_ACCOUNT:
                    purchase_order = Order.objects.get(order_id=buyorderid)
                    admin = User.objects.get(username='******')
                    ordermanager.cancel_purchase_order(
                        purchase_order, TRADE_STATUS_BADRECEIVINGACCOUNT,
                        PAYMENT_STATUS_BADRECEIVINGACCOUNT, admin)

                owner_payment_methods = ordermanager.get_user_payment_methods(
                    owner_user_id)
                useraccountInfo = useraccountinfomanager.get_user_accountInfo(
                    request.user, 'AXFund')
                messages.error(
                    request,
                    '向汇钱包下单申请失败:{0}'.format(json_response['return_msg']
                                            if json_response else '系统错误'))
                return redirect('purchase')
            else:
                api_tran = APIUserTransactionManager.get_trans_by_reference_order(
                    reference_order_id)
                if api_tran is None:
                    raise ValueError('ERR_API_SELLER_NO_API_RECORD')

                bill_no, payment_method, payment_account, payment_qrcode_url = parseInfo(
                    api_tran)
                ordermanager.post_open_payment_order(buyorderid,
                                                     payment_method, bill_no,
                                                     payment_qrcode_url,
                                                     username)
                useraccountInfo = useraccountinfomanager.get_user_accountInfo(
                    request.user, 'AXFund')
                return render(
                    request, 'trading/purchase_qrcode.html', {
                        'qrcode_url': payment_qrcode_url,
                        'total_units': quantity,
                        'unit_price': unit_price,
                        'unit_currency': order_currency,
                        'seller_payment_provider': payment_method,
                        'seller_payment_provider_account': payment_account,
                        'total_amount': total_amount
                    })

    except Exception as e:
        error_msg = '创建买单遇到错误: {0}'.format(sys.exc_info()[0])
        logger.exception(error_msg)
        return errorpageview.show_error(request, ERR_CRITICAL_IRRECOVERABLE,
                                        '系统遇到问题,请稍后再试。。。{0}'.format(error_msg))