Esempio n. 1
0
    def test_no_credit_change_for_subscription_downgrade(self):
        """ Same credits for a subscription downgrade. """
        credits = 20

        current_plan = Subscription.get_plan_by_id('pro')
        new_plan = Subscription.get_plan_by_id('standard')

        credits = add_subscription_credits(credits, current_plan, new_plan,
                                           None)

        assert credits == 20
Esempio n. 2
0
    def test_add_credits_to_subscription_upgrade(self):
        """ Add credits to a subscription upgrade. """
        credits = 20

        current_plan = Subscription.get_plan_by_id('standard')
        new_plan = Subscription.get_plan_by_id('pro')

        credits = add_subscription_credits(credits, current_plan, new_plan,
                                           None)

        assert credits == 45
Esempio n. 3
0
    def test_no_credit_change_for_same_subscription(self):
        """ Same credits for the same subscription. """
        credits = 20

        current_plan = Subscription.get_plan_by_id('pro')
        new_plan = Subscription.get_plan_by_id('pro')

        may_29_2015 = datetime.datetime(2015, 5, 29, 0, 0, 0)
        may_29_2015 = pytz.utc.localize(may_29_2015)

        credits = add_subscription_credits(credits, current_plan, new_plan,
                                           may_29_2015)

        assert credits == 20
Esempio n. 4
0
    def update(self, user=None, coupon_code=None, plan=None):
        """
        Update an existing subscription.

        :param user: User to apply the subscription to
        :type user: User instance
        :param coupon_code: Coupon code to apply
        :type coupon_code: str
        :param plan: Plan identifier
        :type plan: str
        :return: bool
        """
        coupon = None
        if coupon_code:
            coupon_code = coupon_code.upper()
            coupon = Coupon.query.filter(Coupon.code == coupon_code).first()

            if not coupon:
                return False

        PaymentSubscription.update(user.payment_id, coupon_code, plan)

        user.previous_plan = user.subscription.plan
        user.subscription.plan = plan
        user.credits = add_subscription_credits(user.credits,
                                                Subscription.get_plan_by_id(
                                                    user.previous_plan),
                                                Subscription.get_plan_by_id(plan),
                                                user.cancelled_subscription_on)

        if coupon:
            user.subscription.coupon = coupon_code
            coupon.redeem()

        db.session.add(user.subscription)
        db.session.commit()

        return True
Esempio n. 5
0
    def create(self, user=None, name=None, plan=None, coupon_code=None,
               token=None):
        """
        Create a recurring subscription.

        :param user: User to apply the subscription to
        :type user: User instance
        :param name: User's billing name
        :type name: str
        :param plan: Plan identifier
        :type plan: str
        :param coupon_code: Coupon code to apply
        :type coupon_code: str
        :param token: Token returned by JavaScript
        :type token: str
        :return: bool
        """
        if token is None:
            return False

        coupon = None
        if coupon_code:
            coupon_code = coupon_code.upper()
            coupon = Coupon.query.filter(Coupon.code == coupon_code).first()

            # The user bypassed the JS and input an invalid coupon code.
            if not coupon:
                return False

        customer = PaymentCustomer.create(token, user.email)
        PaymentSubscription.create(customer.id, coupon_code, plan)

        # Update the user account.
        user.payment_id = customer.id
        user.name = name
        user.previous_plan = plan
        user.credits = add_subscription_credits(user.credits,
                                                Subscription.get_plan_by_id(
                                                    user.previous_plan),
                                                Subscription.get_plan_by_id(plan),
                                                user.cancelled_subscription_on)
        user.cancelled_subscription_on = None

        # Set the subscription details.
        self.user_id = user.id
        self.plan = plan

        # Attach and redeem the coupon.
        if coupon:
            self.coupon = coupon_code
            coupon.redeem()

        # Create the credit card.
        credit_card = CreditCard(user_id=user.id,
                                 **CreditCard.extract_card_params(customer))

        db.session.add(user)
        db.session.add(credit_card)
        db.session.add(self)

        db.session.commit()

        return True