def subscribe_to_plan(uid: str, data: Dict[str, Any]) -> FlaskResponse: """ Subscribe to a plan given a user id, payment token, email, orig_system :param uid: :param data: :return: current subscriptions for user. """ customer = existing_or_new_customer( g.subhub_account, user_id=uid, email=data["email"], source_token=data["pmt_token"], origin_system=data["origin_system"], display_name=data["display_name"], ) existing_plan = has_existing_plan(customer, plan_id=data["plan_id"]) if existing_plan: return dict(message="User already subscribed."), 409 if not customer.get("deleted"): vendor.build_stripe_subscription(customer.id, data["plan_id"], utils.get_indempotency_key()) updated_customer = fetch_customer(g.subhub_account, user_id=uid) newest_subscription = find_newest_subscription( updated_customer["subscriptions"]) return create_return_data(newest_subscription), 201 return dict(message=None), 400
def test_has_existing_plan_not_found(self): has_plan = has_existing_plan(self.customer1, "plan") assert has_plan == False
def test_has_existing_plan_no_subs(self): has_plan = has_existing_plan(self.customer_no_subs, "plan_test1") assert has_plan == False
def test_has_existing_plan_found(self): has_plan = has_existing_plan(self.customer1, "plan_test1") assert has_plan