def check_email():
    """Checks whether an email belongs to an account or not."""
    email = request.args.get("email")
    try:
        user = get_user(email)
        academic = is_academic(email)
        if user:
            return jsonify(status="SUCCESS", data={"type": "existing", "academic": academic})
        else:
            return jsonify(status="SUCCESS", data={"type": "new", "academic": academic})
    except:
        msg = "An error occured whilst trying to check if this email is " + "associated with an existing user."

        logging.exception(msg)
        return jsonify(status="ERROR", msg=msg)
Exemple #2
0
def check_email():
    """Checks whether an email belongs to an account or not."""
    email = request.args.get("email")
    try:
        user = get_user(email)
        academic = is_academic(email)
        if user:
            return jsonify(
                status='SUCCESS',
                data={'type': 'existing', 'academic': academic}
                )
        else:
            return jsonify(
                status='SUCCESS',
                data={'type': 'new', 'academic': academic}
                )
    except:
        msg = "An error occured whilst trying to check if this email is " + \
            "associated with an existing user."

        logging.exception(msg)
        return jsonify(status='ERROR', msg=msg)
def normalize_request(options):
    """Normalize request

    After this, the user should be created and logged in, they
    should have a stripe id associated with them and if a token was passed
    as credit_card then it should be converted into a stripe card id.

    TODO: update existing user if additional information is provided
    """

    credit_card = options.get('credit_card')
    country = options.get('country')
    state = options.get('state')
    coupon_code = options.get('coupon_code')
    domain = options.get('domain')

    options['amount'] = 50

    if country in REGIONS and state in REGIONS[country]:
        options['state'] = REGIONS[country][state]

    def is_token(value):
        """Determines whether the passed argument is a stripe token or not"""
        if value is None:
            return False
        return (value[:3] == 'tok')

    if 'user' in options:
        user = options['user']
    else:
        user = get_current_user()

    if not user:
        # user is not logged in, let's see if the email is attached to an
        # account
        email = options.get('email')
        password = options.get('password')
        name = options.get('name')
        user = get_user(email)

        if user:
            # account exists - try to authenticate
            try:
                authenticate_user(email, password)
            except UserAuthenticationFailedError:
                return do_error('Password is incorrect')
        else:
            # this is a new account
            user = create_user(email, password, name=name)
            session['user'] = user.key.id()

    if not user.name:
        name = options.get('name')
        user.name = name

    if not user.stripe_id:
        # User doesn't have a stripe customer ID
        customer = stripe.Customer.create(
            description=name,
            email=email
        )

        user.stripe_id = customer.id
        if is_token(credit_card):
            card = customer.cards.create(card=credit_card)
            credit_card = card.id
    else:
        # User has a stripe ID
        customer = stripe.Customer.retrieve(user.stripe_id)
        if is_token(credit_card):
            # this is a new card
            card = customer.cards.create(card=credit_card)
            credit_card = card.id

    user.put()

    if 'csr' not in options:
        # We need to generate the CSR
        keypair = get_keypair(False)
        csr = CertificationRequest(keypair=keypair)

        # Set fields
        domain = options.get('domain')
        organization = options.get('organization')
        state = options.get('state')
        country = options.get('country')
        phone_number = options.get('phone_number')
        email = user.email

        csr.set_subject_field('common_name', domain)
        csr.set_subject_field('organization', organization)
        csr.set_subject_field('state', state)
        csr.set_subject_field('country', country)
        csr.set_subject_field('telephone', phone_number)
        csr.set_subject_field('email_address', email)

        options['csr'] = csr.export()
        options['keypair'] = keypair.exportKey()

    options['credit_card'] = credit_card
    options['user'] = user

    if request.args.get('promotion') == 'academic':
        options['academic'] = True

    if options.get('promotion', '') == 'academic':
        options['academic'] = True

    return options