Esempio n. 1
0
def _execute_charge(api_key, amount_cents, fee_cents, currency, user, description):
    client = StripeCustomer(api_key)
    try:
        ret = client.charge(amount_cents,
                            fee_cents,
                            currency,
                            user,
                            description)
    except StripeCardError:
        # Errors caused by us
        msg = 'The user\'s credit card was declined.'
        raise UserCreditCardDeclinedError(msg)
    except (StripeAuthenticationError, StripeInvalidRequestError):
        # Errors caused by us
        msg = 'An error occurred in pooldlib while associating Stripe Connect account with user'
        raise ExternalAPIUsageError(msg)
    except StripeAPIError:
        # Errors caused by stripe
        msg = 'An error occurred with Stripe while associating Stripe Connect account with user'
        raise ExternalAPIError(msg)
    except StripeAPIConnectionError:
        # Errors caused by trucks getting caught in the tubes
        msg = 'An error occurred while connecting to the Stripe Connect API.'
        raise ExternalAPIUnavailableError(msg)
    return ret
Esempio n. 2
0
def associate_stripe_token(user, stripe_token, stripe_private_key, force=False):
    """Exchange a Stripe one-time use token for a customer id in Stripe's
    system. The poold user's stripe customer id will be stored as UserMeta data and
    accessible via ``User.stripe_customer_id``.  If the user is already
    associated with a different Stripe customer id an exception will be raised
    unless ``force=True``.

    Stripe Customer: A user who is utilizing stripe to pay *into* our system.

    :param user: User for which to associate the retrieved Strip user id.
    :type user: :class:`pooldlib.postgresql.models.User`
    :param stripe_token: The single use token returned by Stripe, usually in
                         response to a credit card authorization via stripe.js
    :type stripe_token: string
    :param stripe_private_key: The private key for the stripe application to connect the user to.
    :type stripe_private_key: string
    :param force: If the target user is allready associated with a different
                  Strip user id, do not raise
                  ``PreviousStripeAssociationError`` and update the existing
                  record.
    :type force: boolean

    :raises: :class:`pooldlib.exceptions.PreviousStripeAssociationError`
             :class:`pooldlib.exceptions.ExternalAPIUsageError`
             :class:`pooldlib.exceptions.ExternalAPIError`
             :class:`pooldlib.exceptions.ExternalAPIUnavailableError`
    """
    try:
        stripe_customer = StripeCustomer(stripe_private_key)
        stripe_customer_id = stripe_customer.token_for_customer(stripe_token, user)
        previous_association = hasattr(user, 'stripe_customer_id') and user.stripe_customer_id != stripe_customer_id
        if previous_association and not force:
            msg = 'User has a previously associated stripe customer id and force was not set to True.'
            raise PreviousStripeAssociationError(msg)
    except StripeCardError:
        # Errors caused by us
        msg = 'The user\'s credit card was declined.'
        raise UserCreditCardDeclinedError(msg)
    except (StripeAuthenticationError, StripeInvalidRequestError):
        # Errors caused by us
        msg = 'An error occurred in pooldlib while exchanging one time use token for Stripe user'
        raise ExternalAPIUsageError(msg)
    except StripeAPIError:
        # Errors caused by stripe
        msg = 'An error occurred with Stripe while exchanging one time use token for Stripe user'
        raise ExternalAPIError(msg)
    except StripeAPIConnectionError:
        # Errors caused by trucks getting caught in the tubes
        msg = 'An error occurred while connecting to the Stripe API.'
        raise ExternalAPIUnavailableError(msg)
    update(user, stripe_customer_id=stripe_customer_id)