如何从 Stripe webhook/Payment Intent 获取更多数据

How to get more data from Stripe webhook/Payment Intent

我正在使用 Stripe CLI 在我的本地主机上测试 Stripe webhooks,到目前为止,我已经成功地使用它 return 一个包含 most[=26] 的 PaymentIntent 对象=] 我需要捕获并保存到我的数据库中的数据,但我仍然无法获得文档中提到的一些细节 should be in the Payment Intent,即:

  • 用户支付的支付方式类型
  • 用户在 Stripe 支付元素表单中输入的邮政编码

我正在使用以下代码来获取这两条信息,它们也是 函数中唯一不起作用的行payment_intent.succeeded webhook 被触发:

// Webhooks by default return an Event object that is stored in 
// $payload object, so ["data"]["object"] should return the 
// PaymentIntent object corresponding to the retrieved Event object

$paymentIntent = $this->webhookCall->payload["data"]["object"]; 

...

$transaction->payment_method = $paymentIntent["charges"]["data"]["payment_method_details"]["type"]; 
$transaction->postcode = $paymentIntent["charges"]["data"]["billing_details"]["address"]["postal_code"];

这些行导致 Laravel 日志文件中出现“未定义索引”错误,这似乎表明它们在 PaymentIntent 中不存在,或者我访问它们的方式有问题。

我没有使用过 stripe,但我从你分享的文档中看到了 link。 ["data"] 属性 不是对象而是数组,这意味着您必须像访问数组一样访问它。所以你的代码应该看起来像这样。

$transaction->payment_method = $paymentIntent["charges"]["data"][0]["payment_method_details"]["type"]; 
$transaction->postcode = $paymentIntent["charges"]["data"][0]["billing_details"]["address"]["postal_code"];