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 = PaymentCustomer.create(token=token, email=user.email, plan=plan, coupon=self.coupon) # Update the user account. user.payment_id = customer.id user.name = name 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 test_mark_old_credit_cards(self, session, credit_cards): """ Credit cards that are about to expire should get marked. """ may_29_2015 = datetime.date(2015, 5, 29) june_29_2015 = datetime.date(2015, 6, 29) CreditCard.mark_old_credit_cards(may_29_2015) card = CreditCard.query.filter(CreditCard.exp_date == june_29_2015) assert card.first().is_expiring
def test_avoid_marking_up_to_date_credit_cards(self, session, credit_cards): """ Credit cards that are not expiring should not be marked. """ may_29_2015 = datetime.date(2015, 5, 29) may_28_2016 = datetime.date(2016, 5, 28) CreditCard.mark_old_credit_cards(may_29_2015) card = CreditCard.query.filter(CreditCard.exp_date == may_28_2016) assert not card.first().is_expiring
def update_payment_method(self, user=None, credit_card=None, name=None, token=None): """ Update the subscription. :param user: User to modify :type user: User instance :param credit_card: Card to modify :type credit_card: Credit Card instance :param name: User's billing name :type name: str :param token: Token returned by JavaScript :type token: str :return: bool """ if token is None: return False customer = PaymentCard.update(user.payment_id, token) user.name = name # Update the credit card. new_card = CreditCard.extract_card_params(customer) credit_card.brand = new_card.get('brand') credit_card.last4 = new_card.get('last4') credit_card.exp_date = new_card.get('exp_date') credit_card.is_expiring = new_card.get('is_expiring') db.session.add(user) db.session.add(credit_card) db.session.commit() return True
def mark_old_credit_cards(): """ Mark credit cards that are going to expire soon or have expired. :return: Result of updating the records """ return CreditCard.mark_old_credit_cards()
def credit_cards(db): """ Create credit card fixtures. :param db: Pytest fixture :return: SQLAlchemy database session """ db.session.query(CreditCard).delete() may_29_2015 = datetime.date(2015, 5, 29) june_29_2015 = datetime.datetime(2015, 6, 29, 0, 0, 0) june_29_2015 = pytz.utc.localize(june_29_2015) credit_cards = [{ 'user_id': 1, 'brand': 'Visa', 'last4': 4242, 'exp_date': june_29_2015 }, { 'user_id': 1, 'brand': 'Visa', 'last4': 4242, 'exp_date': timedelta_months(12, may_29_2015) }] for card in credit_cards: db.session.add(CreditCard(**card)) db.session.commit() return db
def test_credit_card_not_expiring_soon(self): """ Credit card is not expiring soon. """ may_29_2015 = datetime.date(2015, 5, 29) exp_dates = (datetime.date(2015, 8, 28), datetime.date(2016, 1, 1), datetime.date(2016, 5, 29)) for date in exp_dates: assert CreditCard.is_expiring_soon(may_29_2015, date) is False
def test_credit_card_expiring_soon(self): """ Credit card is expiring soon. """ may_29_2015 = datetime.date(2015, 5, 29) exp_dates = (datetime.date(2000, 1, 1), datetime.date(2015, 6, 3), datetime.date(2015, 7, 1)) for date in exp_dates: assert CreditCard.is_expiring_soon(may_29_2015, date)
def subscriptions(db): """ Create subscription fixtures. :param db: Pytest fixture :return: SQLAlchemy database session """ subscriber = User.find_by_identity('*****@*****.**') if subscriber: subscriber.delete() db.session.query(Subscription).delete() params = { 'role': 'admin', 'email': '*****@*****.**', 'name': 'Subby', 'password': '******', 'payment_id': 'cus_000' } subscriber = User(**params) # The account needs to be commit before we can assign a subscription to it. db.session.add(subscriber) db.session.commit() # Create a subscription. params = {'user_id': subscriber.id, 'plan': 'gold'} subscription = Subscription(**params) db.session.add(subscription) # Create a credit card. params = { 'user_id': subscriber.id, 'brand': 'Visa', 'last4': '4242', 'exp_date': datetime.date(2015, 6, 1) } credit_card = CreditCard(**params) db.session.add(credit_card) db.session.commit() return db
def create(self, user=None, currency=None, amount=None, coins=None, coupon=None, token=None): """ Create an invoice item. :param user: User to apply the subscription to :type user: User instance :param amount: Stripe currency :type amount: str :param amount: Amount in cents :type amount: int :param coins: Amount of coins :type coins: int :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 customer = PaymentCustomer.create(token=token, email=user.email) if coupon: self.coupon = coupon.upper() coupon = Coupon.query.filter(Coupon.code == self.coupon).first() amount = coupon.apply_discount_to(amount) charge = PaymentCharge.create(customer.id, currency, amount) # Redeem the coupon. if coupon: coupon.redeem() # Add the coins to the user. user.coins += coins # Create the invoice item. period_on = datetime.datetime.utcfromtimestamp(charge.get('created')) card_params = CreditCard.extract_card_params(customer) self.user_id = user.id self.plan = '—' self.receipt_number = charge.get('receipt_number') self.description = charge.get('statement_descriptor') self.period_start_on = period_on self.period_end_on = period_on self.currency = charge.get('currency') self.tax = None self.tax_percent = None self.total = charge.get('amount') self.brand = card_params.get('brand') self.last4 = card_params.get('last4') self.exp_date = card_params.get('exp_date') db.session.add(user) db.session.add(self) db.session.commit() return True