Exemplo n.º 1
0
    def post(self):
        user = self.get_current_user_object()
        sub = user.active_paid_subscription()
        if sub:
            try:
                # TBD:
                customer = stripe.Customer.retrieve(user.stripe_customer_id)
                if customer:
                    subscription = customer.subscriptions.retrieve(sub["id"])
                    if subscription:
                        subscription.delete()

                user.is_paid = 0
                user.save()

                if options.postmark_api_key:
                    pm = postmark.PMMail(
                        api_key=options.postmark_api_key,
                        sender="*****@*****.**",
                        to="*****@*****.**",
                        subject="%s has cancelled a subscription" %
                        (user.display_name()),
                        text_body=
                        "Subscription ID: %s\nBuyer Name:%s\nBuyer Email:%s\nUser ID:%s\n"
                        %
                        (sub["id"], user.display_name(), user.email, user.id))
                    pm.send()

                payment_notifications(user, 'cancellation')
            except stripe.error.InvalidRequestError:
                pass
        self.redirect('/account/settings')
Exemplo n.º 2
0
    def post(self):
        user = self.get_current_user_object()
        if user.stripe_customer_id:
            try:
                customer = stripe.Customer.retrieve(user.stripe_customer_id)
                if customer and customer.subscriptions.total_count > 0:
                    subscription = customer.subscriptions.data[0]
                    if subscription:
                        subscription.delete(at_period_end=True)
                        user.stripe_plan_id = user.stripe_plan_id + "-canceled"
                        user.stripe_plan_rate = None
                        user.save()

                        if options.postmark_api_key:
                            pm = postmark.PMMail(
                                api_key=options.postmark_api_key,
                                sender="*****@*****.**",
                                to="*****@*****.**",
                                subject="%s has cancelled a subscription" %
                                (user.display_name()),
                                text_body=
                                "Subscription ID: %s\nBuyer Name:%s\nBuyer Email:%s\nUser ID:%s\n"
                                % (sub["id"], user.display_name(), user.email,
                                   user.id))
                            pm.send()

                        payment_notifications(user, 'cancellation')
            except stripe.error.InvalidRequestError:
                pass
        return self.redirect('/account/settings?cancel=1')
Exemplo n.º 3
0
    def apply_to_user(self, user_):
        from models import PaymentLog

        promotion = self.get_promotion()

        self.offered_by_user_id = 0

        if not self.offered_by_user_id and promotion is not None:
            shake = promotion.shake()
            if shake is not None:
                # if this promotion is related to a shake
                # associate the voucher with that shake's owner
                self.offered_by_user_id = shake.user_id

        now = datetime.datetime.utcnow()

        # if the user has a voucher, then we need
        # to apply a credit to their account using
        # payment_log in addition to creating the
        # voucher record and claiming it.
        self.claimed_by_user_id = user_.id
        self.claimed_at = now.strftime("%Y-%m-%d %H:%M:%S")
        self.save()

        # now record to payment_log that this
        # user has claimed a voucher.
        next_date = None
        amount = None

        promotion = self.get_promotion()
        # make a sensible "transaction amount" description
        # that we use for the settings screen
        if promotion is not None \
            and promotion.membership_months > 0:
            months = promotion.membership_months
            days = int((365 * (months/12.0)) + 0.5)
            next_date = now + datetime.timedelta(days=days)
            if months >= 12 and months % 12 == 0:
                years = months / 12
                if years == 1:
                    amount = '1 Year'
                elif years > 1:
                    amount = '%d Years' % years
            elif months == 1:
                amount = '1 Month'
            elif months > 1:
                amount = '%d Months' % months

            pl = PaymentLog(
                user_id                   = user_.id,
                status                    = "credit",
                reference_id              = promotion.id,
                transaction_id            = self.voucher_key,
                operation                 = "redeem",
                transaction_date          = now.strftime("%Y-%m-%d %H:%M:%S"),
                next_transaction_date     = next_date.strftime("%Y-%m-%d %H:%M:%S"),
                buyer_email               = user_.email,
                buyer_name                = (user_.full_name or user_.name),
                transaction_amount        = amount,
                payment_reason            = "MLTSHP Paid Account",
                payment_method            = "voucher",
                processor                 = PaymentLog.VOUCHER,
                transaction_serial_number = 0
            )
            pl.save()

            # update user paid status if necessary
            if user_.is_paid != 1:
                user_.is_paid = 1
                user_.save()
                payment_notifications(user_, "redeemed", amount)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    def post(self):
        current_user = self.get_current_user_object()
        # an existing, paying user can't subscribe again...
        # we also won't allow users with unconfirmed emails to subscribe
        # should we message here?
        if current_user.is_paid == 1 or current_user.email_confirmed != 1:
            return self.redirect("/account/settings")

        token_id = self.get_argument("token")
        if token_id is None:
            # invalid request
            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:
                # create a new customer object for this subscription
                customer = stripe.Customer.create(
                    description="MLTSHP user %s" % current_user.name,
                    email=current_user.email,
                    plan=options.stripe_annual_plan_id,
                    card=token_id,  # obtained with Checkout.js
                    metadata={"mltshp_user": current_user.name},
                )
                # if this works, we should have a customer with 1 subscription, this one
                if customer.subscriptions.total_count > 0:
                    sub = customer.subscriptions.data[0]
            else:
                sub = customer.subscriptions.create(
                    plan=options.stripe_annual_plan_id, card=token_id)
        except stripe.error.CardError as ex:
            return self.render("account/return-subscription-completed.html",
                               error=unicode(ex))

        if not sub:
            raise Exception("Error issuing subscription")

        amount = "USD %0.2f" % (sub.plan.amount / 100.0)
        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, LLC",
            payment_reason="MLTSHP Paid Account",
            transaction_serial_number=1,
            subscription_id=sub.id,
            payment_method="CC",
            transaction_amount=amount,
        )
        payment_log.save()
        current_user.is_paid = 1

        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)

        return self.render("account/return-subscription-completed.html")