,python,api,flask,stripe-payments,webhooks"/>

首页 > 解决方案 > stripe.error.InvalidRequestError:无法确定要请求的 URL:客户实例的 ID 无效:

问题描述

我正在尝试检索条带用户的 stripeSubscriptionId 和 stripeCustomerId。这是我的代码:

@blueprint.route("/webhook", methods=["POST"]) #confirms whether a user has subscribed or not
def stripe_webhook():
    payload = request.get_data(as_text=True)
    sig_header = request.headers.get("Stripe-Signature")

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, stripe_keys["endpoint_secret"]
        )

    except ValueError as e:
        # Invalid payload
        return "Invalid payload", 400
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        return "Invalid signature", 400

    # Handle the checkout.session.completed event
    if event["type"] == "checkout.session.completed":
        session = event["data"]["object"]
        # Fulfill the purchase...
        handle_checkout_session(session)

    return "Success", 200

def handle_checkout_session(session):
    subID = stripe.Customer.retrieve(id, status)
    logging.warn(str(subID))

@blueprint.route("/create-checkout-session")
def create_checkout_session():
    domain_url = "http://localhost:5000/"
    stripe.api_key = stripe_keys["secret_key"]

    try:
        checkout_session = stripe.checkout.Session.create(
            
            success_url=domain_url + "success?session_id={CHECKOUT_SESSION_ID}",
            cancel_url=domain_url + "cancel",
            payment_method_types=["card"],
            mode="subscription",
            line_items=[
                {
                    "price": stripe_keys["price_id"],
                    "quantity": 1,
                }
            ]
        )
        return jsonify({"sessionId": checkout_session["id"]})
    except Exception as e:
        return jsonify(error=str(e)), 403

然而我得到:stripe.error.InvalidRequestError: Could not determine which URL to request: Customer instance has invalid ID: <built-in function id>, <class 'builtin_function_or_method'>. ID should be of type str (orunicode)

我把 doc 作为参考。但是,尽管阅读了所有文档,但在过去的几个小时内,我似乎无法弄清楚如何从 webhook 或以任何其他方式检索 stripeSubscriptionId 和 stripeCustomerId。我看过其他 SO 页面,但找不到一个与我有类似问题的可行解决方案。

标签: pythonapiflaskstripe-paymentswebhooks

解决方案


    subID = stripe.Customer.retrieve(id, status)
    logging.warn(str(subID))

你在id这里没有得到任何地方。您可能想要使用session["id"]. 另外我不确定那里status应该有什么?


推荐阅读