【发布时间】: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: <built-in function id>, <class 'builtin_function_or_method'>. ID should be of type str(orunicode)
我把 doc 作为reference。但是,尽管阅读了所有文档,但在过去的几个小时内,我似乎无法弄清楚如何从 webhook 或以任何其他方式检索 stripeSubscriptionId 和 stripeCustomerId。我看过其他 SO 页面,但找不到与我有类似问题的可行解决方案。
【问题讨论】:
标签: python api flask stripe-payments webhooks