Esempio n. 1
0
 def get_or_create(cls, email=None, token=None):
     """
         Check if there is a registered stripe customer with that email
         or create a new one
     """
     try:
         stripe_utils = StripeUtils()
         stripe_customer = cls.objects.get(user__email=email)
         # check if user is not in stripe but in database
         customer = stripe_utils.check_customer(stripe_customer.stripe_id,
                                                stripe_customer.user, token)
         if "deleted" in customer and customer["deleted"]:
             raise StripeCustomer.DoesNotExist()
         return stripe_customer
     except StripeCustomer.DoesNotExist:
         user = CustomUser.objects.get(email=email)
         stripe_utils = StripeUtils()
         stripe_data = stripe_utils.create_customer(token, email, user.name)
         if stripe_data.get('response_object'):
             stripe_cus_id = stripe_data.get('response_object').get('id')
             if hasattr(user, 'stripecustomer'):
                 # User already had a Stripe account and we are here
                 # because the account was deleted in dashboard
                 # So, we simply update the stripe_id
                 user.stripecustomer.stripe_id = stripe_cus_id
                 user.stripecustomer.save()
                 stripe_customer = user.stripecustomer
             else:
                 # The user never had an associated Stripe account
                 # So, create one
                 stripe_customer = StripeCustomer.objects.create(
                     user=user, stripe_id=stripe_cus_id)
             return stripe_customer
         else:
             return None
Esempio n. 2
0
class TestStripeCustomerDescription(TestCase):
    """
    A class to test setting the description field of the stripe customer
    https://stripe.com/docs/api#metadata
    """
    def setUp(self):
        self.customer_password = '******'
        self.customer_email = '*****@*****.**'
        self.customer_name = "Monty Python"
        self.customer = mommy.make('membership.CustomUser')
        self.customer.set_password(self.customer_password)
        self.customer.email = self.customer_email
        self.customer.save()
        self.stripe_utils = StripeUtils()
        stripe.api_key = settings.STRIPE_API_PRIVATE_KEY_TEST
        self.token = stripe.Token.create(card={
            "number": '4111111111111111',
            "exp_month": 12,
            "exp_year": 2022,
            "cvc": '123'
        }, )
        self.failed_token = stripe.Token.create(card={
            "number": '4000000000000341',
            "exp_month": 12,
            "exp_year": 2022,
            "cvc": '123'
        }, )

    def test_creating_stripe_customer(self):
        stripe_data = self.stripe_utils.create_customer(
            self.token.id, self.customer.email, self.customer_name)
        self.assertEqual(stripe_data.get('error'), None)
        customer_data = stripe_data.get('response_object')
        self.assertEqual(customer_data.description, self.customer_name)
Esempio n. 3
0
    def get_or_create(cls, email=None, token=None):
        """
            Check if there is a registered stripe customer with that email
            or create a new one
        """
        stripe_customer = None
        try:
            stripe_utils = StripeUtils()
            stripe_customer = cls.objects.get(user__email=email)
            # check if user is not in stripe but in database
            customer = stripe_utils.check_customer(stripe_customer.stripe_id,
                                                   stripe_customer.user, token)

            if not customer.sources.data:
                stripe_utils.update_customer_token(customer, token)
            return stripe_customer

        except StripeCustomer.DoesNotExist:
            user = CustomUser.objects.get(email=email)

            stripe_utils = StripeUtils()
            stripe_data = stripe_utils.create_customer(token, email)
            if stripe_data.get('response_object'):
                stripe_cus_id = stripe_data.get('response_object').get('id')

                stripe_customer = StripeCustomer.objects.\
                    create(user=user, stripe_id=stripe_cus_id)

                return stripe_customer
            else:
                return None
Esempio n. 4
0
    def get_or_create(cls, email=None, token=None):
        """
            Check if there is a registered stripe customer with that email
            or create a new one
        """
        stripe_customer = None
        try:
            stripe_utils = StripeUtils()
            stripe_customer = cls.objects.get(user__email=email)
            # check if user is not in stripe but in database
            customer = stripe_utils.check_customer(stripe_customer.stripe_id,
                                                   stripe_customer.user, token)

            if not customer.sources.data:
                stripe_utils.update_customer_token(customer, token)
            return stripe_customer

        except StripeCustomer.DoesNotExist:
            user = CustomUser.objects.get(email=email)

            stripe_utils = StripeUtils()
            stripe_data = stripe_utils.create_customer(token, email)
            if stripe_data.get('response_object'):
                stripe_cus_id = stripe_data.get('response_object').get('id')

                stripe_customer = StripeCustomer.objects.\
                    create(user=user, stripe_id=stripe_cus_id)

                return stripe_customer
            else:
                return None
Esempio n. 5
0
 def create_stripe_api_customer(cls,
                                email=None,
                                token=None,
                                customer_name=None):
     """
         This method creates a Stripe API customer with the given
         email, token and customer_name. This is different from
         get_or_create method below in that it does not create a
         CustomUser and associate the customer created in stripe
         with it, while get_or_create does that before creating the
         stripe user.
     """
     stripe_utils = StripeUtils()
     stripe_data = stripe_utils.create_customer(token, email, customer_name)
     if stripe_data.get('response_object'):
         stripe_cus_id = stripe_data.get('response_object').get('id')
         return stripe_cus_id
     else:
         return None