def post(self): user = self.get_current_user_object() state = MigrationState.has_migrated(user.id) if not state: migrate_for_user.delay_or_run(user.id) return self.redirect("/account/settings?migrated=1")
def get(self, user_name=None, page=None): if not user_name: raise tornado.web.HTTPError(404) user = User.get("name = %s", user_name) if not user or user.deleted: raise tornado.web.HTTPError(404) can_follow = False current_user = self.get_current_user_object() can_follow = not current_user.has_subscription(user) # we get None for page argument when non specified, # otherwise we get a string. if not page: page = 1 page = int(page) url_format = '/user/%s/' % user.name url_format = url_format + '%d' following_shakes = user.following() following_shakes_count = len(following_shakes) user_shake = user.shake() followers = user_shake.subscribers() follower_count = len(followers) count = user_shake.sharedfiles_count() images = user_shake.sharedfiles(page=page) has_data_to_migrate = False if current_user.id == user.id: # if the user is looking at their own shake, then # check to see if they haven't migrated yet; remind # them at the top of their shake if they do... has_data_to_migrate = not MigrationState.has_migrated( current_user.id) other_shakes = user.shakes(include_managed=False, include_only_group_shakes=True) if not images and page != 1: raise tornado.web.HTTPError(404) return self.render("account/index.html", images=images, user=user, current_user_obj=current_user, count=count, page=page, url_format=url_format, can_follow=can_follow, following_shakes=following_shakes[:10], following_shakes_count=following_shakes_count, followers=followers[:10], follower_count=follower_count, other_shakes=other_shakes, has_data_to_migrate=has_data_to_migrate)
def get(self): user = self.get_current_user_object() payments = [] if user.is_paid: payments = PaymentLog.last_payments(count=3, user_id=user.id) already_requested = self.get_secure_cookie("image_request") cancel_flag = "canceled" in (user.stripe_plan_id or "") migrated_flag = self.get_argument('migrated', 0) promotions = Promotion.active() has_data_to_migrate = not MigrationState.has_migrated(user.id) return self.render("account/settings.html", user=user, payments=payments, already_requested=already_requested, promotions=promotions, plan_name=plan_name(user.stripe_plan_id), has_data_to_migrate=has_data_to_migrate, migrated_flag=migrated_flag, cancel_flag=cancel_flag)
def post(self): current_user = self.get_current_user_object() token_id = None if current_user.stripe_customer_id is None: token_id = self.get_argument("token") if token_id is None: # invalid request raise Exception("Invalid request") plan_id = self.get_argument("plan_id") if plan_id not in ("mltshp-single", "mltshp-double"): raise Exception("Invalid request") quantity = 1 if plan_id == "mltshp-double": quantity = int(float(self.get_argument("quantity"))) if quantity < 24 or quantity > 500: raise "Invalid request" customer = None sub = None stripe_customer_id = current_user.stripe_customer_id if stripe_customer_id is not None: try: customer = stripe.Customer.retrieve(stripe_customer_id) except stripe.error.InvalidRequestError: pass if customer and getattr(customer, "deleted", False): customer = None try: if customer is None: if token_id is None: # FIXME: handle this more gracefully... raise "Invalid request" # create a new customer object for this subscription customer = stripe.Customer.create( description="MLTSHP user %s" % current_user.name, email=current_user.email, metadata={"mltshp_user": current_user.name}, source=token_id, ) # if this works, we should have a customer with 1 subscription, this one if customer.subscriptions.total_count > 0: sub = customer.subscriptions.data[0] if sub.plan != plan_id: sub.plan = plan_id if plan_id == "mltshp-double": sub.quantity = quantity else: sub.quantity = 1 sub.save() else: if plan_id == "mltshp-double": sub = customer.subscriptions.create(plan=plan_id, quantity=quantity) else: sub = customer.subscriptions.create(plan=plan_id) except stripe.error.CardError as ex: return self.render("account/return-subscription-completed.html", error=unicode(ex), has_data_to_migrate=False) if not sub: raise Exception("Error issuing subscription") amount = "USD %0.2f" % ((sub.plan.amount / 100.0) * quantity) payment_log = PaymentLog( processor=PaymentLog.STRIPE, user_id=current_user.id, status="subscribed", reference_id=sub.id, # ?? transaction_id=sub.id, # ?? operation="order", transaction_date=datetime.datetime.fromtimestamp( sub.current_period_start).strftime("%Y-%m-%d %H:%M:%S"), next_transaction_date=datetime.datetime.fromtimestamp( sub.current_period_end).strftime("%Y-%m-%d %H:%M:%S"), buyer_email=current_user.email, buyer_name=current_user.display_name(), recipient_email="*****@*****.**", recipient_name="MLTSHP, Inc.", payment_reason="MLTSHP Membership", transaction_serial_number=1, subscription_id=sub.id, payment_method="CC", transaction_amount=amount, ) payment_log.save() current_user.is_paid = 1 current_user.stripe_plan_id = plan_id if plan_id == "mltshp-double": current_user.stripe_plan_rate = quantity else: current_user.stripe_plan_rate = None if current_user.stripe_customer_id != customer.id: current_user.stripe_customer_id = customer.id current_user.save() if options.postmark_api_key: pm = postmark.PMMail( api_key=options.postmark_api_key, sender="*****@*****.**", to="*****@*****.**", subject="%s has created a subscription" % (payment_log.buyer_name), text_body= "Subscription ID: %s\nBuyer Name:%s\nBuyer Email:%s\nUser ID:%s\n" % (payment_log.subscription_id, payment_log.buyer_name, payment_log.buyer_email, current_user.id)) pm.send() payment_notifications(current_user, "subscription", amount) has_data_to_migrate = not MigrationState.has_migrated(current_user.id) return self.render("account/return-subscription-completed.html", has_data_to_migrate=has_data_to_migrate)
def get(self): user = self.get_current_user_object() state = MigrationState.has_migrated(user.id) return self.render("account/migrate.html", name=user.name, has_migrated=state)
def get(self): user = self.get_current_user_object() payments = [] if user.is_paid: payments = [] if user.stripe_customer_id: customer_id = user.stripe_customer_id charges = stripe.Charge.list(limit=5, customer=customer_id) for charge in charges.data: payments.append({ "transaction_amount": "USD %0.2f" % (charge.amount / 100.0, ), "refund_amount": charge.refunded and "USD %0.2f" % (charge.amount_refunded / 100.0, ) or "", "created_at": datetime.datetime.fromtimestamp(charge.created), "status": "charged", "is_pending": charge.status == "pending", "is_failed": charge.status == "failed", "is_success": charge.status == "succeeded", "is_refund": charge.refunded, }) else: log = PaymentLog.last_payments(count=5, user_id=user.id) for payment in log: payments.append({ "transaction_amount": payment.transaction_amount, "refund_amount": "", "created_at": payment.created_at, "status": payment.status, "is_pending": False, "is_success": True, "is_failed": False, "is_refund": False, }) already_requested = self.get_secure_cookie("image_request") cancel_flag = "canceled" in (user.stripe_plan_id or "") updated_flag = self.get_argument("update", "") == "1" migrated_flag = self.get_argument("migrated", 0) past_due = False source_card_type = None source_last_4 = None source_expiration = None promotions = Promotion.active() has_data_to_migrate = not MigrationState.has_migrated(user.id) if user.stripe_customer_id: customer = None try: customer = stripe.Customer.retrieve(user.stripe_customer_id) except stripe.error.InvalidRequestError: pass if customer and not hasattr(customer, 'deleted'): if customer.subscriptions.total_count >= 1: subscriptions = [ sub for sub in customer.subscriptions.data if sub.plan.id == user.stripe_plan_id ] if subscriptions: subscription = subscriptions[0] past_due = subscription.status == "past_due" if customer.sources.total_count > 0: if customer.sources.data[0].object == "card": card = customer.sources.data[0] elif customer.sources.data[0].object == "source": card = customer.sources.data[0].card source_card_type = card.brand source_last_4 = card.last4 source_expiration = "%d/%d" % (card.exp_month, card.exp_year) return self.render("account/settings.html", user=user, payments=payments, already_requested=already_requested, promotions=promotions, plan_name=plan_name(user.stripe_plan_id), past_due=past_due, stripe_public_key=options.stripe_public_key, source_card_type=source_card_type, source_last_4=source_last_4, source_expiration=source_expiration, has_data_to_migrate=has_data_to_migrate, updated_flag=updated_flag, migrated_flag=migrated_flag, cancel_flag=cancel_flag)