def cancel(self, user=None, discard_credit_card=True): """ Cancel an existing subscription. :param user: User to apply the subscription to :type user: User instance :param discard_credit_card: Delete the user's credit card :type discard_credit_card: bool :return: bool """ PaymentSubscription.cancel(user.payment_id) user.payment_id = None user.cancelled_subscription_on = datetime.datetime.now(pytz.utc) user.previous_plan = user.subscription.plan db.session.add(user) db.session.delete(user.subscription) # Explicitly delete the credit card because the FK is on the # user, not subscription so we can't depend on cascading deletes. # This is for cases where you may want to keep a user's card # on file even if they cancelled. if discard_credit_card: db.session.delete(user.credit_card) db.session.commit() return True
def update(self, user=None, coupon=None, plan=None): """ Update an existing subscription. :param user: User to apply the subscription to :type user: User instance :param coupon: Coupon code to apply :type coupon: str :param plan: Plan identifier :type plan: str :return: bool """ PaymentSubscription.update(user.payment_id, coupon, plan) user.previous_plan = user.subscription.plan user.subscription.plan = plan user.coins = add_subscription_coins( user.coins, Subscription.get_plan_by_id(user.previous_plan), Subscription.get_plan_by_id(plan), user.cancelled_subscription_on) if coupon: user.subscription.coupon = coupon coupon = Coupon.query.filter(Coupon.code == coupon).first() if coupon: coupon.redeem() db.session.add(user.subscription) db.session.commit() return True
def cancel(self, user=None, discard_credit_card=True): """ Cancel an existing subscription. :param user: User to apply the subscription to :type user: User instance :param discard_credit_card: Delete the user's credit card :type discard_credit_card: bool :return: bool """ PaymentSubscription.cancel(user.payment_id) user.payment_id = None user.cancelled_subscription_on = datetime.datetime.now(pytz.utc) user.previous_plan = user.subscription.plan db.session.add(user) db.session.delete(user.subscription) if discard_credit_card: db.session.delete(user.credit_card) db.session.commit() return True
def cancel(self, user=None, discard_credit_card=True): """Cancel an existing subscription""" PaymentSubscription.cancel(user.payment_id) user.payment_id = None user.cancelled_subscription_on = datetime.datetime.now(pytz.utc) db.session.add(user) db.session.delete(user.subscription) if discard_credit_card: db.session.delete(user.credit_card) db.session.commit() return True
def create(self, user=None, name=None, plan=None, coupon=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: Coupon code to apply :type coupon: str :param token: Token returned by JavaScript :type token: str :return: bool """ if token is None: return False if coupon: self.coupon = coupon.upper() customer = PaymentSubscription.create(token=token, email=user.email, plan=plan, coupon=self.coupon) # Update the user account. user.payment_id = customer.id user.name = name user.cancelled_subscription_on = None user.previous_plan = plan user.coins = add_subscription_coins(user.coins, 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 # Redeem the coupon. if coupon: coupon = Coupon.query.filter(Coupon.code == self.coupon).first() 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
def update(self, user=None, plan=None, coupon=None): """Updating an existing subscription plan""" PaymentSubscription.update(customer_id=user.payment_id, coupon=coupon, plan=plan) user.subscription.plan = plan if coupon: user.subscription.coupon = coupon coupon = Coupon.query.filter(Coupon.coupon == coupon).first() if coupon: coupon.redeem() db.session.add(user.subscription) db.session.commit() return True
def create(self, user=None, plan=None, coupon=None, token=None, name=None): """Create a recurring subscription""" if token is None: return False if coupon: self.coupon = coupon.upper() customer = PaymentSubscription.create(token=token, email=user.email, coupon=None, plan=plan) # Updating user details user.name = name user.payment_id = customer.id user.cancelled_subscription_on = None # Set the subscription details self.user_id = user.id self.plan = plan # Redeem the coupon if coupon: coupon = Coupon.query.filter(Coupon.code == self.coupon).first() coupon.redeem() credit_card = CreditCard(user_id=user.id, **CreditCard.extract_card_params(customer)) db.session.add(user) db.session.add(self) db.session.add(credit_card) db.session.commit() return True