【问题标题】:stripe.error.InvalidRequestError: Could not determine which URL to request: Customer instance has invalid ID: <built-in function id>stripe.error.InvalidRequestError:无法确定要请求的 URL:客户实例的 ID 无效:<内置函数 ID>
【发布时间】:2021-02-27 05:59:13
【问题描述】:

我正在尝试检索条带用户的 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: &lt;built-in function id&gt;, &lt;class 'builtin_function_or_method'&gt;. ID should be of type str(orunicode)

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

【问题讨论】:

    标签: python api flask stripe-payments webhooks


    【解决方案1】:
        subID = stripe.Customer.retrieve(id, status)
        logging.warn(str(subID))
    

    您不会在此处获得id。您可能想使用session["id"]。另外我不确定status 应该在那里?

    【讨论】:

    • 订阅状态。我会检查代码 rn。我假设订阅状态应该是stripe.Customer.retrieve(session['status']) 还是应该只是session['status']?另外,是否会检索 stripeSubscriptionId 或 stripeCustomerId,因为我想知道如何同时获得两者。
    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2017-02-04
    • 2016-02-18
    • 2020-12-04
    • 2016-11-25
    • 1970-01-01
    • 2017-01-18
    相关资源
    最近更新 更多