我正在使用 ASP.NET Web 表单应用程序并设置了 Stripe Checkout 页面。 Webhook 由 Stripe 触发,除了我无法从 Checkout.Session.Completed 事件中检索客户之外,一切正常。以下是来自 Stripe 的 JSON,显示了该事件:
{
"id": "evt_1OTx6dJqbmj5op6EbTwFDGjI",
"object": "event",
"api_version": "2023-10-16",
"created": 1704160163,
"data": {
"object": {
"id": "cs_test_c1Bo8DeRxy9jIJICpbdXaxG4zwki2caw6ph8qenMDpbXTsujxaNzh9ZQdT",
"object": "checkout.session",
"after_expiration": null,
"allow_promotion_codes": null,
"amount_subtotal": null,
"amount_total": null,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_address_collection": null,
"cancel_url": null,
"client_reference_id": null,
"client_secret": null,
"consent": {
"promotions": null,
"terms_of_service": "accepted"
},
"consent_collection": {
"payment_method_reuse_agreement": {
"position": "hidden"
},
"promotions": "none",
"terms_of_service": "required"
},
"created": 1704160119,
"currency": null,
"currency_conversion": null,
"custom_fields": [
],
"custom_text": {
"after_submit": null,
"shipping_address": null,
"submit": null,
"terms_of_service_acceptance": {
"message": "I agree to the [Terms of Service](https://test.com/terms.aspx)"
}
},
"customer": "cus_PGt0WkKRwNskR5",
"customer_creation": null,
"customer_details": {
"address": null,
"email": "[email protected]",
"name": null,
"phone": null,
"tax_exempt": null,
"tax_ids": null
},
"customer_email": null,
"expires_at": 1704246519,
"invoice": null,
"invoice_creation": null,
"livemode": false,
"locale": null,
"metadata": {
},
"mode": "setup",
"payment_intent": null,
"payment_link": null,
"payment_method_collection": "always",
"payment_method_configuration_details": null,
"payment_method_options": {
},
"payment_method_types": [
"card"
],
"payment_status": "no_payment_required",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"redirect_on_completion": "always",
"return_url": "https://test.com/done.aspx?session_id={CHECKOUT_SESSION_ID}",
"setup_intent": "seti_1OTx5vJqbmj5op6EqHc02Tae",
"shipping_address_collection": null,
"shipping_cost": null,
"shipping_details": null,
"shipping_options": [
],
"status": "complete",
"submit_type": null,
"subscription": null,
"success_url": null,
"total_details": null,
"ui_mode": "embedded",
"url": null
}
},
"livemode": false,
"pending_webhooks": 3,
"request": {
"id": null,
"idempotency_key": null
},
"type": "checkout.session.completed"
}
如您所见,“客户”有一个值,我正在使用以下代码来尝试检索它:
try {
var stripeEvent=EventUtility.ConstructEvent(
json,
signature,
endpointSecret
);
switch (stripeEvent.Type) {
case Events.CheckoutSessionCompleted:
var objCheckout=stripeEvent.Data.Object as Session;
var info2=$"Hit Checkout Session ID '{objCheckout.Id}' customer '{objCheckout.Customer?.ToString()}'";
MailSvc.SendMail ( "[email protected]", "Session Checkout Complete", false, "[email protected]", info2 );
break;
default:
var info3=$"Missed this here - {stripeEvent.Type}";
MailSvc.SendMail ( "[email protected]", "Webhook event error", false, "[email protected]", info3 );
break;
}
}
catch (StripeException ex) {
Console.WriteLine ( ex.Message );
throw ( ex );
}
所有代码都工作正常,除了我无法检索“客户”的值以便我可以处理交易。我找不到任何代码来帮助我解决这个问题。有什么想法吗?
在您的 webhook 处理代码中,您需要调用 Retrieve a customer API 来检索客户对象,以便您可以使用完整的客户数据来处理订单。