Josh-XT/AGiXT

make sure if the stripe request fails, an exception gets thrown.

Closed this issue · 0 comments

# TODO make sure if the stripe request fails, an exception gets thrown.

                requirements = json.load(file)
        if not requirements:
            requirements = {}
        if "stripe_id" not in requirements:
            requirements["stripe_id"] = "None"
        return requirements

    def get_subscribed_products(self, stripe_api_key, stripe_customer_id):
        import stripe

        # TODO make sure if the stripe request fails, an exception gets thrown.
        stripe.api_key = stripe_api_key
        logging.info(f"Checking subscriptions for customer {stripe_customer_id}...")
        all_subscriptions = stripe.Subscription.list(
            customer=stripe_customer_id,
            expand=["data.items.data.price"],
        )
        logging.info(f"Found {len(all_subscriptions)} subscriptions.")
        relevant_subscriptions = []
        for subscription in all_subscriptions:
            logging.info(f"Checking subscription {subscription['id']}")
            if subscription.status == "active":
                logging.info(f"Subscription {subscription['id']} active.")
                relevant_to_this_app = None
                for item in subscription["items"]:
                    product_id = item["price"]["product"]
                    product = stripe.Product.retrieve(product_id)
                    try:
                        relevant_app = product["metadata"]["APP_NAME"]
                    except:
                        relevant_app = "[No App Defined in Product]"
                        logging.warning(
                            f"Subscription detected with items missing relevant APP_NAME metadata: {subscription['id']}"
                        )
                    print(product)
                    print(relevant_app)
                    if relevant_app == getenv("APP_NAME"):
                        if relevant_to_this_app == False:
                            raise Exception(
                                f"Subscription detected with items from multiple apps (or products missing metadata): {subscription['id']}"
                            )
                        relevant_to_this_app = True
                        logging.info(
                            f"Subscription {subscription['id']} relevant to this app {getenv('APP_NAME')}."
                        )
                    else:
                        if relevant_to_this_app == True:
                            raise Exception(
                                f"Subscription detected with items from multiple apps (or products missing metadata): {subscription['id']}"
                            )
                        relevant_to_this_app = False
                        logging.info(
                            f"Subscription {subscription['id']} not relevant to this app {getenv('APP_NAME')}, is for {relevant_app}."
                        )
                    if relevant_to_this_app:
                        relevant_subscriptions.append(subscription)
            else:
                logging.info(f"Subscription {subscription['id']} not active.")
        return relevant_subscriptions

    def get_user_preferences(self):
        session = get_session()
        user = session.query(User).filter(User.id == self.user_id).first()