python - 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 (or
unicode)
我把 doc 作为参考。但是,尽管阅读了所有文档,但在过去的几个小时内,我似乎无法弄清楚如何从 webhook 或以任何其他方式检索 stripeSubscriptionId 和 stripeCustomerId。我看过其他 SO 页面,但找不到一个与我有类似问题的可行解决方案。
解决方案
subID = stripe.Customer.retrieve(id, status)
logging.warn(str(subID))
你在id
这里没有得到任何地方。您可能想要使用session["id"]
. 另外我不确定那里status
应该有什么?
推荐阅读
- extjs - 如何更新 extJs 中的存储(出现 404 错误)?
- database - 估计 Google BigTable 中的数据大小
- c - 如何在C中打印菱形图案?
- docker - 在 Docker 中启动 JFrog 的问题
- javascript - BroadcastDispatcher 完成事件仅调用一次 (discord.js)
- html - 在 HTML 中,我应该为幻想或其他发明的语言使用什么语言属性代码?
- objective-c - 使用 vImageScale_ARGB8888 缩放图像时图像质量受到影响 - Cocoa Objective C
- freemarker - 如何使用 freemarker 创建 csv 输出?
- javascript - HTML表单不向节点js发送数据
- spring - 使用spring cloud gateway登录后spring could oauth2重定向到“/”