【发布时间】:2019-11-29 10:56:46
【问题描述】:
我正在尝试从 Stripe 结帐发布请求接收 webhook。但是 Stripe 文档只显示了 ASP.net 的示例代码,而我使用的是 WCF。 https://stripe.com/docs/payments/checkout/fulfillment#webhooks
这是 Stripe 结账的示例请求。
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "checkout.session.completed",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2019-05-16",
"data": {
"object": {
"id": "cs_00000000000000",
"object": "checkout.session",
"billing_address_collection": null,
"cancel_url": "https://example.com/cancel",
"client_reference_id": null,
"customer": null,
"customer_email": null,
"display_items": [
{
"amount": 1500,
"currency": "usd",
"custom": {
"description": "Comfortable cotton t-shirt",
"images": null,
"name": "T-shirt"
},
"quantity": 2,
"type": "custom"
}
],
"livemode": false,
"locale": null,
"payment_intent": "pi_00000000000000",
"payment_method_types": [
"card"
],
"submit_type": null,
"subscription": null,
"success_url": "https://example.com/success"
}
}
}
起初我尝试使用 Stripe.Event 类作为参数。
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void FromStripeCheckoutWrapped(Event data);
但是所有数据都是空值。 所以,我尝试根据json请求使用我自己的Model类。
public partial class Temperatures
{
[DataMember]
public long Created { get; set; }
[DataMember]
public bool Livemode { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public string Type { get; set; }
[DataMember]
public string Object { get; set; }
[DataMember]
public object Request { get; set; }
[DataMember]
public long PendingWebhooks { get; set; }
[DataMember]
public string ApiVersion { get; set; }
[DataMember]
public Data Data { get; set; }
} ...
但他们都没有收到正确的数据。
我尝试使用 String 参数接收的最后一件事,就像我在 stackoverflow 但在 Java 中发现的问题一样。 How to Receive Webhook from Stripe in Java 他说他可以用这种方式打印数据。但我做不到。
我让它成功的唯一方法是输入 json 请求中写的每一个参数。我也尝试将 Wrapped 和 Bare 作为 WebMessageBodyStyle。
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void FromStripeCheckoutWrapped(long created, bool livemode, string id ...);
然后我可以正确获取数据..但是像上面那样做所有事情都太长了。
如何以有效且正确的方式接收 POST 请求?
请同时参考 Java 和 Stripe webhook 文档中的问题。
我只想将 POST 请求读取为 String 对象,而不管请求结构如何,以便我可以解析它。
【问题讨论】:
-
在
DataMember属性上设置Name参数以匹配 JSON 中属性名称的大小写。创建创建 -
@rene 我试过了,但没用..
-
请参考以下链接。也许它对你有用。 stackoverflow.com/questions/52489002/…
-
如果您正在寻找最新的 ASP.NET Core MVC 或 ASP.NET Web 表单中的 Stripe checkout 解决方案,请查看演示:techtolia.com/Stripe
标签: c# json wcf stripe-payments webhooks