Beispiel #1
0
 def test_left_daily(self):
     ''' test method Dashboard.left_daily() '''
     u = self.getUser()
     now = datetime.datetime.now()
     
     # Empty database has no users
     self.failUnlessEqual(Dashboard.left_daily(), 0)
     
     # create 1 user which left today
     s = Subscription(owner=u, monthly_fee=0)        
     s.date_cancelled = now
     s.date_deleted = now
     s.save()
     
     self.failUnlessEqual(Dashboard.left_daily(), 1)
     
     # Delete all data
     u.delete()        
     self.failUnlessEqual(Dashboard.left_monthly(), 0)
Beispiel #2
0
 def test_left_weekly(self):
     ''' test method Dashboard.left_weekly() '''
     
     # Empty database has no users
     self.failUnlessEqual(Dashboard.left_weekly(), 0)
     
     # create 1 user which left 3 days before
     u = self.getUser()
     date = datetime.datetime.now() - datetime.timedelta(days=3)        
     
     s = Subscription(owner=u, monthly_fee=0)        
     s.date_cancelled = date
     s.date_deleted = date
     s.save()
     
     self.failUnlessEqual(Dashboard.left_weekly(), 1)
 
     # Delete all data
     u.delete()  
     self.failUnlessEqual(Dashboard.left_monthly(), 0)
Beispiel #3
0
 def getSubscription(self, user):
     ''' create and return default subscription for current user '''
     s = Subscription(owner=user, monthly_fee=0)
     s.save()
     return s
Beispiel #4
0
    def test_churn_acquisition(self):
        ''' test method Dashboard.churn_acquisition() '''
        
        # Empty database has no users
        self.failUnlessEqual(Dashboard.churn_acquisition(), '0% / 0% / 0%')
        
        # create user which left today
        date = datetime.datetime.now()
        u = self.getUser()
        s = Subscription(owner=u, monthly_fee=0)        
        s.date_cancelled = date
        s.date_deleted = date
        s.save()
        
        self.failUnlessEqual(Dashboard.churn_acquisition(), '100% / 100% / 100%')

        # create user which left 3 days before
        date -= datetime.timedelta(days=3)     
        s.date_cancelled = date
        s.date_deleted = date
        s.save()        
        self.failUnlessEqual(Dashboard.churn_acquisition(), '0% / 100% / 100%')
        
        # create user which left 23 days before
        date -= datetime.timedelta(days=20)
        s.date_cancelled = date
        s.date_deleted = date
        s.save()        
        self.failUnlessEqual(Dashboard.churn_acquisition(), '0% / 0% / 100%')
        
        # Delete all data
        u.delete()
        self.failUnlessEqual(Dashboard.churn_acquisition(), '0% / 0% / 0%')
Beispiel #5
0
 def test_churn_rage_per_time(self):
     ''' test method Dashboard.churn_rage_per_time() '''
     
     # Empty database has no users
     self.failUnlessEqual(Dashboard.churn_rage_per_time(), '0 / 0 / 0')
     
     # create user which left 20 days before 
     u = self.getUser()
     now = datetime.datetime.now()
     date = now - datetime.timedelta(days=20)     
     s = Subscription(owner=u, monthly_fee=0)        
     s.date_cancelled = date
     s.date_deleted = date
     s.save()
     
     self.failUnlessEqual(Dashboard.churn_rage_per_time(), '0 / 0 / 1')
 
     # create user which left 3 days before 
     date = now - datetime.timedelta(days=3)
     s.date_cancelled = date
     s.date_deleted = date
     s.save()
     
     self.failUnlessEqual(Dashboard.churn_rage_per_time(), '0 / 1 / 1')
 
     # create user which left today
     s.date_cancelled = now
     s.date_deleted = now
     s.save()        
     self.failUnlessEqual(Dashboard.churn_rage_per_time(), '1 / 1 / 1')
     
     # Delete all data
     u.delete()
     self.failUnlessEqual(Dashboard.churn_rage_per_time(), '0 / 0 / 0')
Beispiel #6
0
def create_subscription(request):

    data = request.data

    # run the `create_stripe_data` management command and get the price_id
    # from the Stripe console and add it to `.env` as SUBSCRIPTION_PRICE_ID
    price_id = os.environ.get("SUBSCRIPTION_PRICE_ID", "price_abc124")

    try:

        logger.info("Creating Stripe Customer")
        stripe_customer = stripe.Customer.create(email=request.user.email)
        logger.info("Stripe Customer created")
        if "id" in stripe_customer:
            stripe_customer_id = stripe_customer["id"]
        else:
            # what happens if Customer.create is called with
            # an email that already exists?
            return JsonResponse({"message": "Error creating Stripe Customer"})

        # Attach the payment method to the customer
        logger.info("Attaching payment info to customer")
        stripe.PaymentMethod.attach(
            data["paymentMethodId"],
            customer=stripe_customer_id,
        )

        # Set the default payment method on the customer
        logger.info("Setting default payment method for customer")
        stripe.Customer.modify(
            stripe_customer_id,
            invoice_settings={
                "default_payment_method": data["paymentMethodId"],
            },
        )

        # Create the subscription
        logger.info("Creating Stripe Subscription")
        stripe_subscription = stripe.Subscription.create(
            customer=stripe_customer_id,
            items=[{
                "price": price_id
            }],
            expand=["latest_invoice.payment_intent"],
        )

        # save the subscription to the user
        user = request.user

        logger.info("Creating Subscription (Django object)")
        subscription_object = Subscription(
            stripe_customer_id=stripe_customer_id,
            # leave a grace period of a few days before cancelling subscription
            valid_through=datetime.fromtimestamp(
                stripe_subscription["current_period_end"] +
                TWO_DAYS  # two days in seconds
            ),
            stripe_subscription_id=stripe_subscription["id"],
        )

        logger.info("Save subscription model")
        subscription_object.save()
        logger.info("Save subscription model to user")
        user.subscription = subscription_object
        user.save()

        return Response(stripe_subscription)
    except Exception as e:
        return Response({"message": str(e)})
Beispiel #7
0
 def parse_subscription(self, row: List[str], users: List[User]):
     if row[1]:
         user = self.find_user(*self.get_user(row[1]), users)
         for i, ceil in enumerate(row[25: 31]):
             if ceil == self.TRUE:
                 user.subscriptions.append(Subscription(self.SUB_MAP[i]))