Пример #1
0
def index():
    account_href = request.user.account_href
    if not account_href:
        try:
            account = balanced.Customer.query.filter(
                email=self.request.user.email).one()
        except balanced.exc.NoResultFound:
            return redirect(
                url_for('accounts.index', reason='no-balanced-account'))
        else:
            request.user.account_href = account_href = account.href
            Session.commit()

    # let's create a login token for this user
    token_uri = 'https://dashboard.balancedpayments.com/v1/logins'
    data = {
        'account_href': account_href,
        'redirect_uri': config['DOMAIN_URI'],
    }
    result = requests.post(token_uri,
                           data=json.dumps(data),
                           auth=HTTPBasicAuth(balanced.config.api_key_secret,
                                              ''),
                           headers={'content-type': 'application/json'})
    data = json.loads(result.content)
    return 'transactions/interstitial.mako', {
        'redirect_to': data['token_uri'],
    }
Пример #2
0
def verify():
    # user cancelled out of authentication process
    if 'email_address' not in request.args:
        return redirect('/')
    email_address = request.args['email_address']
    listing_id = request.args['listing_id']
    merchant_uri = request.args['merchant_uri']
    marketplace = balanced.Marketplace.my_marketplace

    try:
        account = marketplace.create_merchant(email_address=email_address,
                                              merchant_uri=merchant_uri)
    except balanced.exc.HTTPError as ex:
        # shit, that sucked
        if getattr(ex, 'category_code', None) == 'duplicate-email-address':
            account = marketplace.accounts.filter(
                email_address=email_address).one()
        else:
            raise
    if account:
        user = User.create_guest_user(email_address=email_address)
        user.associate_balanced_account(account.uri)
        Session.commit()
        session['email_address'] = email_address
    return redirect(url_for('listing.complete', listing=listing_id))
Пример #3
0
def verify():
    # user cancelled out of authentication process
    if 'email' not in request.args:
        return redirect('/')
    email = request.args['email']
    listing_id = request.args['listing_id']
    merchant_href = request.args['merchant_href']
    marketplace = balanced.Marketplace.my_marketplace

    try:
        account = balanced.Customer(
            email=email, merchant_href=merchant_href).save()
    except balanced.exc.HTTPError as ex:
        # shit, that sucked
        if getattr(ex, 'category_code', None) == 'duplicate-email-address':
            account = marketplace.accounts.filter(
                email=email).one()
        else:
            raise
    if account:
        user = User.create_guest_user(email=email)
        user.associate_balanced_customer(account.href)
        Session.commit()
        session['email'] = email
    return redirect(url_for('listing.complete', listing=listing_id))
Пример #4
0
def index():
    account_uri = request.user.account_uri
    if not account_uri:
        try:
            account = balanced.Account.query.filter(
                email_address=self.request.user.email_address).one()
        except balanced.exc.NoResultFound:
            return redirect(url_for('accounts.index',
                reason='no-balanced-account'))
        else:
            request.user.account_uri = account_uri = account.uri
            Session.commit()

    # let's create a login token for this user
    token_uri = 'https://dashboard.balancedpayments.com/v1/logins'
    data = {
        'account_uri': account_uri,
        'redirect_uri': config['DOMAIN_URI'],
    }
    result = requests.post(token_uri, data=json.dumps(data),
        auth=HTTPBasicAuth(balanced.config.api_key_secret, ''),
        headers={'content-type': 'application/json'})
    data = json.loads(result.content)
    return 'transactions/interstitial.mako', {
        'redirect_to': data['token_uri'],
        }
Пример #5
0
def create(**kwargs):
    manager = ListingManager(request)

    forms = kwargs.pop("forms")
    listing_form = find_form(forms, ListingForm)
    guest_listing_form = find_form(forms, GuestListingForm)
    bank_account_form = find_form(forms, BankAccountForm)

    if request.user.is_authenticated:
        form = listing_form
    else:
        form = guest_listing_form
        session["email"] = form.email.data

    if not form.validate():
        return index(**kwargs)

    try:
        listing_id = manager.create(form, bank_account_form)
    except balanced.exc.HTTPError as ex:
        if ex.status_code == 300:
            # we've created the user locally, persist this information
            Session.commit()

            return redirect(
                url_for(
                    "redirect.show",
                    listing=form.listing_id.data,
                    redirect_uri=ex.response.headers["location"],
                    email=session.get("email"),
                )
            )
        elif ex.status_code == 400:
            if "merchant.postal_code" in ex.description:
                form["postal_code"].errors.append(ex.description)
                return index(**kwargs)
            elif "merchant.phone" in ex.description:
                form["phone"].errors.append(ex.description)
                return index(**kwargs)
        raise
    except Exception as ex:
        if ex.message == "Password mismatch":
            flash(
                "Sorry, this email address is already assigned to an "
                "account and your password does not match, please try "
                "again.",
                "error",
            )
            return self.index(**kwargs)
        raise

    Session.commit()

    return redirect(url_for("listing.complete", listing=listing_id))
Пример #6
0
    def add_dummy_data(self):
        for name, email, password in config['DEFAULT_USERS']:
            user = User.query.filter(User.email_address == email).count()
            if not user:
                user = User(name=name, email_address=email, password=password)
                Session.add(user)

        for i in range(4):
            listing = Listing.query.filter(Listing.id == i + 1).count()
            if not listing:
                listing = Listing(id=i + 1)
                Session.add(listing)

        Session.commit()
Пример #7
0
    def add_dummy_data(self):
        for name, email, password in config['DEFAULT_USERS']:
            user = User.query.filter(User.email_address == email).count()
            if not user:
                user = User(name=name, email_address=email, password=password)
                Session.add(user)

        for i in range(4):
            listing = Listing.query.filter(Listing.id == i + 1).count()
            if not listing:
                listing = Listing(id=i + 1)
                Session.add(listing)

        Session.commit()
Пример #8
0
def create(**kwargs):
    manager = ListingManager(request)

    forms = kwargs.pop('forms')
    listing_form = find_form(forms, ListingForm)
    guest_listing_form = find_form(forms, GuestListingForm)
    bank_account_form = find_form(forms, BankAccountForm)

    if request.user.is_authenticated:
        form = listing_form
    else:
        form = guest_listing_form
        session['email_address'] = form.email_address.data

    if not form.validate():
        return index(**kwargs)

    try:
        listing_id = manager.create(form, bank_account_form)
    except balanced.exc.HTTPError as ex:
        if ex.status_code == 300:
            # we've created the user locally, persist this information
            Session.commit()

            return redirect(
                url_for('redirect.show',
                        listing=form.listing_id.data,
                        redirect_uri=ex.response.headers['location'],
                        email_address=session.get('email_address')))
        elif ex.status_code == 400:
            if 'merchant.postal_code' in ex.description:
                form['postal_code'].errors.append(ex.description)
                return index(**kwargs)
            elif 'merchant.phone_number' in ex.description:
                form['phone_number'].errors.append(ex.description)
                return index(**kwargs)
        raise
    except Exception as ex:
        if ex.message == 'Password mismatch':
            flash(
                'Sorry, this email address is already assigned to an '
                'account and your password does not match, please try '
                'again.', 'error')
            return self.index(**kwargs)
        raise

    Session.commit()

    return redirect(url_for('listing.complete', listing=listing_id))
Пример #9
0
def create(**kwargs):
    manager = ListingManager(request)

    forms = kwargs.pop('forms')
    listing_form = find_form(forms, ListingForm)
    guest_listing_form = find_form(forms, GuestListingForm)
    bank_account_form = find_form(forms, BankAccountForm)

    if request.user.is_authenticated:
        form = listing_form
    else:
        form = guest_listing_form
        session['email_address'] = form.email_address.data

    if not form.validate():
        return index(**kwargs)

    try:
        listing_id = manager.create(form, bank_account_form)
    except balanced.exc.HTTPError as ex:
        if ex.status_code == 300:
            # we've created the user locally, persist this information
            Session.commit()

            return redirect(url_for('redirect.show',
                listing=form.listing_id.data,
                redirect_uri=ex.response.headers['location'],
                email_address=session.get('email_address')))
        elif ex.status_code == 400:
            if 'merchant.postal_code' in ex.description:
                form['postal_code'].errors.append(ex.description)
                return index(**kwargs)
            elif 'merchant.phone_number' in ex.description:
                form['phone_number'].errors.append(ex.description)
                return index(**kwargs)
        raise
    except Exception as ex:
        if ex.message == 'Password mismatch':
            flash('Sorry, this email address is already assigned to an '
                'account and your password does not match, please try '
                'again.', 'error')
            return self.index(**kwargs)
        raise

    Session.commit()

    return redirect(url_for('listing.complete', listing=listing_id))
Пример #10
0
 def create_guest_user(email_address, name=None, password=None):
     try:
         user = User.query.filter(
             User.email_address == email_address).one()
     except NoResultFound:
         Session.flush()
         user = User(email_address=email_address, name=name,
             password=password)
         Session.add(user)
         try:
             Session.flush()
         except:
             Session.rollback()
             Session.commit()
             Session.add(user)
             Session.commit()
     return user
Пример #11
0
def create(**kwargs):
    account_form = kwargs.pop('forms')[0]
    if account_form.validate():
        user = account_form.create_user()
        Session.add(user)
        try:
            Session.commit()
        except IntegrityError:
            flash('This account already exists!', 'warning')
            Session.rollback()
        else:
            user.lookup_balanced_customer()
            Session.commit()
            session['user_guid'] = user.guid
            return redirect(url_for('accounts.index'))

    return new(account_form)
Пример #12
0
def create(**kwargs):
    account_form = kwargs.pop("forms")[0]
    if account_form.validate():
        user = account_form.create_user()
        Session.add(user)
        try:
            Session.commit()
        except IntegrityError:
            flash("This account already exists!", "warning")
            Session.rollback()
        else:
            user.lookup_balanced_account()
            Session.commit()
            session["user_guid"] = user.guid
            return redirect(url_for("accounts.index"))

    return new(account_form)
Пример #13
0
 def create_guest_user(email_address, name=None, password=None):
     try:
         user = User.query.filter(User.email_address == email_address).one()
     except NoResultFound:
         Session.flush()
         user = User(email_address=email_address,
                     name=name,
                     password=password)
         Session.add(user)
         try:
             Session.flush()
         except:
             Session.rollback()
             Session.commit()
             Session.add(user)
             Session.commit()
     return user
Пример #14
0
    def add_dummy_data(self):
        user = User(
            name='Dummy User', email=self.dummy_email_generator(),
            password='******')
        Session.add(user)
        user.create_balanced_customer()

        for i in range(4):
            owner = User.fetch_one_at_random()
            if not user.balanced_customer.bank_accounts.count():
                self.dummy_bank_account_generator(owner)
            listing = Listing.query.filter(Listing.id == i + 1).count()
            if not listing:
                listing = Listing(id=i + 1, owner_guid=owner.guid)
                Session.add(listing)

        Session.commit()
Пример #15
0
def create(**kwargs):
    account_form = kwargs.pop('forms')[0]
    if account_form.validate():
        user = account_form.create_user()
        Session.add(user)
        try:
            Session.commit()
        except IntegrityError:
            flash('This account already exists!', 'warning')
            Session.rollback()
        else:
            user.lookup_balanced_customer()
            Session.commit()
            session['user_guid'] = user.guid
            return redirect(url_for('accounts.index'))

    return new(account_form)
Пример #16
0
def login(**kwargs):
    login_form = kwargs.pop("forms")[0]
    if login_form.validate():
        try:
            user = login_form.login()
        except Exception as ex:
            if ex.message == "No password":
                flash("There is no password associated with this account.")
                return index(login_form)
            raise
        if not user:
            flash("Wrong email address or password", "error")
            return login_show(login_form)
        user.lookup_balanced_account()
        Session.commit()
        session["user_guid"] = user.guid
        return redirect(request.args.get("redirect_uri", url_for("accounts.index")))
    return login_show(login_form)
Пример #17
0
def login(**kwargs):
    login_form = kwargs.pop('forms')[0]
    if login_form.validate():
        try:
            user = login_form.login()
        except Exception as ex:
            if ex.message == 'No password':
                flash('There is no password associated with this account.')
                return index(login_form)
            raise
        if not user:
            flash('Wrong email address or password', 'error')
            return login_show(login_form)
        user.lookup_balanced_customer()
        Session.commit()
        session['user_guid'] = user.guid
        return redirect(
            request.args.get('redirect_uri', url_for('accounts.index')))
    return login_show(login_form)
Пример #18
0
def login(**kwargs):
    login_form = kwargs.pop('forms')[0]
    if login_form.validate():
        try:
            user = login_form.login()
        except Exception as ex:
            if ex.message == 'No password':
                flash('There is no password associated with this account.')
                return index(login_form)
            raise
        if not user:
            flash('Wrong email address or password', 'error')
            return login_show(login_form)
        user.lookup_balanced_customer()
        Session.commit()
        session['user_guid'] = user.guid
        return redirect(request.args.get('redirect_uri',
            url_for('accounts.index'))
        )
    return login_show(login_form)
Пример #19
0
def update(listing, **kwargs):
    manager = RentalManager(request)

    forms = kwargs.pop('forms')
    purchase_form = find_form(forms, PurchaseForm)
    guest_purchase_form = find_form(forms, GuestPurchaseForm)
    card_href = request.form.get('card_href', None)
    name = None

    if request.user.is_authenticated:
        email_address = request.user.email
    else:
        email_address = guest_purchase_form.email.data
        name = guest_purchase_form.name.data

    try:
        rental = manager.rent(listing, email_address, card_href, name)
    except balanced.exc.HTTPError as ex:
        msg = 'Error debiting account, your card has not been charged "{}"'
        flash(msg.format(ex.message), 'error')
        Session.rollback()
    except Exception as ex:
        if ex.message == 'No card on file':
            return show(listing, purchase_form, guest_purchase_form, True)
        raise
    else:
        Session.commit()

        email.send_email(rental.buyer.email,
                         'Rental Receipt',
                         'receipt.mako',
                         name=rental.buyer.email,
                         listing=listing,
                         charge=balanced.Order.fetch(rental.order_href))
        session['rental_user_guid'] = rental.buyer.guid
        session['rental_email'] = rental.buyer.email
        return redirect(
            url_for('rent.confirmed', listing=listing, rental=rental))
    return show(listing, purchase_form, guest_purchase_form)
Пример #20
0
def update(listing, **kwargs):
    manager = RentalManager(request)

    forms = kwargs.pop('forms')
    purchase_form = find_form(forms, PurchaseForm)
    guest_purchase_form = find_form(forms, GuestPurchaseForm)
    card_uri = request.form.get('card_uri', None)
    name = None

    if request.user.is_authenticated:
        email_address = request.user.email_address
    else:
        email_address = guest_purchase_form.email_address.data
        name = guest_purchase_form.name.data

    try:
        rental = manager.rent(listing, email_address, card_uri, name)
    except balanced.exc.HTTPError as ex:
        msg = 'Error debiting account, your card has not been charged "{}"'
        flash(msg.format(ex.message), 'error')
        Session.rollback()
    except Exception as ex:
        if ex.message == 'No card on file':
            return show(listing, purchase_form, guest_purchase_form, True)
        raise
    else:
        Session.commit()

        email.send_email(rental.buyer.email_address,
            'Rental Receipt',
            'receipt.mako',
            name=rental.buyer.email_address, listing=listing,
            charge=balanced.Transaction.find(rental.debit_uri)
        )
        session['rental_user_guid'] = rental.buyer.guid
        session['rental_email_address'] = rental.buyer.email_address
        return redirect(url_for('rent.confirmed', listing=listing, rental=rental))
    return show(listing, purchase_form, guest_purchase_form)
Пример #21
0
def verify():
    # user cancelled out of authentication process
    if "email_address" not in request.args:
        return redirect("/")
    email_address = request.args["email_address"]
    listing_id = request.args["listing_id"]
    merchant_uri = request.args["merchant_uri"]
    marketplace = balanced.Marketplace.my_marketplace

    try:
        account = marketplace.create_merchant(email_address=email_address, merchant_uri=merchant_uri)
    except balanced.exc.HTTPError as ex:
        # shit, that sucked
        if getattr(ex, "category_code", None) == "duplicate-email-address":
            account = marketplace.accounts.filter(email_address=email_address).one()
        else:
            raise
    if account:
        user = User.create_guest_user(email_address=email_address)
        user.associate_balanced_account(account.uri)
        Session.commit()
        session["email_address"] = email_address
    return redirect(url_for("listing.complete", listing=listing_id))