Esempio n. 1
0
def update_stripe_subscription_statuses():
    """Update Stripe subscriptions with their current status
    by querying Stripe api"""
    stripe.api_key = get_stripe_secret_key()
    connect_account = get_stripe_connect_account()
    if stripe.api_key is None:
        logging.error(
            "Stripe api key not set refusing to update subscription statuses")
    if connect_account is None:
        logging.error(
            "Stripe connect account not set. Refusing to update subscription statuses"
        )
    if stripe_connect_active():
        for subscription in Subscription.query.all():
            try:
                stripeSubscription = stripe.Subscription.retrieve(
                    stripe_account=connect_account.id,
                    id=subscription.stripe_subscription_id,
                )
                subscription.stripe_status = stripeSubscription.status
                database.session.commit()
            except Exception as e:
                logging.warning(
                    f"Could not update stripe subscription status: {e}")
    else:
        logging(
            "Refusing to update subscription status since Stripe connect is not active"
        )
Esempio n. 2
0
def stripe_onboarding():
    # Determine if in live or test mode
    payment_provider = PaymentProvider.query.first()
    stripe.api_key = get_stripe_secret_key()

    company = Company.query.first()

    # Use existing stripe_connect_account_id, otherwise create stripe connect account
    try:
        print("Trying if there's an existing stripe account")
        account = get_stripe_connect_account()
        print(f"Yes, stripe account found: {account.id}")
    except (
            stripe.error.PermissionError,
            stripe.error.InvalidRequestError,
            AttributeError,
    ):
        print("Could not find a stripe account, Creating stripe account")
        account = create_stripe_connect_account(company)
        if payment_provider.stripe_livemode:
            payment_provider.stripe_live_connect_account_id = account.id
        else:
            payment_provider.stripe_test_connect_account_id = account.id

    database.session.commit()

    session["account_id"] = account.id
    account_link_url = _generate_account_link(account.id)
    try:
        return jsonify({"url": account_link_url})
    except Exception as e:
        return jsonify(error=str(e)), 403
Esempio n. 3
0
def invoices():
    stripe.api_key = get_stripe_secret_key()
    connect_account = get_stripe_connect_account()
    invoices = stripe.Invoice.list(stripe_account=connect_account.id)

    return render_template(
        "admin/invoices.html",
        invoices=invoices,
        datetime=datetime,
    )
Esempio n. 4
0
def fetch_stripe_upcoming_invoices():
    """Fetch all Stripe upcoming invoices and populate the invoices table"""
    all_subscriptions = Subscription.query.all()
    upcoming_invoices = []
    stripe.api_key = get_stripe_secret_key()
    connect_account = get_stripe_connect_account()

    # Prepare to delete all previous upcomingInvoice
    UpcomingInvoice.query.delete(
    )  # don't commit until finished fetching latest collection # noqa
    for subscription in all_subscriptions:
        try:
            log.info(
                f"Getting upcoming invoice for Stripe subscription: {subscription.stripe_subscription_id}"  # noqa
            )
            if subscription.stripe_subscription_id is not None:
                upcoming_invoice = stripe.Invoice.upcoming(
                    subscription=subscription.stripe_subscription_id,
                    stripe_account=connect_account.id,
                )
                upcoming_invoices.append(upcoming_invoice)
                # Store the UpcomingInvoice
                upcomingInvoice = UpcomingInvoice()
                upcomingInvoice.subscription = subscription
                upcomingInvoice.stripe_invoice_id = None
                upcomingInvoice.stripe_subscription_id = (
                    subscription.stripe_subscription_id)
                upcomingInvoice.stripe_invoice_status = upcoming_invoice.status
                upcomingInvoice.stripe_amount_due = upcoming_invoice.amount_due
                upcomingInvoice.stripe_amount_paid = upcoming_invoice.amount_paid
                upcomingInvoice.stripe_next_payment_attempt = (
                    upcoming_invoice.next_payment_attempt)
                upcomingInvoice.stripe_currency = upcoming_invoice.currency

                database.session.add(upcomingInvoice)

        except stripe.error.InvalidRequestError as e:
            log.error(
                f"Cannot get stripe subscription id: {subscription.stripe_subscription_id}, {e}"  # noqa
            )
        except Exception as e:
            log.error(
                f"Error checking for upcoming invoice for {subscription.id}, {e}"
            )
        database.session.commit()
Esempio n. 5
0
    def stripe_subscription_active(self):
        if self.stripe_subscription_id is not None:

            stripe.api_key = get_stripe_secret_key()
            connect_account = get_stripe_connect_account()
            try:
                subscription = stripe.Subscription.retrieve(
                    stripe_account=connect_account.id,
                    id=self.stripe_subscription_id)
                if subscription.pause_collection is not None:
                    return False
                elif subscription.status == "active":
                    return True
            except stripe.error.InvalidRequestError as e:
                logging.error("Could not get stripe subscription status")
                logging.error(e)

        return False
Esempio n. 6
0
def get_subscription_status(stripe_external_id: str) -> str:
    status_on_error = "Unknown"
    if stripe_external_id is None:
        return status_on_error
    try:
        stripe.api_key = get_stripe_secret_key()
        connect_account = get_stripe_connect_account()
        subscription = stripe.Subscription.retrieve(
            stripe_account=connect_account.id, id=stripe_external_id)
        if subscription.pause_collection is not None:
            return "paused"
        else:
            return "active"
    except stripe.error.InvalidRequestError as e:
        print(e)
        return status_on_error
    except ValueError as e:
        print(e)
        return status_on_error
Esempio n. 7
0
def update_stripe_subscription_statuses():
    """Update Stripe subscriptions with their current status
    by querying Stripe api"""
    stripe.api_key = get_stripe_secret_key()
    connect_account = get_stripe_connect_account()
    if stripe.api_key is None:
        log.error(
            "Stripe api key not set refusing to update subscription statuses")
    if connect_account is None:
        log.error(
            "Stripe connect account not set. Refusing to update subscription statuses"
        )
    if stripe_connect_active():
        try:
            stripeSubscriptions = stripe.Subscription.list(
                stripe_account=connect_account.id, limit=100)
            for stripeSubscription in stripeSubscriptions.auto_paging_iter():

                subscription = (database.session.query(Subscription).where(
                    Subscription.stripe_subscription_id ==
                    stripeSubscription.id).first())
                if subscription:

                    subscription.stripe_status = stripeSubscription.status
                    log.info(subscription.stripe_status)
                    log.info(subscription.stripe_subscription_id)
                    database.session.commit()
                else:
                    log.warning(
                        "subscription is in stripe but not in the subscribie database"
                    )
        except Exception as e:
            log.warning(f"Could not update stripe subscription status: {e}")
    else:
        log.warning(
            "Refusing to update subscription status since Stripe connect is not active"
        )
Esempio n. 8
0
def stripe_connect():
    account = None
    stripe_express_dashboard_url = None
    stripe.api_key = get_stripe_secret_key()
    payment_provider = PaymentProvider.query.first()

    try:
        account = get_stripe_connect_account()
        if account is not None and account.charges_enabled and account.payouts_enabled:
            payment_provider.stripe_active = True
        else:
            payment_provider.stripe_active = False
    except (
            stripe.error.PermissionError,
            stripe.error.InvalidRequestError,
            AttributeError,
    ) as e:
        print(e)
        account = None

    # Setup Stripe webhook endpoint if it dosent already exist
    if account:
        try:
            stripe_express_dashboard_url = stripe.Account.create_login_link(
                account.id).url
        except stripe.error.InvalidRequestError:
            stripe_express_dashboard_url = None

    database.session.commit()
    return render_template(
        "admin/settings/stripe/stripe_connect.html",
        stripe_onboard_path=url_for("admin.stripe_onboarding"),
        account=account,
        payment_provider=payment_provider,
        stripe_express_dashboard_url=stripe_express_dashboard_url,
    )