def test_retrieve_error(self): self.retrieve_product_mock.side_effect = InvalidRequestError( "message", param="prod_id" ) with self.assertRaises(InvalidRequestError): vendor.retrieve_stripe_product("prod_test1")
def _get_all_plans() -> List[Dict[str, str]]: plans = vendor.retrieve_plan_list(100) logger.info("number of plans", count=len(plans)) stripe_plans = [] products = {} # type: Dict for plan in plans: try: product = products[plan["product"]] except KeyError: product = vendor.retrieve_stripe_product(plan["product"]) products[plan["product"]] = product plan_name = format_plan_nickname(product_name=product["name"], plan_interval=plan["interval"]) stripe_plans.append({ "plan_id": plan["id"], "product_id": plan["product"], "interval": plan["interval"], "amount": plan["amount"], "currency": plan["currency"], "plan_name": plan_name, "product_name": product["name"], }) return stripe_plans
def format_subscriptions_data( subscription_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """ Prepare Subscription Data for response :param subscription_data: :return: """ products = {} # type: Dict subscriptions = [] for subscription in subscription_data: try: product = products[subscription["plan"]["product"]] except KeyError: product = retrieve_stripe_product(subscription["plan"]["product"]) products[subscription["plan"]["product"]] = product intents = None if subscription.get("status", None) == "incomplete": invoice = retrieve_stripe_invoice(subscription["latest_invoice"]) if invoice["charge"]: intents = retrieve_stripe_charge(invoice["charge"]) intents = convert_to_dict(intents) subscriptions.append( format_subscription(subscription, product, intents)) return subscriptions
def create_return_data(subscriptions) -> JsonDict: """ Create json object subscriptions object :param subscriptions: :return: JSON data to be consumed by client. """ return_data: Dict[str, Any] = {} return_data["subscriptions"] = [] products = {} # type: Dict for subscription in subscriptions["data"]: try: product = products[subscription["plan"]["product"]] except KeyError: product = vendor.retrieve_stripe_product( subscription["plan"]["product"]) products[subscription["plan"]["product"]] = product plan_name = format_plan_nickname( product_name=product["name"], plan_interval=subscription["plan"]["interval"]) if subscription["status"] == "incomplete": invoice = vendor.retrieve_stripe_invoice( subscription["latest_invoice"]) if invoice["charge"]: intents = vendor.retrieve_stripe_customer(invoice["charge"]) logger.debug("intents", intents=intents) return_data["subscriptions"].append({ "current_period_end": subscription["current_period_end"], "current_period_start": subscription["current_period_start"], "ended_at": subscription["ended_at"], "plan_name": plan_name, "plan_id": subscription["plan"]["id"], "status": subscription["status"], "subscription_id": subscription["id"], "cancel_at_period_end": subscription["cancel_at_period_end"], "failure_code": intents["failure_code"], "failure_message": intents["failure_message"], }) continue return_data["subscriptions"].append( create_subscription_object_without_failure(subscription, plan_name)) return return_data
def test_retrieve_success(self): self.retrieve_product_mock.side_effect = [ APIConnectionError("message"), self.product, ] product = vendor.retrieve_stripe_product("prod_test1") assert product == self.product # nosec
def _get_all_plans() -> List[Dict[str, str]]: plans = retrieve_plan_list(100) logger.debug("number of plans", count=len(plans)) stripe_plans = [] products = {} # type: Dict for plan in plans: try: product = products[plan["product"]] except KeyError: product = retrieve_stripe_product(plan["product"]) products[plan["product"]] = product stripe_plans.append(format_plan(plan, product)) return stripe_plans
def find_stripe_product(product_id: str) -> Product: """ Find the Stripe Product by ID :raise EntityNotFoundError :param plan_id: :return: """ try: product = retrieve_stripe_product(product_id) return product except InvalidRequestError as e: if e.http_status == 404: raise EntityNotFoundError(message="Product not found", error_number=4002) raise e
def create_update_data(customer) -> Dict[str, Any]: """ Provide readable data for customer update to display :param customer: :return: return_data dict """ payment_sources = customer["sources"]["data"] return_data: Dict[str, Any] = dict() return_data["subscriptions"] = [] return_data["payment_type"] = "" return_data["last4"] = "" return_data["exp_month"] = "" return_data["exp_year"] = "" if len(payment_sources) > 0: first_payment_source = payment_sources[0] return_data["payment_type"] = first_payment_source.get("funding") return_data["last4"] = first_payment_source.get("last4") return_data["exp_month"] = first_payment_source.get("exp_month") return_data["exp_year"] = first_payment_source.get("exp_year") products = {} # type: Dict for subscription in customer["subscriptions"]["data"]: try: product = products[subscription["plan"]["product"]] except KeyError: product = vendor.retrieve_stripe_product( subscription["plan"]["product"]) products[subscription["plan"]["product"]] = product plan_name = format_plan_nickname( product_name=product["name"], plan_interval=subscription["plan"]["interval"]) if subscription["status"] == "incomplete": invoice = vendor.retrieve_stripe_invoice( subscription["latest_invoice"]) if invoice["charge"]: intents = vendor.retrieve_stripe_customer(invoice["charge"]) intents = intents.to_dict() return_data["subscriptions"].append({ "current_period_end": subscription["current_period_end"], "current_period_start": subscription["current_period_start"], "ended_at": subscription["ended_at"], "plan_name": plan_name, "plan_id": subscription["plan"]["id"], "status": subscription["status"], "cancel_at_period_end": subscription["cancel_at_period_end"], "subscription_id": subscription["id"], "failure_code": intents["failure_code"], "failure_message": intents["failure_message"], }) continue return_data["cancel_at_period_end"] = subscription[ "cancel_at_period_end"] return_data["subscriptions"].append( create_subscription_object_without_failure(subscription, plan_name)) return return_data
def test_retrieve_stripe_product_error(): disable_base() with pytest.raises(APIError): vendor.retrieve_stripe_product("no_prod") enable_base()