Exemple #1
0
def subscribe_to_plan(uid, data) -> 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["orig_system"],
        display_name=data["display_name"],
    )
    existing_plan = has_existing_plan(customer, plan_id=data["plan_id"])
    if existing_plan:
        return {"message": "User already subscribed."}, 409
    if not customer.get("deleted"):
        Subscription.create(customer=customer.id,
                            items=[{
                                "plan": data["plan_id"]
                            }])
        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
    else:
        return dict(message=None), 400
Exemple #2
0
def subscribe_to_plan(uid, data) -> FlaskResponse:
    """
    Subscribe to a plan given a user id, payment token, email, orig_system
    :param uid:
    :param data:
    :return: current subscriptions for user.
    """
    try:
        customer = existing_or_new_customer(
            g.subhub_account,
            user_id=uid,
            email=data["email"],
            source_token=data["pmt_token"],
            origin_system=data["orig_system"],
        )
    except InvalidRequestError as e:
        return {"message": f"Unable to subscribe. {e}"}, 400
    existing_plan = has_existing_plan(customer, plan_id=data["plan_id"])
    if existing_plan:
        return {"message": "User already subscribed."}, 409
    if existing_plan:
        return {"message": "User already subscribed."}, 409
    try:
        stripe.Subscription.create(customer=customer.id,
                                   items=[{
                                       "plan": data["plan_id"]
                                   }])
    except InvalidRequestError as e:
        return {"message": f"Unable to subscribe: {e}"}, 400
    updated_customer = stripe.Customer.retrieve(customer.id)
    return create_return_data(updated_customer["subscriptions"]), 201
Exemple #3
0
def test_existing_or_new_customer_invalid_origin_system():
    """
    GIVEN create a stripe customer
    WHEN An invalid origin system is provided
    THEN An exception should be raised
    """
    origin_system = "NOT_VALID"
    with pytest.raises(InvalidRequestError) as request_error:
        existing_or_new_customer(
            g.subhub_account,
            user_id="test_mozilla",
            source_token="tok_visa",
            email="*****@*****.**",
            origin_system=origin_system,
            display_name="John Tester",
        )
    msg = f"origin_system={origin_system} not one of allowed origin system values, please contact a system administrator in the #subscription-platform channel."
    assert msg == str(request_error.value)
Exemple #4
0
def test_existing_or_new_customer_invalid_origin_system():
    """
    GIVEN create a stripe customer
    WHEN An invalid origin system is provided
    THEN An exception should be raised
    """
    origin_system = "NOT_VALID"
    with pytest.raises(InvalidRequestError) as request_error:
        existing_or_new_customer(
            g.subhub_account,
            user_id="test_mozilla",
            source_token="tok_visa",
            email="*****@*****.**",
            origin_system=origin_system,
            display_name="John Tester",
        )
    msg = f"origin_system={origin_system} not one of {CFG.ALLOWED_ORIGIN_SYSTEMS}"
    assert msg == str(request_error.value)